Quickstart
Install the SDK, read the protocol config, then take size from a resting ask. Every sample below runs as written.
Install
npm install @otcdex/sdk @solana/web3.jsConnect
TypeScript
import { Connection } from '@solana/web3.js';
import { OtcClient } from '@otcdex/sdk';
const connection = new Connection(process.env.RPC_URL!, 'confirmed');
const client = new OtcClient(connection);
const config = await client.getConfig();
console.log('fee bps:', config.feeBps); // 30 = 0.30% total, 0.15% per side
console.log('paused:', config.paused);Buy from an existing ask
TypeScript
import { quoteFill, fillListing } from '@otcdex/sdk';
import { Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
const listing = await client.getListing(listingAddress);
// Pure arithmetic, no network call. 50 = 0.5% slippage headroom.
const quote = quoteFill(listing!, amountToBuy, config.feeBps, 50);
console.log('you pay: ', quote.buyerPays);
console.log('seller gets: ', quote.sellerReceives);
console.log('protocol fee: ', quote.protocolFee);
const ix = fillListing({
buyer: wallet.publicKey,
listing: listing!.address,
tokenMint: listing!.tokenMint,
quoteMint: listing!.quoteMint,
tokenProgram: listing!.tokenProgram,
quoteTokenProgram: listing!.quoteProgram,
buyerTokenAccount,
buyerQuoteAccount,
sellerQuoteAccount,
amount: amountToBuy,
maxPricePerToken: quote.maxPricePerToken,
// Delivery floor. Required: it is what stops a transfer-fee mint from
// shipping you fewer tokens than you paid for.
minTokensOut: quote.buyerReceives,
});
await sendAndConfirmTransaction(connection, new Transaction().add(ix), [wallet]);The 50 passed to quoteFill() is slippage headroom in basis points. It widens the bound you sign, it does not change the price you pay.