Crate soroban_client

Source
Expand description

A rust client library for interacting with Soroban smart contracts on the stellar blockchain through the Stellar RPC

§Example: Create account on testnet and fetch balance using simulation

 #[tokio::main]
async fn main() {
    let rpc = Server::new("https://soroban-testnet.stellar.org", Options::default()).unwrap();

    let native_id = "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC";
    let native_sac = Contracts::new(native_id).unwrap();

    // Generate an account
    let kp = Keypair::random().unwrap();

    // Create the account using friendbot
    let account = rpc.request_airdrop(&kp.public_key()).await.unwrap();

    let source_account = Rc::new(RefCell::new(
        Account::new(&kp.public_key(), &account.sequence_number()).unwrap(),
    ));

   
    let account_address = Address::new(&kp.public_key()).unwrap();
    let tx = TransactionBuilder::new(source_account, Networks::testnet(), None)
        .fee(1000u32)
        .add_operation(
            native_sac.call(
                "balance",
                Some(vec![account_address.to_sc_val().unwrap()])))
        .build();

    let response = rpc.simulate_transaction(tx, None).await.unwrap();
    if let Some((ScVal::I128(Int128Parts { hi, lo }), _auth)) = response.to_result() {
        // Divide to convert from stroops to XLM
        let balance = i128_from_pieces(hi, lo) / 10000000;
        println!("Account {} has {} XLM", kp.public_key(), balance);
    }
}

§Example: Fetching last 3 transfer events from the native asset contract on testnet

#[tokio::main]
async fn main() {
    let rpc = Server::new("https://soroban-testnet.stellar.org", Options::default()).unwrap();

    let native_id = "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC";

    let response = rpc.get_latest_ledger().await.unwrap();
    let ledger = response.sequence;

    let transfer = ScVal::Symbol(ScSymbol("transfer".try_into().unwrap()));
    let native = ScVal::String(ScString("native".try_into().unwrap()));
    let events = rpc
        .get_events(
            Pagination::From(ledger - 100),
            vec![EventFilter::new(crate::soroban_rpc::EventType::All)
                .contract(native_id)
                .topic(vec![
                    Topic::Val(transfer),
                    Topic::Any, // From account
                    Topic::Any, // To account
                    Topic::Val(native),
                ])],
            3
        )
        .await
        .unwrap();

    println!("{:?}", events);
}

Modules§

account
Account represents a single account in the Stellar network and its sequence number. Create a new Account object.
address
Address represents a single address in the Stellar network.
asset
Asset class represents an asset, either the native asset (XLM) or an asset code / issuer account ID pair
claimant
contract
Contract represents a single contract in the Stellar network
error
Error module
get_liquidity_pool
hashing
Utility Sha256 Hash Function
keypair
Keypair represents public (and secret) keys of the account.
liquidity_pool_asset
liquidity_pool_id
memo
muxed_account
network
Contains passphrases for common networks
operation
Operations are individual commands that modify the ledger.
signer_key
signing
This module provides the signing functionality used by the stellar network
soroban
soroban_data_builder
soroban_rpc
Soroban bindings
transaction
Transaction module
transaction_builder
Builder pattern to construct new transactions that interact with Stellar environment
utils
Util Functions
xdr
Re-exporting XDR from stellar-xdr

Structs§

EventFilter
List of filters for the returned events. Events matching any of the filters are included. To match a filter, an event must match both a contractId and a topic. Maximum 5 filters are allowed per request.
Options
Additionnal options
ResourceLeeway
Contains configuration for how resources will be calculated when simulating transactions.
Server
The main struct to use to interact with the stellar RPC

Enums§

Durability
Representation of the ledger entry durability to be used with Server::get_contract_data
Pagination
Set the boundaries while fetching data from the RPC
Topic
Topic to match on in the filter

Constants§

SUBMIT_TRANSACTION_TIMEOUT
The default transaction submission timeout for RPC requests, in milliseconds.

Statics§

VERSION
Current version of this crate