pumpfun 4.6.0

Rust SDK to interact with the Pump.fun Solana program.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use std::error::Error;

use base64::Engine;
use borsh::{BorshDeserialize, BorshSerialize};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use solana_client::{
    nonblocking::pubsub_client::PubsubClient,
    rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter},
    rpc_response::{Response, RpcLogsResponse},
};
use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;

use super::types::Cluster;
use crate::{constants, error};

/// Event emitted when a new token is created
///
/// This event contains information about a newly created token, including its
/// metadata, mint address, bonding curve address, and the accounts involved.
#[derive(BorshSerialize, BorshDeserialize, Debug, Serialize, Deserialize)]
pub struct CreateEvent {
    pub name: String,
    pub symbol: String,
    pub uri: String,
    pub mint: Pubkey,
    pub bonding_curve: Pubkey,
    pub user: Pubkey,
    pub creator: Pubkey,
    pub timestamp: i64,
    pub virtual_token_reserves: u64,
    pub virtual_sol_reserves: u64,
    pub real_token_reserves: u64,
    pub token_total_supply: u64,
}

/// Event emitted when a token is bought or sold
///
/// This event contains details about a trade transaction, including the amounts
/// exchanged, the type of trade (buy/sell), and the updated bonding curve state.
#[derive(BorshSerialize, BorshDeserialize, Debug, Serialize, Deserialize)]
pub struct TradeEvent {
    pub mint: Pubkey,
    pub sol_amount: u64,
    pub token_amount: u64,
    pub is_buy: bool,
    pub user: Pubkey,
    pub timestamp: i64,
    pub virtual_sol_reserves: u64,
    pub virtual_token_reserves: u64,
    pub real_sol_reserves: u64,
    pub real_token_reserves: u64,
    pub fee_recipient: Pubkey,
    pub fee_basis_points: u64,
    pub fee: u64,
    pub creator: Pubkey,
    pub creator_fee_basis_points: u64,
    pub creator_fee: u64,
    pub track_volume: bool,
    pub total_unclaimed_tokens: u64,
    pub total_claimed_tokens: u64,
    pub current_sol_volume: u64,
    pub last_update_timestamp: i64,
}

/// Event emitted when a bonding curve operation completes
///
/// This event signals the completion of a bonding curve operation,
/// providing information about the involved accounts.
#[derive(BorshSerialize, BorshDeserialize, Debug, Serialize, Deserialize)]
pub struct CompleteEvent {
    pub user: Pubkey,
    pub mint: Pubkey,
    pub bonding_curve: Pubkey,
    pub timestamp: i64,
}

/// Event emitted when global parameters are updated
///
/// This event contains information about updates to the global program parameters,
/// including fee settings and initial bonding curve configuration values.
#[derive(BorshSerialize, BorshDeserialize, Debug, Serialize, Deserialize)]
pub struct SetParamsEvent {
    pub initial_virtual_token_reserves: u64,
    pub initial_virtual_sol_reserves: u64,
    pub initial_real_token_reserves: u64,
    pub final_real_sol_reserves: u64,
    pub token_total_supply: u64,
    pub fee_basis_points: u64,
    pub withdraw_authority: Pubkey,
    pub enable_migrate: bool,
    pub pool_migration_fee: u64,
    pub creator_fee_basis_points: u64,
    pub fee_recipients: [Pubkey; 8],
    pub timestamp: i64,
    pub set_creator_authority: Pubkey,
    pub admin_set_creator_authority: Pubkey,
}

/// Enum representing all possible event types emitted by the Pump.fun program
///
/// This enum acts as a container for the different event types that can be
/// emitted by the program. It's used to provide a unified type for event handlers.
#[derive(Debug, Serialize, Deserialize)]
pub enum PumpFunEvent {
    Create(CreateEvent),
    Trade(TradeEvent),
    Complete(CompleteEvent),
    SetParams(SetParamsEvent),
    Unhandled(String, Vec<u8>), // For unhandled events
    Unknown(String, Vec<u8>),   // For unknown events
}

/// Represents an active WebSocket subscription to Pump.fun events
///
/// This struct manages the lifecycle of an event subscription, automatically
/// unsubscribing when dropped to ensure proper cleanup of resources.
pub struct Subscription {
    pub task: JoinHandle<()>,
    pub unsubscribe: Box<dyn Fn() + Send>,
}

impl Subscription {
    pub fn new(task: JoinHandle<()>, unsubscribe: Box<dyn Fn() + Send>) -> Self {
        Subscription { task, unsubscribe }
    }
}

impl Drop for Subscription {
    fn drop(&mut self) {
        (self.unsubscribe)();
        self.task.abort();
    }
}

