Trade
Quotes and swaps
SDK quotes use the same integer math as the program, with the same rounding. If the pool doesn't move between the quote and the swap, the swap returns exactly the quoted amount, to the unit.
swap below is still open to anyone, for integrators who want to trade against the locked pool directly.Quote a swap
// Buy with 0.1 SOL, accept up to 1% slippage.
const q = await client.quote(mint, true, 100_000_000n, 100);
q.amountOut; // tokens (base units) you get
q.minOut; // amountOut minus 1%: the slippage guard
q.fee; // the 1% fee, lamports
q.priceImpactBps; // execution vs spot, fee includedamountIn is lamports on a buy and token base units on a sell; amountOut is the other side. The default slippage is 1%.
| amountOut | bigint | Tokens on a buy, lamports on a sell. |
| fee | bigint | The total 1% fee in lamports, always charged in SOL. |
| teamFee | bigint | The 20% of the fee that goes to the team; the rest stays in the pool. |
| priceImpactBps | bigint | Execution price vs spot price, fee included. |
| spotPriceQ64 | bigint | Spot price before the swap, Q64.64. |
| solReserveAfter, tokenReserveAfter | bigint | The pool's reserves after this swap. |
| minOut | bigint | Only on client.quote: amountOut reduced by the slippage you passed. |
Swap
swapInstructions quotes and returns ready-to-sign instructions with min_out already set. It also creates the user's token account if needed.
import { Transaction } from "@solana/web3.js";
const { quote, instructions } = await client.swapInstructions(mint, wallet.publicKey, true, 100_000_000n, 100);
const tx = new Transaction().add(...instructions);
const sig = await wallet.sendTransaction(tx, connection);If the pool moves past your slippage before the swap lands, it fails with SlippageExceeded instead of filling at a worse price. Swaps are never paused.
Pure functions
When you already have the reserves (from the Data API or your own indexer), quote without any RPC call:
import { quoteBuy, quoteSell, minOutWithSlippage, spotPrice, swapInstructions } from "@proglet/sdk";
const q = quoteBuy(pool.solReserve, pool.tokenReserve, 100_000_000n);
const minOut = minOutWithSlippage(q.amountOut, 50); // 0.5%
const ixs = swapInstructions({ mint, user, isBuy: true, amountIn: 100_000_000n, minOut });
const price = spotPrice(pool.solReserve, pool.tokenReserve); // SOL per whole token, for displayquote_swap on chain
The program has a read-only quote_swap(is_buy, amount_in) instruction. Simulate it and read the Anchor return data (a borsh SwapQuote) to quote without this SDK's math. simulateQuote does it for you:
const onChain = await client.simulateQuote(mint, true, 100_000_000n, anyExistingAccount);Fees
- Every swap pays 1% (
SWAP_FEE_BPS), always in SOL: taken from the SOL you pay on a buy, from the SOL you get on a sell. - 80% of the fee stays in the pool, so trading deepens it. 20% (
TEAM_SHARE_BPS) goes to the pool's team fee vault. - Anyone can call
sweepto move a pool's team fees, but only to the configured treasury.
