Swap

Swap parameters

Swaps (BUY/SELL) are possible using exacts amounts IN or OUT. That makes 4 types of swaps, handled as an enum in the program. This translates to the following type for Anchor:

/// Swap inputs are gathered in a single object (Anchor's conversion from the rust enum)
/// Every usual type of swap is supported:
// - buyExactIn: {amountIn: number, minAmountOut: number}
// - buyExactOut: {maxAmountIn: number, amountOut: number}
// - sellExactIn: {amountIn: number, minAmountOut: number}
// - sellExactOut: {maxAmountIn: number, amountOut: number}
const swapParameters = {
  buyExactIn: [
    new anchor.BN(10_000_000), // 0.01 SOL
    new anchor.BN(1),
  ],
};

We'll use buyExactIn for the rest of this example.

SOL wrapping

If the quote token is WSOL, buying will require some wrapped SOL.

const transferIx = anchor.web3.SystemProgram.transfer({
  fromPubkey: wallet.publicKey,
  toPubkey: userTokenAccount1.address,
  lamports: swapParameters.buyExactIn[0].toNumber(),
});

const syncNativeIx = createSyncNativeInstruction(userTokenAccount1.address);

const tx = new anchor.web3.Transaction().add(transferIx, syncNativeIx);

Fee addresses

Protocol and KOTM addresses are specified on the config account, that can be fetched on-chain.

Transaction

Last updated