--- sidebar_position: 1 title: Staking V2 --- import StakingV2Guide from '@site/src/components/_staking-v2-guide.mdx'; # Staking V2 Guide TRON's Stake 2.0 (V2) system replaces the legacy Stake 1.0 model, providing more flexibility for resource management and delegation. This guide covers all V2 staking operations available in TronWeb. :::tip Stake 2.0 was introduced in JAVA-TRON v4.7.0.1. All new staking operations should use V2 methods. V1 methods (`freezeBalance`/`unfreezeBalance`) are deprecated. ::: ## Overview | Feature | Stake 1.0 (V1) | Stake 2.0 (V2) | |---------|----------------|----------------| | Freeze | `freezeBalance` | `freezeBalanceV2` | | Unfreeze | `unfreezeBalance` | `unfreezeBalanceV2` + `withdrawExpireUnfreeze` | | Cancel Unfreeze | N/A | `cancelUnfreezeBalanceV2` | | Resource Delegation | Built into freeze | Separate `delegateResource` | | Flexibility | All-or-nothing | Partial freeze/unfreeze | ## Freeze TRX Freeze TRX to obtain **BANDWIDTH** or **ENERGY** resources. ```javascript const tx = await tronWeb.transactionBuilder.freezeBalanceV2( 1000000000, // amount in SUN (1000 TRX) 'ENERGY', // resource type: 'BANDWIDTH' or 'ENERGY' ); const signedTx = await tronWeb.trx.sign(tx); const receipt = await tronWeb.trx.sendRawTransaction(signedTx); ``` **Parameters:** | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | `amount` | number | 0 | Amount of TRX to freeze, in SUN | | `resource` | string | `'BANDWIDTH'` | Resource type: `'BANDWIDTH'` or `'ENERGY'` | | `address` | string | Default address | Owner address | | `options` | object | `{}` | Transaction options (e.g., `{ permissionId }`) | ## Unfreeze TRX Unfreeze TRX in two steps: ### Step 1: Initiate Unstaking ```javascript // Start the unstaking process (14-day waiting period) const tx = await tronWeb.transactionBuilder.unfreezeBalanceV2( 500000000, // amount in SUN (500 TRX) 'ENERGY', // resource type ); const signedTx = await tronWeb.trx.sign(tx); const receipt = await tronWeb.trx.sendRawTransaction(signedTx); ``` ### Step 2: Withdraw After Waiting Period After the 14-day waiting period, withdraw the unfrozen TRX: ```javascript const tx = await tronWeb.transactionBuilder.withdrawExpireUnfreeze(); const signedTx = await tronWeb.trx.sign(tx); const receipt = await tronWeb.trx.sendRawTransaction(signedTx); ``` ### Cancel Unstaking Cancel a pending unstaking operation before the waiting period expires: ```javascript const tx = await tronWeb.transactionBuilder.cancelUnfreezeBalanceV2(); const signedTx = await tronWeb.trx.sign(tx); const receipt = await tronWeb.trx.sendRawTransaction(signedTx); ``` ## Resource Delegation Delegate your frozen resources (bandwidth or energy) to another account. ### Delegate Resources ```javascript const tx = await tronWeb.transactionBuilder.delegateResource( 1000000000, // amount in SUN 'TReceiverAddr', // receiver address 'ENERGY', // resource type 'TOwnerAddr', // owner address (optional) false, // lock delegation (optional) 0, // lock period (optional) ); const signedTx = await tronWeb.trx.sign(tx); const receipt = await tronWeb.trx.sendRawTransaction(signedTx); ``` **Parameters:** | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | `amount` | number | 0 | Amount to delegate, in SUN | | `receiverAddress` | string | - | Receiver's address | | `resource` | string | `'BANDWIDTH'` | Resource type | | `address` | string | Default address | Owner address | | `lock` | boolean | `false` | Whether to lock the delegation | | `lockPeriod` | number | - | Lock period in blocks (optional) | | `options` | object | `{}` | Transaction options | :::caution The receiver address must be different from the owner address. ::: ### Undelegate Resources ```javascript const tx = await tronWeb.transactionBuilder.undelegateResource( 500000000, // amount in SUN 'TReceiverAddr', // address to undelegate from 'ENERGY', // resource type ); const signedTx = await tronWeb.trx.sign(tx); const receipt = await tronWeb.trx.sendRawTransaction(signedTx); ``` ## Query Delegation Info ### Get Delegated Resources ```javascript // Get delegation details between two addresses const result = await tronWeb.trx.getDelegatedResourceV2(fromAddress, toAddress); console.log(result.delegatedResource); // { // from: 'address', // to: 'address', // frozen_balance_for_bandwidth: 0, // frozen_balance_for_energy: 1000000000, // ... // } ``` ### Get Delegation Index ```javascript // Get all accounts involved in delegations const result = await tronWeb.trx.getDelegatedResourceAccountIndexV2(address); console.log(result.fromAccounts); // Accounts that delegated to this address console.log(result.toAccounts); // Accounts this address delegated to ``` ### Get Maximum Delegatable Amount ```javascript const result = await tronWeb.trx.getCanDelegatedMaxSize(address, 'ENERGY'); console.log(result.max_size); // Maximum amount that can be delegated ``` ### Get Withdrawable Amount ```javascript // Check how much TRX can be withdrawn after unstaking const result = await tronWeb.trx.getCanWithdrawUnfreezeAmount(address); console.log(result.amount); // Amount available for withdrawal ``` ### Get Available Unfreeze Count ```javascript // Check remaining unstaking slots const result = await tronWeb.trx.getAvailableUnfreezeCount(address); console.log(result.count); // Remaining unstaking times ``` ## Complete Staking Workflow Example ```javascript import TronWeb from 'tronweb'; const tronWeb = new TronWeb({ fullHost: 'https://api.trongrid.io', privateKey: 'your-private-key', }); // 1. Freeze 1000 TRX for ENERGY const freezeTx = await tronWeb.transactionBuilder.freezeBalanceV2( 1000_000_000, 'ENERGY' ); const signedFreeze = await tronWeb.trx.sign(freezeTx); await tronWeb.trx.sendRawTransaction(signedFreeze); // 2. Delegate 500 TRX of ENERGY to another account const delegateTx = await tronWeb.transactionBuilder.delegateResource( 500_000_000, 'TReceiverAddress', 'ENERGY' ); const signedDelegate = await tronWeb.trx.sign(delegateTx); await tronWeb.trx.sendRawTransaction(signedDelegate); // 3. Check delegation details const delegation = await tronWeb.trx.getDelegatedResourceV2( tronWeb.defaultAddress.base58, 'TReceiverAddress' ); console.log('Delegated:', delegation); // 4. Undelegate resources const undelegateTx = await tronWeb.transactionBuilder.undelegateResource( 500_000_000, 'TReceiverAddress', 'ENERGY' ); const signedUndelegate = await tronWeb.trx.sign(undelegateTx); await tronWeb.trx.sendRawTransaction(signedUndelegate); // 5. Start unstaking (14-day waiting period begins) const unfreezeTx = await tronWeb.transactionBuilder.unfreezeBalanceV2( 1000_000_000, 'ENERGY' ); const signedUnfreeze = await tronWeb.trx.sign(unfreezeTx); await tronWeb.trx.sendRawTransaction(signedUnfreeze); // 6. After 14 days, withdraw unfrozen TRX const withdrawTx = await tronWeb.transactionBuilder.withdrawExpireUnfreeze(); const signedWithdraw = await tronWeb.trx.sign(withdrawTx); await tronWeb.trx.sendRawTransaction(signedWithdraw); ``` ## Related API References - [transactionBuilder.freezeBalanceV2](../API%20List/transactionBuilder/freezeBalanceV2.md) - [transactionBuilder.unfreezeBalanceV2](../API%20List/transactionBuilder/unfreezeBalanceV2.md) - [transactionBuilder.cancelUnfreezeBalanceV2](../API%20List/transactionBuilder/cancelUnfreezeBalanceV2.md) - [transactionBuilder.delegateResource](../API%20List/transactionBuilder/delegateResource.md) - [transactionBuilder.undelegateResource](../API%20List/transactionBuilder/undelegateResource.md) - [transactionBuilder.withdrawExpireUnfreeze](../API%20List/transactionBuilder/withdrawExpireUnfreeze.md) - [trx.getDelegatedResourceV2](../API%20List/trx/getDelegatedResourceV2.md) - [trx.getDelegatedResourceAccountIndexV2](../API%20List/trx/getDelegatedResourceAccountIndexV2.md) - [trx.getCanDelegatedMaxSize](../API%20List/trx/getCanDelegatedMaxSize.md) - [trx.getCanWithdrawUnfreezeAmount](../API%20List/trx/getCanWithdrawUnfreezeAmount.md) - [trx.getAvailableUnfreezeCount](../API%20List/trx/getAvailableUnfreezeCount.md)