Skip to content
Logo

Staking (Stake 2.0)

On TRON you obtain network resources — energy and bandwidth — by staking ("freezing") TRX. tronz implements the Stake 2.0 model with a set of lazy builders.

Freeze (stake)

freeze_balance() stakes TRX for a resource. It defaults to Energy.

use tronz::Trx;
use tronz::primitives::ResourceCode;
 
# async fn run(provider: impl tronz::TronProvider) -> anyhow::Result<()> {
let pending = provider
    .freeze_balance()
    .amount("10".parse()?)
    .resource(ResourceCode::Energy)
    .send()
    .await?;
 
let info = pending.get_receipt().await?;
println!("status: {:?}", info.status);
# Ok(()) }

Delegate

Once staked, you can delegate the resulting resource to another account with delegate_resource(). Cap the amount to what's actually delegatable using get_can_delegate_max:

use tronz::Trx;
use tronz::primitives::ResourceCode;
 
# async fn run(provider: impl tronz::TronProvider, me: tronz::Address, receiver: tronz::Address) -> anyhow::Result<()> {
let max = provider.get_can_delegate_max(me, ResourceCode::Energy).await?;
let amount = "10".parse::<Trx>()?.min(max);
 
let pending = provider
    .delegate_resource()
    .resource(ResourceCode::Energy)
    .amount(amount)
    .to(receiver)
    .send()
    .await?;
# Ok(()) }

You can optionally lock a delegation for a number of seconds with .lock_period(secs) (max 864_000 per protocol). Reclaim a delegation with undelegate_resource() (using .from(addr) for the delegatee).

Claim rewards

claim_rewards() withdraws accrued block/vote rewards. TRON permits this at most once per 24 hours per account, so check the pending reward first:

# async fn run(provider: impl tronz::TronProvider, me: tronz::Address) -> anyhow::Result<()> {
let reward = provider.get_reward(me).await?;
println!("pending reward: {}", reward);
 
if reward.as_sun() > 0 {
    let pending = provider.claim_rewards().send().await?;
    let info = pending.get_receipt().await?;
    println!("claimed: {:?}", info.status);
}
# Ok(()) }

Unfreeze (unstake)

unfreeze_balance() begins releasing a stake. The funds enter an unbonding window; once it expires, withdraw them with withdraw_expire_unfreeze().

use tronz::Trx;
use tronz::primitives::ResourceCode;
 
# async fn run(provider: impl tronz::TronProvider) -> anyhow::Result<()> {
// Start unstaking.
provider
    .unfreeze_balance()
    .amount("10".parse()?)
    .resource(ResourceCode::Energy)
    .send()
    .await?;
 
// Later, after the unbonding window expires:
provider.withdraw_expire_unfreeze().send().await?;
# Ok(()) }

See the full runnable version in Examples → Staking.