Skip to content

Commit

Permalink
move prepare erc20 payment data above allowance, impl handle_allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Sep 10, 2024
1 parent ce7903d commit f1063c7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
22 changes: 3 additions & 19 deletions mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
46 changes: 15 additions & 31 deletions mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +42,17 @@ struct TakerRefundArgs {
token_address: Address,
}

struct TakerValidationArgs<'a> {
swap_id: Vec<u8>,
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.
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -683,17 +678,6 @@ impl EthCoin {
}
}

struct TakerValidationArgs<'a> {
swap_id: Vec<u8>,
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],
Expand Down
34 changes: 32 additions & 2 deletions mm2src/coins/eth/eth_swap_v2/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -169,3 +169,33 @@ fn check_decoded_length(decoded: &Vec<Token>, 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(())
}
}

0 comments on commit f1063c7

Please sign in to comment.