Skip to content

fix(order_handler,referral_storage): pool_amount drift, execution_fee double-refund/absorption, zero-threshold tier gate (#629-631, #636) - #676

Merged
abayomicornelius merged 2 commits into
SO4-Markets:mainfrom
prodbycorne:fix/liquidation-fee-and-order-fee-629-636
Aug 24, 2026
Merged

fix(order_handler,referral_storage): pool_amount drift, execution_fee double-refund/absorption, zero-threshold tier gate (#629-631, #636)#676
abayomicornelius merged 2 commits into
SO4-Markets:mainfrom
prodbycorne:fix/liquidation-fee-and-order-fee-629-636

Conversation

@prodbycorne

Copy link
Copy Markdown
Contributor

Summary

Test plan

Closes #629
Closes #630
Closes #631
Closes #636

…ution_fee double-refund and fee absorption

Closes SO4-Markets#629
Closes SO4-Markets#630
Closes SO4-Markets#631

- SO4-Markets#629: liquidate_position paid the keeper's liquidation_execution_fee
  via market_token::withdraw_from_pool, a raw token transfer with no
  knowledge of data_store's pool_amount ledger. Every other real
  withdrawal out of the pool in this codebase pairs the transfer with
  a matching apply_delta_to_pool_amount call; this one didn't,
  permanently drifting pool_amount above the pool's real balance on
  every liquidation with a nonzero fee configured. Added the missing
  apply_delta_to_pool_amount(-fee_to_transfer) call, plus a regression
  test asserting pool_amount drops by exactly the keeper fee.

- SO4-Markets#630/SO4-Markets#631 (shared root cause): create_order/create_orders stored the
  entire record_transfer_in snapshot delta as collateral_delta_amount,
  which — since the router pushes collateral and execution_fee in one
  combined SendTokens deposit — already includes the fee. Two
  consequences:
    - cancel_order's two-transfer refund (collateral_delta_amount +
      execution_fee) double-refunded the fee, either reverting the
      cancellation outright or, worse, paying it out of other users'
      concurrently-deposited funds.
    - execute_order never paid execution_fee to the keeper at all;
      instead the fee-inclusive collateral_delta_amount was fed
      straight into increase_position's collateral or the swap's
      amount_in, silently becoming extra position margin or extra
      slippage instead of keeper compensation.
  Fixed at the source: collateral_delta_amount is now
  received - execution_fee (rejecting an underfunded order), so the
  two fields are mutually exclusive portions of the single deposit.
  execute_order now transfers order.execution_fee from order_vault to
  the keeper on every successful dispatch branch, mirroring the
  refund/incentive logic cancel_order and cleanup_expired_order
  already have for the same field.

cargo test -p order-handler: 53 passed, 3 failed (all three pre-existing
and unrelated — an oracle stale-price panic in keeper-heartbeat tests,
reproduced identically on a clean upstream/main checkout in SO4-Markets#675).
cancel_order_refunds_execution_fee_to_user — the repo's own existing
regression test for the SO4-Markets#630 double-refund — now passes.
Closes SO4-Markets#636

set_tier_upgrade_threshold validated tier but never threshold_usd.
increment_referrer_volume auto-upgrades the moment
cumulative_volume >= threshold, and cumulative_volume is unsigned, so
a threshold of 0 (fat-finger, or a copy-paste of an uninitialized
variable) instantly qualifies every referrer for that tier at their
very first trade. Rejects threshold_usd == 0 with the existing
InvalidInput error, plus a regression test.
@drips-wave

drips-wave Bot commented Aug 24, 2026

Copy link
Copy Markdown

@prodbycorne Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment