Add a selling method based on quantity

This commit is contained in:
wei
2025-06-07 23:16:46 +08:00
parent 94c2a22ab7
commit b333811483
7 changed files with 318 additions and 17 deletions
+137 -4
View File
@@ -9,6 +9,7 @@ pub mod swqos;
pub mod pumpfun;
use std::sync::Arc;
use std::sync::Mutex;
use swqos::{FeeClient, JitoClient, NextBlockClient, NozomiClient, SolRpcClient, ZeroSlotClient};
use rustls::crypto::{ring::default_provider, CryptoProvider};
@@ -23,6 +24,8 @@ use common::{pumpfun::logs_data::TradeInfo, pumpfun::logs_events::PumpfunEvent,
use common::pumpfun::logs_subscribe::SubscriptionHandle;
use ipfs::TokenMetadataIPFS;
use constants::pumpfun::trade_type::{COPY_BUY, SNIPER_BUY};
pub struct PumpFun {
pub payer: Arc<Keypair>,
pub rpc: Arc<SolanaRpcClient>,
@@ -31,6 +34,8 @@ pub struct PumpFun {
pub cluster: Cluster,
}
static INSTANCE: Mutex<Option<Arc<PumpFun>>> = Mutex::new(None);
impl Clone for PumpFun {
fn clone(&self) -> Self {
Self {
@@ -105,13 +110,29 @@ impl PumpFun {
fee_clients.push(Arc::new(rpc_client));
}
Self {
let instance = Self {
payer,
rpc,
fee_clients,
priority_fee: cluster.clone().priority_fee,
cluster: cluster.clone(),
}
};
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()
}
/// Create a new token
@@ -172,7 +193,7 @@ impl PumpFun {
}
/// Buy tokens
pub async fn buy(
pub async fn sniper_buy(
&self,
mint: Pubkey,
creator: Pubkey,
@@ -194,11 +215,38 @@ impl PumpFun {
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
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,
) -> Result<(), anyhow::Error> {
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
}
/// Buy tokens using Jito
pub async fn buy_with_tip(
pub async fn sniper_buy_with_tip(
&self,
mint: Pubkey,
creator: Pubkey,
@@ -220,6 +268,33 @@ impl PumpFun {
self.priority_fee.clone(),
self.cluster.clone().lookup_table_key,
recent_blockhash,
SNIPER_BUY.to_string(),
).await
}
pub async fn copy_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,
COPY_BUY.to_string(),
).await
}
@@ -265,6 +340,26 @@ impl PumpFun {
).await
}
/// Sell tokens by amount
pub async fn sell_by_amount(
&self,
mint: Pubkey,
creator: Pubkey,
amount: u64,
recent_blockhash: Hash,
) -> Result<(), anyhow::Error> {
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
}
pub async fn sell_by_percent_with_tip(
&self,
mint: Pubkey,
@@ -286,6 +381,25 @@ impl PumpFun {
).await
}
pub async fn sell_by_amount_with_tip(
&self,
mint: Pubkey,
creator: Pubkey,
amount: u64,
recent_blockhash: Hash,
) -> Result<(), anyhow::Error> {
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
}
/// Sell tokens using Jito
pub async fn sell_with_tip(
&self,
@@ -375,4 +489,23 @@ impl PumpFun {
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
}
#[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)
}
}