add bot trade

This commit is contained in:
William
2025-01-03 17:06:19 +08:00
parent bec7305f0e
commit 5c0b8c87e2
7 changed files with 19 additions and 15 deletions
+6 -2
View File
@@ -20,7 +20,7 @@ Add the following to your `Cargo.toml`:
```toml ```toml
[dependencies] [dependencies]
mai3-pumpfun-sdk = "2.4.0" mai3-pumpfun-sdk = "2.4.2"
``` ```
## Usage ## Usage
@@ -50,6 +50,9 @@ let callback = |event: DexEvent| {
DexEvent::NewTrade(trade_info) => { DexEvent::NewTrade(trade_info) => {
println!("Received new trade event: {:?}", trade_info); println!("Received new trade event: {:?}", trade_info);
}, },
DexEvent::NewBotTrade(trade_info) => {
println!("Received new bot trade event: {:?}", trade_info);
}
DexEvent::Error(err) => { DexEvent::Error(err) => {
println!("Received error: {}", err); println!("Received error: {}", err);
} }
@@ -60,7 +63,8 @@ let callback = |event: DexEvent| {
let subscription = tokens_subscription( let subscription = tokens_subscription(
ws_url, ws_url,
commitment, commitment,
callback callback,
None
).await.unwrap(); ).await.unwrap();
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "mai3-pumpfun-sdk" name = "mai3-pumpfun-sdk"
version = "2.4.1" version = "2.4.2"
edition = "2021" edition = "2021"
authors = ["William <william@mai3.io>"] authors = ["William <william@mai3.io>"]
repository = "https://github.com/MiracleAI-Labs/pumpfun-sdk" repository = "https://github.com/MiracleAI-Labs/pumpfun-sdk"
+1 -1
View File
@@ -3,7 +3,7 @@ use serde::{Serialize, Deserialize};
#[derive(Debug)] #[derive(Debug)]
pub enum DexInstruction { pub enum DexInstruction {
CreateToken(CreateTokenInfo), CreateToken(CreateTokenInfo),
Trade(TradeInfo), UserTrade(TradeInfo),
BotTrade(TradeInfo), BotTrade(TradeInfo),
Other, Other,
} }
@@ -4,7 +4,7 @@ use crate::instruction::logs_data::{CreateTokenInfo, TradeInfo};
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DexEvent { pub enum DexEvent {
NewToken(CreateTokenInfo), NewToken(CreateTokenInfo),
NewTrade(TradeInfo), NewUserTrade(TradeInfo),
NewBotTrade(TradeInfo), NewBotTrade(TradeInfo),
Error(String), Error(String),
} }
@@ -9,7 +9,7 @@ impl LogFilter {
const PROGRAM_ID: &'static str = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P"; const PROGRAM_ID: &'static str = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P";
/// Parse transaction logs and return instruction type and data /// Parse transaction logs and return instruction type and data
pub fn parse_instruction(logs: &[String], payer: Option<Pubkey>) -> ClientResult<Vec<DexInstruction>> { pub fn parse_instruction(logs: &[String], bot_wallet: Option<Pubkey>) -> ClientResult<Vec<DexInstruction>> {
let mut current_instruction = None; let mut current_instruction = None;
let mut program_data = String::new(); let mut program_data = String::new();
let mut invoke_depth = 0; let mut invoke_depth = 0;
@@ -65,14 +65,14 @@ impl LogFilter {
}, },
"trade" => { "trade" => {
if let Ok(trade_info) = parse_trade_data(&program_data) { if let Ok(trade_info) = parse_trade_data(&program_data) {
if let Some(payer_pubkey) = payer { if let Some(bot_wallet_pubkey) = bot_wallet {
if trade_info.user == payer_pubkey.to_string() { if trade_info.user == bot_wallet_pubkey.to_string() {
instructions.push(DexInstruction::BotTrade(trade_info)); instructions.push(DexInstruction::BotTrade(trade_info));
} else { } else {
instructions.push(DexInstruction::Trade(trade_info)); instructions.push(DexInstruction::UserTrade(trade_info));
} }
} else { } else {
instructions.push(DexInstruction::Trade(trade_info)); instructions.push(DexInstruction::UserTrade(trade_info));
} }
} }
}, },
@@ -34,7 +34,7 @@ pub async fn tokens_subscription<F>(
ws_url: &str, ws_url: &str,
commitment: CommitmentConfig, commitment: CommitmentConfig,
callback: F, callback: F,
payer: Option<Pubkey>, bot_wallet: Option<Pubkey>,
) -> Result<SubscriptionHandle, Box<dyn std::error::Error>> ) -> Result<SubscriptionHandle, Box<dyn std::error::Error>>
where where
F: Fn(DexEvent) + Send + Sync + 'static, F: Fn(DexEvent) + Send + Sync + 'static,
@@ -66,14 +66,14 @@ where
continue; continue;
} }
let instructions = LogFilter::parse_instruction(&msg.value.logs, payer).unwrap(); let instructions = LogFilter::parse_instruction(&msg.value.logs, bot_wallet).unwrap();
for instruction in instructions { for instruction in instructions {
match instruction { match instruction {
DexInstruction::CreateToken(token_info) => { DexInstruction::CreateToken(token_info) => {
callback(DexEvent::NewToken(token_info)); callback(DexEvent::NewToken(token_info));
} }
DexInstruction::Trade(trade_info) => { DexInstruction::UserTrade(trade_info) => {
callback(DexEvent::NewTrade(trade_info)); callback(DexEvent::NewUserTrade(trade_info));
} }
DexInstruction::BotTrade(trade_info) => { DexInstruction::BotTrade(trade_info) => {
callback(DexEvent::NewBotTrade(trade_info)); callback(DexEvent::NewBotTrade(trade_info));
+1 -1
View File
@@ -22,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
DexEvent::NewToken(token_info) => { DexEvent::NewToken(token_info) => {
println!("Received new token event: {:?}", token_info); println!("Received new token event: {:?}", token_info);
}, },
DexEvent::NewTrade(trade_info) => { DexEvent::NewUserTrade(trade_info) => {
println!("Received new trade event: {:?}", trade_info); println!("Received new trade event: {:?}", trade_info);
}, },
DexEvent::NewBotTrade(trade_info) => { DexEvent::NewBotTrade(trade_info) => {