feat: Add PumpSwap trading functionality module

This commit is contained in:
sgxiang
2025-06-06 22:29:30 +08:00
parent 63109713f4
commit 94c2a22ab7
19 changed files with 2334 additions and 21 deletions
+91
View File
@@ -7,12 +7,15 @@ use tonic::transport::Channel;
use log::error;
use solana_sdk::transaction::VersionedTransaction;
use crate::common::pumpswap::PumpSwapInstruction;
use crate::common::AnyResult;
use solana_sdk::pubkey::Pubkey;
use crate::common::pumpfun::logs_data::DexInstruction;
use crate::common::pumpfun::logs_events::PumpfunEvent;
use crate::common::pumpswap::logs_events::PumpSwapEvent;
use crate::common::pumpfun::logs_filters::LogFilter;
use crate::common::pumpswap::logs_filters::LogFilter as PumpswapLogFilter;
use crate::swqos::jito_grpc::shredstream::shredstream_proxy_client::ShredstreamProxyClient;
use crate::swqos::jito_grpc::shredstream::SubscribeEntriesRequest;
@@ -77,6 +80,47 @@ impl ShredStreamGrpc {
Ok(())
}
pub async fn shredstream_subscribe_pumpswap<F>(&self, callback: F) -> AnyResult<()>
where
F: Fn(PumpSwapEvent) + Send + Sync + 'static,
{
let request = tonic::Request::new(SubscribeEntriesRequest {});
let mut client = (*self.shredstream_client).clone();
let mut stream = client.subscribe_entries(request).await?.into_inner();
let (mut tx, mut rx) = mpsc::channel::<TransactionWithSlot>(CHANNEL_SIZE);
let callback = Box::new(callback);
tokio::spawn(async move {
while let Some(message) = stream.next().await {
match message {
Ok(msg) => {
if let Ok(entries) = bincode::deserialize::<Vec<Entry>>(&msg.entries) {
for entry in entries {
for transaction in entry.transactions {
let _ = tx.try_send(TransactionWithSlot {
transaction: transaction.clone(),
slot: msg.slot,
});
}
}
}
}
Err(error) => {
error!("Stream error: {error:?}");
break;
}
}
}
});
while let Some(transaction_with_slot) = rx.next().await {
if let Err(e) = Self::process_pumpswap_transaction(transaction_with_slot, &*callback).await {
error!("Error processing transaction: {:?}", e);
}
}
Ok(())
}
async fn process_pumpfun_transaction<F>(transaction_with_slot: TransactionWithSlot, callback: &F, bot_wallet: Option<Pubkey>) -> AnyResult<()>
where
F: Fn(PumpfunEvent) + Send + Sync,
@@ -111,4 +155,51 @@ impl ShredStreamGrpc {
}
Ok(())
}
async fn process_pumpswap_transaction<F>(transaction_with_slot: TransactionWithSlot, callback: &F) -> AnyResult<()>
where
F: Fn(PumpSwapEvent) + Send + Sync,
{
let slot = transaction_with_slot.slot;
let versioned_tx = transaction_with_slot.transaction;
let instructions = PumpswapLogFilter::parse_pumpswap_compiled_instruction(versioned_tx).unwrap();
for instruction in instructions {
match instruction {
PumpSwapInstruction::CreatePool(mut create_event) => {
create_event.slot = slot;
callback(PumpSwapEvent::CreatePool(create_event));
}
PumpSwapInstruction::Deposit(mut deposit_event) => {
deposit_event.slot = slot;
callback(PumpSwapEvent::Deposit(deposit_event));
}
PumpSwapInstruction::Withdraw(mut withdraw_event) => {
withdraw_event.slot = slot;
callback(PumpSwapEvent::Withdraw(withdraw_event));
}
PumpSwapInstruction::Buy(mut buy_event) => {
buy_event.slot = slot;
callback(PumpSwapEvent::Buy(buy_event));
}
PumpSwapInstruction::Sell(mut sell_event) => {
sell_event.slot = slot;
callback(PumpSwapEvent::Sell(sell_event));
}
PumpSwapInstruction::UpdateFeeConfig(mut update_fee_event) => {
update_fee_event.slot = slot;
callback(PumpSwapEvent::UpdateFeeConfig(update_fee_event));
}
PumpSwapInstruction::UpdateAdmin(mut update_admin_event) => {
update_admin_event.slot = slot;
callback(PumpSwapEvent::UpdateAdmin(update_admin_event));
}
PumpSwapInstruction::Disable(mut disable_event) => {
disable_event.slot = slot;
callback(PumpSwapEvent::Disable(disable_event));
}
_ => {}
}
}
Ok(())
}
}
+186
View File
@@ -349,4 +349,190 @@ impl YellowstoneGrpc {
Ok(())
}
// ------------------------------------------------------------
// PumpSwap
// ------------------------------------------------------------
/// 订阅PumpSwap事件
pub async fn subscribe_pumpswap<F>(&self, callback: F) -> AnyResult<()>
where
F: Fn(crate::common::pumpswap::logs_events::PumpSwapEvent) + Send + Sync + 'static,
{
// 使用constants中定义的AMM_PROGRAM
let pump_program_id = crate::constants::pumpswap::accounts::AMM_PROGRAM;
let addrs = vec![pump_program_id.to_string()];
// 创建过滤器
let transactions = self.get_subscribe_request_filter(addrs, vec![], vec![]);
// 订阅事件
let (mut subscribe_tx, mut stream) = self.subscribe_with_request(transactions).await?;
// 创建通道
let (mut tx, mut rx) = mpsc::channel::<TransactionPretty>(1000);
// 创建回调函数
let callback = Box::new(callback);
// 启动处理流的任务
tokio::spawn(async move {
while let Some(message) = stream.next().await {
match message {
Ok(msg) => {
if let Err(e) = Self::handle_stream_message(msg, &mut tx, &mut subscribe_tx).await {
error!("Error handling message: {:?}", e);
break;
}
}
Err(error) => {
error!("Stream error: {error:?}");
break;
}
}
}
});
// 处理交易
while let Some(transaction_pretty) = rx.next().await {
if let Err(e) = Self::process_pumpswap_transaction(transaction_pretty, &*callback).await {
error!("Error processing transaction: {:?}", e);
}
}
Ok(())
}
/// 使用过滤器订阅PumpSwap事件
pub async fn subscribe_pumpswap_with_filter<F>(
&self,
callback: F,
account_include: Option<Vec<String>>,
account_exclude: Option<Vec<String>>
) -> AnyResult<()>
where
F: Fn(crate::common::pumpswap::logs_events::PumpSwapEvent) + Send + Sync + 'static,
{
// 使用constants中定义的AMM_PROGRAM
let pump_program_id = crate::constants::pumpswap::accounts::AMM_PROGRAM;
let addrs = vec![pump_program_id.to_string()];
// 创建过滤器
let account_include = account_include.unwrap_or_default();
let account_exclude = account_exclude.unwrap_or_default();
let transactions = self.get_subscribe_request_filter(account_include, account_exclude, addrs);
// 订阅事件
let (mut subscribe_tx, mut stream) = self.subscribe_with_request(transactions).await?;
// 创建通道
let (mut tx, mut rx) = mpsc::channel::<TransactionPretty>(1000);
// 创建回调函数
let callback = Box::new(callback);
// 启动处理流的任务
tokio::spawn(async move {
while let Some(message) = stream.next().await {
match message {
Ok(msg) => {
if let Err(e) = Self::handle_stream_message(msg, &mut tx, &mut subscribe_tx).await {
error!("Error handling message: {:?}", e);
break;
}
}
Err(error) => {
error!("Stream error: {error:?}");
break;
}
}
}
});
// 处理交易
while let Some(transaction_pretty) = rx.next().await {
if let Err(e) = Self::process_pumpswap_transaction(transaction_pretty, &*callback).await {
error!("Error processing transaction: {:?}", e);
}
}
Ok(())
}
/// 处理PumpSwap交易
async fn process_pumpswap_transaction<F>(
transaction_pretty: TransactionPretty,
callback: &F
) -> AnyResult<()>
where
F: Fn(crate::common::pumpswap::logs_events::PumpSwapEvent) + Send + Sync,
{
let slot = transaction_pretty.slot;
let trade_raw: solana_transaction_status::EncodedTransactionWithStatusMeta = transaction_pretty.tx;
// 检查交易元数据
let meta = trade_raw.meta.as_ref()
.ok_or_else(|| anyhow::anyhow!("Missing transaction metadata"))?;
// 检查交易是否成功
if meta.err.is_some() {
return Ok(());
}
// 获取日志
let logs = if let solana_transaction_status::option_serializer::OptionSerializer::Some(logs) = &meta.log_messages {
logs
} else {
&vec![]
};
// 解析PumpSwap事件
let events = crate::common::pumpswap::logs_filters::LogFilter::parse_pumpswap_logs(logs);
// 处理事件
for mut event in events {
// 设置签名和slot
match &mut event {
crate::common::pumpswap::logs_events::PumpSwapEvent::Buy(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
crate::common::pumpswap::logs_events::PumpSwapEvent::Sell(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
crate::common::pumpswap::logs_events::PumpSwapEvent::CreatePool(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
crate::common::pumpswap::logs_events::PumpSwapEvent::Deposit(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
crate::common::pumpswap::logs_events::PumpSwapEvent::Withdraw(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
crate::common::pumpswap::logs_events::PumpSwapEvent::Disable(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
crate::common::pumpswap::logs_events::PumpSwapEvent::UpdateAdmin(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
crate::common::pumpswap::logs_events::PumpSwapEvent::UpdateFeeConfig(e) => {
e.signature = transaction_pretty.signature.to_string();
e.slot = slot;
},
_ => {}
}
// 调用回调函数
callback(event);
}
Ok(())
}
}