Harden the in-contract swap path against fee-on-transfer tokens, stale path prices, and slippage edge cases, and lock the behavior in with tests.
Requirements and Context
contracts/finchippay-contract/src/lib.rs already contains swap primitives — validate_swap_path, compute_swap_fee, compute_required_amount_in, get_swap_fee_bps, and get_fee_collector_address, plus SwapItem/TokenTotal types and ContractError::{SlippageExceeded, ExcessiveAmountIn, InvalidFeeBps}. The swap surface is a natural DeFi attack target, but the token-fee interaction (fee-on-transfer inputs), path-price staleness, and exact-output slippage are under-tested.
Objectives
- Enforce that the amount actually received (not requested) drives the swap math, so fee-on-transfer inputs cannot under-collateralise the output side.
- Add staleness validation for multi-hop paths (e.g., reject paths that pass the same token twice or use a zero-liquidity hop).
- Add comprehensive slippage tests for
min_amount_out and max_amount_in, including exact-output rounding.
- Verify the protocol fee and
FeeCollector accounting under edge-case amounts (dust, large values).
Suggested Execution
git checkout -b fix/swap-hardening.
- Update swap helpers in
lib.rs to reconcile received-vs-expected amounts.
- Add
tests/swap_hardening.rs with mock pools and a fee-on-transfer token.
- Run
cargo fmt --check && cargo clippy -- -D warnings && cargo test.
Acceptance Criteria
Guidelines
- Reuse existing
compute_swap_fee / compute_required_amount_in; do not rewrite the fee model.
- Add events for swaps (amount in, amount out, fee, path length) to aid off-chain reconciliation.
Timeframe: 96 hours
Requirements and Context
contracts/finchippay-contract/src/lib.rsalready contains swap primitives —validate_swap_path,compute_swap_fee,compute_required_amount_in,get_swap_fee_bps, andget_fee_collector_address, plusSwapItem/TokenTotaltypes andContractError::{SlippageExceeded, ExcessiveAmountIn, InvalidFeeBps}. The swap surface is a natural DeFi attack target, but the token-fee interaction (fee-on-transfer inputs), path-price staleness, and exact-output slippage are under-tested.Objectives
min_amount_outandmax_amount_in, including exact-output rounding.FeeCollectoraccounting under edge-case amounts (dust, large values).Suggested Execution
git checkout -b fix/swap-hardening.lib.rsto reconcile received-vs-expected amounts.tests/swap_hardening.rswith mock pools and a fee-on-transfer token.cargo fmt --check && cargo clippy -- -D warnings && cargo test.Acceptance Criteria
InvalidPath/ a staleness error (≥4 tests).min_amount_out/max_amount_inare respected under rounding (≥4 tests).FeeCollectorfor edge-case amounts.Guidelines
compute_swap_fee/compute_required_amount_in; do not rewrite the fee model.Timeframe: 96 hours