# 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](/api/examples/rust/connection.md) - Establish gRPC connections with authentication
* [QUIC Transaction Submission](/api/examples/rust/quic.md) - Send raw signed transaction bytes over QUIC
* [Submit Single Transaction](/api/examples/rust/submit-single-transactions.md) - Send individual transactions with tips
* [Submit Batched Transactions](/api/examples/rust/submit-batched-transactions.md) - Send atomic transaction bundles
* [Tip Floor Stream](/api/examples/rust/tip-floor-stream.md) - Stream real-time tip floor data
* [Keepalive](/api/examples/rust/keepalive.md) - 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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nextblock.io/api/examples/rust.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
