Trade
Instructions

Program

Instructions

Every instruction with its accounts, arguments, errors and event.

Every instruction with its accounts in order, its arguments, the program errors it raises and the event it emits. Amounts are USDC base units (6 decimals). A PDA's seeds are listed as text constants and account names.

deposit

Moves USDC from the owner's token account into the vault and adds it to the trader account's deposited total, creating the trader account on the first deposit.

Accounts
FieldTypeDescription
ownersigner, writableThe trader wallet.
configreadPDA ["config"]
traderwritablePDA ["trader", owner]
sourcewritableThe owner's USDC token account.
vaultwritableThe program-owned USDC vault, config.vault.
token_programreadSPL Token. TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
system_programreadSystem program. 11111111111111111111111111111111
event_authorityreadAnchor event authority PDA ["__event_authority"], for events emitted by CPI.
programreadThis program.
Arguments
FieldTypeDescription
amountu64USDC base units (6 decimals). Above zero.

Raises: ZeroAmount, MathOverflow. Emits DepositMade. Discriminator f223c68952e1f2b6.

escape_withdraw

Once more than escape_slots slots have passed since the last batch landed, pays the owner their available equity (at most what the vault holds beyond unswept fees) and closes the trader account to the owner. No sequencer signature.

Accounts
FieldTypeDescription
ownersigner, writableThe trader wallet.
configreadPDA ["config"]
traderwritablePDA ["trader", owner]
vaultwritableThe program-owned USDC vault, config.vault.
destinationwritableThe owner's USDC token account.
token_programreadSPL Token. TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
event_authorityreadAnchor event authority PDA ["__event_authority"], for events emitted by CPI.
programreadThis program.

Raises: EscapeNotOpen, MathOverflow. Emits Escaped. Discriminator 387f1fcd723094e4.

initialize

Creates the config and the USDC vault, and sets the escape window, the sequencer and the fee recipient. The signer becomes the admin.

Accounts
FieldTypeDescription
adminsigner, writableThe config admin.
configwritablePDA ["config"]
usdc_mintreadThe USDC mint, a classic SPL Token mint (Token-2022 is refused).
vaultwritableThe program-owned USDC vault, config.vault. PDA ["vault"]
token_programreadSPL Token. TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
system_programreadSystem program. 11111111111111111111111111111111
Arguments
FieldTypeDescription
escape_slotsu64Slots without a batch before escape_withdraw opens. Above zero.
sequencerpubkeyThe engine key that posts batches and co-signs withdrawals.
fee_recipientpubkeyThe wallet whose USDC associated token account receives swept protocol fees.

Raises: InvalidEscapeSlots. Emits no event. Discriminator afaf6d1f0d989bed.

post_batch

Posts settlement batch number last + 1: the state hash, the cumulative protocol fees, and one entry per trader account passed as writable remaining accounts, in entry order. Each entry sets the settled equity and how much of the deposits and withdrawals the engine had credited.

Accounts
FieldTypeDescription
sequencersignerThe engine's key, matched against config.sequencer.
configwritablePDA ["config"]
event_authorityreadAnchor event authority PDA ["__event_authority"], for events emitted by CPI.
programreadThis program.
Arguments
FieldTypeDescription
batch_numberu64Exactly the last batch number plus one.
state_hash[u8; 32]SHA-256 of the engine's whole state after the batch.
fees_totalu64Cumulative protocol fees, in USDC base units. Never lower than the last batch reported.
entriesVec<BatchEntry>One per trader account in the remaining accounts, in the same order.
BatchEntry
FieldTypeDescription
settled_equityu64The account's equity at the batch, floored at zero.
deposits_creditedu64How much of Trader.deposited the engine had credited at the batch.
withdrawals_creditedu64How much of Trader.withdrawn the engine had credited at the batch.

Raises: NotSequencer, BatchOutOfOrder, FeesDecreased, EntryCountMismatch, InvalidTraderAccount, TraderAlreadyInBatch, CreditExceedsDeposits, CreditExceedsWithdrawals, MathOverflow. Emits BatchPosted. Discriminator ac7bea660ed54c24.

register_session

Records a session key and its expiry in the trader account, creating the account if needed. The default (all-zero) key revokes and sets the expiry to 0.

Accounts
FieldTypeDescription
ownersigner, writableThe trader wallet.
traderwritablePDA ["trader", owner]
system_programreadSystem program. 11111111111111111111111111111111
event_authorityreadAnchor event authority PDA ["__event_authority"], for events emitted by CPI.
programreadThis program.
Arguments
FieldTypeDescription
session_keypubkeyThe session key; the default key revokes.
expiryi64Unix seconds.

Raises: no program errors. Emits SessionRegistered. Discriminator 65746a2b98bd046e.

sweep_fees

Pays every reported, unswept protocol fee from the vault to the fee recipient's USDC associated token account. Anyone can send it.

Accounts
FieldTypeDescription
configwritablePDA ["config"]
vaultwritableThe program-owned USDC vault, config.vault.
fee_destinationwritableassociated token account of config.fee_recipient
token_programreadSPL Token. TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
event_authorityreadAnchor event authority PDA ["__event_authority"], for events emitted by CPI.
programreadThis program.

Raises: NothingToSweep, MathOverflow. Emits FeesSwept. Discriminator afe1624776422294.

update_config

Replaces the sequencer and the fee recipient. Admin only.

Accounts
FieldTypeDescription
adminsignerThe config admin.
configwritablePDA ["config"]
Arguments
FieldTypeDescription
sequencerpubkeyThe new sequencer.
fee_recipientpubkeyThe new fee recipient.

Raises: NotAdmin. Emits no event. Discriminator 1d9efcbf0a53db63.

withdraw

Pays an amount up to the trader's available equity from the vault to the owner's token account. Signed by the owner and co-signed by the sequencer.

Accounts
FieldTypeDescription
ownersignerThe trader wallet.
sequencersignerThe engine's key, matched against config.sequencer.
configreadPDA ["config"]
traderwritablePDA ["trader", owner]
vaultwritableThe program-owned USDC vault, config.vault.
destinationwritableThe owner's USDC token account.
token_programreadSPL Token. TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
event_authorityreadAnchor event authority PDA ["__event_authority"], for events emitted by CPI.
programreadThis program.
Arguments
FieldTypeDescription
amountu64USDC base units. Above zero and at most the available equity.

Raises: NotSequencer, ZeroAmount, InsufficientEquity, MathOverflow. Emits Withdrawn. Discriminator b712469c946da122.