Files
sol-trade-sdk/src/lib.rs
T

652 lines
19 KiB
Rust
Raw Normal View History

2024-12-31 16:51:58 +08:00
pub mod accounts;
pub mod constants;
pub mod error;
pub mod instruction;
2025-01-23 17:48:10 +08:00
pub mod grpc;
2025-01-23 21:15:55 +08:00
pub mod common;
2025-02-21 20:47:28 +08:00
pub mod ipfs;
2025-04-10 12:29:42 +08:00
pub mod swqos;
2025-04-02 13:39:59 +08:00
pub mod pumpfun;
pub mod pumpswap;
2025-02-21 20:47:28 +08:00
use std::sync::Arc;
2025-06-07 23:16:46 +08:00
use std::sync::Mutex;
2025-01-23 17:48:10 +08:00
use swqos::{FeeClient, JitoClient, NextBlockClient, NozomiClient, SolRpcClient, ZeroSlotClient};
2025-04-02 13:39:59 +08:00
use rustls::crypto::{ring::default_provider, CryptoProvider};
use solana_hash::Hash;
2025-01-23 17:48:10 +08:00
use solana_sdk::{
2025-02-21 20:47:28 +08:00
commitment_config::CommitmentConfig,
pubkey::Pubkey,
2025-04-02 13:39:59 +08:00
signature::{Keypair, Signer},
2024-12-31 16:51:58 +08:00
};
2025-01-14 19:18:48 +08:00
use common::{pumpfun::logs_data::TradeInfo, pumpfun::logs_events::PumpfunEvent, pumpfun::logs_subscribe, Cluster, PriorityFee, SolanaRpcClient};
use common::pumpfun::logs_subscribe::SubscriptionHandle;
2025-02-21 20:47:28 +08:00
use ipfs::TokenMetadataIPFS;
2025-01-03 14:43:26 +08:00
use constants::trade_type::{COPY_BUY, SNIPER_BUY};
use constants::trade_platform::{PUMPFUN, PUMPFUN_SWAP, RAYDIUM};
2025-06-07 23:16:46 +08:00
2024-12-31 16:51:58 +08:00
pub struct PumpFun {
pub payer: Arc<Keypair>,
2025-04-02 13:39:59 +08:00
pub rpc: Arc<SolanaRpcClient>,
pub fee_clients: Vec<Arc<FeeClient>>,
2025-02-21 23:02:15 +08:00
pub priority_fee: PriorityFee,
2025-04-02 13:39:59 +08:00
pub cluster: Cluster,
}
2025-06-07 23:16:46 +08:00
static INSTANCE: Mutex<Option<Arc<PumpFun>>> = Mutex::new(None);
2025-04-02 13:39:59 +08:00
impl Clone for PumpFun {
fn clone(&self) -> Self {
Self {
payer: self.payer.clone(),
rpc: self.rpc.clone(),
fee_clients: self.fee_clients.clone(),
priority_fee: self.priority_fee.clone(),
cluster: self.cluster.clone(),
}
}
2025-01-09 00:12:43 +08:00
}
2024-12-31 16:51:58 +08:00
impl PumpFun {
2025-02-15 18:53:17 +08:00
#[inline]
2025-04-02 13:39:59 +08:00
pub async fn new(
2025-02-21 20:47:28 +08:00
payer: Arc<Keypair>,
2025-04-02 13:39:59 +08:00
cluster: &Cluster,
2024-12-31 16:51:58 +08:00
) -> Self {
2025-04-02 13:39:59 +08:00
if CryptoProvider::get_default().is_none() {
let _ = default_provider()
.install_default()
.map_err(|e| anyhow::anyhow!("Failed to install crypto provider: {:?}", e));
}
2024-12-31 16:51:58 +08:00
2025-04-02 13:39:59 +08:00
let rpc = SolanaRpcClient::new_with_commitment(
cluster.clone().rpc_url,
cluster.clone().commitment
);
let rpc = Arc::new(rpc);
2025-04-02 13:39:59 +08:00
let mut fee_clients: Vec<Arc<FeeClient>> = vec![];
if cluster.clone().use_jito {
let jito_client = JitoClient::new(
cluster.clone().rpc_url,
cluster.clone().block_engine_url
).await.expect("Failed to create Jito client");
fee_clients.push(Arc::new(jito_client));
}
if cluster.clone().use_zeroslot {
let zeroslot_client = ZeroSlotClient::new(
cluster.clone().rpc_url,
cluster.clone().zeroslot_url,
cluster.clone().zeroslot_auth_token
);
fee_clients.push(Arc::new(zeroslot_client));
}
if cluster.clone().use_nozomi {
let nozomi_client = NozomiClient::new(
cluster.clone().rpc_url,
cluster.clone().nozomi_url,
cluster.clone().nozomi_auth_token
);
fee_clients.push(Arc::new(nozomi_client));
}
2025-04-02 13:39:59 +08:00
if cluster.clone().use_nextblock {
let nextblock_client = NextBlockClient::new(
cluster.clone().rpc_url,
cluster.clone().nextblock_url,
cluster.clone().nextblock_auth_token
);
fee_clients.push(Arc::new(nextblock_client));
}
2025-01-03 14:43:26 +08:00
if cluster.clone().use_rpc {
let rpc_client = SolRpcClient::new(rpc.clone());
fee_clients.push(Arc::new(rpc_client));
}
2025-06-07 23:16:46 +08:00
let instance = Self {
2024-12-31 16:51:58 +08:00
payer,
rpc,
2025-04-02 13:39:59 +08:00
fee_clients,
priority_fee: cluster.clone().priority_fee,
cluster: cluster.clone(),
2025-06-07 23:16:46 +08:00
};
let mut current = INSTANCE.lock().unwrap();
*current = Some(Arc::new(instance.clone()));
instance
}
/// Get the RPC client instance
pub fn get_rpc(&self) -> &Arc<SolanaRpcClient> {
&self.rpc
}
/// Get the current instance
pub fn get_instance() -> Arc<Self> {
let instance = INSTANCE.lock().unwrap();
instance.as_ref().expect("PumpFun instance not initialized. Please call new() first.").clone()
2024-12-31 16:51:58 +08:00
}
/// Create a new token
pub async fn create(
&self,
mint: Keypair,
ipfs: TokenMetadataIPFS,
) -> Result<(), anyhow::Error> {
pumpfun::create::create(
self.rpc.clone(),
self.payer.clone(),
mint,
ipfs,
self.priority_fee.clone(),
).await
}
pub async fn create_and_buy(
&self,
mint: Keypair,
ipfs: TokenMetadataIPFS,
amount_sol: u64,
slippage_basis_points: Option<u64>,
recent_blockhash: Hash,
) -> Result<(), anyhow::Error> {
pumpfun::create::create_and_buy(
self.rpc.clone(),
self.payer.clone(),
mint,
ipfs,
amount_sol,
slippage_basis_points,
self.priority_fee.clone(),
recent_blockhash,
).await
}
pub async fn create_and_buy_with_tip(
&self,
payer: Arc<Keypair>,
mint: Keypair,
ipfs: TokenMetadataIPFS,
buy_sol_cost: u64,
slippage_basis_points: Option<u64>,
recent_blockhash: Hash,
) -> Result<(), anyhow::Error> {
pumpfun::create::create_and_buy_with_tip(
self.rpc.clone(),
self.fee_clients.clone(),
payer,
mint,
ipfs,
buy_sol_cost,
slippage_basis_points,
self.priority_fee.clone(),
recent_blockhash,
).await
}
2025-04-02 13:39:59 +08:00
2025-01-09 00:12:43 +08:00
/// Buy tokens
2025-06-07 23:16:46 +08:00
pub async fn sniper_buy(
2024-12-31 16:51:58 +08:00
&self,
2025-04-02 13:39:59 +08:00
mint: Pubkey,
creator: Pubkey,
dev_buy_token: u64,
dev_sol_cost: u64,
buy_sol_cost: u64,
2024-12-31 16:51:58 +08:00
slippage_basis_points: Option<u64>,
recent_blockhash: Hash,
2025-04-02 13:39:59 +08:00
) -> Result<(), anyhow::Error> {
pumpfun::buy::buy(
self.rpc.clone(),
self.payer.clone(),
2024-12-31 16:51:58 +08:00
mint,
creator,
dev_buy_token,
dev_sol_cost,
buy_sol_cost,
2025-02-21 20:47:28 +08:00
slippage_basis_points,
2025-04-02 13:39:59 +08:00
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
2025-06-07 23:16:46 +08:00
SNIPER_BUY.to_string(),
).await
}
pub async fn copy_buy(
&self,
mint: Pubkey,
creator: Pubkey,
dev_buy_token: u64,
dev_sol_cost: u64,
buy_sol_cost: u64,
slippage_basis_points: Option<u64>,
recent_blockhash: Hash,
trade_platform: String,
2025-06-07 23:16:46 +08:00
) -> Result<(), anyhow::Error> {
if trade_platform == PUMPFUN {
pumpfun::buy::buy(
self.rpc.clone(),
self.payer.clone(),
mint,
creator,
dev_buy_token,
dev_sol_cost,
buy_sol_cost,
slippage_basis_points,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
COPY_BUY.to_string(),
).await
} else if trade_platform == PUMPFUN_SWAP {
pumpswap::buy::buy(
self.rpc.clone(),
self.payer.clone(),
mint,
creator,
buy_sol_cost,
slippage_basis_points,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
None,
None,
None,
None,
None,
).await
} else {
Err(anyhow::anyhow!("Unsupported trade platform: {}", trade_platform))
}
2024-12-31 16:51:58 +08:00
}
2025-01-09 00:12:43 +08:00
/// Buy tokens using Jito
2025-06-07 23:16:46 +08:00
pub async fn sniper_buy_with_tip(
&self,
mint: Pubkey,
creator: Pubkey,
dev_buy_token: u64,
dev_sol_cost: u64,
buy_sol_cost: u64,
slippage_basis_points: Option<u64>,
recent_blockhash: Hash,
) -> Result<(), anyhow::Error> {
pumpfun::buy::buy_with_tip(
self.fee_clients.clone(),
self.payer.clone(),
mint,
creator,
dev_buy_token,
dev_sol_cost,
buy_sol_cost,
slippage_basis_points,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
SNIPER_BUY.to_string(),
).await
}
pub async fn copy_buy_with_tip(
2025-01-03 14:43:26 +08:00
&self,
2025-04-02 13:39:59 +08:00
mint: Pubkey,
creator: Pubkey,
dev_buy_token: u64,
dev_sol_cost: u64,
buy_sol_cost: u64,
2025-01-03 14:43:26 +08:00
slippage_basis_points: Option<u64>,
recent_blockhash: Hash,
trade_platform: String,
2025-04-02 13:39:59 +08:00
) -> Result<(), anyhow::Error> {
if trade_platform == PUMPFUN {
pumpfun::buy::buy_with_tip(
self.fee_clients.clone(),
self.payer.clone(),
mint,
creator,
dev_buy_token,
dev_sol_cost,
buy_sol_cost,
slippage_basis_points,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
COPY_BUY.to_string(),
).await
} else if trade_platform == PUMPFUN_SWAP {
pumpswap::buy::buy_with_tip(
self.rpc.clone(),
self.fee_clients.clone(),
self.payer.clone(),
mint,
creator,
buy_sol_cost,
slippage_basis_points,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
None,
None,
None,
None,
None,
).await
} else {
Err(anyhow::anyhow!("Unsupported trade platform: {}", trade_platform))
}
2025-01-03 14:43:26 +08:00
}
2025-01-09 00:12:43 +08:00
/// Sell tokens
2024-12-31 16:51:58 +08:00
pub async fn sell(
&self,
2025-04-02 13:39:59 +08:00
mint: Pubkey,
creator: Pubkey,
amount_token: u64,
recent_blockhash: Hash,
2025-04-02 13:39:59 +08:00
) -> Result<(), anyhow::Error> {
pumpfun::sell::sell(
self.rpc.clone(),
self.payer.clone(),
mint.clone(),
creator,
2025-02-21 20:47:28 +08:00
amount_token,
2025-04-02 13:39:59 +08:00
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
2025-02-21 20:47:28 +08:00
).await
2025-01-07 13:18:31 +08:00
}
2025-01-09 00:12:43 +08:00
/// Sell tokens by percentage
2025-01-07 13:18:31 +08:00
pub async fn sell_by_percent(
&self,
2025-04-02 13:39:59 +08:00
mint: Pubkey,
creator: Pubkey,
2025-01-07 13:18:31 +08:00
percent: u64,
amount_token: u64,
recent_blockhash: Hash,
trade_platform: String,
2025-04-02 13:39:59 +08:00
) -> Result<(), anyhow::Error> {
if trade_platform == PUMPFUN {
pumpfun::sell::sell_by_percent(
self.rpc.clone(),
self.payer.clone(),
mint.clone(),
creator,
percent,
amount_token,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
).await
} else if trade_platform == PUMPFUN_SWAP {
pumpswap::sell::sell_by_percent(
self.rpc.clone(),
self.payer.clone(),
mint.clone(),
creator,
percent,
None,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
None,
None,
None,
None,
None,
).await
} else {
Err(anyhow::anyhow!("Unsupported trade platform: {}", trade_platform))
}
2024-12-31 16:51:58 +08:00
}
2025-06-07 23:16:46 +08:00
/// Sell tokens by amount
pub async fn sell_by_amount(
&self,
mint: Pubkey,
creator: Pubkey,
amount: u64,
recent_blockhash: Hash,
trade_platform: String,
2025-06-07 23:16:46 +08:00
) -> Result<(), anyhow::Error> {
if trade_platform == PUMPFUN {
pumpfun::sell::sell_by_amount(
self.rpc.clone(),
self.payer.clone(),
mint.clone(),
creator,
amount,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
).await
} else if trade_platform == PUMPFUN_SWAP {
pumpswap::sell::sell_by_amount(
self.rpc.clone(),
self.payer.clone(),
mint.clone(),
creator,
amount,
None,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
None,
None,
None,
None,
None,
).await
} else {
Err(anyhow::anyhow!("Unsupported trade platform: {}", trade_platform))
}
2025-06-07 23:16:46 +08:00
}
2025-04-02 14:08:48 +08:00
pub async fn sell_by_percent_with_tip(
2025-01-14 19:18:48 +08:00
&self,
2025-04-02 13:39:59 +08:00
mint: Pubkey,
creator: Pubkey,
2025-01-14 19:18:48 +08:00
percent: u64,
amount_token: u64,
recent_blockhash: Hash,
trade_platform: String,
2025-04-02 13:39:59 +08:00
) -> Result<(), anyhow::Error> {
if trade_platform == PUMPFUN {
pumpfun::sell::sell_by_percent_with_tip(
self.fee_clients.clone(),
self.payer.clone(),
mint,
creator,
percent,
amount_token,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
).await
} else if trade_platform == PUMPFUN_SWAP {
pumpswap::sell::sell_by_percent_with_tip(
self.rpc.clone(),
self.fee_clients.clone(),
self.payer.clone(),
mint,
creator,
percent,
None,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
None,
None,
None,
None,
None,
).await
} else {
Err(anyhow::anyhow!("Unsupported trade platform: {}", trade_platform))
}
2025-01-14 19:18:48 +08:00
}
2025-06-07 23:16:46 +08:00
pub async fn sell_by_amount_with_tip(
&self,
mint: Pubkey,
creator: Pubkey,
amount: u64,
recent_blockhash: Hash,
trade_platform: String,
2025-06-07 23:16:46 +08:00
) -> Result<(), anyhow::Error> {
if trade_platform == PUMPFUN {
pumpfun::sell::sell_by_amount_with_tip(
self.fee_clients.clone(),
self.payer.clone(),
mint,
creator,
amount,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
).await
} else if trade_platform == PUMPFUN_SWAP {
pumpswap::sell::sell_by_amount_with_tip(
self.rpc.clone(),
self.fee_clients.clone(),
self.payer.clone(),
mint,
creator,
amount,
None,
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
None,
None,
None,
None,
None,
).await
} else {
Err(anyhow::anyhow!("Unsupported trade platform: {}", trade_platform))
}
2025-06-07 23:16:46 +08:00
}
2025-01-09 00:12:43 +08:00
/// Sell tokens using Jito
2025-04-02 14:08:48 +08:00
pub async fn sell_with_tip(
2025-01-03 14:43:26 +08:00
&self,
2025-04-02 13:39:59 +08:00
mint: Pubkey,
creator: Pubkey,
amount_token: u64,
recent_blockhash: Hash,
2025-04-02 13:39:59 +08:00
) -> Result<(), anyhow::Error> {
2025-04-02 14:08:48 +08:00
pumpfun::sell::sell_with_tip(
2025-04-02 13:39:59 +08:00
self.fee_clients.clone(),
self.payer.clone(),
2025-01-03 14:43:26 +08:00
mint,
creator,
2025-02-21 20:47:28 +08:00
amount_token,
2025-04-02 13:39:59 +08:00
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
2025-02-21 20:47:28 +08:00
).await
2024-12-31 16:51:58 +08:00
}
2025-01-07 13:18:31 +08:00
2025-02-15 18:53:17 +08:00
#[inline]
2025-01-07 13:18:31 +08:00
pub async fn tokens_subscription<F>(
&self,
ws_url: &str,
commitment: CommitmentConfig,
callback: F,
bot_wallet: Option<Pubkey>,
) -> Result<SubscriptionHandle, Box<dyn std::error::Error>>
where
2025-01-25 16:01:16 +08:00
F: Fn(PumpfunEvent) + Send + Sync + 'static,
2025-01-07 13:18:31 +08:00
{
logs_subscribe::tokens_subscription(ws_url, commitment, callback, bot_wallet).await
}
2025-02-15 18:53:17 +08:00
#[inline]
2025-01-07 13:18:31 +08:00
pub async fn stop_subscription(&self, subscription_handle: SubscriptionHandle) {
subscription_handle.shutdown().await;
}
2025-01-09 01:21:20 +08:00
2025-02-15 18:53:17 +08:00
#[inline]
2025-04-02 13:39:59 +08:00
pub async fn get_sol_balance(&self, payer: &Pubkey) -> Result<u64, anyhow::Error> {
pumpfun::common::get_sol_balance(&self.rpc, payer).await
2025-01-09 01:21:20 +08:00
}
2025-01-13 18:42:21 +08:00
2025-02-15 18:53:17 +08:00
#[inline]
2025-04-02 13:39:59 +08:00
pub async fn get_payer_sol_balance(&self) -> Result<u64, anyhow::Error> {
pumpfun::common::get_sol_balance(&self.rpc, &self.payer.pubkey()).await
2025-02-21 21:15:10 +08:00
}
#[inline]
2025-04-02 13:39:59 +08:00
pub async fn get_token_balance(&self, payer: &Pubkey, mint: &Pubkey) -> Result<u64, anyhow::Error> {
println!("get_token_balance payer: {}, mint: {}, cluster: {}", payer, mint, self.cluster.rpc_url);
pumpfun::common::get_token_balance(&self.rpc, payer, mint).await
2025-01-13 18:42:21 +08:00
}
2025-02-15 18:53:17 +08:00
#[inline]
2025-04-02 13:39:59 +08:00
pub async fn get_payer_token_balance(&self, mint: &Pubkey) -> Result<u64, anyhow::Error> {
pumpfun::common::get_token_balance(&self.rpc, &self.payer.pubkey(), mint).await
2025-02-21 20:47:28 +08:00
}
2025-01-25 17:16:52 +08:00
2025-02-21 21:15:10 +08:00
#[inline]
pub fn get_payer_pubkey(&self) -> Pubkey {
self.payer.pubkey()
}
#[inline]
pub fn get_payer(&self) -> &Keypair {
self.payer.as_ref()
}
2025-02-21 20:47:28 +08:00
#[inline]
pub fn get_token_price(&self,virtual_sol_reserves: u64, virtual_token_reserves: u64) -> f64 {
2025-04-02 13:39:59 +08:00
pumpfun::common::get_token_price(virtual_sol_reserves, virtual_token_reserves)
2025-02-21 20:47:28 +08:00
}
#[inline]
pub fn get_buy_price(&self, amount: u64, trade_info: &TradeInfo) -> u64 {
2025-04-02 13:39:59 +08:00
pumpfun::common::get_buy_price(amount, trade_info)
2025-02-21 20:47:28 +08:00
}
#[inline]
pub async fn transfer_sol(&self, payer: &Keypair, receive_wallet: &Pubkey, amount: u64) -> Result<(), anyhow::Error> {
2025-04-02 13:39:59 +08:00
pumpfun::common::transfer_sol(&self.rpc, payer, receive_wallet, amount).await
2024-12-31 16:51:58 +08:00
}
#[inline]
pub async fn close_token_account(&self, mint: &Pubkey) -> Result<(), anyhow::Error> {
pumpfun::common::close_token_account(&self.rpc, self.payer.as_ref(), mint).await
}
2025-06-07 23:16:46 +08:00
#[inline]
pub async fn get_current_price(&self, mint: &Pubkey) -> Result<f64, anyhow::Error> {
let (bonding_curve, _) = pumpfun::common::get_bonding_curve_account_v2(&self.rpc, mint).await?;
let virtual_sol_reserves = bonding_curve.virtual_sol_reserves;
let virtual_token_reserves = bonding_curve.virtual_token_reserves;
Ok(pumpfun::common::get_token_price(virtual_sol_reserves, virtual_token_reserves))
}
#[inline]
pub async fn get_real_sol_reserves(&self, mint: &Pubkey) -> Result<u64, anyhow::Error> {
let (bonding_curve, _) = pumpfun::common::get_bonding_curve_account_v2(&self.rpc, mint).await?;
let actual_sol_reserves = bonding_curve.real_sol_reserves;
Ok(actual_sol_reserves)
}
2025-06-11 21:08:02 +08:00
#[inline]
pub async fn get_real_sol_reserves_with_pumpswap(&self, pool_address: &Pubkey) -> Result<u64, anyhow::Error> {
let pool = pumpswap::pool::Pool::fetch(&self.rpc, pool_address).await?;
let (_, quote_amount) = pool.get_token_balances(&self.rpc).await?;
Ok(quote_amount)
}
2024-12-31 16:51:58 +08:00
}