feat(examples): add nonce and token balance monitoring capabilities

- feat(examples): add nonce_listen_example for nonce account tracking
- feat(examples): add token_balance_listen_example for balance monitoring
- feat(parser): implement NonceAccountEvent with nonce state parsing
- feat(types): add AccountNonce event type to filtering system
- docs: update README with new monitoring examples
- chore: bump version to 0.4.2
This commit is contained in:
ysq
2025-09-09 21:24:21 +08:00
parent 2e8c0a1ee5
commit 528003a688
7 changed files with 230 additions and 5 deletions
+81
View File
@@ -0,0 +1,81 @@
use solana_streamer_sdk::streaming::{
event_parser::{
common::{filter::EventTypeFilter, EventType},
UnifiedEvent,
},
grpc::ClientConfig,
yellowstone_grpc::{AccountFilter, TransactionFilter},
YellowstoneGrpc,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Starting Yellowstone gRPC Streamer...");
test_grpc().await?;
Ok(())
}
async fn test_grpc() -> Result<(), Box<dyn std::error::Error>> {
println!("Subscribing to Yellowstone gRPC events...");
// Create low-latency configuration
let mut config: ClientConfig = ClientConfig::low_latency();
// Enable performance monitoring, has performance overhead, disabled by default
config.enable_metrics = true;
let grpc = YellowstoneGrpc::new_with_config(
"https://solana-yellowstone-grpc.publicnode.com:443".to_string(),
None,
config,
)?;
println!("GRPC client created successfully");
let callback = create_event_callback();
// Will try to parse corresponding protocol events from transactions
let protocols = vec![];
println!("Protocols to monitor: {:?}", protocols);
// Filter accounts
let account_include = vec![];
let account_exclude = vec![];
let account_required = vec![];
// Listen to transaction data
let transaction_filter =
TransactionFilter { account_include, account_exclude, account_required };
let nonce_account = "use_your_nonce_account_here".to_string();
// Listen to account data belonging to owner programs -> account event monitoring
let account_filter = AccountFilter { account: vec![nonce_account], owner: vec![] };
// Event filtering
let event_type_filter = Some(EventTypeFilter { include: vec![EventType::AccountNonce] });
println!("Starting to listen for events, press Ctrl+C to stop...");
println!("Starting subscription...");
grpc.subscribe_events_immediate(
protocols,
None,
transaction_filter,
account_filter,
event_type_filter,
None,
callback,
)
.await?;
// 支持 stop 方法,测试代码 - 异步1000秒之后停止
let grpc_clone = grpc.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1000)).await;
grpc_clone.stop().await;
});
println!("Waiting for Ctrl+C to stop...");
tokio::signal::ctrl_c().await?;
Ok(())
}
fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|event: Box<dyn UnifiedEvent>| {
println!("🎉 Event received! {:?}", event);
}
}
+82
View File
@@ -0,0 +1,82 @@
use solana_streamer_sdk::streaming::{
event_parser::{
common::{filter::EventTypeFilter, EventType},
UnifiedEvent,
},
grpc::ClientConfig,
yellowstone_grpc::{AccountFilter, TransactionFilter},
YellowstoneGrpc,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Starting Yellowstone gRPC Streamer...");
test_grpc().await?;
Ok(())
}
async fn test_grpc() -> Result<(), Box<dyn std::error::Error>> {
println!("Subscribing to Yellowstone gRPC events...");
// Create low-latency configuration
let mut config: ClientConfig = ClientConfig::low_latency();
// Enable performance monitoring, has performance overhead, disabled by default
config.enable_metrics = true;
let grpc = YellowstoneGrpc::new_with_config(
"https://solana-yellowstone-grpc.publicnode.com:443".to_string(),
None,
config,
)?;
println!("GRPC client created successfully");
let callback = create_event_callback();
// Will try to parse corresponding protocol events from transactions
let protocols = vec![];
println!("Protocols to monitor: {:?}", protocols);
// Filter accounts
let account_include = vec![];
let account_exclude = vec![];
let account_required = vec![];
// Listen to transaction data
let transaction_filter =
TransactionFilter { account_include, account_exclude, account_required };
let account_to_listen = "use_your_token_account_here".to_string();
// Listen to account data belonging to owner programs -> account event monitoring
let account_filter = AccountFilter { account: vec![account_to_listen], owner: vec![] };
// Event filtering
let event_type_filter = Some(EventTypeFilter { include: vec![EventType::AccountCommon] });
println!("Starting to listen for events, press Ctrl+C to stop...");
println!("Starting subscription...");
grpc.subscribe_events_immediate(
protocols.clone(),
None,
transaction_filter.clone(),
account_filter.clone(),
event_type_filter.clone(),
None,
callback,
)
.await?;
// 支持 stop 方法,测试代码 - 异步1000秒之后停止
let grpc_clone = grpc.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1000)).await;
grpc_clone.stop().await;
});
println!("Waiting for Ctrl+C to stop...");
tokio::signal::ctrl_c().await?;
Ok(())
}
fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|event: Box<dyn UnifiedEvent>| {
println!("🎉 Event received! {:?}", event);
}
}