/// Parses base64-encoded program log data into a structured PumpFunEvent
///
/// This function decodes the base64 data from program logs, identifies the event type
/// using the discriminator (first 8 bytes), and deserializes the remaining data into
/// the appropriate event structure.
///
/// # Arguments
///
/// * `signature` - Transaction signature associated with the event
/// * `data` - Base64-encoded event data from program logs
///
/// # Returns
///
/// Returns a parsed PumpFunEvent if successful, or an error if parsing fails
pub fn parse_event(
    signature: &str,
    data: &str,
) -> Result<PumpFunEvent, Box<dyn Error + Send + Sync>> {
    // Decode base64
    let decoded = base64::engine::general_purpose::STANDARD.decode(data)?;

    // Get event type from the first 8 bytes
    if decoded.len() < 8 {
        return Err(format!("Data too short to contain discriminator: {}", data).into());
    }

    let discriminator = &decoded[..8];
    match discriminator {
        // CreateEvent
        [27, 114, 169, 77, 222, 235, 99, 118] => Ok(PumpFunEvent::Create(
            CreateEvent::try_from_slice(&decoded[8..])
                .map_err(|e| format!("Failed to decode CreateEvent: {}", e))?,
        )),
        // TradeEvent
        [189, 219, 127, 211, 78, 230, 97, 238] => Ok(PumpFunEvent::Trade(
            TradeEvent::try_from_slice(&decoded[8..])
                .map_err(|e| format!("Failed to decode TradeEvent: {}", e))?,
        )),
        // CompleteEvent
        [95, 114, 97, 156, 212, 46, 152, 8] => Ok(PumpFunEvent::Complete(
            CompleteEvent::try_from_slice(&decoded[8..])
                .map_err(|e| format!("Failed to decode CompleteEvent: {}", e))?,
        )),
        // SetParamsEvent
        [223, 195, 159, 246, 62, 48, 143, 131] => Ok(PumpFunEvent::SetParams(
            SetParamsEvent::try_from_slice(&decoded[8..])
                .map_err(|e| format!("Failed to decode SetParamsEvent: {}", e))?,
        )),
        // Other unhandled Pump.fun events
        [64, 69, 192, 104, 29, 30, 25, 107]
        | [245, 59, 70, 34, 75, 185, 109, 92]
        | [147, 250, 108, 120, 247, 29, 67, 222]
        | [79, 172, 246, 49, 205, 91, 206, 232]
        | [146, 159, 189, 172, 146, 88, 56, 244]
        | [122, 2, 127, 1, 14, 191, 12, 175]
        | [189, 233, 93, 185, 92, 148, 234, 148]
        | [97, 97, 215, 144, 93, 146, 22, 124]
        | [134, 36, 13, 72, 232, 101, 130, 216]
        | [237, 52, 123, 37, 245, 251, 72, 210]
        | [142, 203, 6, 32, 127, 105, 191, 162]
        | [197, 122, 167, 124, 116, 81, 91, 255]
        | [182, 195, 137, 42, 35, 206, 207, 247] => {
            Ok(PumpFunEvent::Unhandled(signature.to_string(), decoded))
        }
        // Unknown event type
        _ => Ok(PumpFunEvent::Unknown(signature.to_string(), decoded)),
    }
}

