Integration

Read markets and build transactions.

Everything you need to read markets and build trades lives in the program's IDL (target/idl/carry.json) and the PDA seeds below.

Derive accounts

import { PublicKey } from "@solana/web3.js"

const enc = new TextEncoder()
const coinBytes = (coin: string) => { const b = new Uint8Array(8); b.set(enc.encode(coin)); return b }
const i64 = (n: bigint) => { const b = new Uint8Array(8); new DataView(b.buffer).setBigInt64(0, n, true); return b }
const u32 = (n: number) => { const b = new Uint8Array(4); new DataView(b.buffer).setUint32(0, n, true); return b }

export const pool = (program: PublicKey) => PublicKey.findProgramAddressSync([enc.encode("pool")], program)[0]
export const vault = (program: PublicKey) => PublicKey.findProgramAddressSync([enc.encode("vault")], program)[0]
export const market = (program: PublicKey, venue: 0 | 1, coin: string, maturity: bigint) =>
  PublicKey.findProgramAddressSync([enc.encode("market"), Uint8Array.of(venue), coinBytes(coin), i64(maturity)], program)[0]
export const position = (program: PublicKey, mkt: PublicKey, owner: PublicKey, id: number) =>
  PublicKey.findProgramAddressSync([enc.encode("position"), mkt.toBytes(), owner.toBytes(), u32(id)], program)[0]
export const lp = (program: PublicKey, owner: PublicKey) =>
  PublicKey.findProgramAddressSync([enc.encode("lp"), owner.toBytes()], program)[0]

Read a market

const m = await program.account.market.fetch(market(programId, 0, "BTC", maturity))
const periodsPerYear = m.periodSecs === 3600 ? 8760 : 1095
const anchorApr = Number(m.anchor) * periodsPerYear / 1e12

Quote a trade off chain

Mirror the integer math exactly, so the rate you show is the rate the program will lock.

const SCALE = 1_000_000_000_000n
function fixedRate(anchor: bigint, skew: bigint, n0: bigint, n1: bigint, nMax: bigint, payFixed: boolean) {
  const num = skew * (n0 + n1)
  const den = 2n * nMax
  const q = num / den
  const up = num % den !== 0n && num > 0n
  const down = num % den !== 0n && num < 0n
  return anchor + (payFixed ? (up ? q + 1n : q) : (down ? q - 1n : q))
}
const fee = (notional: bigint, spread: bigint, remaining: bigint) =>
  (notional * spread * remaining + SCALE - 1n) / SCALE

Open a position

const ix = await program.methods
  .openPosition(id, { payFixed: {} }, new BN(notional), new BN(margin), new BN(limitRate))
  .accounts({ owner, pool, market: mkt, position: pos, ownerUsdc, vault, feeVault, tokenProgram: TOKEN_PROGRAM_ID, systemProgram: SystemProgram.programId })
  .instruction()

Add a compute budget instruction with a measured limit, put the owner first as fee payer, and send the transaction for the wallet to sign before anything else.

Deposits and withdrawals take every market

deposit, withdraw pass every active market account as remaining accounts, strictly sorted by address, so the share price marks the whole book.

const markets = (await program.account.market.all()).map((a) => a.publicKey)
markets.sort((a, b) => Buffer.compare(a.toBuffer(), b.toBuffer()))
await program.methods.deposit(new BN(amount))
  .accounts({ owner, pool, lp: lpAcc, ownerUsdc, vault, tokenProgram: TOKEN_PROGRAM_ID, systemProgram: SystemProgram.programId })
  .remainingAccounts(markets.map((pubkey) => ({ pubkey, isSigner: false, isWritable: false })))
  .rpc()

Listen to events

FundingPrinted, PositionOpened, MarginAdded, PositionExited, Deposited, WithdrawalRequested, Withdrawn, FeesSwept, MarketCreated, MarketClosed, PoolUpdated, PausedSet, AuthorityTransferred.

program.addEventListener("fundingPrinted", (e) => console.log(e.venue, e.coin, e.sourceTs.toString(), e.rate.toString()))