Skip to main content

Building and Sending Transactions

The TransactionBuilder provides a fluent API for constructing, signing, and submitting transactions to the Savitri Network.

Simple Transfer

use savitri_sdk::{Wallet, TransactionBuilder, RpcClient};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = RpcClient::from_url("http://localhost:8545")?;
let wallet = Wallet::from_private_key_hex("your_private_key_hex")?;

// Get current nonce
let nonce = client.get_nonce(wallet.address()).await?;

// Build and sign
let signed_tx = TransactionBuilder::new()
.to("recipient_address_64hex")
.value(1_000_000_000_000_000_000) // 1 SAVT (18 decimals)
.nonce(nonce)
.fee(1_000_000_000_000_000) // 0.001 SAVT fee
.build_and_sign(&wallet)?;

// Submit
let result = client.send_raw_transaction(&serialize_tx(&signed_tx)).await?;
println!("TX hash: {}", result.tx_hash);

Ok(())
}

Builder API

Required Fields

MethodTypeDescription
.from(address)StringSender address (auto-filled from wallet if omitted)
.to(address)StringRecipient address
.value(amount)u128Transfer amount in smallest unit

Optional Fields

MethodTypeDefaultDescription
.nonce(n)u640Transaction nonce (get from get_nonce())
.fee(f)u128NoneTransaction fee
.data(bytes)Vec<u8>NonePayload for contract calls

Build Without Signing

let unsigned = TransactionBuilder::new()
.from("sender_address")
.to("recipient_address")
.value(1000)
.nonce(5)
.build()?;

// unsigned.from, unsigned.to, unsigned.value, unsigned.nonce, unsigned.fee, unsigned.data

Build and Sign

let signed = TransactionBuilder::new()
.to("recipient_address")
.value(1000)
.nonce(5)
.fee(100)
.build_and_sign(&wallet)?;

// signed.transaction -- the UnsignedTransaction
// signed.public_key -- 32-byte ed25519 public key
// signed.signature -- 64-byte ed25519 signature

If .from() is not called, the wallet's address is used automatically.

Oracle Calls

let signed = TransactionBuilder::new()
.oracle_call(
"oracle_contract_address",
"request_data",
b"temperature_sensor_01",
)
.value(0)
.nonce(nonce)
.build_and_sign(&wallet)?;

Governance Calls

Vote on a Proposal

use savitri_sdk::GovernanceAction;

let signed = TransactionBuilder::new()
.governance_call(
"governance_contract_address",
proposal_id, // u64
GovernanceAction::Vote(true), // true = support, false = oppose
)
.nonce(nonce)
.build_and_sign(&wallet)?;

Execute an Approved Proposal

let signed = TransactionBuilder::new()
.governance_call(
"governance_contract_address",
proposal_id,
GovernanceAction::Execute,
)
.nonce(nonce)
.build_and_sign(&wallet)?;

Create an FL Proposal

let signed = TransactionBuilder::new()
.create_fl_proposal(
"governance_contract_address",
"Upgrade model v2", // title
"Replace BERT with GPT-4", // description
604800, // voting period in seconds (7 days)
)
.nonce(nonce)
.build_and_sign(&wallet)?;

Transaction Signing Format

The signing message is constructed as:

from_hex_bytes || to_hex_bytes || amount_le_u64 || nonce_le_u64 || fee_le_u128

Then SHA-256 hashed, and the hash is signed with Ed25519.

This format matches the server-side verification in savitri-rpc.

Amounts and Decimals

All amounts use 18 decimal places:

Human AmountRaw Value
1 SAVT1_000_000_000_000_000_000
0.1 SAVT100_000_000_000_000_000
0.001 SAVT1_000_000_000_000_000
0.00001 SAVT10_000_000_000_000

Fee Reference

Transaction TypeFee (SAVT)Raw Value
Normal transfer0.0011_000_000_000_000_000
Contract call0.0055_000_000_000_000_000
IoT data0.0000550_000_000_000_000