Configuring Neo Rust SDK

This guide explains how to configure the Neo Rust SDK for different environments and use cases.

Network Selection

The Neo Rust SDK provides built-in methods to connect to different Neo networks:

network_config.rs
use neo3::prelude::*;

async fn network_examples() -> Result<()> {
    // Connect to TestNet (for development)
    let testnet_client = NeoClient::connect_to_testnet().await?;
    
    // Connect to MainNet (for production)
    let mainnet_client = NeoClient::connect_to_mainnet().await?;
    
    // Connect to a local PrivateNet (for local development)
    let local_client = NeoClient::connect("http://localhost:10332").await?;
    
    // Connect to a custom RPC node
    let custom_client = NeoClient::connect("http://seed1.neo.org:10332").await?;
    
    Ok(())
}

Client Configuration

You can configure the NeoClient with custom settings:

client_config.rs
use neo3::prelude::*;
use std::time::Duration;

async fn client_config_example() -> Result<()> {
    // Create a client with custom configuration
    let client = NeoClientBuilder::new()
        .url("http://seed1.neo.org:10332")
        .timeout(Duration::from_secs(30))
        .max_retries(3)
        .build()
        .await?;
    
    Ok(())
}

Logging Configuration

The Neo Rust SDK uses the Rust log crate for logging. You can configure it with various logging implementations such as env_logger:

Cargo.toml
[dependencies]
neo3 = "0.1.9"
log = "0.4"
env_logger = "0.10"

Then initialize the logger in your application:

main.rs
use neo3::prelude::*;
use log::{info, warn, error, debug};

fn main() -> Result<()> {
    // Initialize logger
    env_logger::init();
    
    // Now SDK operations will log according to the RUST_LOG environment variable
    // e.g., RUST_LOG=debug,neo=trace
    
    info!("Application started");
    
    // Your Neo SDK code here
    
    Ok(())
}

Feature Flags

The Neo Rust SDK provides several feature flags that can be enabled or disabled to customize functionality:

Cargo.toml
[dependencies]
neo3 = { version = "0.1.9", features = ["sgx", "web-socket", "nns"] }

# Available features:
# - "sgx": Enable Intel SGX secure enclave support
# - "web-socket": Enable WebSocket support for real-time notifications
# - "nns": Enable Neo Name Service integration
# - "extended-keys": Enable extended key derivation (BIP32/44/39)
# - "default": Includes basic features suitable for most applications

Custom Storage Providers

By default, the Neo Rust SDK uses file-based storage for wallets, but you can implement custom storage providers:

custom_storage.rs
use neo3::prelude::*;
use neo3::wallet::storage::{StorageProvider, StorageError};

// Implement a custom storage provider (e.g., database-backed)
struct DatabaseStorage {
    connection: DbConnection,
}

impl StorageProvider for DatabaseStorage {
    fn load(&self, path: &str) -> Result<Vec<u8>> {
        // Implementation to load wallet data from database
        // ...
    }
    
    fn save(&self, path: &str, data: &[u8]) -> Result<()> {
        // Implementation to save wallet data to database
        // ...
    }
}

fn use_custom_storage() -> Result<()> {
    // Create database storage
    let db_storage = DatabaseStorage { 
        connection: DbConnection::new("connection_string"),
    };
    
    // Create wallet with custom storage
    let wallet = Wallet::new_with_storage(db_storage);
    
    Ok(())
}

Transaction Settings

Configure default transaction settings:

tx_settings.rs
use neo3::prelude::*;

async fn transaction_settings() -> Result<()> {
    // Set up default transaction settings
    let tx_settings = TransactionSettings::new()
        .system_fee(1_0000000)  // 0.1 GAS
        .network_fee(1_0000000) // 0.1 GAS
        .valid_until_blocks(100) // Valid for next 100 blocks
        .build();
    
    // Create a client with custom transaction settings
    let client = NeoClientBuilder::new()
        .url("http://seed1.neo.org:10332")
        .transaction_settings(tx_settings)
        .build()
        .await?;
    
    Ok(())
}

Environment Variables

You can use environment variables to configure the Neo Rust SDK for different environments:

env_config.rs
use neo3::prelude::*;
use std::env;

async fn env_config_example() -> Result<()> {
    // Get config from environment variables
    let node_url = env::var("NEO_NODE_URL")
        .unwrap_or_else(|_| "http://seed1.neo.org:10332".to_string());
    
    let network_type = match env::var("NEO_NETWORK").unwrap_or_else(|_| "testnet".to_string()).as_str() {
        "mainnet" => NetworkType::MainNet,
        "testnet" => NetworkType::TestNet,
        _ => NetworkType::TestNet, // Default to TestNet
    };
    
    // Connect to the configured network
    let client = match network_type {
        NetworkType::MainNet => NeoClient::connect_to_mainnet().await?,
        NetworkType::TestNet => NeoClient::connect_to_testnet().await?,
        _ => NeoClient::connect(&node_url).await?,
    };
    
    Ok(())
}

Next Steps

Now that you've learned how to configure the Neo Rust SDK, you can explore the following topics: