Private Transaction Builder

Private Transaction Builder - Complete API Reference

This document provides a comprehensive guide to building private transactions using the Roru SDK's TransactionBuilder API.

Overview

The TransactionBuilder provides a fluent, type-safe API for constructing private transactions. It handles note selection, change calculation, fee estimation, and transaction structure automatically.

Basic Usage

Simple Transaction

Rust Example:

use roru_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize client
    let client = RoruClient::new(ClientConfig::default()).await?;
    let wallet = client.create_wallet().await?;
    
    // Generate recipient address
    let recipient = wallet.generate_address()?;
    
    // Build transaction
    let tx = TransactionBuilder::new()
        .to(recipient)
        .amount(1_000_000) // 1.0 tokens (6 decimals)
        .build()?;
    
    // Send transaction
    let result = wallet.send_transaction(tx).await?;
    println!("Transaction sent: {:?}", result.tx_hash);
    
    Ok(())
}

TypeScript Example:

Python Example:

Advanced Usage

Custom Input Selection

Rust:

Multiple Outputs

Rust:

With Memo

Rust:

Custom Fee

Rust:

Priority Level

Rust:

API Reference

TransactionBuilder Methods

new() -> TransactionBuilder

Creates a new transaction builder instance.

Returns: TransactionBuilder

Example:

to(address: ShieldedAddress) -> Self

Sets the primary recipient address.

Parameters:

  • address: The shielded address of the recipient

Returns: Self for method chaining

Example:

amount(value: u64) -> Self

Sets the transaction amount in smallest units.

Parameters:

  • value: Amount in smallest token unit (e.g., 1_000_000 = 1.0 tokens with 6 decimals)

Returns: Self for method chaining

Example:

inputs(notes: Vec<Note>) -> Self

Manually specify input notes to use.

Parameters:

  • notes: Vector of notes to spend

Returns: Self for method chaining

Example:

add_output(address: ShieldedAddress, amount: u64) -> Self

Add an additional output to the transaction.

Parameters:

  • address: Recipient address

  • amount: Amount for this output

Returns: Self for method chaining

Example:

memo(data: &[u8]) -> Self

Add a memo field to the transaction.

Parameters:

  • data: Memo data (max 512 bytes)

Returns: Self for method chaining

Example:

fee(amount: u64) -> Self

Set a custom fee amount.

Parameters:

  • amount: Fee amount in smallest units

Returns: Self for method chaining

Example:

priority(level: Priority) -> Self

Set transaction priority level.

Parameters:

  • level: Priority::Low, Priority::Normal, or Priority::High

Returns: Self for method chaining

Example:

asset(asset_id: AssetId) -> Self

Specify the asset for this transaction.

Parameters:

  • asset_id: Asset identifier

Returns: Self for method chaining

Example:

expiry(timestamp: u64) -> Self

Set transaction expiry timestamp.

Parameters:

  • timestamp: Unix timestamp when transaction expires

Returns: Self for method chaining

Example:

build() -> Result<Transaction>

Builds the final transaction.

Returns: Result<Transaction>

Errors:

  • InsufficientBalance: Not enough funds

  • InvalidAddress: Invalid recipient address

  • InvalidAmount: Invalid amount value

  • NoteSelectionFailed: Could not select appropriate notes

Example:

Transaction Structure

Transaction Type

Input Structure

Output Structure

Error Handling

Common Errors

InsufficientBalance:

InvalidAddress:

Best Practices

1. Always Check Balance First

2. Handle Change Properly

The builder automatically handles change, but you can verify:

3. Use Appropriate Priority

4. Validate Before Building

Complete Example

Rust - Full Transaction Flow:

Conclusion

The TransactionBuilder provides:

  • Type Safety: Compile-time validation

  • Flexibility: Multiple configuration options

  • Automatic Handling: Note selection, change, fees

  • Error Handling: Clear error messages

  • Developer Experience: Fluent, intuitive API

Use the builder for all private transaction creation in your applications.

Last updated