From f1063c7216fe753289fefb3e12c8316858b2b8cc Mon Sep 17 00:00:00 2001 From: laruh Date: Tue, 10 Sep 2024 20:03:46 +0700 Subject: [PATCH] move prepare erc20 payment data above allowance, impl handle_allowance --- .../eth/eth_swap_v2/eth_maker_swap_v2.rs | 22 ++------- .../eth/eth_swap_v2/eth_taker_swap_v2.rs | 46 ++++++------------- mm2src/coins/eth/eth_swap_v2/mod.rs | 34 +++++++++++++- 3 files changed, 50 insertions(+), 52 deletions(-) diff --git a/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs b/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs index fb9446b88b..1f010ddafa 100644 --- a/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs +++ b/mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs @@ -3,7 +3,7 @@ use crate::coin_errors::{ValidatePaymentError, ValidatePaymentResult}; use crate::eth::{decode_contract_call, get_function_input_data, wei_from_big_decimal, EthCoin, EthCoinType, MakerPaymentStateV2, SignedEthTx, MAKER_SWAP_V2}; use crate::{ParseCoinAssocTypes, RefundMakerPaymentSecretArgs, RefundMakerPaymentTimelockArgs, SendMakerPaymentArgs, - SpendMakerPaymentArgs, SwapTxTypeWithSecretHash, Transaction, TransactionErr, ValidateMakerPaymentArgs, + SpendMakerPaymentArgs, SwapTxTypeWithSecretHash, TransactionErr, ValidateMakerPaymentArgs, WaitForPaymentSpendError}; use common::executor::Timer; use common::now_sec; @@ -82,28 +82,12 @@ impl EthCoin { platform: _, token_addr, } => { - let allowed = self - .allowance(maker_swap_v2_contract) - .compat() - .await - .map_err(|e| TransactionErr::Plain(ERRL!("{}", e)))?; let data = try_tx_s!( self.prepare_maker_erc20_payment_data(&payment_args, payment_amount, *token_addr) .await ); - if allowed < payment_amount { - let approved_tx = self.approve(maker_swap_v2_contract, U256::max_value()).compat().await?; - self.wait_for_required_allowance(maker_swap_v2_contract, payment_amount, args.time_lock) - .compat() - .await - .map_err(|e| { - TransactionErr::Plain(ERRL!( - "Allowed value was not updated in time after sending approve transaction {:02x}: {}", - approved_tx.tx_hash_as_bytes(), - e - )) - })?; - } + self.handle_allowance(maker_swap_v2_contract, payment_amount, args.time_lock) + .await?; self.sign_and_send_transaction( U256::from(ZERO_VALUE), Action::Call(maker_swap_v2_contract), diff --git a/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs b/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs index 126f8a00d5..ffda7626dc 100644 --- a/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs +++ b/mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs @@ -2,8 +2,8 @@ use super::{check_decoded_length, validate_from_to_and_status, validate_payment_ EthPaymentType, PrepareTxDataError, ZERO_VALUE}; use crate::eth::{decode_contract_call, get_function_input_data, wei_from_big_decimal, EthCoin, EthCoinType, ParseCoinAssocTypes, RefundFundingSecretArgs, RefundTakerPaymentArgs, SendTakerFundingArgs, - SignedEthTx, SwapTxTypeWithSecretHash, TakerPaymentStateV2, Transaction, TransactionErr, - ValidateSwapV2TxError, ValidateSwapV2TxResult, ValidateTakerFundingArgs, TAKER_SWAP_V2}; + SignedEthTx, SwapTxTypeWithSecretHash, TakerPaymentStateV2, TransactionErr, ValidateSwapV2TxError, + ValidateSwapV2TxResult, ValidateTakerFundingArgs, TAKER_SWAP_V2}; use crate::{FundingTxSpend, GenTakerFundingSpendArgs, GenTakerPaymentSpendArgs, SearchForFundingSpendErr, WaitForPaymentSpendError}; use common::executor::Timer; @@ -42,6 +42,17 @@ struct TakerRefundArgs { token_address: Address, } +struct TakerValidationArgs<'a> { + swap_id: Vec, + amount: U256, + dex_fee: U256, + receiver: Address, + taker_secret_hash: &'a [u8], + maker_secret_hash: &'a [u8], + funding_time_lock: u64, + payment_time_lock: u64, +} + impl EthCoin { /// Calls `"ethTakerPayment"` or `"erc20TakerPayment"` swap contract methods. /// Returns taker sent payment transaction. @@ -92,25 +103,9 @@ impl EthCoin { platform: _, token_addr, } => { - let allowed = self - .allowance(taker_swap_v2_contract) - .compat() - .await - .map_err(|e| TransactionErr::Plain(ERRL!("{}", e)))?; let data = try_tx_s!(self.prepare_taker_erc20_funding_data(&funding_args, *token_addr).await); - if allowed < payment_amount { - let approved_tx = self.approve(taker_swap_v2_contract, U256::max_value()).compat().await?; - self.wait_for_required_allowance(taker_swap_v2_contract, payment_amount, args.funding_time_lock) - .compat() - .await - .map_err(|e| { - TransactionErr::Plain(ERRL!( - "Allowed value was not updated in time after sending approve transaction {:02x}: {}", - approved_tx.tx_hash_as_bytes(), - e - )) - })?; - } + self.handle_allowance(taker_swap_v2_contract, payment_amount, args.funding_time_lock) + .await?; self.sign_and_send_transaction( U256::from(ZERO_VALUE), Action::Call(taker_swap_v2_contract), @@ -683,17 +678,6 @@ impl EthCoin { } } -struct TakerValidationArgs<'a> { - swap_id: Vec, - amount: U256, - dex_fee: U256, - receiver: Address, - taker_secret_hash: &'a [u8], - maker_secret_hash: &'a [u8], - funding_time_lock: u64, - payment_time_lock: u64, -} - /// Validation function for ETH taker payment data fn validate_eth_taker_payment_data( decoded: &[Token], diff --git a/mm2src/coins/eth/eth_swap_v2/mod.rs b/mm2src/coins/eth/eth_swap_v2/mod.rs index 52e2137a93..1591452af4 100644 --- a/mm2src/coins/eth/eth_swap_v2/mod.rs +++ b/mm2src/coins/eth/eth_swap_v2/mod.rs @@ -1,9 +1,9 @@ -use crate::eth::EthCoin; -use crate::ParseCoinAssocTypes; +use crate::eth::{EthCoin, ParseCoinAssocTypes, Transaction, TransactionErr}; use enum_derives::EnumFromStringify; use ethabi::{Contract, Token}; use ethcore_transaction::SignedTransaction as SignedEthTx; use ethereum_types::{Address, U256}; +use futures::compat::Future01CompatExt; use mm2_err_handle::mm_error::MmError; use mm2_number::BigDecimal; use web3::types::Transaction as Web3Tx; @@ -169,3 +169,33 @@ fn check_decoded_length(decoded: &Vec, expected_len: usize) -> Result<(), } Ok(()) } + +impl EthCoin { + async fn handle_allowance( + &self, + swap_contract: Address, + payment_amount: U256, + time_lock: u64, + ) -> Result<(), TransactionErr> { + let allowed = self + .allowance(swap_contract) + .compat() + .await + .map_err(|e| TransactionErr::Plain(ERRL!("{}", e)))?; + + if allowed < payment_amount { + let approved_tx = self.approve(swap_contract, U256::max_value()).compat().await?; + self.wait_for_required_allowance(swap_contract, payment_amount, time_lock) + .compat() + .await + .map_err(|e| { + TransactionErr::Plain(ERRL!( + "Allowed value was not updated in time after sending approve transaction {:02x}: {}", + approved_tx.tx_hash_as_bytes(), + e + )) + })?; + } + Ok(()) + } +}