Smart Contract
Expiry Futures Smart Contracts
Expiry Futures Smart Contracts
This page documents expiry-futures instruction files in execution order.
For expiry futures:
Long locked backing = SOL.
Short locked backing = USDC.
open_future.rs
Current flow:
- Validate future parameters, owner, pool, custodies, oracle accounts, funding account, and token accounts.
- Read current SOL and USDC prices.
- Calculate position size and collateral value.
- Calculate fixed interest rate.
- Calculate
locked_amount. - For a long,
locked_amountis SOL backing. - For a short,
locked_amountis USDC backing. - Check free liquidity in the locked custody.
- Long checks
sol_custody.token_owned - sol_custody.token_locked >= locked_amount. - Short checks
usdc_custody.token_owned - usdc_custody.token_locked >= locked_amount. - Transfer user collateral into the selected collateral custody.
- Increase collateral custody
token_owned. - Increase locked custody
token_locked. - Add fixed-rate exposure.
- Store future state.
Liquidity check:
- This file correctly checks free liquidity before creating a new lock.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
open_limit_future.rs
Current flow:
- Validate limit future parameters, owner, pool, custodies, oracle accounts, funding account, and token accounts.
- Read prices.
- Calculate future size, collateral, fixed rate, and future
locked_amount. - Transfer user collateral into the selected collateral custody.
- Increase collateral custody
token_owned. - Store pending future state.
- Do not increase
token_locked. - Do not add fixed-rate exposure yet.
Liquidity check:
- Current code does not check pool backing liquidity at placement.
- Because the file does not lock liquidity at placement, backing liquidity is checked in
execute_limit_future.rs.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
execute_limit_future.rs
Current flow:
- Validate the pending future and keeper/execution authority.
- Check limit trigger conditions.
- Set execution time and entry fields.
- Calculate liquidation price.
- Long execution checks
sol_custody.token_owned - sol_custody.token_locked >= future.locked_amount. - Short execution checks
usdc_custody.token_owned - usdc_custody.token_locked >= future.locked_amount. - Long execution increases
sol_custody.token_lockedbyfuture.locked_amount. - Short execution increases
usdc_custody.token_lockedbyfuture.locked_amount. - Add fixed-rate exposure.
- Check
sol_custody.token_locked <= sol_custody.token_owned. - Check
usdc_custody.token_locked <= usdc_custody.token_owned. - Mark the limit future as executed.
Liquidity check:
- This file checks free liquidity before increasing
token_locked. - This is the correct file where execution-time free-liquidity validation belongs.
- The file keeps a final post-lock solvency assertion.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
close_future.rs
Current flow:
- Validate the future, owner, close percentage, pool, custodies, oracle accounts, and token accounts.
- Calculate closed size.
- Calculate closed collateral amount.
- Calculate
locked_amount_to_release. - Calculate current PnL.
- Calculate closing fee.
- Calculate
settlement_usd = collateral + PnL - fee, floored at zero. - Convert
settlement_usdinto the selected receive asset. - 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 selected receive custodytoken_owned. - Release locked backing.
- Reduce or close the future.
- Remove fixed-rate exposure.
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 subtracts settlement from selected custody.
- 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_future.rs
Current flow:
- This file delegates to
cancel_limit_future.rs. - It maps close-limit-future parameters into cancel-limit-future parameters.
Liquidity check:
- It inherits the refund behavior from
cancel_limit_future.rs.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
cancel_limit_future.rs
Current flow:
- Validate pending future, owner, close percentage, 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 future.
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.
settle_expired_future.rs
Current flow:
- Validate the expired future, settler, pool, custodies, oracle accounts, and token accounts.
- Read final settlement price.
- Mark the future expired.
- Calculate final settlement amount and PnL.
- Select the locked backing asset as settlement asset: long settles in SOL and short settles in USDC.
- Require settlement payout is bounded by
future.locked_amount. - Transfer settlement to the owner.
- Subtract settlement from locked custody
token_owned. - Release locked backing.
- Mark the future settled.
Liquidity check by case:
- Long settles in SOL and checks payout is bounded by released locked SOL.
- Short settles in USDC and checks payout is bounded by released locked USDC.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
add_collateral_future.rs
Current flow:
- Validate the future, owner, pool, custodies, oracle accounts, funding account, and token accounts.
- Calculate collateral value from selected payment asset.
- Transfer user collateral into custody.
- Increase selected custody
token_owned. - Increase future 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_future.rs
Current flow:
- Validate the future, owner, pool, custodies, oracle accounts, and token accounts.
- Calculate withdrawn collateral value.
- Calculate new collateral value.
- Check minimum collateral requirement.
- Check max leverage.
- Check the position remains healthy.
- Check selected custody has
token_owned - token_locked >= requested_amount. - Transfer requested collateral amount to the user.
- Subtract requested amount from selected custody
token_owned. - Update future collateral fields.
Liquidity check:
- Collateral withdrawal does not release locked backing.
- Current code checks free liquidity before transfer.
- The check is
receive_custody.token_owned - receive_custody.token_locked >= withdrawal_amount.
Extra checks:
- It checks minimum collateral.
- It checks max leverage.
- It checks the future remains healthy after withdrawal.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
liquidate_future.rs
Current flow:
- Validate the future, liquidator, pool, custodies, oracle accounts, and token accounts.
- Check the future is liquidatable.
- Calculate liquidator reward and any remaining owner amount as USD amounts.
- Select the locked backing asset as payout asset: long pays SOL and short pays USDC.
- Convert owner amount and liquidator reward into locked backing tokens.
- Require total payout is bounded by
future.locked_amount. - Transfer owner amount and liquidator reward from locked backing custody.
- Subtract transferred tokens from locked backing custody
token_owned. - Release locked backing.
- Mark future liquidated or settled.
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.
execute_tp_sl_order.rs Future Branch
Current flow:
- Validate TP/SL order and trigger condition.
- Calculate closed future size 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 and remove fixed-rate exposure.
- Reduce or close the future.
Liquidity check by case:
- Future long TP/SL, receive SOL: checks payout is bounded by released locked SOL.
- Future long TP/SL, receive USDC: checks free USDC.
- Future short TP/SL, receive USDC: checks payout is bounded by released locked USDC.
- Future short TP/SL, receive SOL: checks free SOL.
To-do / extra edits:
- This has been perfected and there is nothing left to do.