Skip to content

Commit

Permalink
review: remove redundant validation
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Nov 7, 2024
1 parent ed02392 commit 1301084
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
13 changes: 7 additions & 6 deletions mm2src/coins/eth/eth_swap_v2/eth_maker_swap_v2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{validate_from_to_and_status, validate_payment_args, EthPaymentType, PaymentMethod, PrepareTxDataError,
use super::{validate_amount, validate_from_to_and_status, EthPaymentType, PaymentMethod, PrepareTxDataError,
ZERO_VALUE};
use crate::coin_errors::{ValidatePaymentError, ValidatePaymentResult};
use crate::eth::{decode_contract_call, get_function_input_data, wei_from_big_decimal, EthCoin, EthCoinType,
Expand Down Expand Up @@ -132,9 +132,9 @@ impl EthCoin {
ValidatePaymentError::InternalError("Expected swap_v2_contracts to be Some, but found None".to_string())
})?
.maker_swap_v2_contract;
validate_payment_args(args.taker_secret_hash, args.maker_secret_hash, &args.amount)
.map_to_mm(ValidatePaymentError::InternalError)?;
let maker_address = public_to_address(args.maker_pub);
let taker_secret_hash = args.taker_secret_hash.try_into()?;
let maker_secret_hash = args.maker_secret_hash.try_into()?;
validate_amount(&args.amount).map_to_mm(ValidatePaymentError::InternalError)?;
let swap_id = self.etomic_swap_id_v2(args.time_lock, args.maker_secret_hash);
let maker_status = self
.payment_status_v2(
Expand All @@ -155,6 +155,7 @@ impl EthCoin {
args.maker_payment_tx.tx_hash()
))
})?;
let maker_address = public_to_address(args.maker_pub);
validate_from_to_and_status(
tx_from_rpc,
maker_address,
Expand All @@ -169,8 +170,8 @@ impl EthCoin {
swap_id,
amount,
taker: self.my_addr().await,
taker_secret_hash: args.taker_secret_hash.try_into()?,
maker_secret_hash: args.maker_secret_hash.try_into()?,
taker_secret_hash,
maker_secret_hash,
payment_time_lock: args.time_lock,
}
};
Expand Down
13 changes: 7 additions & 6 deletions mm2src/coins/eth/eth_swap_v2/eth_taker_swap_v2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{check_decoded_length, validate_from_to_and_status, validate_payment_args, validate_payment_state,
use super::{check_decoded_length, validate_amount, validate_from_to_and_status, validate_payment_state,
EthPaymentType, PaymentMethod, PrepareTxDataError, ZERO_VALUE};
use crate::eth::{decode_contract_call, get_function_input_data, wei_from_big_decimal, EthCoin, EthCoinType,
ParseCoinAssocTypes, RefundFundingSecretArgs, RefundTakerPaymentArgs, SendTakerFundingArgs,
Expand Down Expand Up @@ -154,9 +154,9 @@ impl EthCoin {
ValidateSwapV2TxError::Internal("Expected swap_v2_contracts to be Some, but found None".to_string())
})?
.taker_swap_v2_contract;
validate_payment_args(args.taker_secret_hash, args.maker_secret_hash, &args.trading_amount)
.map_err(ValidateSwapV2TxError::Internal)?;
let taker_address = public_to_address(args.taker_pub);
let taker_secret_hash = args.taker_secret_hash.try_into()?;
let maker_secret_hash = args.maker_secret_hash.try_into()?;
validate_amount(&args.trading_amount).map_err(ValidateSwapV2TxError::Internal)?;
let swap_id = self.etomic_swap_id_v2(args.payment_time_lock, args.maker_secret_hash);
let taker_status = self
.payment_status_v2(
Expand All @@ -175,6 +175,7 @@ impl EthCoin {
args.funding_tx.tx_hash()
))
})?;
let taker_address = public_to_address(args.taker_pub);
validate_from_to_and_status(
tx_from_rpc,
taker_address,
Expand All @@ -191,8 +192,8 @@ impl EthCoin {
amount: payment_amount,
dex_fee,
receiver: self.my_addr().await,
taker_secret_hash: args.taker_secret_hash.try_into()?,
maker_secret_hash: args.maker_secret_hash.try_into()?,
taker_secret_hash,
maker_secret_hash,
funding_time_lock: args.funding_time_lock,
payment_time_lock: args.payment_time_lock,
}
Expand Down
12 changes: 1 addition & 11 deletions mm2src/coins/eth/eth_swap_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,10 @@ pub(crate) fn validate_from_to_and_status(
}

// TODO validate premium when add its support in swap_v2
fn validate_payment_args<'a>(
taker_secret_hash: &'a [u8],
maker_secret_hash: &'a [u8],
trading_amount: &BigDecimal,
) -> Result<(), String> {
fn validate_amount(trading_amount: &BigDecimal) -> Result<(), String> {
if !trading_amount.is_positive() {
return Err("trading_amount must be a positive value".to_string());
}
if taker_secret_hash.len() != 32 {
return Err("taker_secret_hash must be 32 bytes".to_string());
}
if maker_secret_hash.len() != 32 {
return Err("maker_secret_hash must be 32 bytes".to_string());
}
Ok(())
}

Expand Down

0 comments on commit 1301084

Please sign in to comment.