Module rpc

Module rpc 

Source
Expand description

§RPC Communication with the delta Base Layer

This module provides a client for communicating with delta base layer through RPC (Remote Procedure Call). It enables applications to query network state, submit transactions, retrieve vault data, and subscribe to network events.

§Overview

The primary interface is the BaseRpcClient which handles connection management, request serialization, and response parsing. The client provides methods for:

All RPC operations use the tonic library for gRPC communication, with automatic serialization and deserialization between protocol buffer and native Rust types.

§Usage

To use the RPC client, you need a running delta base layer at some known URL.

§Example

use delta_base_sdk::{
    core::Shard,
    crypto::{ed25519, Hash256, IntoSignatureEnum},
    rpc::{BaseRpcClient, RpcError},
    vaults::{Address, ReadableNativeBalance, Vault},
};

async fn rpc_example() -> Result<(), Box<dyn Error>> {
    // Connect to base layer
    let client = BaseRpcClient::new("http://localhost:50051").await?;

    // Get a vault
    let pubkey = ed25519::PrivKey::generate().pub_key();
    let vault = client.get_vault(Address::new(pubkey.owner(), 1)).await?;
    println!("Vault balance: {}", vault.balance());

    // Check a transaction status
    // In a real app, this would be a valid signature
    let signature = ed25519::Signature::from_str("<MY SIGNATURE>")?
        .into_signature_enum(pubkey);
    let status = client.get_transaction_status(signature.hash_sha256()).await?;
    println!("Transaction status: {:?}", status);

    // Query current epoch
    let epoch = client.get_epoch().await?;
    println!("Current epoch: {}", epoch);

    // Subscribe to network events (requires tokio to run)
    let mut events = client.stream_base_layer_events(0).await?;

    // Process the first 5 events
    let mut count = 0;
    while let Some(event) = events.next().await {
        if let Ok(event) = event {
            println!("Received event: {:?}", event);
            count += 1;
            if count >= 5 {
                break;
            }
        }
    }
    Ok(())
}

Structs§

BaseRpcClient
Client for communicating with the delta base layer through RPC

Enums§

RpcError
Errors that can occur when using the BaseRpcClient