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 intoAug 24, 2026
Conversation
…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.
|
@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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
liquidate_positionpaid the keeper'sliquidation_execution_feeviamarket_token::withdraw_from_pool— a raw token transfer with no knowledge ofdata_store'spool_amountledger. Every other real pool withdrawal in this codebase pairs the transfer with a matchingapply_delta_to_pool_amount; this one didn't, permanently driftingpool_amountabove the pool's real balance on every liquidation with a nonzero fee configured. Added the missing delta call.create_order/create_ordersstored the entirerecord_transfer_insnapshot ascollateral_delta_amount— which already includesexecution_fee, since the router pushes both in one combined deposit. This meantcancel_orderdouble-refunded the fee (reverting the cancellation, or paying it out of other users' funds), andexecute_ordernever paid the fee to the keeper at all — instead silently folding it into position collateral or swap input. Fixed at the source:collateral_delta_amount = received - execution_fee(rejecting an underfunded order), andexecute_ordernow paysorder.execution_feeto the keeper on every successful dispatch, mirroringcancel_order/cleanup_expired_order's existing handling of the same field.referral_storage::set_tier_upgrade_thresholdvalidatedtierbut neverthreshold_usd— a0threshold instantly qualifies every referrer for that tier at their first trade, sincecumulative_volume >= 0is always true. Rejectsthreshold_usd == 0with the existingInvalidInputerror.Test plan
cargo build -p order-handler -p referral-storage— compiles cleancargo test -p order-handler— 53 passed, 3 failed (all three pre-existing and unrelated — an oracle stale-price panic in keeper-heartbeat tests, already confirmed unrelated on a cleanupstream/maincheckout in fix: position TTL renewal, shared TTL-bump constants, keeper/router doc fixes (#657-660) #675)cancel_order_refunds_execution_fee_to_user— the repo's own existing regression test written for the order_handler::cancel_order double-refunds execution_fee — collateral_delta_amount already includes it, per the repo's own #258 regression test #630 bug — now passes (was failing before this fix)liquidate_position_keeper_fee_decrements_pool_amount(order_handler::liquidate_position pays the keeper's liquidation_execution_fee via withdraw_from_pool without decrementing pool_amount, permanently drifting the pool ledger above real balance #629): assertspool_amountdrops by exactly the keeper feeset_tier_upgrade_threshold_rejects_zero(referral_storage::set_tier_upgrade_threshold accepts 0, instantly qualifying every referrer for that tier #636)cargo test -p referral-storage— all passingCloses #629
Closes #630
Closes #631
Closes #636