Fix #4: Replace global fee pot with per-market fee ledger - #120
Open
funds0033-cmyk wants to merge 3 commits into
Open
Fix #4: Replace global fee pot with per-market fee ledger#120funds0033-cmyk wants to merge 3 commits into
funds0033-cmyk wants to merge 3 commits into
Conversation
…e ledger Co-authored-by: Cursor <cursoragent@cursor.com>
Keep the per-market fee ledger and forfeited-pool path, take main's pause ABI, and assign unique error codes so pause, duration, dispute, and unsettled-fee errors no longer collide. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Closes #4
Problem
AccumulatedFeeswas a single globali128shared across all markets:cancel_marketsubtracted an estimate from that global number, so cancelling one large market could wipe out fees earned by unrelated markets.withdraw_feesdrained the entire pot, including fees still backing open markets.There was no way to answer "how much of this belongs to market X?"
New storage model
Invariant:
AccumulatedFees == Σ MarketFees(i) + LegacyFeesMarketFees(market_id)LegacyFeesmarket_id = 0)AccumulatedFeesForfeitedPool(market_id)FeeLedgerMigratedBehavior changes
place_bet— credits the market's own ledger, bumps the cached global total by the same amount.cancel_market— zeros only that market's ledger; other markets untouched (previously decremented the global pot by an estimate, or zeroed it entirely).resolve_market— two-sided: dust credited to that market's ledger. Empty-side: that market's fees are locked intoForfeitedPool.locked_fees(from its own ledger, not estimated off the global pot); principal stays inForfeitedPooland fees return to the market's ledger after the dispute window viafinalize_zero_side/ first claim.withdraw_fees(caller, recipient, market_id)— newmarket_idargument; pays out only that ledger. Market must be resolved and not cancelled (FeesNotSettled = 29otherwise).market_id = 0withdraws the legacy bucket. Cannot drain another market in the same call.request_withdraw_fees(..., amount, market_id)— same scoping; the 20% cap applies to that market's fees, not the global total. Pending requests storemarket_idso execution only debits that entry.Migration
Old
AccumulatedFeescan't be split across markets retroactively. On first fee access after upgrade (or via adminmigrate_fee_ledger):LegacyFeesFeeLedgerMigratedAccumulatedFeesunchangedNew bets go to
MarketFees(id); cancel/withdraw on a new market never touches the legacy bucket. Fresh deploys setFeeLedgerMigrated = trueandLegacyFees = 0ininitialize.New public API
get_market_fees(market_id)— that market's ledger (0= legacy)get_legacy_fees()— equivalent toget_market_fees(0)migrate_fee_ledger(admin)— explicit upgrade helper (also runs lazily)Tests
test_cancel_preserves_unrelated_market_feestest_withdraw_fees_scoped_to_one_markettest_reject_withdraw_fees_from_open_markettest_legacy_fee_migration_does_not_double_countmarket_idand resolve the market before withdrawal (fees are only withdrawable post-settlement).Branch:
fix/issue-4-per-market-fee-ledger