2025-06-12 00:00:30 +08:00
|
|
|
use std::{str::FromStr, sync::Arc};
|
|
|
|
|
|
2025-07-09 22:29:29 +08:00
|
|
|
use sol_trade_sdk::{
|
2025-07-10 02:53:01 +08:00
|
|
|
common::{bonding_curve::BondingCurveAccount, AnyResult, PriorityFee, TradeConfig},
|
2025-07-10 18:14:21 +08:00
|
|
|
constants::pumpfun::global_constants::TOKEN_TOTAL_SUPPLY,
|
2025-07-09 22:29:29 +08:00
|
|
|
match_event,
|
2025-07-10 18:14:21 +08:00
|
|
|
streaming::{
|
|
|
|
|
event_parser::{
|
|
|
|
|
protocols::{
|
|
|
|
|
bonk::{BonkPoolCreateEvent, BonkTradeEvent},
|
|
|
|
|
pumpfun::{PumpFunCreateTokenEvent, PumpFunTradeEvent},
|
|
|
|
|
pumpswap::{
|
|
|
|
|
PumpSwapBuyEvent, PumpSwapCreatePoolEvent, PumpSwapDepositEvent,
|
|
|
|
|
PumpSwapSellEvent, PumpSwapWithdrawEvent,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Protocol, UnifiedEvent,
|
|
|
|
|
},
|
|
|
|
|
ShredStreamGrpc, YellowstoneGrpc,
|
|
|
|
|
},
|
2025-07-09 22:29:29 +08:00
|
|
|
swqos::{SwqosConfig, SwqosRegion},
|
|
|
|
|
trading::{
|
2025-07-10 22:15:53 +08:00
|
|
|
core::params::PumpFunParams, factory::DexType,
|
2025-07-10 18:14:21 +08:00
|
|
|
pumpfun::common::get_bonding_curve_account_v2,
|
2025-07-09 22:29:29 +08:00
|
|
|
},
|
|
|
|
|
SolanaTrade,
|
|
|
|
|
};
|
2025-07-06 23:36:18 +08:00
|
|
|
use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey, signature::Keypair};
|
2025-01-03 00:38:45 +08:00
|
|
|
|
2025-01-02 19:44:38 +08:00
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2025-07-10 18:14:21 +08:00
|
|
|
test_create_solana_trade_client().await?;
|
2025-07-09 22:29:29 +08:00
|
|
|
test_pumpfun().await?;
|
2025-07-10 18:14:21 +08:00
|
|
|
test_pumpswap().await?;
|
|
|
|
|
test_bonk().await?;
|
|
|
|
|
test_grpc().await?;
|
|
|
|
|
test_shreds().await?;
|
2025-06-24 20:56:15 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 18:14:21 +08:00
|
|
|
/// 创建 SolanaTrade 客户端的示例
|
|
|
|
|
async fn test_create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|
|
|
|
println!("Creating SolanaTrade client...");
|
|
|
|
|
|
2025-06-24 20:56:15 +08:00
|
|
|
let payer = Keypair::new();
|
2025-07-06 23:36:18 +08:00
|
|
|
let rpc_url = "https://mainnet.helius-rpc.com/?api-key=xxxxxx".to_string();
|
2025-07-10 18:14:21 +08:00
|
|
|
|
|
|
|
|
// 配置各种 SWQOS 服务
|
2025-07-07 03:28:02 +08:00
|
|
|
let swqos_configs = vec![
|
|
|
|
|
SwqosConfig::Jito(SwqosRegion::Frankfurt),
|
|
|
|
|
SwqosConfig::NextBlock("your api_token".to_string(), SwqosRegion::Frankfurt),
|
|
|
|
|
SwqosConfig::Bloxroute("your api_token".to_string(), SwqosRegion::Frankfurt),
|
|
|
|
|
SwqosConfig::ZeroSlot("your api_token".to_string(), SwqosRegion::Frankfurt),
|
|
|
|
|
SwqosConfig::Temporal("your api_token".to_string(), SwqosRegion::Frankfurt),
|
|
|
|
|
SwqosConfig::Default(rpc_url.clone()),
|
|
|
|
|
];
|
2025-07-10 18:14:21 +08:00
|
|
|
|
|
|
|
|
// 定义交易配置
|
2025-07-06 23:36:18 +08:00
|
|
|
let trade_config = TradeConfig {
|
|
|
|
|
rpc_url: rpc_url.clone(),
|
2025-06-24 20:56:15 +08:00
|
|
|
commitment: CommitmentConfig::confirmed(),
|
|
|
|
|
priority_fee: PriorityFee::default(),
|
2025-07-06 23:36:18 +08:00
|
|
|
swqos_configs,
|
2025-06-24 20:56:15 +08:00
|
|
|
lookup_table_key: None,
|
|
|
|
|
};
|
2025-07-10 18:14:21 +08:00
|
|
|
|
2025-07-06 23:36:18 +08:00
|
|
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
2025-07-10 18:14:21 +08:00
|
|
|
println!("SolanaTrade client created successfully!");
|
|
|
|
|
|
|
|
|
|
Ok(solana_trade_client)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn test_pumpfun() -> AnyResult<()> {
|
|
|
|
|
println!("Testing PumpFun trading...");
|
|
|
|
|
|
|
|
|
|
let solana_trade_client = test_create_solana_trade_client().await?;
|
2025-07-09 22:29:29 +08:00
|
|
|
let creator = Pubkey::from_str("xxxxxx")?; // dev account
|
|
|
|
|
let buy_sol_cost = 100_000; // 0.0001 SOL
|
2025-06-24 20:56:15 +08:00
|
|
|
let slippage_basis_points = Some(100);
|
2025-07-10 18:14:21 +08:00
|
|
|
let recent_blockhash = solana_trade_client.rpc.get_latest_blockhash().await?;
|
2025-07-09 22:29:29 +08:00
|
|
|
let mint_pubkey = Pubkey::from_str("xxxxxx")?; // token mint
|
2025-07-10 18:14:21 +08:00
|
|
|
|
2025-06-24 20:56:15 +08:00
|
|
|
println!("Buying tokens from PumpFun...");
|
|
|
|
|
// get bonding curve
|
2025-07-09 22:29:29 +08:00
|
|
|
let (bonding_curve, bonding_curve_pda) =
|
|
|
|
|
get_bonding_curve_account_v2(&solana_trade_client.rpc, &mint_pubkey).await?;
|
|
|
|
|
let virtual_token_reserves = bonding_curve.virtual_token_reserves;
|
|
|
|
|
let virtual_sol_reserves = bonding_curve.virtual_sol_reserves;
|
|
|
|
|
let real_token_reserves = bonding_curve.real_token_reserves;
|
|
|
|
|
let real_sol_reserves = bonding_curve.real_sol_reserves;
|
2025-06-24 20:56:15 +08:00
|
|
|
let bonding_curve = BondingCurveAccount {
|
2025-07-09 22:29:29 +08:00
|
|
|
discriminator: bonding_curve.discriminator,
|
|
|
|
|
account: bonding_curve_pda,
|
2025-06-24 20:56:15 +08:00
|
|
|
virtual_token_reserves: virtual_token_reserves,
|
|
|
|
|
virtual_sol_reserves: virtual_sol_reserves,
|
|
|
|
|
real_token_reserves: real_token_reserves,
|
|
|
|
|
real_sol_reserves: real_sol_reserves,
|
|
|
|
|
token_total_supply: TOKEN_TOTAL_SUPPLY,
|
|
|
|
|
complete: false,
|
|
|
|
|
creator: creator,
|
|
|
|
|
};
|
2025-07-09 22:29:29 +08:00
|
|
|
// 如果是狙击开发者
|
|
|
|
|
// let bonding_curve =
|
|
|
|
|
// BondingCurveAccount::new(&mint_pubkey, dev_buy_token, dev_cost_sol, creator);
|
|
|
|
|
// buy
|
2025-06-24 20:56:15 +08:00
|
|
|
solana_trade_client
|
2025-07-10 18:14:21 +08:00
|
|
|
.buy(
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::PumpFun,
|
2025-07-10 18:14:21 +08:00
|
|
|
mint_pubkey,
|
|
|
|
|
Some(creator),
|
|
|
|
|
buy_sol_cost,
|
|
|
|
|
slippage_basis_points,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
Some(Box::new(PumpFunParams {
|
|
|
|
|
bonding_curve: Some(Arc::new(bonding_curve.clone())),
|
|
|
|
|
})),
|
|
|
|
|
)
|
2025-07-09 22:29:29 +08:00
|
|
|
.await?;
|
|
|
|
|
// sell
|
|
|
|
|
println!("Selling tokens from PumpFun...");
|
|
|
|
|
let amount_token = 0; // 写上真实的amount_token
|
|
|
|
|
solana_trade_client
|
2025-07-10 18:14:21 +08:00
|
|
|
.sell(
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::PumpFun,
|
2025-07-10 18:14:21 +08:00
|
|
|
mint_pubkey,
|
|
|
|
|
Some(creator),
|
|
|
|
|
amount_token,
|
|
|
|
|
slippage_basis_points,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
None,
|
|
|
|
|
)
|
2025-06-24 20:56:15 +08:00
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn test_pumpswap() -> AnyResult<()> {
|
2025-07-10 18:14:21 +08:00
|
|
|
println!("Testing PumpSwap trading...");
|
|
|
|
|
|
|
|
|
|
let solana_trade_client = test_create_solana_trade_client().await?;
|
2025-06-24 20:56:15 +08:00
|
|
|
let creator = Pubkey::from_str("11111111111111111111111111111111")?; // dev account
|
2025-07-09 22:29:29 +08:00
|
|
|
let buy_sol_cost = 100_000; // 0.0001 SOL
|
2025-06-12 00:00:30 +08:00
|
|
|
let slippage_basis_points = Some(100);
|
2025-07-10 18:14:21 +08:00
|
|
|
let recent_blockhash = solana_trade_client.rpc.get_latest_blockhash().await?;
|
2025-06-24 20:56:15 +08:00
|
|
|
let mint_pubkey = Pubkey::from_str("2zMMhcVQEXDtdE6vsFS7S7D5oUodfJHE8vd1gnBouauv")?; // token mint
|
2025-07-10 18:14:21 +08:00
|
|
|
|
2025-06-12 00:00:30 +08:00
|
|
|
println!("Buying tokens from PumpSwap...");
|
2025-07-09 22:29:29 +08:00
|
|
|
// buy
|
2025-06-24 20:56:15 +08:00
|
|
|
solana_trade_client
|
2025-07-10 18:14:21 +08:00
|
|
|
.buy(
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::PumpFun,
|
2025-07-10 18:14:21 +08:00
|
|
|
mint_pubkey,
|
|
|
|
|
Some(creator),
|
|
|
|
|
buy_sol_cost,
|
|
|
|
|
slippage_basis_points,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
None,
|
|
|
|
|
)
|
2025-07-09 22:29:29 +08:00
|
|
|
.await?;
|
|
|
|
|
// sell
|
|
|
|
|
println!("Selling tokens from PumpSwap...");
|
|
|
|
|
let amount_token = 0; // 写上真实的amount_token
|
|
|
|
|
solana_trade_client
|
2025-07-10 18:14:21 +08:00
|
|
|
.sell(
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::PumpSwap,
|
2025-07-10 18:14:21 +08:00
|
|
|
mint_pubkey,
|
|
|
|
|
Some(creator),
|
|
|
|
|
amount_token,
|
|
|
|
|
slippage_basis_points,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
None,
|
|
|
|
|
)
|
2025-06-12 00:00:30 +08:00
|
|
|
.await?;
|
2025-07-09 22:29:29 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-10 00:23:29 +08:00
|
|
|
async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
|
2025-07-10 18:14:21 +08:00
|
|
|
println!("Testing Bonk trading...");
|
|
|
|
|
|
|
|
|
|
let solana_trade_client = test_create_solana_trade_client().await?;
|
|
|
|
|
let buy_sol_cost = 100_000; // 0.0001 SOL
|
|
|
|
|
let slippage_basis_points = Some(100); // 1%
|
2025-07-09 22:29:29 +08:00
|
|
|
let recent_blockhash = solana_trade_client.rpc.get_latest_blockhash().await?;
|
2025-07-10 18:14:21 +08:00
|
|
|
let mint_pubkey = Pubkey::from_str("xxxxxxx")?;
|
|
|
|
|
|
2025-07-10 00:23:29 +08:00
|
|
|
println!("Buying tokens from letsbonk.fun...");
|
2025-07-09 22:29:29 +08:00
|
|
|
// buy
|
|
|
|
|
solana_trade_client
|
2025-07-10 18:14:21 +08:00
|
|
|
.buy(
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::Bonk,
|
2025-07-10 18:14:21 +08:00
|
|
|
mint_pubkey,
|
|
|
|
|
None,
|
|
|
|
|
buy_sol_cost,
|
|
|
|
|
slippage_basis_points,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
None,
|
|
|
|
|
)
|
2025-07-09 22:29:29 +08:00
|
|
|
.await?;
|
|
|
|
|
// sell
|
2025-07-10 00:23:29 +08:00
|
|
|
println!("Selling tokens from letsbonk.fun...");
|
2025-07-10 18:14:21 +08:00
|
|
|
let amount_token = 0; // 写上真实的amount_token
|
2025-07-09 22:29:29 +08:00
|
|
|
solana_trade_client
|
2025-07-10 18:14:21 +08:00
|
|
|
.sell(
|
2025-07-10 22:15:53 +08:00
|
|
|
DexType::Bonk,
|
2025-07-10 18:14:21 +08:00
|
|
|
mint_pubkey,
|
|
|
|
|
None,
|
|
|
|
|
amount_token,
|
|
|
|
|
slippage_basis_points,
|
|
|
|
|
recent_blockhash,
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
None,
|
|
|
|
|
)
|
2025-07-09 22:29:29 +08:00
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn test_grpc() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
// 使用 GRPC 客户端订阅事件
|
|
|
|
|
println!("正在订阅 GRPC 事件...");
|
|
|
|
|
|
|
|
|
|
let grpc = YellowstoneGrpc::new(
|
|
|
|
|
"https://solana-yellowstone-grpc.publicnode.com:443".to_string(),
|
|
|
|
|
None,
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// 定义回调函数处理 PumpSwap 事件
|
|
|
|
|
let callback = |event: Box<dyn UnifiedEvent>| {
|
|
|
|
|
match_event!(event, {
|
2025-07-10 00:23:29 +08:00
|
|
|
BonkPoolCreateEvent => |e: BonkPoolCreateEvent| {
|
|
|
|
|
println!("BonkPoolCreateEvent: {:?}", e.base_mint_param.symbol);
|
2025-07-09 22:29:29 +08:00
|
|
|
},
|
2025-07-10 00:23:29 +08:00
|
|
|
BonkTradeEvent => |e: BonkTradeEvent| {
|
|
|
|
|
println!("BonkTradeEvent: {:?}", e);
|
2025-07-09 22:29:29 +08:00
|
|
|
},
|
|
|
|
|
PumpFunTradeEvent => |e: PumpFunTradeEvent| {
|
|
|
|
|
println!("PumpFunTradeEvent: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpFunCreateTokenEvent => |e: PumpFunCreateTokenEvent| {
|
|
|
|
|
println!("PumpFunCreateTokenEvent: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapBuyEvent => |e: PumpSwapBuyEvent| {
|
|
|
|
|
println!("Buy event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapSellEvent => |e: PumpSwapSellEvent| {
|
|
|
|
|
println!("Sell event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapCreatePoolEvent => |e: PumpSwapCreatePoolEvent| {
|
|
|
|
|
println!("CreatePool event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapDepositEvent => |e: PumpSwapDepositEvent| {
|
|
|
|
|
println!("Deposit event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapWithdrawEvent => |e: PumpSwapWithdrawEvent| {
|
|
|
|
|
println!("Withdraw event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 订阅 PumpSwap 事件
|
|
|
|
|
println!("开始监听事件,按 Ctrl+C 停止...");
|
2025-07-10 18:14:21 +08:00
|
|
|
let protocols = vec![Protocol::PumpFun, Protocol::PumpSwap, Protocol::Bonk];
|
2025-07-09 22:29:29 +08:00
|
|
|
grpc.subscribe_events(protocols, None, None, None, callback)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn test_shreds() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
// 使用 ShredStream 客户端订阅事件
|
|
|
|
|
println!("正在订阅 ShredStream 事件...");
|
|
|
|
|
|
|
|
|
|
let shred_stream = ShredStreamGrpc::new("http://127.0.0.1:10800".to_string()).await?;
|
|
|
|
|
|
|
|
|
|
// 定义回调函数处理 PumpSwap 事件
|
|
|
|
|
let callback = |event: Box<dyn UnifiedEvent>| {
|
|
|
|
|
match_event!(event, {
|
2025-07-10 00:23:29 +08:00
|
|
|
BonkPoolCreateEvent => |e: BonkPoolCreateEvent| {
|
|
|
|
|
println!("BonkPoolCreateEvent: {:?}", e.base_mint_param.symbol);
|
2025-07-09 22:29:29 +08:00
|
|
|
},
|
2025-07-10 00:23:29 +08:00
|
|
|
BonkTradeEvent => |e: BonkTradeEvent| {
|
|
|
|
|
println!("BonkTradeEvent: {:?}", e);
|
2025-07-09 22:29:29 +08:00
|
|
|
},
|
|
|
|
|
PumpFunTradeEvent => |e: PumpFunTradeEvent| {
|
|
|
|
|
println!("PumpFunTradeEvent: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpFunCreateTokenEvent => |e: PumpFunCreateTokenEvent| {
|
|
|
|
|
println!("PumpFunCreateTokenEvent: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapBuyEvent => |e: PumpSwapBuyEvent| {
|
|
|
|
|
println!("Buy event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapSellEvent => |e: PumpSwapSellEvent| {
|
|
|
|
|
println!("Sell event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapCreatePoolEvent => |e: PumpSwapCreatePoolEvent| {
|
|
|
|
|
println!("CreatePool event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapDepositEvent => |e: PumpSwapDepositEvent| {
|
|
|
|
|
println!("Deposit event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
PumpSwapWithdrawEvent => |e: PumpSwapWithdrawEvent| {
|
|
|
|
|
println!("Withdraw event: {:?}", e);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 订阅 PumpSwap 事件
|
|
|
|
|
println!("开始监听事件,按 Ctrl+C 停止...");
|
2025-07-10 18:14:21 +08:00
|
|
|
let protocols = vec![Protocol::PumpFun, Protocol::PumpSwap, Protocol::Bonk];
|
2025-07-09 22:29:29 +08:00
|
|
|
shred_stream
|
|
|
|
|
.shredstream_subscribe(protocols, None, callback)
|
|
|
|
|
.await?;
|
|
|
|
|
|
2025-06-12 00:00:30 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|