#amm #crypto #solana #defi

orca_whirlpools_client

Rust client to interact with Orca's on-chain Whirlpool program

27 releases (2 stable)

new 1.0.1 Dec 16, 2024
0.4.3 Nov 28, 2024

#690 in Magic Beans

Download history 126/week @ 2024-08-30 167/week @ 2024-09-06 102/week @ 2024-09-13 26/week @ 2024-09-20 39/week @ 2024-09-27 7/week @ 2024-10-04 160/week @ 2024-10-11 26/week @ 2024-10-18 792/week @ 2024-10-25 615/week @ 2024-11-01 296/week @ 2024-11-08 457/week @ 2024-11-15 652/week @ 2024-11-22 184/week @ 2024-11-29 112/week @ 2024-12-06 140/week @ 2024-12-13

1,311 downloads per month
Used in orca_whirlpools

Apache-2.0

1.5MB
23K SLoC

Orca Whirlpools Client SDK

Overview

This package provides developers with low-level functionalities for interacting with the Whirlpool Program on Solana. It serves as a foundational tool that allows developers to manage and integrate detailed operations into their Rust projects, particularly those related to Orca's Whirlpool Program. This package offers granular control for advanced use cases.

NOTE: To ensure compatibility, use version 1.18 of the solana-sdk crate, which matches the version used to build the Whirlpool program.

Key Features

  • Codama Client: The package includes a set of generated client code based on the Whirlpool Program IDL. This ensures all the necessary program information is easily accessible in a structured format. It handles all decoding and encoding of instructions and account data, making it much easier to interact with the program.
  • PDA (Program Derived Addresses) Utilities: This feature contains utility functions that help derive Program Derived Addresses (PDAs) for accounts within the Whirlpool Program, simplifying address derivation for developers.

Installation

cargo add orca_whirlpools_client

Usage

Here are some basic examples of how to use the package:

Deriving a PDA

To derive a PDA for a Whirlpool account, you can use the get_whirlpool_address PDA utility.

use orca_whirlpools_client::get_whirlpool_address;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;

fn main() {
    let whirlpool_config_address = Pubkey::from_str("FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR").unwrap();
    let token_mint_a = Pubkey::from_str("So11111111111111111111111111111111111111112").unwrap(); // wSOL
    let token_mint_b = Pubkey::from_str("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k").unwrap(); // DevUSDC
    let tick_spacing = 64;

    let (whirlpool_pda, _bump) = get_whirlpool_address(&whirlpool_config_address, &token_mint_a, &token_mint_b, tick_spacing).unwrap();
    println!("{:?}", whirlpool_pda);
}

Example: Initialize Pool Instruction

The following example demonstrates how to create an InitializePool instruction:

use orca_whirlpools_client::{
    instructions::InitializePoolV2Builder,
    get_fee_tier_address,
    get_token_badge_address,
    get_whirlpool_address,
};
use solana_sdk::{
    pubkey::Pubkey,
    signer::{keypair::Keypair, Signer},
};
use std::str::FromStr;

fn main() {
    let whirlpool_config_address = Pubkey::from_str("FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR").unwrap();
    let token_mint_a = Pubkey::from_str("So11111111111111111111111111111111111111112").unwrap(); // wSOL
    let token_mint_b = Pubkey::from_str("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k").unwrap(); // DevUSDC
    let (token_badge_a, _bump) = get_token_badge_address(&whirlpool_config_address, &token_mint_a).unwrap();
    let (token_badge_b, _bump) = get_token_badge_address(&whirlpool_config_address, &token_mint_b).unwrap();
    let wallet = Keypair::new(); // CAUTION: this wallet is not persistent
    let tick_spacing = 8;
    let (whirlpool_pda, _bump) = get_whirlpool_address(&whirlpool_config_address, &token_mint_a, &token_mint_b, tick_spacing).unwrap();
    let token_vault_a = Keypair::new();
    let token_vault_b = Keypair::new();
    let (fee_tier, _bump) = get_fee_tier_address(&whirlpool_config_address, tick_spacing).unwrap();
    let token_program_a = Pubkey::from_str("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA").unwrap();
    let token_program_b = Pubkey::from_str("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA").unwrap();
    let initial_sqrt_price = 7459106261056563200u128;    

    let initialize_pool_v2_instruction = InitializePoolV2Builder::new()
        .whirlpools_config(whirlpool_config_address)
        .token_mint_a(token_mint_a)
        .token_mint_b(token_mint_b)
        .token_badge_a(token_badge_a)
        .token_badge_b(token_badge_b)
        .funder(wallet.pubkey())
        .whirlpool(whirlpool_pda)
        .token_vault_a(token_vault_a.pubkey())
        .token_vault_b(token_vault_b.pubkey())
        .fee_tier(fee_tier)
        .token_program_a(token_program_a)
        .token_program_b(token_program_b)
        .tick_spacing(tick_spacing)
        .initial_sqrt_price(initial_sqrt_price)
        .instruction();

    println!("{:?}", initialize_pool_v2_instruction);
}

Dependencies

~17–37MB
~692K SLoC