# Rust

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

## Overview

These examples demonstrate how to:

* Establish secure gRPC connections with authentication
* Submit raw transaction bytes over QUIC for the lowest transport overhead
* 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`:

```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

* [Connection](https://docs.nextblock.io/api/examples/rust/connection) - Establish gRPC connections with authentication
* [QUIC Transaction Submission](https://docs.nextblock.io/api/examples/rust/quic) - Send raw signed transaction bytes over QUIC
* [Submit Single Transaction](https://docs.nextblock.io/api/examples/rust/submit-single-transactions) - Send individual transactions with tips
* [Submit Batched Transactions](https://docs.nextblock.io/api/examples/rust/submit-batched-transactions) - Send atomic transaction bundles
* [Tip Floor Stream](https://docs.nextblock.io/api/examples/rust/tip-floor-stream) - Stream real-time tip floor data
* [Keepalive](https://docs.nextblock.io/api/examples/rust/keepalive) - Maintain persistent connections

## Quick Start

1. **Generate gRPC client** from [nextblock-proto](https://github.com/nextblock-ag/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
