Skip to content

Commit

Permalink
can't clamp the fee to 1 since it's probably higher than the fee
Browse files Browse the repository at this point in the history
noticed this in the integration tests, the fee might be a fraction so
clamping it to one yields a fee higher than the actually one, thus fails
the ratio check
  • Loading branch information
mariocynicys committed May 3, 2024
1 parent 972e332 commit 2b1080f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,17 @@ impl<MakerCoin: MmCoin + MakerCoinSwapOpsV2, TakerCoin: MmCoin + TakerCoinSwapOp
return Self::change_state(Aborted::new(reason), state_machine).await;
}

// Clamp the expected fee to 1 unit to avoid accidental division by zero.
let expected_taker_payment_spend_fee = self.taker_payment_spend_trade_fee.amount.max(1.into());
let expected_taker_payment_spend_fee = self.taker_payment_spend_trade_fee.amount;
let taker_payment_spend_fee =
big_decimal_from_sat_unsigned(taker_data.taker_payment_spend_fee, state_machine.taker_coin.decimals());
let ratio = &taker_payment_spend_fee / &expected_taker_payment_spend_fee;

// To avoid accidental division by zero, let's set the ratio to 1 if the expected fee is 0.
let ratio = if expected_taker_payment_spend_fee == 0.into() {
1.into()
} else {
&taker_payment_spend_fee / &expected_taker_payment_spend_fee
};

if ratio < 0.95.try_into().unwrap() {
let diff = &expected_taker_payment_spend_fee - &taker_payment_spend_fee;
let reason = AbortReason::TakerPaymentSpentFeeTooLow(diff);
Expand Down

0 comments on commit 2b1080f

Please sign in to comment.