Integration

Everything below reads or writes the program directly. There is no off-chain service to call.

Find the accounts

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

const PROGRAM = new PublicKey("5sNmL3fVsgLEBEQBiVHDR9gwrdUo2uEn7RWhcWhE8Qf6");
const [market] = PublicKey.findProgramAddressSync([Buffer.from("market")], PROGRAM);
const [mint] = PublicKey.findProgramAddressSync([Buffer.from("power")], PROGRAM);
const [poolTokens] = PublicKey.findProgramAddressSync([Buffer.from("pool_tokens")], PROGRAM);
const vaultOf = (owner: PublicKey) =>
  PublicKey.findProgramAddressSync([Buffer.from("vault"), owner.toBuffer()], PROGRAM)[0];
const PYTH_SOL_USD = new PublicKey("7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE");

Read the price the program reads

const acc = await connection.getAccountInfo(PYTH_SOL_USD);
const d = acc!.data;
const full = d[40] === 1;
const expo = d.readInt32LE(89);
const price = Number(d.readBigInt64LE(73)) * 10 ** expo;
const conf = Number(d.readBigUInt64LE(81)) * 10 ** expo;
const publishTime = Number(d.readBigInt64LE(93));

Value a token

const nf = Number(market.nfWad) / 1e18;
const indexUsd = (nf * price * price) / 100;
const indexSolPerToken = (nf * price) / 100;

Quote a swap

const FEE_BPS = 30n;
function swapOut(amountIn: bigint, reserveIn: bigint, reserveOut: bigint) {
  const a = amountIn * (10_000n - FEE_BPS);
  return (reserveOut * a) / (reserveIn * 10_000n + a);
}
// buy: swapOut(lamportsIn, market.poolSol, market.poolTok)
// sell: swapOut(tokensIn, market.poolTok, market.poolSol)

Pass a min_out below your quote to bound slippage; the program refuses anything less with Slippage.

Check a vault

const debtLamports = (debt * BigInt(Math.round(price * 1e8)) / 10_000_000_000n) * market.nfWad / 10n ** 18n;
const healthy = vault.collateral * 10_000n >= debtLamports * 15_000n;

A vault that is not healthy can be liquidated by anyone holding tokens: call liquidate(amount) with amount ≤ debt / 2.

Keep funding current

poke takes only the market and the price account and can be sent by anyone. Funding is also applied inside every mint, withdraw and liquidation.

Next: API reference