Function swap_instructions
pub async fn swap_instructions(
rpc: &RpcClient,
whirlpool_address: Address,
amount: u64,
specified_mint: Address,
swap_type: SwapType,
config: SwapConfig,
) -> 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.specified_mint- The public key of the token mint being swapped.swap_type- The type of swap (SwapType::ExactInorSwapType::ExactOut).config- The parameters to build the swap instruction.
§Returns
A Result containing SwapInstructions on success:
instructions- A vector ofInstructionobjects required to execute the swap.quote- ASwapQuoteproviding the computed details of the swap.additional_signers- A vector ofKeypairobjects 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::{swap_instructions, SwapConfig, SwapType, WhirlpoolDeployment};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_pubkey::Pubkey;
use std::str::FromStr;
#[tokio::main]
async fn main() {
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 config = SwapConfig {
slippage_tolerance_bps: Some(100),
signer: Some(wallet.pubkey()),
whirlpool_deployment: Some(WhirlpoolDeployment::devnet()),
};
let result = swap_instructions(
&rpc,
whirlpool_address,
1_000_000,
specified_mint: mint_address,
SwapType::ExactIn,config
).await.unwrap();
println!("Quote estimated token out: {:?}", result.quote);
println!("Number of Instructions: {}", result.instructions.len());
}