Function close_position_instructions

pub async fn close_position_instructions(
    rpc: &RpcClient,
    position_mint_address: Address,
    config: ClosePositionConfig,
) -> Result<ClosePositionInstruction, Box<dyn Error>>
Expand description

Generates instructions to close a liquidity position.

This function collects all fees and rewards, removes any remaining liquidity, and closes the position. It returns the necessary instructions, quotes for fees and rewards, and the liquidity quote for the closed position.

§Arguments

  • rpc - A reference to a Solana RPC client for fetching accounts and pool data.
  • position_mint_address - The public key of the NFT mint address representing the position to be closed.
  • config - The parameters to build the close position instruction.

§Returns

A Result containing ClosePositionInstruction on success:

  • instructions - A vector of Instruction objects required to execute the position closure.
  • additional_signers - A vector of Keypair objects representing additional signers required for the instructions.
  • quote - The computed quote for decreasing liquidity, including liquidity delta, token estimates, and minimum tokens.
  • fees_quote - Details of the fees available to collect from the position:
    • fee_owed_a - The amount of fees available to collect in token A.
    • fee_owed_b - The amount of fees available to collect in token B.
  • rewards_quote - Details of the rewards available to collect from the position:
    • rewards - An array containing up to three CollectRewardQuote entries, one for each reward token.
      • Each entry includes rewards_owed, the amount of the respective reward token available to collect.

§Errors

This function will return an error if:

  • The authority account is invalid or missing.
  • The position, token mint, or reward accounts are not found or have invalid data.
  • Any RPC request to the blockchain fails.

§Example

use crate::utils::load_wallet;
use orca_whirlpools::{close_position_instructions, ClosePositionConfig, 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 position_mint_address =
        Pubkey::from_str("HqoV7Qv27REUtmd9UKSJGGmCRNx3531t33bDG1BUfo9K").unwrap();

    let config = ClosePositionConfig {
        slippage_tolerance_bps: Some(100),
        authority: Some(wallet.pubkey()),
        whirlpool_deployment: Some(WhirlpoolDeployment::devnet()),
    };
    let result = close_position_instructions(&rpc, position_mint_address, config)
        .await
        .unwrap();

    println!("Quote token max B: {:?}", result.quote.token_est_b);
    println!("Fees Quote: {:?}", result.fees_quote);
    println!("Rewards Quote: {:?}", result.rewards_quote);
    println!("Number of Instructions: {}", result.instructions.len());
}