Rust

Complete Rust examples for integrating with NextBlock's gRPC API using Tonic and the Solana SDK.

Overview

These examples demonstrate how to:

  • Establish secure gRPC connections with authentication

  • Submit single and batched transactions with proper tipping

  • Stream real-time tip floor data for dynamic tip optimization

  • Maintain persistent connections with keepalive mechanisms

Prerequisites

Add these dependencies to your Cargo.toml:

[dependencies]
tonic = "0.10"
tokio = { version = "1.0", features = ["full"] }
solana-sdk = "1.17"
solana-client = "1.17"
base64 = "0.21"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rand = "0.8"
chrono = { version = "0.4", features = ["serde"] }
once_cell = "1.19"

# Add your generated proto dependencies here
# nextblock-proto = { path = "./generated" }

Examples

Core Examples

Quick Start

  1. Generate gRPC client from nextblock-proto

  2. Set up your environment with API key and endpoint

  3. Start with connection example to establish authenticated gRPC connection

  4. Use tip floor streaming to optimize transaction tips dynamically

  5. Submit transactions using single or batched submission methods

Key Features

Authentication

All examples include proper API key authentication using Tonic's interceptor pattern.

Error Handling

Comprehensive error handling with retry logic and graceful degradation.

Connection Management

Persistent connections with keepalive, health monitoring, and automatic recovery.

Dynamic Tipping

Real-time tip optimization based on current network conditions from tip floor API.

Transaction Building

Complete transaction building with Solana SDK integration and proper signing.

Best Practices

  1. Use TLS in production - Always enable TLS for production environments

  2. Implement keepalive - Maintain persistent connections for better performance

  3. Monitor tip floors - Use streaming API to adjust tips dynamically

  4. Handle errors gracefully - Implement proper retry logic with exponential backoff

  5. Validate inputs - Always validate public keys and transaction parameters

  6. Use appropriate endpoints - Choose the endpoint closest to your location

Last updated