Perpetual futures: initial margin, and a pool-maintained price average with a trading band - #176
Merged
Merged
Conversation
…e with a trading band Initial margin: max_leverage becomes initial_margin_bps, which must exceed maintenance_margin_bps (InitialMarginNotAboveMaintenance). open_position requires net collateral of at least initial_margin_bps of the size (InitialMarginNotMet); the separate maintenance check at open is implied. Price band: the pool keeps a time-weighted average of the oracle price (average_price, last_oracle_price, average_price_timestamp, ten-minute window). Each fold credits the elapsed time to the price seen at the previous read, so one manipulated read after an idle spell moves nothing. open_position, close_position, add_liquidity and remove_liquidity refuse an oracle price more than max_price_deviation_bps from the average (PriceOutsideBand). liquidate_position and the new permissionless update_price_average() fold without the check, so liquidations run through a genuine move and the average can catch up with it. All three implementations; anchor 37, anchor-v1 37, quasar 28 tests. Claude-Session: https://claude.ai/code/session_019G9tytYrS3Qp42fZ1hBnDu (cherry picked from commit 1f1a46e)
This was referenced Oct 1, 2026
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.
From Noah Prince's review of the book's Perpetual Futures chapter. All three implementations (Anchor v2, Anchor v1, Quasar).
Initial margin
max_leveragebecomesinitial_margin_bps, validated abovemaintenance_margin_bps(InitialMarginNotAboveMaintenance).open_positionrequires net collateral of at leastinitial_margin_bpsof the size (InitialMarginNotMet, replacingLeverageTooHigh); the separate maintenance check at open is implied by the initial margin being higher. Maximum leverage is one over the initial margin.Price band
The pool keeps a time-weighted average of the oracle price:
average_price,last_oracle_price,average_price_timestamp, overPRICE_AVERAGE_WINDOW_SECONDS(600). Each update credits the elapsed time to the price seen at the previous read, so a single manipulated read after an idle spell moves nothing (test_one_manipulated_read_after_idle_does_not_move_average).open_position,close_position,add_liquidityandremove_liquidityrefuse an oracle price more thanmax_price_deviation_bpsfrom the stored average (PriceOutsideBand).liquidate_positionfolds the price in without the check, so liquidations run through a genuine move.update_price_average()lets the average catch up after a genuine move.Tests
Anchor v2: 37, Anchor v1: 37, Quasar: 28, all passing;
cargo fmtandclippy -D warningsclean.Split out of #175, which is the fundraiser fix only. #179 (the haircut risk model, replacing the closed #83) builds on this branch; merge this one first.
🤖 Generated with Claude Code
https://claude.ai/code/session_019G9tytYrS3Qp42fZ1hBnDu