mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-08 14:47:45 +00:00
update readme
This commit is contained in:
@@ -21,3 +21,113 @@ Add the following to your `Cargo.toml`:
|
||||
[dependencies]
|
||||
mai3-pumpfun-sdk = "2.3.0"
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### logs subscription for token create and trade transaction
|
||||
```rust
|
||||
use mai3_pumpfun_sdk::instruction::{
|
||||
logs_events::DexEvent,
|
||||
logs_subscribe::{tokens_subscription, stop_subscription}
|
||||
};
|
||||
use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
|
||||
|
||||
use std::str::FromStr;
|
||||
use tokio::signal;
|
||||
|
||||
let ws_url = "wss://api.mainnet-beta.solana.com";
|
||||
|
||||
// Set commitment
|
||||
let commitment = CommitmentConfig::confirmed();
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Start subscription
|
||||
let subscription = tokens_subscription(
|
||||
ws_url,
|
||||
commitment,
|
||||
callback
|
||||
).await.unwrap();
|
||||
|
||||
// Wait for a while to receive events
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
|
||||
|
||||
// Stop subscription
|
||||
stop_subscription(subscription).await;
|
||||
```
|
||||
|
||||
### pumpfun Create, Buy, Sell
|
||||
```rust
|
||||
use anchor_client::{
|
||||
solana_sdk::{
|
||||
native_token::LAMPORTS_PER_SOL,
|
||||
signature::{Keypair, Signature},
|
||||
signer::Signer,
|
||||
},
|
||||
Cluster,
|
||||
};
|
||||
use mai3_pumpfun_sdk::{accounts::BondingCurveAccount, utils::CreateTokenMetadata, PriorityFee, PumpFun};
|
||||
|
||||
// Create a new PumpFun client
|
||||
let payer: Keypair = Keypair::new();
|
||||
let client: PumpFun = PumpFun::new(Cluster::Mainnet, &payer, None, None);
|
||||
|
||||
// Mint keypair
|
||||
let mint: Keypair = Keypair::new();
|
||||
|
||||
// Token metadata
|
||||
let metadata: CreateTokenMetadata = CreateTokenMetadata {
|
||||
name: "Lorem ipsum".to_string(),
|
||||
symbol: "LIP".to_string(),
|
||||
description: "Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quam, nisi.".to_string(),
|
||||
file: "/path/to/image.png".to_string(),
|
||||
twitter: None,
|
||||
telegram: None,
|
||||
website: Some("https://example.com".to_string()),
|
||||
};
|
||||
|
||||
// Optional priority fee to expedite transaction processing (e.g., 100 LAMPORTS per compute unit, equivalent to a 0.01 SOL priority fee)
|
||||
let fee: Option<PriorityFee> = Some(PriorityFee {
|
||||
limit: Some(100_000),
|
||||
price: Some(100_000_000),
|
||||
});
|
||||
|
||||
// Create token with metadata
|
||||
let signature: Signature = client.create(&mint, metadata.clone(), fee).await?;
|
||||
println!("Created token: {}", signature);
|
||||
|
||||
// Print amount of SOL and LAMPORTS
|
||||
let amount_sol: u64 = 1;
|
||||
let amount_lamports: u64 = LAMPORTS_PER_SOL * amount_sol;
|
||||
println!("Amount in SOL: {}", amount_sol);
|
||||
println!("Amount in LAMPORTS: {}", amount_lamports);
|
||||
|
||||
// Create and buy tokens with metadata
|
||||
let signature: Signature = client.create_and_buy(&mint, metadata.clone(), amount_lamports, None, fee).await?;
|
||||
println!("Created and bought tokens: {}", signature);
|
||||
|
||||
// Print the curve
|
||||
let curve: BondingCurveAccount = client.get_bonding_curve_account(&mint.pubkey())?;
|
||||
println!("{:?}", curve);
|
||||
|
||||
// Buy tokens (ATA will be created automatically if needed)
|
||||
let signature: Signature = client.buy(&mint.pubkey(), amount_lamports, None, fee).await?;
|
||||
println!("Bought tokens: {}", signature);
|
||||
|
||||
// Sell tokens (sell all tokens)
|
||||
let signature: Signature = client.sell(&mint.pubkey(), None, None, fee).await?;
|
||||
println!("Sold tokens: {}", signature);
|
||||
```
|
||||
|
||||
@@ -9,11 +9,9 @@ use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
use futures::StreamExt;
|
||||
use crate::instruction::{
|
||||
logs_events::DexEvent,
|
||||
logs_data::DexInstruction,
|
||||
logs_filters::LogFilter
|
||||
};
|
||||
use crate::{constants, instruction::{
|
||||
logs_data::DexInstruction, logs_events::DexEvent, logs_filters::LogFilter
|
||||
}};
|
||||
|
||||
/// Subscription handle containing task and unsubscribe logic
|
||||
pub struct SubscriptionHandle {
|
||||
@@ -35,14 +33,14 @@ pub async fn create_pubsub_client(ws_url: &str) -> PubsubClient {
|
||||
/// 启动订阅
|
||||
pub async fn tokens_subscription<F>(
|
||||
ws_url: &str,
|
||||
program_address: &str,
|
||||
commitment: CommitmentConfig,
|
||||
callback: F,
|
||||
) -> Result<SubscriptionHandle, Box<dyn std::error::Error>>
|
||||
where
|
||||
F: Fn(DexEvent) + Send + Sync + 'static,
|
||||
{
|
||||
let logs_filter = RpcTransactionLogsFilter::Mentions(vec![program_address.to_string()]);
|
||||
let program_address = constants::accounts::PUMPFUN.to_string();
|
||||
let logs_filter = RpcTransactionLogsFilter::Mentions(vec![program_address]);
|
||||
|
||||
let logs_config = RpcTransactionLogsConfig {
|
||||
commitment: Some(commitment),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
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 mai3_pumpfun_sdk::instruction::{
|
||||
logs_events::DexEvent,
|
||||
logs_subscribe::{tokens_subscription, stop_subscription}
|
||||
};
|
||||
use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
|
||||
|
||||
use std::str::FromStr;
|
||||
@@ -8,19 +9,10 @@ use tokio::signal;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
start_token_subscription().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@@ -42,7 +34,6 @@ pub async fn start_token_subscription() -> Result<(), Box<dyn std::error::Error>
|
||||
// Start subscription
|
||||
let subscription = tokens_subscription(
|
||||
ws_url,
|
||||
program_address,
|
||||
commitment,
|
||||
callback
|
||||
).await.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user