From f9d8b8eeb77b9d36ea146d4e9c1e9b2bd4babf2a Mon Sep 17 00:00:00 2001 From: Mohammad Nassar Date: Sun, 21 Sep 2025 15:25:02 +0300 Subject: [PATCH] chore(payments): check non-zero amount before transfer --- src/payments.cairo | 67 ++++++++++++++++++----------------- src/tests/test_payments.cairo | 1 - 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/payments.cairo b/src/payments.cairo index 77885c2..8cde447 100644 --- a/src/payments.cairo +++ b/src/payments.cairo @@ -214,42 +214,45 @@ pub mod payments { let fee_a = self._calculate_fee(order_a_actual_sell_amount); // For `order_b`, the actual sold amount is `order_a_actual_buy_amount`. let fee_b = self._calculate_fee(order_a_actual_buy_amount); - assert_with_byte_array( - sell_token.transfer_from(order_a.user, fee_recipient, fee_a.into()), - transfer_failed_error( - token: order_a.sell_token, sender: order_a.user, amount: fee_a, - ), - ); - assert_with_byte_array( - buy_token.transfer_from(order_b.user, fee_recipient, fee_b.into()), - transfer_failed_error( - token: order_a.buy_token, sender: order_b.user, amount: fee_b, - ), - ); + + // Transfer fees only if they are non-zero + if fee_a.is_non_zero() { + assert_with_byte_array( + sell_token.transfer_from(order_a.user, fee_recipient, fee_a.into()), + transfer_failed_error( + token: order_a.sell_token, sender: order_a.user, amount: fee_a, + ), + ); + } + if fee_b.is_non_zero() { + assert_with_byte_array( + buy_token.transfer_from(order_b.user, fee_recipient, fee_b.into()), + transfer_failed_error( + token: order_a.buy_token, sender: order_b.user, amount: fee_b, + ), + ); + } // Transfer the actual amounts. - assert_with_byte_array( - sell_token - .transfer_from( - order_a.user, order_b.user, (order_a_actual_sell_amount - fee_a).into(), + let net_sell_amount = order_a_actual_sell_amount - fee_a; + let net_buy_amount = order_a_actual_buy_amount - fee_b; + + if net_sell_amount.is_non_zero() { + assert_with_byte_array( + sell_token.transfer_from(order_a.user, order_b.user, net_sell_amount.into()), + transfer_failed_error( + token: order_a.sell_token, sender: order_a.user, amount: net_sell_amount, ), - transfer_failed_error( - token: order_a.sell_token, - sender: order_a.user, - amount: order_a_actual_sell_amount - fee_a, - ), - ); - assert_with_byte_array( - buy_token - .transfer_from( - order_b.user, order_a.user, (order_a_actual_buy_amount - fee_b).into(), + ); + } + if net_buy_amount.is_non_zero() { + assert_with_byte_array( + buy_token.transfer_from(order_b.user, order_a.user, net_buy_amount.into()), + transfer_failed_error( + token: order_a.buy_token, sender: order_b.user, amount: net_buy_amount, ), - transfer_failed_error( - token: order_a.buy_token, - sender: order_b.user, - amount: order_a_actual_buy_amount - fee_b, - ), - ); + ); + } // Emit an event. self diff --git a/src/tests/test_payments.cairo b/src/tests/test_payments.cairo index eb5c7fc..424cb28 100644 --- a/src/tests/test_payments.cairo +++ b/src/tests/test_payments.cairo @@ -574,7 +574,6 @@ fn test_invalid_trade_scenarios() { initial_balance: constants::INITIAL_BALANCE, ); let dispatcher = IPaymentsSafeDispatcher { contract_address }; - let mut spy = snforge_std::spy_events(); // Add tokens. cheat_caller_address_once(:contract_address, caller_address: testing_constants::APP_GOVERNOR);