Documentation
    Preparing search index...

    Function swapInstructions

    • Generates the instructions necessary to execute a token swap in an Orca Whirlpool. It handles both exact input and exact output swaps, fetching the required accounts, tick arrays, and determining the swap quote.

      Type Parameters

      • T extends SwapParams

        The type of swap (exact input or output).

      Parameters

      • rpc: Rpc<
            GetAccountInfoApi & GetMultipleAccountsApi & GetMinimumBalanceForRentExemptionApi & GetEpochInfoApi,
        >

        The Solana RPC client.

      • params: T

        The swap parameters, specifying either the input or output amount and the mint address of the token being swapped.

      • poolAddress: Address

        The address of the Whirlpool against which the swap will be made.

      • OptionalslippageToleranceBps: number = SLIPPAGE_TOLERANCE_BPS

        The maximum acceptable slippage tolerance for the swap, in basis points (BPS).

      • Optionalsigner: TransactionSigner = FUNDER

        The wallet or signer executing the swap.

      Returns Promise<SwapInstructions<T>>

      • A promise that resolves to an object containing the swap instructions and the swap quote.
      import { setWhirlpoolsConfig, swapInstructions } from '@orca-so/whirlpools';
      import { createSolanaRpc, devnet, address } from '@solana/kit';
      import { loadWallet } from './utils';

      await setWhirlpoolsConfig('solanaDevnet');
      const devnetRpc = createSolanaRpc(devnet('https://api.devnet.solana.com'));
      const wallet = await loadWallet(); // CAUTION: This wallet is not persistent.
      const whirlpoolAddress = address("3KBZiL2g8C7tiJ32hTv5v3KM7aK9htpqTw4cTXz1HvPt");
      const mintAddress = address("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k");
      const inputAmount = 1_000_000n;

      const { instructions, quote } = await swapInstructions(
      devnetRpc,
      { inputAmount, mint: mintAddress },
      whirlpoolAddress,
      100,
      wallet
      );

      console.log(`Quote estimated token out: ${quote.tokenEstOut}`);
      console.log(`Number of instructions:, ${instructions.length}`);