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 crate::utils::load_wallet;
use orca_whirlpools::{
set_whirlpools_config_address, swap_instructions, SwapType, WhirlpoolsConfigInput,
};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
#[tokio::main]
async fn main() {
set_whirlpools_config_address(WhirlpoolsConfigInput::SolanaDevnet).unwrap();
let rpc = RpcClient::new("https://api.devnet.solana.com".to_string());
let wallet = load_wallet();
let whirlpool_address =
Pubkey::from_str("3KBZiL2g8C7tiJ32hTv5v3KM7aK9htpqTw4cTXz1HvPt").unwrap();
let mint_address = Pubkey::from_str("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k").unwrap();
let input_amount = 1_000_000;
let result = swap_instructions(
&rpc,
whirlpool_address,
input_amount,
mint_address,
SwapType::ExactIn,
Some(100),
Some(wallet.pubkey()),
)
.await
.unwrap();
println!("Quote estimated token out: {:?}", result.quote);
println!("Number of Instructions: {}", result.instructions.len());
}