Smart Contract
Options Smart Contracts
Options Smart Contracts
This page documents option instruction files in execution order.
For options:
Call option locked backing = SOL.
Put option locked backing = USDC.
open_option.rs
Current flow:
- Validate the option parameters, owner, pool, custodies, oracle accounts, funding account, and token accounts.
- Read current oracle prices.
- Price the option with Black-Scholes.
- Calculate
quantity_scaledfrom the amount the user pays. - Calculate
locked_amount. - For a call,
locked_amountis the underlying token amount, for example SOL contracts. - For a put,
locked_amountisquantity * strikein USDC. - Calculate the open fee.
- Check the user funding account has
premium + fee. - Check
locked_custody.token_owned - locked_custody.token_locked >= locked_amount. - Transfer
premium + feefrom the user topay_custody. - Increase
pay_custody.token_ownedbypremium + fee. - Store premium fields on the option account.
- Increase
locked_custody.token_lockedbylocked_amount. - Check
locked_custody.token_owned >= locked_custody.token_locked. - Add fixed-rate exposure for the locked custody.
- Store option state.
Liquidity check:
- The file checks free locked-custody liquidity before user payment and before increasing
token_locked. - User premium does not count toward the collateral needed to open the same option.
- The file keeps the final
locked_custody.token_owned >= locked_custody.token_lockedassertion after locking.
Extra detail:
- If
pay_custody == locked_custody, the newly paid premium is still transferred after the lock-availability check. - This keeps option backing strict: the pool must already have enough free backing before selling the option.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
open_limit_option.rs
Current flow:
- Validate the limit option parameters, owner, pool, custodies, oracle accounts, funding account, and token accounts.
- Read current oracle prices.
- Calculate the expected quantity and premium fields.
- Calculate the open fee.
- Calculate
total_payment = premium + fee. - Check the user funding account has
total_payment. - Transfer
total_paymentfrom the user topay_custody. - Increase
pay_custody.token_ownedbytotal_payment. - Calculate the future
locked_amount. - Store the pending option with
executed = false. - Do not increase
locked_custody.token_locked. - Do not add fixed-rate exposure yet.
Liquidity check:
- No pool liquidity is locked at placement.
- Because no lock is created at placement, free-liquidity backing should be checked at execution, not placement.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
execute_limit_option.rs
Current flow:
- Validate the pending option account and keeper/execution authority.
- Check the limit trigger conditions.
- Recalculate the option quantity and locked amount at execution.
- For a call, calculate the SOL amount to lock.
- For a put, calculate the USDC amount to lock.
- Check
locked_custody.token_owned - locked_custody.token_locked >= new_locked_amount. - Increase
locked_custody.token_lockedby the new locked amount. - Check
locked_custody.token_owned >= locked_custody.token_locked. - Add fixed-rate exposure.
- Mark the option as executed.
- Store execution fields.
Liquidity check:
- This file checks free liquidity before increasing
token_locked. - It also keeps a post-lock solvency assertion after the lock is created.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
close_option.rs
Current flow:
- Validate the option, owner, quantity, pool, custodies, and token accounts.
- Calculate the proportional amount being closed.
- Calculate the close premium using current Black-Scholes price.
- Calculate the protocol close fee.
- Calculate the raw refund amount from the locked backing asset.
- Subtract the close fee from the raw refund.
- Convert the refund value into the user's selected receive asset.
- If the receive asset matches the locked backing asset, require
settlement_tokens <= unlock_amount. - If the receive asset is the cross asset, require
receive_custody.token_owned - receive_custody.token_locked >= settlement_tokens. - Unlock the option backing.
- Subtract
settlement_tokensfrom the selected receive custodytoken_owned. - Transfer
settlement_tokensto the user. - Reduce or close the option account.
- Remove fixed-rate exposure.
Liquidity check by case:
- Call, receive SOL: allows payout from locked SOL, bounded by the SOL unlock amount.
- Call, receive USDC: checks free USDC because USDC is cross-asset.
- Put, receive USDC: allows payout from locked USDC, bounded by the USDC unlock amount.
- Put, receive SOL: checks free SOL because SOL is cross-asset.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
close_limit_option.rs
Executed option close flow:
- Validate the option, owner, quantity, pool, custodies, oracle accounts, and token accounts.
- If the option is executed and valid, calculate close premium and close fee.
- Calculate the refund from the locked backing asset.
- Convert the refund value into the selected receive asset.
- Check same-asset payout against unlocked backing or cross-asset payout against free liquidity.
- Decrease
locked_custody.token_lockedby the unlock amount. - Subtract
settlement_tokensfrom selected receive custodytoken_owned. - Transfer
settlement_tokensto the user. - Reduce or close the option account.
- Remove fixed-rate exposure.
Executed option liquidity check by case:
- Call, receive SOL: allows payout from locked SOL, bounded by the unlock amount.
- Call, receive USDC: checks free USDC because USDC is cross-asset.
- Put, receive USDC: allows payout from locked USDC, bounded by the unlock amount.
- Put, receive SOL: checks free SOL because SOL is cross-asset.
Unexecuted option cancel/refund flow:
- If the option is not executed, validate the funding account mint matches
pay_custody. - Calculate the proportional premium refund.
- Check
pay_custody.token_owned - pay_custody.token_locked >= premium_refund. - Transfer the premium refund to the user.
- Subtract
premium_refundfrompay_custody.token_owned. - Reduce or close the pending option account.
Unexecuted option liquidity note:
- This refund is not locked backing.
- Current code uses
pay_custody.token_owned - pay_custody.token_locked >= premium_refund.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
cancel_limit_option.rs
Current flow:
- This file is an alias/delegation path.
- It calls
close_limit_option.rswith the same parameters.
Liquidity check:
- It inherits all executed and unexecuted logic from
close_limit_option.rs.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
exercise_option.rs
Current flow:
- Validate the option, owner, exercise quantity, pool, locked custody, and token accounts.
- Calculate
unlock_amountproportionally. - Check
locked_custody.token_locked >= unlock_amount. - For a call, require current price is above strike.
- For a call, calculate profit from
current price - strike. - Require call exercise profit is bounded by
unlock_amount. - Transfer call exercise profit from locked SOL custody to the user.
- For a put, require strike is above current price.
- For a put, calculate profit from
strike - current price. - Require put exercise profit is bounded by
unlock_amount. - Transfer put exercise profit from locked USDC custody to the user.
- Subtract the transferred profit from
locked_custody.token_owned. - Decrease
locked_custody.token_lockedbyunlock_amount. - Remove fixed-rate exposure.
- Update option exercise state.
Liquidity check:
- Same-asset payout from locked backing is allowed.
- The file checks locked balance is at least the unlock amount.
- It explicitly checks
profit <= unlock_amount.
Accounting note:
- The file transfers profit from custody.
- The file reduces
token_locked. - The file reduces
token_ownedby the transferred profit amount.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
auto_exercise.rs
Current flow:
- Check whether an expired option is in the money.
- Calculate the profit amount.
- Require profit is bounded by the locked option amount.
- Transfer profit immediately from locked custody to the user.
- Subtract transferred profit from locked custody
token_owned. - Mark the option invalid/exercised.
- Decrease
locked_custody.token_lockedby the option amount. - Remove fixed-rate exposure.
Liquidity check:
- Token transfer happens immediately in this instruction.
- Same-asset payout from locked backing is allowed.
- Profit payout must be bounded by the locked option amount.
To-do / extra edits:
- This has been perfected and there is nothing left to do.
execute_tp_sl_order.rs Option Branch
Current flow:
- Validate the TP/SL order.
- Calculate the option settlement amount.
- 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.
- Decrease locked SOL for calls or locked USDC for puts.
- Remove fixed-rate exposure.
- Subtract settlement from selected receive custody
token_owned. - Reduce or close the option account.
Liquidity check by case:
- Call TP/SL, receive SOL: checks payout is bounded by released locked SOL.
- Call TP/SL, receive USDC: checks free USDC.
- Put TP/SL, receive USDC: checks payout is bounded by released locked USDC.
- Put TP/SL, receive SOL: checks free SOL.
To-do / extra edits:
- This has been perfected and there is nothing left to do.