Expand description

Solana Farm Client

Solana Farm Client provides an easy way to interact with pools, farms, vaults, and funds, query on-chain objects metadata, and perform common operations with accounts.

Client’s methods accept human readable names (tokens, polls, etc.) and UI (decimal) amounts, so you can simply call client.swap(&keypair, Protocol::Orca, “SOL”, “USDC”, 0.1, 0.0) to swap 0.1 SOL for RAY in a Raydium pool. All metadata required to lookup account addresses, decimals, etc. is stored on-chain.

Under the hood it leverages the official Solana RPC Client which can be accessed with client.rpc_client, for example: client.rpc_client.get_latest_blockhash().

Naming convention for Pools and Farms is [PROTOCOL].[TOKEN_A]-[TOKEN_B]-[VERSION] Naming convention for Vaults is [PROTOCOL].[STRATEGY].[TOKEN_A]-[TOKEN_B]-[VERSION] There are single token pools where TOKEN_B is not present. A list of supported protocols can be obtained with get_protocols(). If VERSION is omitted then Pool, Farm, or Vault with the latest version will be used.

A few examples:

use {

solana_farm_client::client::FarmClient,

solana_sdk::{pubkey::Pubkey, signer::Signer},

};

let client = FarmClient::new(“https://api.mainnet-beta.solana.com”);

let keypair = FarmClient::read_keypair_from_file(

&(std::env::var(“HOME”).unwrap().to_string() + “/.config/solana/id.json”),

)

.unwrap();

// get SOL account balance

client.get_account_balance(&keypair.pubkey()).unwrap();

// get SPL token account balance

client

.get_token_account_balance(&keypair.pubkey(), “SRM”)

.unwrap();

// get token metadata

client.get_token(“SRM”).unwrap();

// find Raydium pools with RAY and SRM tokens

client.find_pools(Protocol::Raydium, “RAY”, “SRM”).unwrap();

// find Saber pools with USDC and USDT tokens

client.find_pools(Protocol::Saber, “USDC”, “USDT”).unwrap();

// get pool metadata

client.get_pool(“RDM.RAY-SRM”).unwrap();

// get farm metadata

client.get_farm(“RDM.RAY-SRM”).unwrap();

// find all vaults with RAY and SRM tokens

client.find_vaults(“RAY”, “SRM”).unwrap();

// get vault metadata

client.get_vault(“RDM.STC.RAY-SRM”).unwrap();

// get fund metadata

client.get_fund(“TestFund”).unwrap();

// get the list of all pools

client.get_pools().unwrap();

// find farms for specific LP token

client.find_farms_with_lp(“LP.RDM.RAY-SRM-V4”).unwrap();

// get Raydium pool price

client.get_pool_price(“RDM.RAY-SRM”).unwrap();

// or specify version for specific pool

client.get_pool_price(“RDM.RAY-SRM-V4”).unwrap();

// get oracle price

client.get_oracle_price(“SOL”, 0, 0.0).unwrap();

// list official program IDs

client.get_program_ids().unwrap();

// swap in the Raydium pool

client.swap(&keypair, Protocol::Raydium, “SOL”, “RAY”, 0.01, 0.0).unwrap();

// swap in the Saber pool

client.swap(&keypair, Protocol::Saber, “USDC”, “USDT”, 0.01, 0.0).unwrap();

// deposit liquidity to the Raydium pool (zero second token amount means calculate it automatically)

client

.add_liquidity_pool(&keypair, “RDM.GRAPE-USDC”, 0.1, 0.0)

.unwrap();

// withdraw your liquidity from the Raydium pool (zero amount means remove all tokens)

client

.remove_liquidity_pool(&keypair, “RDM.GRAPE-USDC”, 0.0)

.unwrap();

// stake LP tokens to the Raydium farm (zero amount means stake all)

client.stake(&keypair, “RDM.GRAPE-USDC”, 0.0).unwrap();

// get staked balance

client.get_user_stake_balance(&keypair.pubkey(), “RDM.GRAPE-USDC”).unwrap();

// harvest rewards

client.harvest(&keypair, “RDM.GRAPE-USDC”).unwrap();

// unstake LP tokens from the farm (zero amount means unstake all)

client.unstake(&keypair, “RDM.GRAPE-USDC”, 0.0).unwrap();

// deposit liquidity to the vault (zero second token amount means calculate it automatically)

client

.add_liquidity_vault(&keypair, “RDM.STC.RAY-SRM”, 0.01, 0.0)

.unwrap();

// withdraw liquidity from the vault (zero amount means remove all tokens)

client

.remove_liquidity_vault(&keypair, “RDM.STC.RAY-SRM”, 0.0)

.unwrap();

// request liquidity deposit to the fund

client

.request_deposit_fund(&keypair, “TestFund”, “USDC”, 0.01)

.unwrap();

// request liquidity withdrawal from the fund (zero amount means withdraw everything)

client

.request_withdrawal_fund(&keypair, “TestFund”, “USDC”, 0.0)

.unwrap();

// list all vaults that belong to particular fund

client.get_fund_vaults(“TestFund”).unwrap();

// transfer SOL to another wallet

client

.transfer(&keypair, &Pubkey::new_unique(), 0.001)

.unwrap();

// transfer SPL tokens to another wallet

client

.token_transfer(&keypair, “SRM”, &Pubkey::new_unique(), 0.001)

.unwrap();

// create associated token account for the wallet

client.get_or_create_token_account(&keypair, “SRM”).unwrap();

// list all active token accounts for the wallet

client.get_wallet_tokens(&keypair.pubkey()).unwrap();

// get vault stats

client.get_vault_info(“RDM.STC.RAY-SRM”).unwrap();

// get user stats for particular vault

client

.get_vault_user_info(&keypair.pubkey(), “RDM.STC.RAY-SRM”)

.unwrap();

// create a new instruction for depositing liquidity to the vault, neither sign nor send it

client

.new_instruction_add_liquidity_vault(&keypair.pubkey(), “RDM.STC.RAY-SRM”, 0.1, 0.0)

.unwrap();

// get fund stats and parameters

client.get_fund_info(“TestFund”).unwrap();

// get fund custody info

client.get_fund_custody(“TestFund”, “USDC”, FundCustodyType::DepositWithdraw).unwrap();

// get information about fund assets

client.get_fund_assets(&fund_name, FundAssetType::Vault).unwrap();

client.get_fund_assets(&fund_name, FundAssetType::Custody).unwrap();

Structs

Farm Client

Type Definitions