Skip to content

Commit

Permalink
require BlockNumber field in call_request func, use BlockNumber::Pend…
Browse files Browse the repository at this point in the history
…ing in payment_status_v2, leave todo in legacy payment_status to use BlockNumber::Pending instead of Latest
  • Loading branch information
laruh committed Dec 1, 2024
1 parent 43c2d94 commit 0ffffd9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
28 changes: 19 additions & 9 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4568,7 +4568,9 @@ impl EthCoin {
let function = ERC20_CONTRACT.function("balanceOf")?;
let data = function.encode_input(&[Token::Address(address)])?;

let res = coin.call_request(address, *token_addr, None, Some(data.into())).await?;
let res = coin
.call_request(address, *token_addr, None, Some(data.into()), BlockNumber::Latest)
.await?;
let decoded = function.decode_output(&res.0)?;
match decoded[0] {
Token::Uint(number) => Ok(number),
Expand Down Expand Up @@ -4632,7 +4634,7 @@ impl EthCoin {
let function = ERC20_CONTRACT.function("balanceOf")?;
let data = function.encode_input(&[Token::Address(address)])?;
let res = self
.call_request(address, token_address, None, Some(data.into()))
.call_request(address, token_address, None, Some(data.into()), BlockNumber::Latest)
.await?;
let decoded = function.decode_output(&res.0)?;

Expand All @@ -4659,7 +4661,7 @@ impl EthCoin {
let my_address = self.derivation_method.single_addr_or_err().await?;
let data = function.encode_input(&[Token::Address(my_address), Token::Uint(token_id_u256)])?;
let result = self
.call_request(my_address, token_addr, None, Some(data.into()))
.call_request(my_address, token_addr, None, Some(data.into()), BlockNumber::Latest)
.await?;
let decoded = function.decode_output(&result.0)?;
match decoded[0] {
Expand Down Expand Up @@ -4690,7 +4692,7 @@ impl EthCoin {
let data = function.encode_input(&[Token::Uint(token_id_u256)])?;
let my_address = self.derivation_method.single_addr_or_err().await?;
let result = self
.call_request(my_address, token_addr, None, Some(data.into()))
.call_request(my_address, token_addr, None, Some(data.into()), BlockNumber::Latest)
.await?;
let decoded = function.decode_output(&result.0)?;
match decoded[0] {
Expand Down Expand Up @@ -4768,6 +4770,7 @@ impl EthCoin {
to: Address,
value: Option<U256>,
data: Option<Bytes>,
block_number: BlockNumber,
) -> Result<Bytes, web3::Error> {
let request = CallRequest {
from: Some(from),
Expand All @@ -4779,7 +4782,7 @@ impl EthCoin {
..CallRequest::default()
};

self.call(request, Some(BlockId::Number(BlockNumber::Latest))).await
self.call(request, Some(BlockId::Number(block_number))).await
}

fn allowance(&self, spender: Address) -> Web3RpcFut<U256> {
Expand All @@ -4795,7 +4798,7 @@ impl EthCoin {
let data = function.encode_input(&[Token::Address(my_address), Token::Address(spender)])?;

let res = coin
.call_request(my_address, *token_addr, None, Some(data.into()))
.call_request(my_address, *token_addr, None, Some(data.into()), BlockNumber::Latest)
.await?;
let decoded = function.decode_output(&res.0)?;

Expand Down Expand Up @@ -5228,9 +5231,16 @@ impl EthCoin {
.single_addr_or_err()
.await
.map_err(|e| ERRL!("{}", e))?;
coin.call_request(my_address, swap_contract_address, None, Some(data.into()))
.await
.map_err(|e| ERRL!("{}", e))
coin.call_request(
my_address,
swap_contract_address,
None,
Some(data.into()),
// TODO worth changing this to BlockNumber::Pending
BlockNumber::Latest,
)
.await
.map_err(|e| ERRL!("{}", e))
};

Box::new(fut.boxed().compat().and_then(move |bytes| {
Expand Down
14 changes: 10 additions & 4 deletions mm2src/coins/eth/eth_swap_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::compat::Future01CompatExt;
use mm2_err_handle::mm_error::MmError;
use mm2_number::BigDecimal;
use num_traits::Signed;
use web3::types::Transaction as Web3Tx;
use web3::types::{BlockNumber, Transaction as Web3Tx};

pub(crate) mod eth_maker_swap_v2;
pub(crate) mod eth_taker_swap_v2;
Expand Down Expand Up @@ -82,7 +82,13 @@ impl EthCoin {
let function = contract_abi.function(function_name)?;
let data = function.encode_input(&[swap_id])?;
let bytes = self
.call_request(self.my_addr().await, swap_address, None, Some(data.into()))
.call_request(
self.my_addr().await,
swap_address,
None,
Some(data.into()),
BlockNumber::Pending,
)
.await?;
let decoded_tokens = function.decode_output(&bytes.0)?;

Expand Down Expand Up @@ -133,8 +139,8 @@ pub(crate) fn validate_from_to_and_status(
) -> Result<(), MmError<ValidatePaymentV2Err>> {
if status != U256::from(expected_status) {
return MmError::err(ValidatePaymentV2Err::UnexpectedPaymentState(format!(
"Payment state is not `PaymentSent`, got {}",
status
"tx {:?} Payment state is not `PaymentSent`, got {}",
tx_from_rpc.hash, status
)));
}
if tx_from_rpc.from != Some(expected_from) {
Expand Down

0 comments on commit 0ffffd9

Please sign in to comment.