Build buy and sell transactions
Build exact-input and exact-output swaps with explicit token units and limits.
Resolve trade accounts
Read the pool and both mint accounts together before building a trade. Read each mint’s owning token program and active transfer-tax schedule.
The nested Trade group requires the user signer, writable pool, pool authority, both token programs, both mints, both user token accounts, and both writable vaults. User accounts must match their respective mints and belong to the signer. The program uses wallet ownership, not a delegate allowance.
The snippet uses addresses from those reads. It also uses pool_addresses to derive the authority.
Build a buy
A buy sends stored quote token B and receives stored base token A. Set a_to_b = false. Use swap for exact input. amount is gross quote input. limit is minimum net base output after transfer tax.
The function takes the Trade account group above. It uses the shared instruction helper. Sign the resulting transaction with trade.user and its fee payer.
Build a sell
A sell sends stored base token A and receives stored quote token B. Set a_to_b = true. Keep the account roles unchanged. amount is gross base input. limit is minimum net quote output.
This function uses the same imports, Trade type, and helper as the buy example. It works on a trading curve or AMM. A curve sell cannot spend virtual quote reserves.
Request exact output
Use SwapExactOut when you need a specified net receipt. amount becomes required net output. limit becomes maximum gross input. For a buy, the units are base output and quote input. For a sell, reverse the direction and units.
The example assumes trade, required_base_atoms, and maximum_quote_atoms are already prepared.
Calculate a quote
Use integer amounts throughout. Select the input and output mint from a_to_b. The formulas below describe a full exact-input fill. input_bps and output_bps are active mint transfer taxes. Their caps also come from the mints.
- Calculate the input transfer tax. Subtract it from gross input.
- Calculate the 125-basis-point protocol fee on received input. Calculate the separate 50-basis-point creator fee only for a standard curve. Round each fee up.
- Subtract both trade fees. Use the remainder as effective pricing input.
- Add virtual reserves to real reserves for curve pricing. Apply the constant-product output formula and round down.
- Subtract output transfer tax. Compare that net amount with the user’s output limit.
fossa_dex::math exposes fee, gross, output, and input. The exact handler remains authoritative for cap checks and partial fills. Re-read both tax schedules when the Solana epoch changes.
transfer_fee(gross, bps, cap) = min(cap, ceil(gross × bps / 10_000))
received = gross_input - input_transfer_fee
effective = received - ceil(received × 125 / 10_000) - creator_fee
gross_output = floor(reserve_out × effective / (reserve_in + effective))
net_output = gross_output - output_transfer_fee
Handle curve limits
Before quoting a curve, check status == 0 and reserve_b < target_b. A curve at its target rejects trades until graduation. An AMM uses status == 1.
For an exact-input curve buy, the handler clips effective input to target_b - reserve_b. It recalculates the gross input needed to cover transfer tax and separately rounded trade fees. The minimum output still applies. An exact-output request must fit without clipping its requested output.
The cap uses gross base tokens leaving the curve: at most 75% of initial total supply per buy. Public curves use initial_base; model curves use the fixed 100-million-token supply. The base reserve must also remain at or above minimum_a.
Do not estimate final volume from the requested input. Read confirmed transfers after execution. See trade history.