add bot trade event

This commit is contained in:
William
2025-01-03 17:01:07 +08:00
parent 862651034e
commit bec7305f0e
6 changed files with 28 additions and 7 deletions
@@ -4,6 +4,7 @@ use serde::{Serialize, Deserialize};
pub enum DexInstruction {
CreateToken(CreateTokenInfo),
Trade(TradeInfo),
BotTrade(TradeInfo),
Other,
}
@@ -5,5 +5,6 @@ use crate::instruction::logs_data::{CreateTokenInfo, TradeInfo};
pub enum DexEvent {
NewToken(CreateTokenInfo),
NewTrade(TradeInfo),
NewBotTrade(TradeInfo),
Error(String),
}
+11 -2
View File
@@ -2,13 +2,14 @@ use crate::instruction::logs_data::{CreateTokenInfo, TradeInfo};
use crate::instruction::logs_parser::{parse_create_token_data, parse_trade_data};
use crate::error::ClientResult;
use crate::instruction::logs_data::DexInstruction;
use anchor_client::solana_sdk::pubkey::Pubkey;
pub struct LogFilter;
impl LogFilter {
const PROGRAM_ID: &'static str = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P";
/// Parse transaction logs and return instruction type and data
pub fn parse_instruction(logs: &[String]) -> ClientResult<Vec<DexInstruction>> {
pub fn parse_instruction(logs: &[String], payer: Option<Pubkey>) -> ClientResult<Vec<DexInstruction>> {
let mut current_instruction = None;
let mut program_data = String::new();
let mut invoke_depth = 0;
@@ -64,7 +65,15 @@ impl LogFilter {
},
"trade" => {
if let Ok(trade_info) = parse_trade_data(&program_data) {
instructions.push(DexInstruction::Trade(trade_info));
if let Some(payer_pubkey) = payer {
if trade_info.user == payer_pubkey.to_string() {
instructions.push(DexInstruction::BotTrade(trade_info));
} else {
instructions.push(DexInstruction::Trade(trade_info));
}
} else {
instructions.push(DexInstruction::Trade(trade_info));
}
}
},
_ => {}
@@ -8,15 +8,18 @@ use crate::instruction::{
logs_filters::LogFilter
};
use anchor_client::solana_sdk::pubkey::Pubkey;
pub async fn process_logs<F>(
signature: &str,
logs: Vec<String>,
callback: F,
payer: Option<Pubkey>,
) -> ClientResult<()>
where
F: Fn(&str, DexInstruction) + Send + Sync,
{
let instructions = LogFilter::parse_instruction(&logs)?;
let instructions = LogFilter::parse_instruction(&logs, payer)?;
for instruction in instructions {
callback(signature, instruction);
}
@@ -3,8 +3,7 @@ use anchor_client::solana_client::{
rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter}
};
use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
use anchor_client::solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey};
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
@@ -35,6 +34,7 @@ pub async fn tokens_subscription<F>(
ws_url: &str,
commitment: CommitmentConfig,
callback: F,
payer: Option<Pubkey>,
) -> Result<SubscriptionHandle, Box<dyn std::error::Error>>
where
F: Fn(DexEvent) + Send + Sync + 'static,
@@ -66,7 +66,7 @@ where
continue;
}
let instructions = LogFilter::parse_instruction(&msg.value.logs).unwrap();
let instructions = LogFilter::parse_instruction(&msg.value.logs, payer).unwrap();
for instruction in instructions {
match instruction {
DexInstruction::CreateToken(token_info) => {
@@ -75,6 +75,9 @@ where
DexInstruction::Trade(trade_info) => {
callback(DexEvent::NewTrade(trade_info));
}
DexInstruction::BotTrade(trade_info) => {
callback(DexEvent::NewBotTrade(trade_info));
}
_ => {}
}
}
+5 -1
View File
@@ -25,6 +25,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
DexEvent::NewTrade(trade_info) => {
println!("Received new trade event: {:?}", trade_info);
},
DexEvent::NewBotTrade(trade_info) => {
println!("Received new bot trade event: {:?}", trade_info);
},
DexEvent::Error(err) => {
println!("Received error: {}", err);
}
@@ -35,7 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let subscription = tokens_subscription(
ws_url,
commitment,
callback
callback,
None
).await.unwrap();
// Wait for a while to receive events