Perpetual Futures Smart Contracts
Perpetual Futures Smart Contracts
This page documents perpetual-futures instruction files in execution order.
For perpetual futures:
Long locked backing = SOL.
Short locked backing = USDC.
open_perp_position.rs
Current flow:
- Validate perp parameters, owner, pool, custodies, oracle accounts, funding account, and token accounts.
- Read current prices.
- Calculate position size, collateral value, and leverage.
- Calculate required backing liquidity.
- For market long, check
sol_custody.token_owned - sol_custody.token_locked >= required_liquidity. - For market short, check
usdc_custody.token_owned - usdc_custody.token_locked >= required_liquidity. - For limit placement, skip the backing-liquidity check because no liquidity is locked yet.
- Transfer user collateral into selected collateral custody.
- If market order, increase locked custody
token_locked. - Increase selected collateral custody
token_owned. - Store position state.
- Update pool open interest.
Liquidity check:
- Market orders check free liquidity before locking.
- Limit placement does not require backing liquidity because it does not lock liquidity.
- Limit execution checks backing liquidity in
execute_limit_perp.rs.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
open_limit_perp.rs
Current flow:
- Map limit-perp parameters into
open_perp_position.rs. - Set
order_type = Limit. - Delegate to
open_perp_position.rs.
Liquidity check:
- It inherits the corrected limit-placement behavior from
open_perp_position.rs. - Because a pending limit perp does not lock at placement, liquidity is checked when
execute_limit_perp.rscreates the lock.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
execute_limit_perp.rs
Current flow:
- Validate the pending perp and keeper/execution authority.
- Check limit trigger conditions.
- Calculate liquidation price and borrow-fee snapshot.
- Long execution checks
sol_custody.token_owned - sol_custody.token_locked >= position.locked_amount. - Short execution checks
usdc_custody.token_owned - usdc_custody.token_locked >= position.locked_amount. - Long execution increases
sol_custody.token_lockedbyposition.locked_amount. - Short execution increases
usdc_custody.token_lockedbyposition.locked_amount. - Check
sol_custody.token_locked <= sol_custody.token_owned. - Check
usdc_custody.token_locked <= usdc_custody.token_owned. - Mark the limit position executed.
Liquidity check:
- Current code checks free liquidity before locking.
- Current code keeps a post-lock
token_locked <= token_ownedassertion. - Long execution requires free SOL and short execution requires free USDC.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
close_perp_position.rs
Current flow:
- Validate the position, owner, close percentage, pool, custodies, oracle accounts, and token accounts.
- Calculate closed size.
- Calculate closed collateral amount.
- Calculate PnL for the closed portion.
- Calculate borrow interest for the closed portion.
- Calculate trade fees for the closed portion.
- Calculate
settlement_usd = collateral + PnL - interest - trade fees, floored at zero. - Convert settlement into selected receive asset.
- Calculate
locked_amount_to_release. - If the receive asset matches the locked backing asset, require
settlement_tokens <= locked_amount_to_release. - If the receive asset is the cross asset, require
receive_custody.token_owned - receive_custody.token_locked >= settlement_tokens. - Transfer
settlement_tokensto the user. - Subtract
settlement_tokensfrom the custody that actually pays. - Release locked backing.
- Reduce or close position state.
- Update pool open interest.
Liquidity check by case:
- Close long, receive SOL: checks
settlement_tokens <= locked_amount_to_release. - Close long, receive USDC: checks free USDC.
- Close short, receive USDC: checks
settlement_tokens <= locked_amount_to_release. - Close short, receive SOL: checks free SOL.
Accounting note:
- The file transfers
settlement_tokens. - The file subtracts
settlement_tokensfrom the custody that actually pays. - It no longer subtracts closed collateral from collateral custody as a second payout source.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
close_limit_perp.rs
Current flow:
- This file delegates to
cancel_limit_order.rs. - It maps close-limit-perp parameters into cancel-limit-order parameters.
Liquidity check:
- It inherits refund logic from
cancel_limit_order.rs.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
cancel_limit_order.rs
Current flow:
- Validate pending perp, owner, pool, custodies, and token accounts.
- Calculate proportional collateral refund.
- Convert refund value into selected receive asset.
- Check free liquidity in the selected receive custody.
- Transfer refund to the user.
- Subtract the transferred refund tokens from the selected receive custody
token_owned. - Reduce or close the pending position.
Liquidity check:
- Pending collateral refund is not locked backing.
- Current code checks free liquidity before transfer.
- The selected receive custody is the paying custody.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
update_position_size.rs
Increase flow:
- Validate position, owner, pool, custodies, oracle accounts, and token accounts.
- Calculate new size and collateral values.
- Calculate required extra backing liquidity.
- Long increase checks free SOL.
- Short increase checks free USDC.
- Transfer extra collateral from user to custody.
- Increase locked custody
token_locked. - Increase collateral custody
token_owned. - Increase position size, collateral, and locked amount.
Increase liquidity check:
- This path correctly checks free liquidity before increasing the lock.
Decrease flow:
- Validate requested size decrease.
- Calculate proportional collateral to return.
- Calculate
locked_amount_to_release. - Calculate PnL for the decreased portion.
- Calculate settlement value.
- Convert settlement into selected receive asset.
- If the receive asset matches the locked backing asset, require the payout is bounded by
locked_amount_to_release. - If the receive asset is the cross asset, require free liquidity in the receive custody.
- Transfer settlement to the user.
- Release locked backing.
- Subtract the transferred token amount from the custody that pays.
- Reduce position size, collateral, and locked amount.
Decrease liquidity check by case:
- Decrease long, receive SOL: checks payout is bounded by released locked SOL.
- Decrease long, receive USDC: checks free USDC.
- Decrease short, receive USDC: checks payout is bounded by released locked USDC.
- Decrease short, receive SOL: checks free SOL.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
add_collateral_perp.rs
Current flow:
- Validate position, owner, pool, custodies, oracle accounts, funding account, and token accounts.
- Calculate collateral value from selected payment asset.
- Transfer collateral from user to custody.
- Increase selected custody
token_owned. - Increase position collateral fields.
- Recalculate health values.
Liquidity check:
- No pool payout happens.
- No new locked backing is created.
- No free-liquidity check is needed.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
remove_collateral_perp.rs
Current flow:
- Validate position, owner, pool, custodies, oracle accounts, and token accounts.
- Calculate removed collateral value.
- Calculate new collateral and margin values.
- Check new margin stays above liquidation margin plus buffer.
- Check selected custody has
token_owned - token_locked >= withdrawal_tokens. - Transfer withdrawal to the user.
- Subtract withdrawn amount from selected custody
token_owned. - Update position collateral fields.
Liquidity check:
- The current check uses free liquidity, not total
token_owned. - Collateral withdrawal does not release locked backing.
- Correct logic should require
receive_custody.token_owned - receive_custody.token_locked >= withdrawal_tokens.
Extra checks:
- It checks leverage and margin health.
- It includes a liquidation-margin buffer.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
liquidate_perp.rs
Current flow:
- Validate position, liquidator, pool, custodies, oracle accounts, and token accounts.
- Check the position is liquidatable.
- Calculate owner settlement and liquidator reward as USD amounts.
- Select the locked backing asset as payout asset: long pays SOL and short pays USDC.
- Convert owner settlement and liquidator reward into locked backing tokens.
- Require total payout is bounded by
position.locked_amount. - Transfer owner settlement and liquidator reward from locked backing custody.
- Subtract the transferred payout tokens from locked backing custody
token_owned. - Release locked backing.
- Mark position liquidated.
- Update pool open interest.
Liquidity check:
- These payouts come from locked backing custody.
- Long liquidation pays SOL.
- Short liquidation pays USDC.
- Total payout must be less than or equal to released locked backing.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
update_borrow_fees.rs
Current flow:
- Validate the perp position, pool, and relevant custody.
- Check the position is not liquidated.
- Calculate elapsed time since last borrow-fee update.
- Calculate borrow fees from utilization and borrow rate.
- Add fees to position accrued borrow fees.
- Update borrow-fee timestamp and snapshot fields.
Liquidity check:
- No pool payout happens.
- No lock is created or released.
- No free-liquidity check is needed.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
manage_tp_sl_orders.rs
Current flow:
- Validate the owner, orderbook, and target position.
- Add, update, or remove take-profit and stop-loss orders.
- Store trigger price, size percent, and receive asset.
- Emit TP/SL order events.
Liquidity check:
- No pool payout happens.
- No lock is created or released.
- No free-liquidity check is needed.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
execute_tp_sl_order.rs Perp Branch
Current flow:
- Validate TP/SL order and trigger condition.
- Calculate closed perp size.
- Calculate PnL, fees, and settlement.
- Convert settlement into selected receive asset.
- Check same-asset payout against released locked backing or cross-asset payout against free liquidity.
- Transfer settlement to the user.
- Release locked backing.
- Subtract the transferred settlement tokens from the custody that pays.
- Reduce or close the perp position.
Liquidity check by case:
- Perp long TP/SL, receive SOL: checks payout is bounded by released locked SOL.
- Perp long TP/SL, receive USDC: checks free USDC.
- Perp short TP/SL, receive USDC: checks payout is bounded by released locked USDC.
- Perp short TP/SL, receive SOL: checks free SOL.
To-do / extra edits:
- This has been perfected and there is nothing left to do.