Function orca_whirlpools_docs::orca_whirlpools::swap_instructions
pub async fn swap_instructions(
rpc: &RpcClient,
whirlpool_address: Pubkey,
amount: u64,
specified_mint: Pubkey,
swap_type: SwapType,
slippage_tolerance_bps: Option<u16>,
signer: Option<Pubkey>,
) -> Result<SwapInstructions, Box<dyn Error>>
Expand description
Generates the instructions necessary to execute a token swap.
This function generates instructions for executing swaps, supporting both exact input and exact output scenarios. It calculates the necessary accounts, tick arrays, and swap quote using the provided parameters.
§Arguments
rpc
- A reference to the Solana RPC client for fetching accounts and interacting with the blockchain.whirlpool_address
- The public key of the Whirlpool against which the swap will be executed.amount
- The token amount specified for the swap. ForSwapType::ExactIn
, this is the input token amount. ForSwapType::ExactOut
, this is the output token amount.specified_mint
- The public key of the token mint being swapped.swap_type
- The type of swap (SwapType::ExactIn
orSwapType::ExactOut
).slippage_tolerance_bps
- An optional slippage tolerance, in basis points (BPS). Defaults to the global setting if not provided.signer
- An optional public key of the wallet or account executing the swap. Defaults to the global funder if not provided.
§Returns
A Result
containing SwapInstructions
on success:
instructions
- A vector ofInstruction
objects required to execute the swap.quote
- ASwapQuote
providing the computed details of the swap.additional_signers
- A vector ofKeypair
objects representing any additional signers required for the instructions.
§Errors
Returns an error if:
- The signer is invalid or missing.
- The Whirlpool or token mint accounts are not found or have invalid data.
- Any RPC request to the blockchain fails.
§Example
use solana_client::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use orca_whirlpools_sdk::{
swap_instructions, SwapType, set_whirlpools_config_address, WhirlpoolsConfigInput,
};
set_whirlpools_config_address(WhirlpoolsConfigInput::SolanaDevnet).unwrap();
let rpc = RpcClient::new("https://api.devnet.solana.com");
let whirlpool_pubkey = Pubkey::from_str("WHIRLPOOL_ADDRESS").unwrap();
let amount = 1_000_000; // Amount to swap.
let specified_mint = Pubkey::from_str("SPECIFIED_MINT_ADDRESS").unwrap();
let slippage_tolerance_bps = Some(100);
let swap_instructions = swap_instructions(
&rpc,
whirlpool_pubkey,
amount,
specified_mint,
SwapType::ExactIn,
slippage_tolerance_bps,
None,
).unwrap();
println!("Number of Instructions: {}", swap_instructions.instructions.len());
println!("Swap Quote: {:?}", swap_instructions.quote);