Skip to main content

subscribe_program_logs

Function subscribe_program_logs 

Source
pub async fn subscribe_program_logs<F, Fut>(
    rpc_endpoint: &str,
    program_id: &str,
    commitment: CommitmentConfig,
    on_notification: F,
) -> Result<LogSubscriptionHandle, SubscriptionError>
where F: Fn(Response<RpcLogsResponse>) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,
Expand description

Subscribe to program log notifications and invoke a callback for each one.

Spawns a background task that:

  1. Connects to the PubSub endpoint derived from rpc_endpoint.
  2. Subscribes to logs mentioning program_id.
  3. For each notification, spawns on_notification(notification) as a Tokio task.
  4. When handle.stop.send(true) is called, drains remaining buffered notifications (up to 1s), waits for all spawned tasks, then returns.

Returns after the subscription is established. If setup fails, an error is returned before any background task is left running.

ยงExample

use std::sync::{Arc, Mutex};
use simulator_client::subscribe_program_logs;
use solana_commitment_config::CommitmentConfig;

let handle = subscribe_program_logs(
    "https://api.mainnet-beta.solana.com",
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
    CommitmentConfig::confirmed(),
    |notification| async move {
        println!("sig: {}", notification.value.signature);
    },
)
.await?;

// ... do other work ...

handle.stop.send(true).ok();
handle.join_handle.await.ok();