/// Subscribes to Pump.fun program events emitted on-chain
///
/// This function establishes a WebSocket connection to the Solana cluster and
/// subscribes to all transaction logs that mention the Pump.fun program. It parses
/// the program data from these logs into strongly-typed event structures.
///
/// Events are delivered through the provided callback function as they occur. The
/// subscription continues until the returned `Subscription` object is dropped.
///
/// # Arguments
///
/// * `cluster` - Solana cluster configuration containing RPC endpoints
/// * `mentioned` - Optional public key to filter events by mentions. If None, subscribes to all Pump.fun events
/// * `commitment` - Optional commitment level for the subscription. If None, uses the
///   default from the cluster configuration
/// * `callback` - A function that will be called for each event with the following parameters:
///   * `signature`: The transaction signature as a String
///   * `event`: The parsed PumpFunEvent if successful, or None if parsing failed
///   * `error`: Any error that occurred during parsing, or None if successful
///   * `response`: The complete RPC logs response for additional context
///
/// # Returns
///
/// Returns a `Subscription` object that manages the lifecycle of the subscription.
/// When this object is dropped, the subscription is automatically terminated. If
/// the subscription cannot be established, returns a ClientError.
///
/// # Errors
///
/// Returns an error if:
/// - The WebSocket connection cannot be established
/// - The subscription request fails
///
/// # Examples
///
/// ```no_run
/// use pumpfun::{PumpFun, common::types::{Cluster, PriorityFee}};
/// use solana_sdk::commitment_config::CommitmentConfig;
/// use std::{sync::Arc, error::Error};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn Error>> {
///     // Create cluster configuration
///     let cluster = Cluster::mainnet(
///         CommitmentConfig::confirmed(),
///         PriorityFee::default()
///     );
///
///     // Define callback to process events
///     let callback = |signature, event, error, _| {
///         if let Some(event) = event {
///             println!("Event received: {:#?} in tx: {}", event, signature);
///         } else if let Some(err) = error {
///             eprintln!("Error parsing event in tx {}: {}", signature, err);
///         }
///     };
///
///     // Subscribe to events
///     let subscription = pumpfun::common::stream::subscribe(cluster, None, None, callback).await?;
///
///     // Keep subscription alive until program terminates
///     tokio::signal::ctrl_c().await?;
///     Ok(())
/// }
/// ```
pub async fn subscribe<F>(
    cluster: Cluster,
    mentioned: Option<String>,
    commitment: Option<CommitmentConfig>,
    callback: F,
) -> Result<Subscription, error::ClientError>
where
    F: Fn(
            String,
            Option<PumpFunEvent>,
            Option<Box<dyn Error + Send + Sync>>,
            Response<RpcLogsResponse>,
        ) + Send
        + Sync
        + 'static,
{
    // Initialize PubsubClient
    let ws_url = &cluster.rpc.ws;
    let pubsub_client = PubsubClient::new(ws_url)
        .await
        .map_err(error::ClientError::PubsubClientError)?;

    let (tx, _) = mpsc::channel(1);
    let (cb_tx, mut cb_rx) = mpsc::channel(1000);

    tokio::spawn(async move {
        while let Some((sig, event, err, log)) = cb_rx.recv().await {
            callback(sig, event, err, log);
        }
    });

    let task = tokio::spawn(async move {
        // Subscribe to logs for the program
        let (mut stream, _unsubscribe) = pubsub_client
            .logs_subscribe(
                RpcTransactionLogsFilter::Mentions(vec![
                    mentioned.unwrap_or(constants::accounts::PUMPFUN.to_string())
                ]),
                RpcTransactionLogsConfig {
                    commitment: Some(commitment.unwrap_or(cluster.commitment)),
                },
            )
            .await
            .unwrap();

        // Process incoming logs
        while let Some(log) = stream.next().await {
            // Get the signature of the transaction
            let signature = &log.value.signature;
            // Check for logs with "Program data:" prefix
            for log_line in &log.value.logs {
                // Extract base64-encoded data
                if let Some(data) = log_line.strip_prefix("Program data: ") {
                    match parse_event(signature, data) {
                        Ok(event) => {
                            let _ = cb_tx
                                .send((signature.to_string(), Some(event), None, log.clone()))
                                .await;
                        }
                        Err(err) => {
                            let _ = cb_tx
                                .send((signature.to_string(), None, Some(err), log.clone()))
                                .await;
                        }
                    }
                }
            }
        }
    });

    Ok(Subscription::new(
        task,
        Box::new(move || {
            let _ = tx.try_send(());
        }),
    ))
}

#[cfg(test)]
mod tests {
    use crate::common::types::PriorityFee;

    use super::*;
    use std::sync::Arc;
    use tokio::sync::Mutex;
    use tokio::time::{timeout, Duration};

    #[cfg(not(skip_expensive_tests))]
    #[tokio::test]
    async fn test_subscribe() {
        if std::env::var("SKIP_EXPENSIVE_TESTS").is_ok() {
            return;
        }

        // Define the cluster
        let cluster = Cluster::mainnet(CommitmentConfig::processed(), PriorityFee::default());

        // Shared vector to collect events
        let events: Arc<Mutex<Vec<PumpFunEvent>>> = Arc::new(Mutex::new(Vec::new()));

        // Define the callback to store events
        let callback = {
            let events = Arc::clone(&events);
            move |signature: String,
                  event: Option<PumpFunEvent>,
                  err: Option<Box<dyn Error + Send + Sync>>,
                  _: Response<RpcLogsResponse>| {
                if let Some(event) = event {
                    let events = Arc::clone(&events);
                    tokio::spawn(async move {
                        let mut events = events.lock().await;
                        events.push(event);
                    });
                } else if err.is_some() {
                    eprintln!("Error in subscription: signature={}", signature);
                }
            }
        };

        // Start the subscription
        let subscription = subscribe(cluster, None, None, callback)
            .await
            .expect("Failed to start subscription");

        // Wait for 30 seconds to collect events
        let wait_duration = Duration::from_secs(30);
        timeout(wait_duration, async {
            loop {
                tokio::time::sleep(Duration::from_secs(1)).await;
            }
        })
        .await
        .unwrap_err(); // Expect a timeout error to end the waiting period

        // Clean up the subscription
        drop(subscription);

        // Verify that at least one event was received
        let events = events.lock().await;
        assert!(
            !events.is_empty(),
            "No events received within {} seconds",
            wait_duration.as_secs()
        );

        println!("Received {} events", events.len());
    }
}