Merge commit 'bfd6809855a37a55b852fee18b507640a955bb0d'
This commit is contained in:
@@ -6,12 +6,14 @@ A comprehensive Rust SDK for seamless interaction with Solana DEX trading progra
|
|||||||
|
|
||||||
1. **PumpFun Trading**: Support for `create`, `buy`, `sell` operations
|
1. **PumpFun Trading**: Support for `create`, `buy`, `sell` operations
|
||||||
2. **PumpSwap Trading**: Support for PumpSwap pool trading operations
|
2. **PumpSwap Trading**: Support for PumpSwap pool trading operations
|
||||||
3. **Logs Subscription**: Subscribe to PumpFun program transaction logs
|
3. **Raydium Trading**: Support for Raydium DEX trading operations
|
||||||
4. **Yellowstone gRPC**: Subscribe to program logs using gRPC
|
4. **Logs Subscription**: Subscribe to PumpFun, PumpSwap, and Raydium program transaction logs
|
||||||
5. **Multiple MEV Protection**: Support for Jito, Nextblock, 0slot, Nozomi services
|
5. **Yellowstone gRPC**: Subscribe to program logs using Yellowstone gRPC
|
||||||
6. **Concurrent Transactions**: Submit transactions using multiple MEV services simultaneously; the fastest succeeds while others fail
|
6. **ShredStream Support**: Subscribe to program logs using ShredStream
|
||||||
7. **IPFS Integration**: Support for token metadata IPFS uploads
|
7. **Multiple MEV Protection**: Support for Jito, Nextblock, 0slot, Nozomi services
|
||||||
8. **Real-time Pricing**: Get real-time token prices and liquidity information
|
8. **Concurrent Transactions**: Submit transactions using multiple MEV services simultaneously; the fastest succeeds while others fail
|
||||||
|
9. **IPFS Integration**: Support for token metadata IPFS uploads
|
||||||
|
10. **Real-time Pricing**: Get real-time token prices and liquidity information
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -37,41 +39,38 @@ sol-trade-sdk = { path = "./sol-trade-sdk", version = "0.1.0" }
|
|||||||
use sol_trade_sdk::{common::pumpfun::logs_events::PumpfunEvent, grpc::YellowstoneGrpc};
|
use sol_trade_sdk::{common::pumpfun::logs_events::PumpfunEvent, grpc::YellowstoneGrpc};
|
||||||
use solana_sdk::signature::Keypair;
|
use solana_sdk::signature::Keypair;
|
||||||
|
|
||||||
// Create gRPC client
|
// Create gRPC client with Yellowstone
|
||||||
let grpc_url = "http://127.0.0.1:10000";
|
let grpc_url = "https://solana-yellowstone-grpc.publicnode.com:443";
|
||||||
let x_token = None; // Optional auth token
|
let x_token = None; // Optional auth token
|
||||||
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
||||||
|
|
||||||
// Define callback function
|
// Define callback function
|
||||||
let callback = |event: PumpfunEvent| {
|
let callback = |event: PumpfunEvent| match event {
|
||||||
match event {
|
PumpfunEvent::NewDevTrade(trade_info) => {
|
||||||
PumpfunEvent::NewToken(token_info) => {
|
println!("Received new dev trade event: {:?}", trade_info);
|
||||||
println!("Received new token event: {:?}", token_info);
|
}
|
||||||
},
|
PumpfunEvent::NewToken(token_info) => {
|
||||||
PumpfunEvent::NewDevTrade(trade_info) => {
|
println!("Received new token event: {:?}", token_info);
|
||||||
println!("Received dev trade event: {:?}", trade_info);
|
}
|
||||||
},
|
PumpfunEvent::NewUserTrade(trade_info) => {
|
||||||
PumpfunEvent::NewUserTrade(trade_info) => {
|
println!("Received new trade event: {:?}", trade_info);
|
||||||
println!("Received new trade event: {:?}", trade_info);
|
}
|
||||||
},
|
PumpfunEvent::NewBotTrade(trade_info) => {
|
||||||
PumpfunEvent::NewBotTrade(trade_info) => {
|
println!("Received new bot trade event: {:?}", trade_info);
|
||||||
println!("Received new bot trade event: {:?}", trade_info);
|
}
|
||||||
}
|
PumpfunEvent::Error(err) => {
|
||||||
PumpfunEvent::Error(err) => {
|
println!("Received error: {}", err);
|
||||||
println!("Received error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let payer_keypair = Keypair::from_base58_string("your_private_key");
|
client.subscribe_pumpfun(callback, None).await?;
|
||||||
client.subscribe_pumpfun(callback, Some(payer_keypair.pubkey())).await?;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. Initialize PumpFun Instance
|
### 2. Initialize SolanaTrade Instance
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use sol_trade_sdk::{common::{Cluster, PriorityFee}, PumpFun};
|
use sol_trade_sdk::{common::{Cluster, PriorityFee}, SolanaTrade};
|
||||||
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
|
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
|
||||||
|
|
||||||
// Configure priority fees
|
// Configure priority fees
|
||||||
@@ -105,15 +104,15 @@ let cluster = Cluster {
|
|||||||
lookup_table_key: None, // Optional lookup table
|
lookup_table_key: None, // Optional lookup table
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create PumpFun instance
|
// Create SolanaTrade instance
|
||||||
let payer = Keypair::from_base58_string("your_private_key");
|
let payer = Keypair::from_base58_string("your_private_key");
|
||||||
let pumpfun = PumpFun::new(Arc::new(payer), &cluster).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), &cluster).await;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. Create Token
|
### 3. Create Token
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use sol_trade_sdk::{PumpFun, ipfs::CreateTokenMetadata, ipfs::create_token_metadata};
|
use sol_trade_sdk::{ipfs::CreateTokenMetadata, ipfs::create_token_metadata};
|
||||||
use solana_sdk::signature::Keypair;
|
use solana_sdk::signature::Keypair;
|
||||||
|
|
||||||
// Create token keypair
|
// Create token keypair
|
||||||
@@ -136,14 +135,14 @@ let api_token = "your_pinata_api_token";
|
|||||||
let ipfs_response = create_token_metadata(metadata, api_token).await?;
|
let ipfs_response = create_token_metadata(metadata, api_token).await?;
|
||||||
|
|
||||||
// Create token
|
// Create token
|
||||||
pumpfun.create(mint_keypair, ipfs_response).await?;
|
solana_trade_client.create(mint_keypair, ipfs_response).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3.1. Create and Buy Token (with MEV protection)
|
### 3.1. Create and Buy Token (with MEV protection)
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// Create token and buy simultaneously with MEV protection
|
// Create token and buy simultaneously with MEV protection
|
||||||
pumpfun.create_and_buy_with_tip(
|
solana_trade_client.create_and_buy_with_tip(
|
||||||
payer.clone(), // payer keypair
|
payer.clone(), // payer keypair
|
||||||
mint_keypair, // mint keypair
|
mint_keypair, // mint keypair
|
||||||
ipfs_response, // IPFS response
|
ipfs_response, // IPFS response
|
||||||
@@ -157,32 +156,39 @@ pumpfun.create_and_buy_with_tip(
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
use solana_sdk::{pubkey::Pubkey, hash::Hash};
|
use solana_sdk::{pubkey::Pubkey, hash::Hash};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use sol_trade_sdk::accounts::BondingCurveAccount;
|
||||||
|
|
||||||
let mint_pubkey = Pubkey::from_str("token_address")?;
|
let mint_pubkey = Pubkey::from_str("token_address")?;
|
||||||
let creator = Pubkey::from_str("creator_address")?;
|
let creator = Pubkey::from_str("creator_address")?;
|
||||||
let recent_blockhash = Hash::default(); // Get latest blockhash
|
let recent_blockhash = Hash::default(); // Get latest blockhash
|
||||||
|
let buy_sol_cost = 50000; // 0.00005 SOL
|
||||||
|
let slippage_basis_points = Some(100); // 1%
|
||||||
|
|
||||||
// Sniper buy (fast purchase when new token launches)
|
// Sniper buy (fast purchase when new token launches)
|
||||||
pumpfun.sniper_buy_with_tip(
|
let dev_buy_token = 100_000; // test value
|
||||||
|
let dev_cost_sol = 10_000; // test value
|
||||||
|
let bonding_curve = BondingCurveAccount::new(&mint_pubkey, dev_buy_token, dev_cost_sol, creator);
|
||||||
|
|
||||||
|
solana_trade_client.sniper_buy(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
1000000, // dev_buy_token
|
buy_sol_cost,
|
||||||
10000, // dev_sol_cost
|
slippage_basis_points,
|
||||||
50000, // buy_sol_cost (lamports)
|
|
||||||
Some(100), // slippage (1%)
|
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
|
Some(Arc::new(bonding_curve)),
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
// Copy buy (follow other traders)
|
// Buy with tip for MEV protection
|
||||||
pumpfun.copy_buy_with_tip(
|
solana_trade_client.buy_with_tip(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
1000000, // dev_buy_token
|
buy_sol_cost,
|
||||||
10000, // dev_sol_cost
|
slippage_basis_points,
|
||||||
50000, // buy_sol_cost (lamports)
|
|
||||||
Some(100), // slippage (1%)
|
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
|
Some(Arc::new(bonding_curve)),
|
||||||
"pumpfun".to_string(), // trading platform
|
"pumpfun".to_string(), // trading platform
|
||||||
|
None, // custom tip fee
|
||||||
).await?;
|
).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -190,22 +196,22 @@ pumpfun.copy_buy_with_tip(
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
// Sell by amount
|
// Sell by amount
|
||||||
pumpfun.sell_by_amount_with_tip(
|
solana_trade_client.sell_by_amount_with_tip(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
1000000, // token amount
|
1000000, // token amount
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
"pumpfun".to_string(),
|
"pumpfun".to_string(), // trading platform
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
// Sell by percentage
|
// Sell by percentage
|
||||||
pumpfun.sell_by_percent_with_tip(
|
solana_trade_client.sell_by_percent_with_tip(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
50, // percentage (50%)
|
50, // percentage (50%)
|
||||||
2000000, // total token amount
|
2000000, // total token amount
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
"pumpfun".to_string(),
|
"pumpfun".to_string(), // trading platform
|
||||||
).await?;
|
).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -213,19 +219,19 @@ pumpfun.sell_by_percent_with_tip(
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
// Get current token price
|
// Get current token price
|
||||||
let price = pumpfun.get_current_price(&mint_pubkey).await?;
|
let price = solana_trade_client.get_current_price(&mint_pubkey).await?;
|
||||||
println!("Current price: {}", price);
|
println!("Current price: {}", price);
|
||||||
|
|
||||||
// Get SOL balance
|
// Get SOL balance
|
||||||
let sol_balance = pumpfun.get_payer_sol_balance().await?;
|
let sol_balance = solana_trade_client.get_payer_sol_balance().await?;
|
||||||
println!("SOL balance: {} lamports", sol_balance);
|
println!("SOL balance: {} lamports", sol_balance);
|
||||||
|
|
||||||
// Get token balance
|
// Get token balance
|
||||||
let token_balance = pumpfun.get_payer_token_balance(&mint_pubkey).await?;
|
let token_balance = solana_trade_client.get_payer_token_balance(&mint_pubkey).await?;
|
||||||
println!("Token balance: {}", token_balance);
|
println!("Token balance: {}", token_balance);
|
||||||
|
|
||||||
// Get liquidity information
|
// Get liquidity information
|
||||||
let sol_reserves = pumpfun.get_real_sol_reserves(&mint_pubkey).await?;
|
let sol_reserves = solana_trade_client.get_real_sol_reserves(&mint_pubkey).await?;
|
||||||
println!("SOL reserves: {} lamports", sol_reserves);
|
println!("SOL reserves: {} lamports", sol_reserves);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -234,87 +240,109 @@ println!("SOL reserves: {} lamports", sol_reserves);
|
|||||||
```rust
|
```rust
|
||||||
use sol_trade_sdk::{common::pumpswap::logs_events::PumpSwapEvent, grpc::YellowstoneGrpc};
|
use sol_trade_sdk::{common::pumpswap::logs_events::PumpSwapEvent, grpc::YellowstoneGrpc};
|
||||||
|
|
||||||
// Create gRPC client (same as above)
|
// Create gRPC client with Yellowstone
|
||||||
let grpc_url = "http://127.0.0.1:10000";
|
let grpc_url = "https://solana-yellowstone-grpc.publicnode.com:443";
|
||||||
let x_token = None;
|
let x_token = None;
|
||||||
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
||||||
|
|
||||||
// Define callback function for PumpSwap events
|
// Define callback function for PumpSwap events
|
||||||
let callback = |event: PumpSwapEvent| {
|
let callback = |event: PumpSwapEvent| match event {
|
||||||
match event {
|
PumpSwapEvent::Buy(buy_event) => {
|
||||||
PumpSwapEvent::Buy(buy_event) => {
|
println!("buy_event: {:?}", buy_event);
|
||||||
println!("PumpSwap Buy Event: {:?}", buy_event);
|
}
|
||||||
},
|
PumpSwapEvent::Sell(sell_event) => {
|
||||||
PumpSwapEvent::Sell(sell_event) => {
|
println!("sell_event: {:?}", sell_event);
|
||||||
println!("PumpSwap Sell Event: {:?}", sell_event);
|
}
|
||||||
},
|
PumpSwapEvent::CreatePool(create_event) => {
|
||||||
PumpSwapEvent::CreatePool(pool_event) => {
|
println!("create_event: {:?}", create_event);
|
||||||
println!("PumpSwap Pool Created: {:?}", pool_event);
|
}
|
||||||
},
|
PumpSwapEvent::Deposit(deposit_event) => {
|
||||||
PumpSwapEvent::Deposit(deposit_event) => {
|
println!("deposit_event: {:?}", deposit_event);
|
||||||
println!("PumpSwap Deposit: {:?}", deposit_event);
|
}
|
||||||
},
|
PumpSwapEvent::Withdraw(withdraw_event) => {
|
||||||
PumpSwapEvent::Withdraw(withdraw_event) => {
|
println!("withdraw_event: {:?}", withdraw_event);
|
||||||
println!("PumpSwap Withdraw: {:?}", withdraw_event);
|
}
|
||||||
},
|
PumpSwapEvent::Disable(disable_event) => {
|
||||||
PumpSwapEvent::Disable(disable_event) => {
|
println!("disable_event: {:?}", disable_event);
|
||||||
println!("PumpSwap Pool Disabled: {:?}", disable_event);
|
}
|
||||||
},
|
PumpSwapEvent::UpdateAdmin(update_admin_event) => {
|
||||||
PumpSwapEvent::UpdateAdmin(admin_event) => {
|
println!("update_admin_event: {:?}", update_admin_event);
|
||||||
println!("PumpSwap Admin Updated: {:?}", admin_event);
|
}
|
||||||
},
|
PumpSwapEvent::UpdateFeeConfig(update_fee_event) => {
|
||||||
PumpSwapEvent::UpdateFeeConfig(fee_event) => {
|
println!("update_fee_event: {:?}", update_fee_event);
|
||||||
println!("PumpSwap Fee Config Updated: {:?}", fee_event);
|
}
|
||||||
},
|
PumpSwapEvent::Error(err) => {
|
||||||
PumpSwapEvent::Error(err) => {
|
println!("error: {}", err);
|
||||||
println!("PumpSwap Error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Subscribe to PumpSwap events
|
// Subscribe to PumpSwap events
|
||||||
|
println!("Monitoring PumpSwap events, press Ctrl+C to stop...");
|
||||||
client.subscribe_pumpswap(callback).await?;
|
client.subscribe_pumpswap(callback).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 8. PumpSwap Trading Operations
|
### 8. PumpSwap Trading Operations
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use solana_sdk::{pubkey::Pubkey, hash::Hash};
|
use std::sync::Arc;
|
||||||
|
use solana_sdk::{pubkey::Pubkey, hash::Hash, signature::Keypair};
|
||||||
|
use solana_client::rpc_client::RpcClient;
|
||||||
|
use sol_trade_sdk::{common::{Cluster, PriorityFee}, SolanaTrade};
|
||||||
|
|
||||||
let mint_pubkey = Pubkey::from_str("token_address")?;
|
let payer = Keypair::new();
|
||||||
let creator = Pubkey::from_str("creator_address")?;
|
// Define cluster configuration
|
||||||
let recent_blockhash = Hash::default();
|
let cluster = Cluster {
|
||||||
|
rpc_url: "https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY".to_string(),
|
||||||
|
commitment: CommitmentConfig::confirmed(),
|
||||||
|
priority_fee: PriorityFee::default(),
|
||||||
|
use_jito: false,
|
||||||
|
use_zeroslot: false,
|
||||||
|
use_nozomi: false,
|
||||||
|
use_nextblock: false,
|
||||||
|
block_engine_url: "".to_string(),
|
||||||
|
zeroslot_url: "".to_string(),
|
||||||
|
zeroslot_auth_token: "".to_string(),
|
||||||
|
nozomi_url: "".to_string(),
|
||||||
|
nozomi_auth_token: "".to_string(),
|
||||||
|
nextblock_url: "".to_string(),
|
||||||
|
nextblock_auth_token: "".to_string(),
|
||||||
|
lookup_table_key: None,
|
||||||
|
use_rpc: true,
|
||||||
|
};
|
||||||
|
|
||||||
// Buy tokens on PumpSwap
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), &cluster).await;
|
||||||
pumpfun.copy_buy_with_tip(
|
let creator = Pubkey::from_str("11111111111111111111111111111111")?; // dev account
|
||||||
mint_pubkey,
|
let buy_sol_cost = 500_000; // 0.0005 SOL
|
||||||
creator,
|
let slippage_basis_points = Some(100);
|
||||||
1000000, // dev_buy_token
|
let rpc = RpcClient::new(cluster.rpc_url);
|
||||||
10000, // dev_sol_cost
|
let recent_blockhash = rpc.get_latest_blockhash().unwrap();
|
||||||
50000, // buy_sol_cost (lamports)
|
let trade_platform = "pumpswap".to_string();
|
||||||
Some(100), // slippage (1%)
|
let mint_pubkey = Pubkey::from_str("YOUR_TOKEN_MINT")?; // token mint
|
||||||
recent_blockhash,
|
|
||||||
"pumpswap".to_string(), // Use PumpSwap platform
|
|
||||||
).await?;
|
|
||||||
|
|
||||||
// Sell tokens on PumpSwap by amount
|
println!("Buying tokens from PumpSwap...");
|
||||||
pumpfun.sell_by_amount_with_tip(
|
solana_trade_client
|
||||||
mint_pubkey,
|
.buy(
|
||||||
creator,
|
mint_pubkey,
|
||||||
1000000, // token amount
|
creator,
|
||||||
recent_blockhash,
|
buy_sol_cost,
|
||||||
"pumpswap".to_string(), // Use PumpSwap platform
|
slippage_basis_points,
|
||||||
).await?;
|
recent_blockhash,
|
||||||
|
None,
|
||||||
|
trade_platform.clone(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Sell tokens on PumpSwap by percentage
|
// Sell 30% * amount_token quantity
|
||||||
pumpfun.sell_by_percent_with_tip(
|
solana_trade_client
|
||||||
mint_pubkey,
|
.sell_by_percent(
|
||||||
creator,
|
mint_pubkey,
|
||||||
50, // percentage (50%)
|
creator,
|
||||||
2000000, // total token amount
|
30, // percentage (30%)
|
||||||
recent_blockhash,
|
100, // total token amount
|
||||||
"pumpswap".to_string(), // Use PumpSwap platform
|
recent_blockhash,
|
||||||
).await?;
|
trade_platform.clone(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 9. PumpSwap Pool Information
|
### 9. PumpSwap Pool Information
|
||||||
@@ -325,15 +353,15 @@ use solana_sdk::pubkey::Pubkey;
|
|||||||
let pool_address = Pubkey::from_str("pool_address")?;
|
let pool_address = Pubkey::from_str("pool_address")?;
|
||||||
|
|
||||||
// Get current price from PumpSwap pool
|
// Get current price from PumpSwap pool
|
||||||
let price = pumpfun.get_current_price_with_pumpswap(&pool_address).await?;
|
let price = solana_trade_client.get_current_price_with_pumpswap(&pool_address).await?;
|
||||||
println!("PumpSwap pool price: {}", price);
|
println!("PumpSwap pool price: {}", price);
|
||||||
|
|
||||||
// Get SOL reserves in PumpSwap pool
|
// Get SOL reserves in PumpSwap pool
|
||||||
let sol_reserves = pumpfun.get_real_sol_reserves_with_pumpswap(&pool_address).await?;
|
let sol_reserves = solana_trade_client.get_real_sol_reserves_with_pumpswap(&pool_address).await?;
|
||||||
println!("PumpSwap SOL reserves: {} lamports", sol_reserves);
|
println!("PumpSwap SOL reserves: {} lamports", sol_reserves);
|
||||||
|
|
||||||
// Get token balance in PumpSwap pool
|
// Get token balance in PumpSwap pool
|
||||||
let token_balance = pumpfun.get_payer_token_balance_with_pumpswap(&pool_address).await?;
|
let token_balance = solana_trade_client.get_payer_token_balance_with_pumpswap(&pool_address).await?;
|
||||||
println!("PumpSwap token balance: {}", token_balance);
|
println!("PumpSwap token balance: {}", token_balance);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+147
-119
@@ -6,12 +6,14 @@
|
|||||||
|
|
||||||
1. **PumpFun交易**: 支持`创建代币`、`购买`、`卖出`功能
|
1. **PumpFun交易**: 支持`创建代币`、`购买`、`卖出`功能
|
||||||
2. **PumpSwap交易**: 支持PumpSwap池的交易操作
|
2. **PumpSwap交易**: 支持PumpSwap池的交易操作
|
||||||
3. **日志订阅**: 订阅PumpFun程序的交易日志
|
3. **Raydium交易**: 支持Raydium DEX的交易操作
|
||||||
4. **Yellowstone gRPC**: 使用gRPC订阅程序日志
|
4. **日志订阅**: 订阅PumpFun、PumpSwap和Raydium程序的交易日志
|
||||||
5. **多种MEV保护**: 支持Jito、Nextblock、0slot、Nozomi等服务
|
5. **Yellowstone gRPC**: 使用Yellowstone gRPC订阅程序日志
|
||||||
6. **并发交易**: 同时使用多个MEV服务发送交易,最快的成功,其他失败
|
6. **ShredStream支持**: 使用ShredStream订阅程序日志
|
||||||
7. **IPFS集成**: 支持代币元数据的IPFS上传
|
7. **多种MEV保护**: 支持Jito、Nextblock、0slot、Nozomi等服务
|
||||||
8. **实时价格**: 获取代币实时价格和流动性信息
|
8. **并发交易**: 同时使用多个MEV服务发送交易,最快的成功,其他失败
|
||||||
|
9. **IPFS集成**: 支持代币元数据的IPFS上传
|
||||||
|
10. **实时价格**: 获取代币实时价格和流动性信息
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
@@ -37,41 +39,38 @@ sol-trade-sdk = { path = "./sol-trade-sdk", version = "0.1.0" }
|
|||||||
use sol_trade_sdk::{common::pumpfun::logs_events::PumpfunEvent, grpc::YellowstoneGrpc};
|
use sol_trade_sdk::{common::pumpfun::logs_events::PumpfunEvent, grpc::YellowstoneGrpc};
|
||||||
use solana_sdk::signature::Keypair;
|
use solana_sdk::signature::Keypair;
|
||||||
|
|
||||||
// 创建gRPC客户端
|
// 创建Yellowstone gRPC客户端
|
||||||
let grpc_url = "http://127.0.0.1:10000";
|
let grpc_url = "https://solana-yellowstone-grpc.publicnode.com:443";
|
||||||
let x_token = None; // 可选的认证令牌
|
let x_token = None; // 可选的认证令牌
|
||||||
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
||||||
|
|
||||||
// 定义回调函数
|
// 定义回调函数
|
||||||
let callback = |event: PumpfunEvent| {
|
let callback = |event: PumpfunEvent| match event {
|
||||||
match event {
|
PumpfunEvent::NewDevTrade(trade_info) => {
|
||||||
PumpfunEvent::NewToken(token_info) => {
|
println!("收到开发者交易事件: {:?}", trade_info);
|
||||||
println!("收到新代币事件: {:?}", token_info);
|
}
|
||||||
},
|
PumpfunEvent::NewToken(token_info) => {
|
||||||
PumpfunEvent::NewDevTrade(trade_info) => {
|
println!("收到新代币事件: {:?}", token_info);
|
||||||
println!("收到开发者交易事件: {:?}", trade_info);
|
}
|
||||||
},
|
PumpfunEvent::NewUserTrade(trade_info) => {
|
||||||
PumpfunEvent::NewUserTrade(trade_info) => {
|
println!("收到用户交易事件: {:?}", trade_info);
|
||||||
println!("收到用户交易事件: {:?}", trade_info);
|
}
|
||||||
},
|
PumpfunEvent::NewBotTrade(trade_info) => {
|
||||||
PumpfunEvent::NewBotTrade(trade_info) => {
|
println!("收到机器人交易事件: {:?}", trade_info);
|
||||||
println!("收到机器人交易事件: {:?}", trade_info);
|
}
|
||||||
}
|
PumpfunEvent::Error(err) => {
|
||||||
PumpfunEvent::Error(err) => {
|
println!("收到错误: {}", err);
|
||||||
println!("收到错误: {}", err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let payer_keypair = Keypair::from_base58_string("your_private_key");
|
client.subscribe_pumpfun(callback, None).await?;
|
||||||
client.subscribe_pumpfun(callback, Some(payer_keypair.pubkey())).await?;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. 初始化PumpFun实例
|
### 2. 初始化SolanaTrade实例
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use sol_trade_sdk::{common::{Cluster, PriorityFee}, PumpFun};
|
use sol_trade_sdk::{common::{Cluster, PriorityFee}, SolanaTrade};
|
||||||
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
|
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
|
||||||
|
|
||||||
// 配置优先费用
|
// 配置优先费用
|
||||||
@@ -105,15 +104,15 @@ let cluster = Cluster {
|
|||||||
lookup_table_key: None, // 可选的查找表
|
lookup_table_key: None, // 可选的查找表
|
||||||
};
|
};
|
||||||
|
|
||||||
// 创建PumpFun实例
|
// 创建SolanaTrade实例
|
||||||
let payer = Keypair::from_base58_string("your_private_key");
|
let payer = Keypair::from_base58_string("your_private_key");
|
||||||
let pumpfun = PumpFun::new(Arc::new(payer), &cluster).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), &cluster).await;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. 创建代币
|
### 3. 创建代币
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use sol_trade_sdk::{PumpFun, ipfs::CreateTokenMetadata, ipfs::create_token_metadata};
|
use sol_trade_sdk::{ipfs::CreateTokenMetadata, ipfs::create_token_metadata};
|
||||||
use solana_sdk::signature::Keypair;
|
use solana_sdk::signature::Keypair;
|
||||||
|
|
||||||
// 创建代币密钥对
|
// 创建代币密钥对
|
||||||
@@ -136,14 +135,14 @@ let api_token = "your_pinata_api_token";
|
|||||||
let ipfs_response = create_token_metadata(metadata, api_token).await?;
|
let ipfs_response = create_token_metadata(metadata, api_token).await?;
|
||||||
|
|
||||||
// 创建代币
|
// 创建代币
|
||||||
pumpfun.create(mint_keypair, ipfs_response).await?;
|
solana_trade_client.create(mint_keypair, ipfs_response).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3.1. 创建并购买代币(带小费)
|
### 3.1. 创建并购买代币(带小费)
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// 创建代币的同时购买,并使用MEV保护
|
// 创建代币的同时购买,并使用MEV保护
|
||||||
pumpfun.create_and_buy_with_tip(
|
solana_trade_client.create_and_buy_with_tip(
|
||||||
payer.clone(), // payer keypair
|
payer.clone(), // payer keypair
|
||||||
mint_keypair, // mint keypair
|
mint_keypair, // mint keypair
|
||||||
ipfs_response, // IPFS响应
|
ipfs_response, // IPFS响应
|
||||||
@@ -157,32 +156,39 @@ pumpfun.create_and_buy_with_tip(
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
use solana_sdk::{pubkey::Pubkey, hash::Hash};
|
use solana_sdk::{pubkey::Pubkey, hash::Hash};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use sol_trade_sdk::accounts::BondingCurveAccount;
|
||||||
|
|
||||||
let mint_pubkey = Pubkey::from_str("代币地址")?;
|
let mint_pubkey = Pubkey::from_str("代币地址")?;
|
||||||
let creator = Pubkey::from_str("创建者地址")?;
|
let creator = Pubkey::from_str("创建者地址")?;
|
||||||
let recent_blockhash = Hash::default(); // 获取最新区块哈希
|
let recent_blockhash = Hash::default(); // 获取最新区块哈希
|
||||||
|
let buy_sol_cost = 50000; // 0.00005 SOL
|
||||||
|
let slippage_basis_points = Some(100); // 1%
|
||||||
|
|
||||||
// 狙击购买(新代币上线时快速购买)
|
// 狙击购买(新代币上线时快速购买)
|
||||||
pumpfun.sniper_buy_with_tip(
|
let dev_buy_token = 100_000; // 测试值
|
||||||
|
let dev_cost_sol = 10_000; // 测试值
|
||||||
|
let bonding_curve = BondingCurveAccount::new(&mint_pubkey, dev_buy_token, dev_cost_sol, creator);
|
||||||
|
|
||||||
|
solana_trade_client.sniper_buy(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
1000000, // dev_buy_token
|
buy_sol_cost,
|
||||||
10000, // dev_sol_cost
|
slippage_basis_points,
|
||||||
50000, // buy_sol_cost (lamports)
|
|
||||||
Some(100), // slippage (1%)
|
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
|
Some(Arc::new(bonding_curve)),
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
// 复制购买(跟随其他交易者)
|
// 使用小费进行MEV保护的购买
|
||||||
pumpfun.copy_buy_with_tip(
|
solana_trade_client.buy_with_tip(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
1000000, // dev_buy_token
|
buy_sol_cost,
|
||||||
10000, // dev_sol_cost
|
slippage_basis_points,
|
||||||
50000, // buy_sol_cost (lamports)
|
|
||||||
Some(100), // slippage (1%)
|
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
|
Some(Arc::new(bonding_curve)),
|
||||||
"pumpfun".to_string(), // 交易平台
|
"pumpfun".to_string(), // 交易平台
|
||||||
|
None, // 自定义小费
|
||||||
).await?;
|
).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -190,22 +196,22 @@ pumpfun.copy_buy_with_tip(
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
// 按数量卖出
|
// 按数量卖出
|
||||||
pumpfun.sell_by_amount_with_tip(
|
solana_trade_client.sell_by_amount_with_tip(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
1000000, // 代币数量
|
1000000, // 代币数量
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
"pumpfun".to_string(),
|
"pumpfun".to_string(), // 交易平台
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
// 按百分比卖出
|
// 按百分比卖出
|
||||||
pumpfun.sell_by_percent_with_tip(
|
solana_trade_client.sell_by_percent_with_tip(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
50, // 百分比 (50%)
|
50, // 百分比 (50%)
|
||||||
2000000, // 总代币数量
|
2000000, // 总代币数量
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
"pumpfun".to_string(),
|
"pumpfun".to_string(), // 交易平台
|
||||||
).await?;
|
).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -213,19 +219,19 @@ pumpfun.sell_by_percent_with_tip(
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
// 获取代币当前价格
|
// 获取代币当前价格
|
||||||
let price = pumpfun.get_current_price(&mint_pubkey).await?;
|
let price = solana_trade_client.get_current_price(&mint_pubkey).await?;
|
||||||
println!("当前价格: {}", price);
|
println!("当前价格: {}", price);
|
||||||
|
|
||||||
// 获取SOL余额
|
// 获取SOL余额
|
||||||
let sol_balance = pumpfun.get_payer_sol_balance().await?;
|
let sol_balance = solana_trade_client.get_payer_sol_balance().await?;
|
||||||
println!("SOL余额: {} lamports", sol_balance);
|
println!("SOL余额: {} lamports", sol_balance);
|
||||||
|
|
||||||
// 获取代币余额
|
// 获取代币余额
|
||||||
let token_balance = pumpfun.get_payer_token_balance(&mint_pubkey).await?;
|
let token_balance = solana_trade_client.get_payer_token_balance(&mint_pubkey).await?;
|
||||||
println!("代币余额: {}", token_balance);
|
println!("代币余额: {}", token_balance);
|
||||||
|
|
||||||
// 获取流动性信息
|
// 获取流动性信息
|
||||||
let sol_reserves = pumpfun.get_real_sol_reserves(&mint_pubkey).await?;
|
let sol_reserves = solana_trade_client.get_real_sol_reserves(&mint_pubkey).await?;
|
||||||
println!("SOL储备: {} lamports", sol_reserves);
|
println!("SOL储备: {} lamports", sol_reserves);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -234,87 +240,109 @@ println!("SOL储备: {} lamports", sol_reserves);
|
|||||||
```rust
|
```rust
|
||||||
use sol_trade_sdk::{common::pumpswap::logs_events::PumpSwapEvent, grpc::YellowstoneGrpc};
|
use sol_trade_sdk::{common::pumpswap::logs_events::PumpSwapEvent, grpc::YellowstoneGrpc};
|
||||||
|
|
||||||
// 创建gRPC客户端(与上面相同)
|
// 创建Yellowstone gRPC客户端
|
||||||
let grpc_url = "http://127.0.0.1:10000";
|
let grpc_url = "https://solana-yellowstone-grpc.publicnode.com:443";
|
||||||
let x_token = None;
|
let x_token = None;
|
||||||
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
let client = YellowstoneGrpc::new(grpc_url.to_string(), x_token)?;
|
||||||
|
|
||||||
// 定义PumpSwap事件的回调函数
|
// 定义PumpSwap事件的回调函数
|
||||||
let callback = |event: PumpSwapEvent| {
|
let callback = |event: PumpSwapEvent| match event {
|
||||||
match event {
|
PumpSwapEvent::Buy(buy_event) => {
|
||||||
PumpSwapEvent::Buy(buy_event) => {
|
println!("buy_event: {:?}", buy_event);
|
||||||
println!("PumpSwap购买事件: {:?}", buy_event);
|
}
|
||||||
},
|
PumpSwapEvent::Sell(sell_event) => {
|
||||||
PumpSwapEvent::Sell(sell_event) => {
|
println!("sell_event: {:?}", sell_event);
|
||||||
println!("PumpSwap卖出事件: {:?}", sell_event);
|
}
|
||||||
},
|
PumpSwapEvent::CreatePool(create_event) => {
|
||||||
PumpSwapEvent::CreatePool(pool_event) => {
|
println!("create_event: {:?}", create_event);
|
||||||
println!("PumpSwap池创建: {:?}", pool_event);
|
}
|
||||||
},
|
PumpSwapEvent::Deposit(deposit_event) => {
|
||||||
PumpSwapEvent::Deposit(deposit_event) => {
|
println!("deposit_event: {:?}", deposit_event);
|
||||||
println!("PumpSwap存款: {:?}", deposit_event);
|
}
|
||||||
},
|
PumpSwapEvent::Withdraw(withdraw_event) => {
|
||||||
PumpSwapEvent::Withdraw(withdraw_event) => {
|
println!("withdraw_event: {:?}", withdraw_event);
|
||||||
println!("PumpSwap提款: {:?}", withdraw_event);
|
}
|
||||||
},
|
PumpSwapEvent::Disable(disable_event) => {
|
||||||
PumpSwapEvent::Disable(disable_event) => {
|
println!("disable_event: {:?}", disable_event);
|
||||||
println!("PumpSwap池禁用: {:?}", disable_event);
|
}
|
||||||
},
|
PumpSwapEvent::UpdateAdmin(update_admin_event) => {
|
||||||
PumpSwapEvent::UpdateAdmin(admin_event) => {
|
println!("update_admin_event: {:?}", update_admin_event);
|
||||||
println!("PumpSwap管理员更新: {:?}", admin_event);
|
}
|
||||||
},
|
PumpSwapEvent::UpdateFeeConfig(update_fee_event) => {
|
||||||
PumpSwapEvent::UpdateFeeConfig(fee_event) => {
|
println!("update_fee_event: {:?}", update_fee_event);
|
||||||
println!("PumpSwap费用配置更新: {:?}", fee_event);
|
}
|
||||||
},
|
PumpSwapEvent::Error(err) => {
|
||||||
PumpSwapEvent::Error(err) => {
|
println!("error: {}", err);
|
||||||
println!("PumpSwap错误: {}", err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 订阅PumpSwap事件
|
// 订阅PumpSwap事件
|
||||||
|
println!("开始监听PumpSwap事件,按Ctrl+C停止...");
|
||||||
client.subscribe_pumpswap(callback).await?;
|
client.subscribe_pumpswap(callback).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 8. PumpSwap交易操作
|
### 8. PumpSwap交易操作
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use solana_sdk::{pubkey::Pubkey, hash::Hash};
|
use std::sync::Arc;
|
||||||
|
use solana_sdk::{pubkey::Pubkey, hash::Hash, signature::Keypair};
|
||||||
|
use solana_client::rpc_client::RpcClient;
|
||||||
|
use sol_trade_sdk::{common::{Cluster, PriorityFee}, SolanaTrade};
|
||||||
|
|
||||||
let mint_pubkey = Pubkey::from_str("代币地址")?;
|
let payer = Keypair::new();
|
||||||
let creator = Pubkey::from_str("创建者地址")?;
|
// 配置集群
|
||||||
let recent_blockhash = Hash::default();
|
let cluster = Cluster {
|
||||||
|
rpc_url: "https://mainnet.helius-rpc.com/?api-key=您的API密钥".to_string(),
|
||||||
|
commitment: CommitmentConfig::confirmed(),
|
||||||
|
priority_fee: PriorityFee::default(),
|
||||||
|
use_jito: false,
|
||||||
|
use_zeroslot: false,
|
||||||
|
use_nozomi: false,
|
||||||
|
use_nextblock: false,
|
||||||
|
block_engine_url: "".to_string(),
|
||||||
|
zeroslot_url: "".to_string(),
|
||||||
|
zeroslot_auth_token: "".to_string(),
|
||||||
|
nozomi_url: "".to_string(),
|
||||||
|
nozomi_auth_token: "".to_string(),
|
||||||
|
nextblock_url: "".to_string(),
|
||||||
|
nextblock_auth_token: "".to_string(),
|
||||||
|
lookup_table_key: None,
|
||||||
|
use_rpc: true,
|
||||||
|
};
|
||||||
|
|
||||||
// 在PumpSwap上购买代币
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), &cluster).await;
|
||||||
pumpfun.copy_buy_with_tip(
|
let creator = Pubkey::from_str("11111111111111111111111111111111")?; // 开发者账户
|
||||||
mint_pubkey,
|
let buy_sol_cost = 500_000; // 0.0005 SOL
|
||||||
creator,
|
let slippage_basis_points = Some(100);
|
||||||
1000000, // dev_buy_token
|
let rpc = RpcClient::new(cluster.rpc_url);
|
||||||
10000, // dev_sol_cost
|
let recent_blockhash = rpc.get_latest_blockhash().unwrap();
|
||||||
50000, // buy_sol_cost (lamports)
|
let trade_platform = "pumpswap".to_string();
|
||||||
Some(100), // slippage (1%)
|
let mint_pubkey = Pubkey::from_str("您的代币铸造地址")?; // 代币铸造地址
|
||||||
recent_blockhash,
|
|
||||||
"pumpswap".to_string(), // 使用PumpSwap平台
|
|
||||||
).await?;
|
|
||||||
|
|
||||||
// 在PumpSwap上按数量卖出代币
|
println!("从PumpSwap购买代币...");
|
||||||
pumpfun.sell_by_amount_with_tip(
|
solana_trade_client
|
||||||
mint_pubkey,
|
.buy(
|
||||||
creator,
|
mint_pubkey,
|
||||||
1000000, // 代币数量
|
creator,
|
||||||
recent_blockhash,
|
buy_sol_cost,
|
||||||
"pumpswap".to_string(), // 使用PumpSwap平台
|
slippage_basis_points,
|
||||||
).await?;
|
recent_blockhash,
|
||||||
|
None,
|
||||||
|
trade_platform.clone(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// 在PumpSwap上按百分比卖出代币
|
// 卖出30%的代币数量
|
||||||
pumpfun.sell_by_percent_with_tip(
|
solana_trade_client
|
||||||
mint_pubkey,
|
.sell_by_percent(
|
||||||
creator,
|
mint_pubkey,
|
||||||
50, // 百分比 (50%)
|
creator,
|
||||||
2000000, // 总代币数量
|
30, // 百分比 (30%)
|
||||||
recent_blockhash,
|
100, // 总代币数量
|
||||||
"pumpswap".to_string(), // 使用PumpSwap平台
|
recent_blockhash,
|
||||||
).await?;
|
trade_platform.clone(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
### 9. PumpSwap池信息
|
### 9. PumpSwap池信息
|
||||||
@@ -325,15 +353,15 @@ use solana_sdk::pubkey::Pubkey;
|
|||||||
let pool_address = Pubkey::from_str("池地址")?;
|
let pool_address = Pubkey::from_str("池地址")?;
|
||||||
|
|
||||||
// 从PumpSwap池获取当前价格
|
// 从PumpSwap池获取当前价格
|
||||||
let price = pumpfun.get_current_price_with_pumpswap(&pool_address).await?;
|
let price = solana_trade_client.get_current_price_with_pumpswap(&pool_address).await?;
|
||||||
println!("PumpSwap池价格: {}", price);
|
println!("PumpSwap池价格: {}", price);
|
||||||
|
|
||||||
// 获取PumpSwap池中的SOL储备
|
// 获取PumpSwap池中的SOL储备
|
||||||
let sol_reserves = pumpfun.get_real_sol_reserves_with_pumpswap(&pool_address).await?;
|
let sol_reserves = solana_trade_client.get_real_sol_reserves_with_pumpswap(&pool_address).await?;
|
||||||
println!("PumpSwap SOL储备: {} lamports", sol_reserves);
|
println!("PumpSwap SOL储备: {} lamports", sol_reserves);
|
||||||
|
|
||||||
// 获取PumpSwap池中的代币余额
|
// 获取PumpSwap池中的代币余额
|
||||||
let token_balance = pumpfun.get_payer_token_balance_with_pumpswap(&pool_address).await?;
|
let token_balance = solana_trade_client.get_payer_token_balance_with_pumpswap(&pool_address).await?;
|
||||||
println!("PumpSwap代币余额: {}", token_balance);
|
println!("PumpSwap代币余额: {}", token_balance);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+67
-8
@@ -37,7 +37,7 @@ use crate::trading::BuyWithTipParams;
|
|||||||
use crate::trading::SellParams;
|
use crate::trading::SellParams;
|
||||||
use crate::trading::SellWithTipParams;
|
use crate::trading::SellWithTipParams;
|
||||||
|
|
||||||
pub struct PumpFun {
|
pub struct SolanaTrade {
|
||||||
pub payer: Arc<Keypair>,
|
pub payer: Arc<Keypair>,
|
||||||
pub rpc: Arc<SolanaRpcClient>,
|
pub rpc: Arc<SolanaRpcClient>,
|
||||||
pub fee_clients: Vec<Arc<FeeClient>>,
|
pub fee_clients: Vec<Arc<FeeClient>>,
|
||||||
@@ -45,9 +45,9 @@ pub struct PumpFun {
|
|||||||
pub cluster: Cluster,
|
pub cluster: Cluster,
|
||||||
}
|
}
|
||||||
|
|
||||||
static INSTANCE: Mutex<Option<Arc<PumpFun>>> = Mutex::new(None);
|
static INSTANCE: Mutex<Option<Arc<SolanaTrade>>> = Mutex::new(None);
|
||||||
|
|
||||||
impl Clone for PumpFun {
|
impl Clone for SolanaTrade {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
payer: self.payer.clone(),
|
payer: self.payer.clone(),
|
||||||
@@ -59,7 +59,7 @@ impl Clone for PumpFun {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PumpFun {
|
impl SolanaTrade {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub async fn new(
|
pub async fn new(
|
||||||
payer: Arc<Keypair>,
|
payer: Arc<Keypair>,
|
||||||
@@ -211,6 +211,7 @@ impl PumpFun {
|
|||||||
buy_sol_cost: u64,
|
buy_sol_cost: u64,
|
||||||
slippage_basis_points: Option<u64>,
|
slippage_basis_points: Option<u64>,
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
|
bonding_curve: Option<Arc<BondingCurveAccount>>,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<(), anyhow::Error> {
|
||||||
pumpfun::buy::buy(
|
pumpfun::buy::buy(
|
||||||
self.rpc.clone(),
|
self.rpc.clone(),
|
||||||
@@ -222,12 +223,12 @@ impl PumpFun {
|
|||||||
self.priority_fee.clone(),
|
self.priority_fee.clone(),
|
||||||
self.cluster.clone().lookup_table_key,
|
self.cluster.clone().lookup_table_key,
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
None,
|
bonding_curve,
|
||||||
SNIPER_BUY.to_string(),
|
SNIPER_BUY.to_string(),
|
||||||
).await
|
).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn copy_buy(
|
pub async fn buy(
|
||||||
&self,
|
&self,
|
||||||
mint: Pubkey,
|
mint: Pubkey,
|
||||||
creator: Pubkey,
|
creator: Pubkey,
|
||||||
@@ -274,6 +275,64 @@ impl PumpFun {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn buy_use_buy_params(
|
||||||
|
&self,
|
||||||
|
buy_params: BuyWithTipParams,
|
||||||
|
custom_buy_tip_fee: Option<f64>,
|
||||||
|
) -> Result<(), anyhow::Error> {
|
||||||
|
let mut priority_fee = buy_params.priority_fee.clone();
|
||||||
|
if custom_buy_tip_fee.is_some() {
|
||||||
|
priority_fee.buy_tip_fee = custom_buy_tip_fee.unwrap();
|
||||||
|
priority_fee.buy_tip_fees = vec![custom_buy_tip_fee.unwrap()];
|
||||||
|
}
|
||||||
|
let mint = buy_params.mint;
|
||||||
|
let creator = buy_params.creator;
|
||||||
|
let buy_sol_cost = buy_params.amount_sol;
|
||||||
|
let slippage_basis_points = buy_params.slippage_basis_points;
|
||||||
|
let recent_blockhash = buy_params.recent_blockhash;
|
||||||
|
if let Some(protocol_params) = buy_params
|
||||||
|
.protocol_params
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<PumpFunParams>() {
|
||||||
|
pumpfun::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,
|
||||||
|
recent_blockhash,
|
||||||
|
protocol_params.bonding_curve.clone(),
|
||||||
|
COPY_BUY.to_string(),
|
||||||
|
).await
|
||||||
|
} else if let Some(protocol_params) = buy_params
|
||||||
|
.protocol_params
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<PumpSwapParams>() {
|
||||||
|
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,
|
||||||
|
recent_blockhash,
|
||||||
|
protocol_params.pool.clone(),
|
||||||
|
protocol_params.pool_base_token_account.clone(),
|
||||||
|
protocol_params.pool_quote_token_account.clone(),
|
||||||
|
protocol_params.user_base_token_account.clone(),
|
||||||
|
protocol_params.user_quote_token_account.clone(),
|
||||||
|
protocol_params.auto_handle_wsol,
|
||||||
|
).await
|
||||||
|
} else {
|
||||||
|
return Err(anyhow::anyhow!("Invalid protocol params for PumpFun"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Buy tokens using Jito
|
/// Buy tokens using Jito
|
||||||
pub async fn sniper_buy_with_tip(
|
pub async fn sniper_buy_with_tip(
|
||||||
&self,
|
&self,
|
||||||
@@ -299,7 +358,7 @@ impl PumpFun {
|
|||||||
).await
|
).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn copy_buy_with_tip_use_buy_params(
|
pub async fn buy_with_tip_use_buy_params(
|
||||||
&self,
|
&self,
|
||||||
buy_params: BuyWithTipParams,
|
buy_params: BuyWithTipParams,
|
||||||
custom_buy_tip_fee: Option<f64>,
|
custom_buy_tip_fee: Option<f64>,
|
||||||
@@ -358,7 +417,7 @@ impl PumpFun {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn copy_buy_with_tip(
|
pub async fn buy_with_tip(
|
||||||
&self,
|
&self,
|
||||||
mint: Pubkey,
|
mint: Pubkey,
|
||||||
creator: Pubkey,
|
creator: Pubkey,
|
||||||
|
|||||||
+135
-55
@@ -1,18 +1,16 @@
|
|||||||
use std::{str::FromStr, sync::Arc};
|
use std::{str::FromStr, sync::Arc};
|
||||||
|
|
||||||
use sol_trade_sdk::{
|
use sol_trade_sdk::{
|
||||||
common::{
|
accounts::BondingCurveAccount, common::{
|
||||||
pumpfun::{
|
pumpfun::{
|
||||||
self,
|
self,
|
||||||
logs_events::PumpfunEvent,
|
logs_events::PumpfunEvent,
|
||||||
logs_subscribe::{stop_subscription, tokens_subscription},
|
logs_subscribe::{stop_subscription, tokens_subscription}, TradeInfo,
|
||||||
},
|
},
|
||||||
pumpswap::{self, PumpSwapEvent},
|
pumpswap::{self, PumpSwapEvent},
|
||||||
raydium::{self, RaydiumEvent},
|
raydium::{self, RaydiumEvent},
|
||||||
AnyResult, Cluster, PriorityFee,
|
AnyResult, Cluster, PriorityFee,
|
||||||
},
|
}, constants::pumpfun::global_constants::TOKEN_TOTAL_SUPPLY, grpc::{ShredStreamGrpc, YellowstoneGrpc}, pumpfun::common::get_bonding_curve_pda, SolanaTrade
|
||||||
grpc::{ShredStreamGrpc, YellowstoneGrpc},
|
|
||||||
PumpFun,
|
|
||||||
};
|
};
|
||||||
use solana_client::rpc_client::RpcClient;
|
use solana_client::rpc_client::RpcClient;
|
||||||
use solana_hash::Hash;
|
use solana_hash::Hash;
|
||||||
@@ -29,7 +27,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// test_pumpswap_with_grpc().await?;
|
// test_pumpswap_with_grpc().await?;
|
||||||
// test_raydium_with_shreds().await?;
|
// test_raydium_with_shreds().await?;
|
||||||
// test_raydium_with_grpc().await?;
|
// test_raydium_with_grpc().await?;
|
||||||
test_sell().await?;
|
// test_pumpfun_sniper().await?;
|
||||||
|
// test_pumpfun().await?;
|
||||||
|
test_pumpswap().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,7 +257,7 @@ async fn test_raydium_with_grpc() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_sell() -> AnyResult<()> {
|
async fn test_pumpfun_sniper() -> AnyResult<()> {
|
||||||
let payer = Keypair::new();
|
let payer = Keypair::new();
|
||||||
// Define cluster configuration
|
// Define cluster configuration
|
||||||
let cluster = Cluster {
|
let cluster = Cluster {
|
||||||
@@ -279,17 +279,137 @@ async fn test_sell() -> AnyResult<()> {
|
|||||||
lookup_table_key: None,
|
lookup_table_key: None,
|
||||||
use_rpc: true,
|
use_rpc: true,
|
||||||
};
|
};
|
||||||
let pumpfun_client = PumpFun::new(Arc::new(payer), &cluster).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), &cluster).await;
|
||||||
let creator = Pubkey::from_str("43tFsRkZyhE1JXGivxWthApHPqWCnDqs7E1ZNdy7gkNz")?;
|
let creator = Pubkey::from_str("xxx")?; // dev account
|
||||||
|
let buy_sol_cost = 500_000; // 0.0005 SOL
|
||||||
|
let slippage_basis_points = Some(100);
|
||||||
|
let rpc = RpcClient::new(cluster.rpc_url);
|
||||||
|
let recent_blockhash = rpc.get_latest_blockhash().unwrap();
|
||||||
|
let mint_pubkey = Pubkey::from_str("xxx")?; // token mint
|
||||||
|
println!("Sniping buy tokens from PumpFun...");
|
||||||
|
// get bonding curve
|
||||||
|
let dev_buy_token = 100_000; // test value
|
||||||
|
let dev_cost_sol = 100_000; // test value
|
||||||
|
let bonding_curve = BondingCurveAccount::new(&mint_pubkey, dev_buy_token, dev_cost_sol, creator);
|
||||||
|
solana_trade_client
|
||||||
|
.sniper_buy(
|
||||||
|
mint_pubkey,
|
||||||
|
creator,
|
||||||
|
buy_sol_cost,
|
||||||
|
slippage_basis_points,
|
||||||
|
recent_blockhash,
|
||||||
|
Some(Arc::new(bonding_curve)),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn test_pumpfun() -> AnyResult<()> {
|
||||||
|
let payer = Keypair::new();
|
||||||
|
// Define cluster configuration
|
||||||
|
let cluster = Cluster {
|
||||||
|
rpc_url: "https://mainnet.helius-rpc.com/?api-key=f2f194bb-6bd6-4f20-9a94-7fe0799ade0b"
|
||||||
|
.to_string(),
|
||||||
|
commitment: CommitmentConfig::confirmed(),
|
||||||
|
priority_fee: PriorityFee::default(),
|
||||||
|
use_jito: false,
|
||||||
|
use_zeroslot: false,
|
||||||
|
use_nozomi: false,
|
||||||
|
use_nextblock: false,
|
||||||
|
block_engine_url: "".to_string(),
|
||||||
|
zeroslot_url: "".to_string(),
|
||||||
|
zeroslot_auth_token: "".to_string(),
|
||||||
|
nozomi_url: "".to_string(),
|
||||||
|
nozomi_auth_token: "".to_string(),
|
||||||
|
nextblock_url: "".to_string(),
|
||||||
|
nextblock_auth_token: "".to_string(),
|
||||||
|
lookup_table_key: None,
|
||||||
|
use_rpc: true,
|
||||||
|
};
|
||||||
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), &cluster).await;
|
||||||
|
let creator = Pubkey::from_str("xxx")?; // dev account
|
||||||
|
let buy_sol_cost = 500_000; // 0.0005 SOL
|
||||||
|
let slippage_basis_points = Some(100);
|
||||||
|
let rpc = RpcClient::new(cluster.rpc_url);
|
||||||
|
let recent_blockhash = rpc.get_latest_blockhash().unwrap();
|
||||||
|
let trade_platform = "pumpfun".to_string();
|
||||||
|
let mint_pubkey = Pubkey::from_str("xxx")?; // token mint
|
||||||
|
println!("Buying tokens from PumpFun...");
|
||||||
|
// get bonding curve
|
||||||
|
// Relevant on-chain information can be obtained from rpc/grpc
|
||||||
|
let virtual_token_reserves = 0;
|
||||||
|
let virtual_sol_reserves = 0;
|
||||||
|
let real_token_reserves = 0;
|
||||||
|
let real_sol_reserves = 0;
|
||||||
|
let bonding_curve = BondingCurveAccount {
|
||||||
|
discriminator: 0,
|
||||||
|
account: get_bonding_curve_pda(&mint_pubkey).unwrap(),
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
solana_trade_client
|
||||||
|
.buy(
|
||||||
|
mint_pubkey,
|
||||||
|
creator,
|
||||||
|
buy_sol_cost,
|
||||||
|
slippage_basis_points,
|
||||||
|
recent_blockhash,
|
||||||
|
Some(Arc::new(bonding_curve)),
|
||||||
|
trade_platform.clone(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
// Sell 30% * amount_token quantity
|
||||||
|
// solana_trade_client
|
||||||
|
// .sell_by_percent(
|
||||||
|
// mint_pubkey,
|
||||||
|
// creator,
|
||||||
|
// 30,
|
||||||
|
// 100,
|
||||||
|
// recent_blockhash,
|
||||||
|
// trade_platform.clone(),
|
||||||
|
// )
|
||||||
|
// .await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn test_pumpswap() -> AnyResult<()> {
|
||||||
|
let payer = Keypair::new();
|
||||||
|
// Define cluster configuration
|
||||||
|
let cluster = Cluster {
|
||||||
|
rpc_url: "https://mainnet.helius-rpc.com/?api-key=f2f194bb-6bd6-4f20-9a94-7fe0799ade0b"
|
||||||
|
.to_string(),
|
||||||
|
commitment: CommitmentConfig::confirmed(),
|
||||||
|
priority_fee: PriorityFee::default(),
|
||||||
|
use_jito: false,
|
||||||
|
use_zeroslot: false,
|
||||||
|
use_nozomi: false,
|
||||||
|
use_nextblock: false,
|
||||||
|
block_engine_url: "".to_string(),
|
||||||
|
zeroslot_url: "".to_string(),
|
||||||
|
zeroslot_auth_token: "".to_string(),
|
||||||
|
nozomi_url: "".to_string(),
|
||||||
|
nozomi_auth_token: "".to_string(),
|
||||||
|
nextblock_url: "".to_string(),
|
||||||
|
nextblock_auth_token: "".to_string(),
|
||||||
|
lookup_table_key: None,
|
||||||
|
use_rpc: true,
|
||||||
|
};
|
||||||
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), &cluster).await;
|
||||||
|
let creator = Pubkey::from_str("11111111111111111111111111111111")?; // dev account
|
||||||
let buy_sol_cost = 500_000; // 0.0005 SOL
|
let buy_sol_cost = 500_000; // 0.0005 SOL
|
||||||
let slippage_basis_points = Some(100);
|
let slippage_basis_points = Some(100);
|
||||||
let rpc = RpcClient::new(cluster.rpc_url);
|
let rpc = RpcClient::new(cluster.rpc_url);
|
||||||
let recent_blockhash = rpc.get_latest_blockhash().unwrap();
|
let recent_blockhash = rpc.get_latest_blockhash().unwrap();
|
||||||
let trade_platform = "pumpswap".to_string();
|
let trade_platform = "pumpswap".to_string();
|
||||||
let mint_pubkey = Pubkey::from_str("FMnWxuES8X7n33SJryf9MKbZNH57tPREtjWqCTMupump")?;
|
let mint_pubkey = Pubkey::from_str("2zMMhcVQEXDtdE6vsFS7S7D5oUodfJHE8vd1gnBouauv")?; // token mint
|
||||||
println!("Buying tokens from PumpSwap...");
|
println!("Buying tokens from PumpSwap...");
|
||||||
pumpfun_client
|
solana_trade_client
|
||||||
.copy_buy(
|
.buy(
|
||||||
mint_pubkey,
|
mint_pubkey,
|
||||||
creator,
|
creator,
|
||||||
buy_sol_cost,
|
buy_sol_cost,
|
||||||
@@ -299,56 +419,16 @@ async fn test_sell() -> AnyResult<()> {
|
|||||||
trade_platform.clone(),
|
trade_platform.clone(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
// pumpfun_client
|
// Sell 30% * amount_token quantity
|
||||||
|
// solana_trade_client
|
||||||
// .sell_by_percent(
|
// .sell_by_percent(
|
||||||
// mint_pubkey,
|
// mint_pubkey,
|
||||||
// creator,
|
// creator,
|
||||||
|
// 30,
|
||||||
// 100,
|
// 100,
|
||||||
// 0,
|
|
||||||
// recent_blockhash,
|
// recent_blockhash,
|
||||||
// trade_platform.clone(),
|
// trade_platform.clone(),
|
||||||
// )
|
// )
|
||||||
// .await?;
|
// .await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn test_wss() -> AnyResult<()> {
|
|
||||||
println!("Starting token subscription\n");
|
|
||||||
|
|
||||||
let ws_url = "wss://api.mainnet-beta.solana.com";
|
|
||||||
|
|
||||||
// Set commitment
|
|
||||||
let commitment = CommitmentConfig::confirmed();
|
|
||||||
|
|
||||||
// Define callback function
|
|
||||||
let callback = |event: PumpfunEvent| match event {
|
|
||||||
PumpfunEvent::NewDevTrade(trade_info) => {
|
|
||||||
println!("Received new dev trade event: {:?}", trade_info);
|
|
||||||
}
|
|
||||||
PumpfunEvent::NewToken(token_info) => {
|
|
||||||
println!("Received new token event: {:?}", token_info);
|
|
||||||
}
|
|
||||||
PumpfunEvent::NewUserTrade(trade_info) => {
|
|
||||||
println!("Received new trade event: {:?}", trade_info);
|
|
||||||
}
|
|
||||||
PumpfunEvent::NewBotTrade(trade_info) => {
|
|
||||||
println!("Received new bot trade event: {:?}", trade_info);
|
|
||||||
}
|
|
||||||
PumpfunEvent::Error(err) => {
|
|
||||||
println!("Received error: {}", err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Start subscription
|
|
||||||
let subscription = tokens_subscription(ws_url, commitment, callback, None)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Wait for a while to receive events
|
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
|
|
||||||
|
|
||||||
// Stop subscription
|
|
||||||
stop_subscription(subscription).await;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ use crate::{
|
|||||||
params::{BuyParams, PumpFunParams, SellParams},
|
params::{BuyParams, PumpFunParams, SellParams},
|
||||||
traits::InstructionBuilder,
|
traits::InstructionBuilder,
|
||||||
},
|
},
|
||||||
PumpFun,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// PumpFun协议的指令构建器
|
/// PumpFun协议的指令构建器
|
||||||
|
|||||||
Reference in New Issue
Block a user