Integration
Finding the pool
Sort the mints ascending, hash them, and derive the pool:
import { PublicKey } from "@solana/web3.js"
import { createHash } from "crypto"
const sorted = mints.slice().sort((a, b) => Buffer.compare(a.toBuffer(), b.toBuffer()))
const mintSet = createHash("sha256").update(Buffer.concat(sorted.map((m) => m.toBuffer()))).digest()
const [pool] = PublicKey.findProgramAddressSync([Buffer.from("pool"), mintSet], PROGRAM_ID)
mint_set_hash in the program uses Solana's hashv (SHA-256 over the concatenated keys), so the two match.
Swapping from your program
Accounts for swap, in order:
| # | Account | Writable | Signer |
|---|---|---|---|
| 0 | trader | yes | |
| 1 | pool | yes | |
| 2 | mint_in | ||
| 3 | mint_out | ||
| 4 | vault_in (pool's ATA of mint_in) | yes | |
| 5 | vault_out (pool's ATA of mint_out) | yes | |
| 6 | trader_in | yes | |
| 7 | trader_out | yes | |
| 8 | token_program_in | ||
| 9 | token_program_out |
Arguments: coin_in: u8, coin_out: u8 (indexes into Pool.mints), amount_in: u64, min_out: u64.
Quoting
Read the Pool account and run the same math off chain, or simulate the transaction and read the Swapped event: amount_out, fee and band_crossings.
Events
| Event | Fields |
|---|---|
PoolCreated | pool, mints |
LiquidityChanged | pool, owner, band, added, radius, amounts |
Swapped | pool, trader, coin_in, coin_out, amount_in, amount_out, fee, band_crossings |
FeesCollected | pool, owner, coin, amount |
Compute
Swap cost does not grow with the number of coins: the program tracks sums over the reserves, as the paper describes, so a two-coin trade touches only those two. It grows with the number of band edges a trade crosses.
Next: API reference