mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-20 04:18:06 +00:00
add bot trade event
This commit is contained in:
@@ -4,6 +4,7 @@ use serde::{Serialize, Deserialize};
|
|||||||
pub enum DexInstruction {
|
pub enum DexInstruction {
|
||||||
CreateToken(CreateTokenInfo),
|
CreateToken(CreateTokenInfo),
|
||||||
Trade(TradeInfo),
|
Trade(TradeInfo),
|
||||||
|
BotTrade(TradeInfo),
|
||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ use crate::instruction::logs_data::{CreateTokenInfo, TradeInfo};
|
|||||||
pub enum DexEvent {
|
pub enum DexEvent {
|
||||||
NewToken(CreateTokenInfo),
|
NewToken(CreateTokenInfo),
|
||||||
NewTrade(TradeInfo),
|
NewTrade(TradeInfo),
|
||||||
|
NewBotTrade(TradeInfo),
|
||||||
Error(String),
|
Error(String),
|
||||||
}
|
}
|
||||||
@@ -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::instruction::logs_parser::{parse_create_token_data, parse_trade_data};
|
||||||
use crate::error::ClientResult;
|
use crate::error::ClientResult;
|
||||||
use crate::instruction::logs_data::DexInstruction;
|
use crate::instruction::logs_data::DexInstruction;
|
||||||
|
use anchor_client::solana_sdk::pubkey::Pubkey;
|
||||||
pub struct LogFilter;
|
pub struct LogFilter;
|
||||||
|
|
||||||
impl LogFilter {
|
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]) -> ClientResult<Vec<DexInstruction>> {
|
pub fn parse_instruction(logs: &[String], payer: 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;
|
||||||
@@ -64,7 +65,15 @@ impl LogFilter {
|
|||||||
},
|
},
|
||||||
"trade" => {
|
"trade" => {
|
||||||
if let Ok(trade_info) = parse_trade_data(&program_data) {
|
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
|
logs_filters::LogFilter
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use anchor_client::solana_sdk::pubkey::Pubkey;
|
||||||
|
|
||||||
pub async fn process_logs<F>(
|
pub async fn process_logs<F>(
|
||||||
signature: &str,
|
signature: &str,
|
||||||
logs: Vec<String>,
|
logs: Vec<String>,
|
||||||
callback: F,
|
callback: F,
|
||||||
|
payer: Option<Pubkey>,
|
||||||
) -> ClientResult<()>
|
) -> ClientResult<()>
|
||||||
where
|
where
|
||||||
F: Fn(&str, DexInstruction) + Send + Sync,
|
F: Fn(&str, DexInstruction) + Send + Sync,
|
||||||
{
|
{
|
||||||
let instructions = LogFilter::parse_instruction(&logs)?;
|
let instructions = LogFilter::parse_instruction(&logs, payer)?;
|
||||||
for instruction in instructions {
|
for instruction in instructions {
|
||||||
callback(signature, instruction);
|
callback(signature, instruction);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ use anchor_client::solana_client::{
|
|||||||
rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter}
|
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 std::sync::Arc;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
@@ -35,6 +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>,
|
||||||
) -> 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,7 +66,7 @@ where
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let instructions = LogFilter::parse_instruction(&msg.value.logs).unwrap();
|
let instructions = LogFilter::parse_instruction(&msg.value.logs, payer).unwrap();
|
||||||
for instruction in instructions {
|
for instruction in instructions {
|
||||||
match instruction {
|
match instruction {
|
||||||
DexInstruction::CreateToken(token_info) => {
|
DexInstruction::CreateToken(token_info) => {
|
||||||
@@ -75,6 +75,9 @@ where
|
|||||||
DexInstruction::Trade(trade_info) => {
|
DexInstruction::Trade(trade_info) => {
|
||||||
callback(DexEvent::NewTrade(trade_info));
|
callback(DexEvent::NewTrade(trade_info));
|
||||||
}
|
}
|
||||||
|
DexInstruction::BotTrade(trade_info) => {
|
||||||
|
callback(DexEvent::NewBotTrade(trade_info));
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
@@ -35,7 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let subscription = tokens_subscription(
|
let subscription = tokens_subscription(
|
||||||
ws_url,
|
ws_url,
|
||||||
commitment,
|
commitment,
|
||||||
callback
|
callback,
|
||||||
|
None
|
||||||
).await.unwrap();
|
).await.unwrap();
|
||||||
|
|
||||||
// Wait for a while to receive events
|
// Wait for a while to receive events
|
||||||
|
|||||||
Reference in New Issue
Block a user