Rust SDK
Build DEX transactions, calculate exact quotes, verify deployed code, and integrate the Jupiter adapter.
Introduction
The Rust SDK builds DEX instructions, derives addresses, decodes accounts, and calculates integer quotes. Builders return unsigned instructions. They do not create token accounts, sign, or submit transactions.
Use fossa_sdk::dex::Dex with an explicit program ID. The SDK does not select a DEX deployment for you. See SDK setup for the package and shared transaction tools.
Jupiter adapter
The fossa-jupiter crate implements Jupiter’s Amm interface for the Fossa DEX. FossaAmm identifies the pool, both mints, and both vaults through get_accounts_to_update. Its update method validates and caches those accounts; quotes reuse the SDK’s integer math and the active transfer-fee schedules without network calls. Failed updates invalidate cached quotes.
The adapter supports exact-input and exact-output swaps in both directions for curves and AMMs. It rejects partial fills and zero-output routes. get_swap_and_account_metas preserves the native instruction’s account order and uses token_transfer_authority as the signer.
The parity suite compares quotes with native DEX execution against committed program and account fixtures. Coverage includes legacy tokens, Token-2022 transfer fees, epoch changes, curves, and graduated AMMs. Swap::Placeholder carries direction and operation bits to swap_instruction_data, which encodes the native instruction for these tests. Jupiter must add routing support before its router can use Fossa pools.
Read a pool
Decode the pool before choosing a direction. mint_a and mint_b are stored asset roles. The unordered pair determines its address, not its trade direction.
Pool::decode checks the owner, discriminator, length, address derivations, and stored account relationships. Decode both mints with TokenMint to select their token programs. Token-account validation remains separate: check each vault’s mint and pool authority, and each user account’s mint and owner.
Quote and build a swap
All amounts use token atomic units. Read the current epoch and select each mint’s active transfer-fee schedule with TokenMint::transfer_fee(epoch). Pass fees in stored A/B order. Do not use a pending transfer-fee schedule early.
The example buys A with B. It quotes the debit, transfer taxes, trade fees, net output, and possible unused input. A 1% slippage tolerance becomes a minimum net output. An oversized final curve buy can use less than the requested input; the rest stays in the buyer’s account.
quote::swap_exact_input and quote::swap_exact_output calculate from a state snapshot. State can change before execution. The on-chain instruction enforces the supplied limit.
Choose swap limits
| Method | amount | limit | Quote helper |
|---|---|---|---|
dex.swap | Maximum gross input debit | Minimum net output | swap_exact_input |
dex.swap_exact_out | Required net output | Maximum gross input debit | swap_exact_output |
Set a_to_b = true to spend A and receive B. Set it to false to spend B and receive A. For exact output, calculate a quote with quote::swap_exact_output(pool, fees, args). Its gross_input includes input transfer tax and trading fees. The per-buy supply cap and graduation capacity still apply. See swap execution rules.
Add and remove liquidity
LiquidityArgs.amount is an exact number of LP-token atoms. It is not a deposit amount. Read the LP mint’s issued supply; the quote adds the pool’s permanently locked share internally.
For deposits, bounds are maximum gross token debits. For withdrawals, bounds are minimum net receipts after transfer tax. The example calculates the deposit required for an exact LP amount and accepts at most 1% more on either side.
When issued LP supply and locked_lp are zero but both reserves remain positive, quote::add_liquidity reconstructs locked units from the reserves and quotes a proportional deposit. It does not treat those reserves as an empty pool.
An emptied direct AMM with both reserves zero needs both deposit bounds as exact gross amounts. Use quote::initial_liquidity to derive the LP amount for that case. A withdrawal by the last direct-pool LP can redeem all accounted reserves when no locked share exists. Graduated founding liquidity remains locked.
Build a withdrawal
Quote the LP amount with quote::remove_liquidity, then set each bound from the net receipt. Do not apply slippage to the gross vault debit: a transfer-tax token can deliver less than that amount. The 0.025 SOL action fee is separate from both token outputs.
Create and graduate pools
| Task | Builder | Preparation |
|---|---|---|
| Public curve | initialize_curve(LaunchKeys, CurveInit) | Fixed-supply mint, token metadata, valid quote configuration, source accounts, vaults, and a fresh LP account signer. |
| Direct AMM | initialize_pool(LaunchKeys, council, PoolInit) | Deposit accounts and amounts; council signer for an authorization exception. |
| Model market | Router Protocol::initialize_model_group | Shared model group, fixed model supply, treasury allocation, backing vault, and the USDC curve accounts. |
| Graduation | graduate(PoolKeys, operator) | Curve at its target and the configured distributor’s signature. |
Use launch preparation for mint ownership, extension sizes, revocation, vault authorities, and required signers. token::transfer_fee_account_len gives the raw Token-2022 account size for transfer-fee support. instruction::create_account allocates that account; token::initialize_account initializes an account when its mint already exists. Builders do not perform this preparation.
quote::public_preset(config, now) checks the cached price age and returns the public curve parameters. quote::model_preset() returns the fixed model-market preset. quote::graduation(pool) calculates retained liquidity, base-token burn, and locked LP accounting. These helpers do not execute graduation or establish launch authorization.
Creating a curve or AMM costs 0.025 SOL. Depositing or withdrawing liquidity also costs 0.025 SOL per action, paid to DEX treasury. Treasury self-payments are skipped. Graduation charges no second creation fee. Account rent, network fees, trade fees, and transfer taxes are separate.
Use SOL and Token-2022
The DEX handles wrapped SOL through its SPL token account. token::create_wrapped_native returns account creation and initialization instructions; its lamport amount excludes the separate rent argument. After adding lamports to an existing wrapped SOL account, use token::sync_native. Use token::close_account to recover its remaining lamports when its ownership and close rules permit.
Decode supported mints and token accounts with TokenMint and token::TokenAccount. Unsupported extensions fail validation. Use TokenAccount::validate to check mint, authority, frozen state, and stricter vault rules. Transfer fees apply independently on each movement. Gross debits and net receipts can differ.
For withheld tax, token::harvest_transfer_tax moves tax from token accounts into the mint. dex.collect_transfer_tax collects approved public-pool tax; router Protocol::collect_reserve_tax handles model-token tax. These instructions have different authorities and fixed recipient rules.
Configuration and maintenance
| Operation | SDK methods |
|---|---|
| Global authority and recipients | initialize_global, update_global |
| Approved quote token | initialize_config, update_config |
| Price observation | refresh_quote; preview with quote::refresh_quote |
| Trading fees | collect_fees; preview with quote::collect_fees |
| Public-token transfer taxes | collect_transfer_tax |
| Existing pool recipient updates | update_recipients |
Read program reference for signer and recipient requirements. Quotes do not replace account validation or on-chain checks. In particular, an oracle estimate needs the configured reference pool, the graduated model-market state, and a valid observation window.
Submit and recover
Combine the returned instructions with any required token-account preparation. Supply every required signer, including fresh accounts created by the transaction. Use the SDK’s shared PreparedTransaction and PendingTransaction tools to sign, retain the exact signed bytes, submit, and reconcile confirmation. See transaction lifecycle and DEX transaction structure.
Reconcile an uncertain signature before preparing a replacement. A fresh transaction can execute an action twice. A successful local quote or simulation does not prove that the transaction executed.
Verify the deployed program
The deployment build record pins the source commit, lockfile digest, build settings, and artifact hash used for verification. The current deployment uses commit 844f876. Later package-version changes can alter compiled bytes even when the program’s Rust source files are unchanged.
Run the verifier from the programs repository with the recorded build prerequisites available. It fetches the pinned public commit into a fresh directory, rebuilds it, checks the artifact hash, and compares every executable byte with finalized on-chain ProgramData. It excludes loader metadata and requires any unused allocation padding to be zero. exactMatch: true confirms the comparison at the returned deployment slot; a mismatch fails verification. No signer is required.