4 releases (2 breaking)

0.3.2 Sep 12, 2024
0.3.1 Aug 26, 2024
0.2.0 Mar 11, 2023
0.1.1 Feb 17, 2023

#2 in #tron

Download history 280/week @ 2024-08-23 37/week @ 2024-08-30 82/week @ 2024-09-06 121/week @ 2024-09-13 44/week @ 2024-09-20 17/week @ 2024-09-27 57/week @ 2024-10-04 38/week @ 2024-10-11 11/week @ 2024-10-18 5/week @ 2024-10-25 20/week @ 2024-11-01 33/week @ 2024-11-08 43/week @ 2024-11-15 84/week @ 2024-11-22 13/week @ 2024-11-29 51/week @ 2024-12-06

193 downloads per month

MIT license

51KB
1K SLoC

Crates.io Crates.io

Description

Rust-idiomatic Tron API client library.

Supported features

Features Support
Transaction signing & broadcasting
Smart contract calls
Basic network querying
Staking TRX for energy and bandwidth
Offline transaction signing
Offline transaction encoding (without CreateTransaction API)
Voting & Proposals

Structure

Crate Description
heliosphere Main crate
heliosphere-core Core types, no_std compatible but alloc required
heliosphere-signer Transaction signing utils, no_std compatible but alloc required

TRC20 transfer example

let api = "https://api.shasta.trongrid.io";
let keypair = Keypair::from_hex_key(
    std::fs::read_to_string(".key")
        .expect("no ./.key found")
        .trim(),
)
.unwrap();
let client = RpcClient::new(api).unwrap();
let from = keypair.address();
let to: Address = "<transfer-to-address>".parse().unwrap();
let usdt: Address = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs".parse().unwrap(); // shasta testnet USDT
let amount: u64 = 1; // 0.000001 USDT

// Fetch account balance
let method_call_balance = MethodCall {
    caller: &from,
    contract: &usdt,
    selector: "balanceOf(address)",
    parameter: &ethabi::encode(&[Token::Address(from.into())]),
};
let res = &ethabi::decode(
    &[ParamType::Uint(256)],
    &client
        .query_contract(&method_call_balance)
        .await
        .unwrap()
        .constant_result(0)
        .unwrap(),
)
.unwrap()[0];
let current_balance = match res {
    Token::Uint(x) => x,
    _ => panic!("Wrong type"),
};
println!("Balance: {}", current_balance);

// Transfer tokens
let method_call = MethodCall {
    caller: &from,
    contract: &usdt,
    selector: "transfer(address,uint256)",
    parameter: &ethabi::encode(&[Token::Address(to.into()), Token::Uint(U256::from(amount))]),
};
// Estimate energy usage
let estimated = client.estimate_energy(&method_call).await.unwrap();
println!("Estimated energy usage: {}", estimated);
// Send tx
let mut tx = client
    .trigger_contract(&method_call, 0, None)
    .await
    .unwrap();
keypair.sign_transaction(&mut tx).unwrap();
let txid = client.broadcast_transaction(&tx).await.unwrap();
println!("Txid: {}", txid);
println!("Confirming...");
client.await_confirmation(txid).await.unwrap();

License

This project is licensed under the MIT license.

Dependencies

~14–25MB
~365K SLoC