add tokens_subscription
This commit is contained in:
+31
-38
@@ -1,26 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
use anchor_client::solana_client::nonblocking::rpc_client::RpcClient;
|
||||
use anchor_spl::associated_token::get_associated_token_address;
|
||||
use anchor_client::{
|
||||
solana_sdk::{
|
||||
native_token::LAMPORTS_PER_SOL,
|
||||
instruction::Instruction,
|
||||
pubkey::Pubkey,
|
||||
signature::Keypair,
|
||||
signer::Signer,
|
||||
transaction::Transaction,
|
||||
commitment_config::CommitmentConfig,
|
||||
compute_budget::ComputeBudgetInstruction,
|
||||
system_program::ID as SYSTEM_PROGRAM_ID,
|
||||
sysvar::rent::ID as RENT_ID,
|
||||
instruction::AccountMeta,
|
||||
program_error::ProgramError,
|
||||
program_pack::Pack,
|
||||
},
|
||||
Cluster,
|
||||
};
|
||||
use mai3_pumpfun_sdk::constants::accounts::PUMPFUN;
|
||||
use mai3_pumpfun_sdk::instruction::logs_subscribe;
|
||||
use mai3_pumpfun_sdk::instruction::logs_subscribe::tokens_subscription;
|
||||
use mai3_pumpfun_sdk::instruction::logs_events::DexEvent;
|
||||
use mai3_pumpfun_sdk::instruction::logs_subscribe::stop_subscription;
|
||||
use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
|
||||
|
||||
use std::str::FromStr;
|
||||
use tokio::signal;
|
||||
|
||||
@@ -35,30 +17,41 @@ pub async fn start_token_subscription() -> Result<(), Box<dyn std::error::Error>
|
||||
println!("Starting token subscription\n");
|
||||
|
||||
let ws_url = "wss://api.mainnet-beta.solana.com";
|
||||
|
||||
// Program address
|
||||
let program_address = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P";
|
||||
|
||||
// Set commitment
|
||||
let commitment = CommitmentConfig::confirmed();
|
||||
|
||||
println!("program_address: {}", PUMPFUN);
|
||||
|
||||
let subscription_callback = |signature: &str, logs: Vec<String>| {
|
||||
println!("=======Signature: {}=============", signature);
|
||||
for log in logs {
|
||||
println!("============Log: {}============", log);
|
||||
|
||||
// Define callback function
|
||||
let callback = |event: DexEvent| {
|
||||
match event {
|
||||
DexEvent::NewToken(token_info) => {
|
||||
println!("Received new token event: {:?}", token_info);
|
||||
},
|
||||
DexEvent::NewTrade(trade_info) => {
|
||||
println!("Received new trade event: {:?}", trade_info);
|
||||
},
|
||||
DexEvent::Error(err) => {
|
||||
println!("Received error: {}", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let subscription = logs_subscribe::start_subscription(
|
||||
// Start subscription
|
||||
let subscription = tokens_subscription(
|
||||
ws_url,
|
||||
&PUMPFUN.to_string(),
|
||||
program_address,
|
||||
commitment,
|
||||
subscription_callback,
|
||||
callback
|
||||
).await.unwrap();
|
||||
|
||||
subscription.task.await.unwrap();
|
||||
// stop_subscription(subscription);
|
||||
// Wait for a while to receive events
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
|
||||
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(1000)).await;
|
||||
|
||||
println!("Subscription closed.");
|
||||
// Stop subscription
|
||||
stop_subscription(subscription).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user