Automate Freeze (Stake) Balance on Tron Blockchain using NodeJs with TronWeb/TronGrid


tron-blockchain Automate Freeze (Stake) Balance on Tron Blockchain using NodeJs with TronWeb/TronGrid blockchain Cryptocurrency javascript nodejs Tron Tron Blockchain

tron-blockchain

We want to freeze the available balance on Tron Blockchain account: this involves first getting available balance using TronWeb NodeJs Library, and then build a transaction using the Transaction Builder, sign the Transaction, and finally broadcast it.

You can get a Free API Key on TronGrid. And you can install the tronweb library using npm.

See following NodeJs code that freeze available balance (aka Stake TRX) on the given Tron wallet – you can do this anytime. The voting round is 6 hours on TRON blockchain.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const TrxAccount = "Tron Account";
const AppKey = 'Tron-Grid-App-Key' 
const privateKey = 'Private Key';
 
const TronWeb = require('tronweb');
 
const tronWeb = new TronWeb({
    fullHost: 'https://api.trongrid.io',
    headers: { "TRON-PRO-API-KEY": AppKey },
    privateKey: privateKey,
});
 
function getBalance(acc) {
    return new Promise((resolve, reject) => {
        tronWeb.trx.getBalance(acc).then(result => {
            resolve(result);
        })
    });
}
 
(async function() {
    const sun = await(getBalance(TrxAccount));
    const trx = tronWeb.fromSun(sun);
    console.log("Balance of SUN = " + sun + " or " + trx + " TRX");
    if (trx >= 1) {
        const trans = await tronWeb.transactionBuilder.freezeBalance(sun, 3, "ENERGY", TrxAccount, TrxAccount, 1);
        const signedtxn = await tronWeb.trx.sign(trans, privateKey);        
        const receipt = await tronWeb.trx.sendRawTransaction(signedtxn);
        console.log(receipt);
        console.log(trx + " TRX frozon!");
    } else {
        console.log("Not enough to Freeze!")
    } 
})();
const TrxAccount = "Tron Account";
const AppKey = 'Tron-Grid-App-Key' 
const privateKey = 'Private Key';

const TronWeb = require('tronweb');

const tronWeb = new TronWeb({
    fullHost: 'https://api.trongrid.io',
    headers: { "TRON-PRO-API-KEY": AppKey },
    privateKey: privateKey,
});

function getBalance(acc) {
    return new Promise((resolve, reject) => {
        tronWeb.trx.getBalance(acc).then(result => {
            resolve(result);
        })
    });
}

(async function() {
    const sun = await(getBalance(TrxAccount));
    const trx = tronWeb.fromSun(sun);
    console.log("Balance of SUN = " + sun + " or " + trx + " TRX");
    if (trx >= 1) {
        const trans = await tronWeb.transactionBuilder.freezeBalance(sun, 3, "ENERGY", TrxAccount, TrxAccount, 1);
        const signedtxn = await tronWeb.trx.sign(trans, privateKey);        
        const receipt = await tronWeb.trx.sendRawTransaction(signedtxn);
        console.log(receipt);
        console.log(trx + " TRX frozon!");
    } else {
        console.log("Not enough to Freeze!")
    } 
})();

The balance is in the unit of SUN – which you can convert to TRX using tronWeb.fromSun. And the amount needs to be at last 1 TRX to freeze.

Example output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Balance of SUN = 12301995 or 12.301995 TRX
{ result: true,
  txid:
   '77c74b7e76XXXXXXXXXXXXe0f6829b74ed',
  transaction:
   { visible: false,
     txID:
      '77c74b7e76cXXXXXXXXXXXXXXXXXXXXXXXXd0656e0f6829b74ed',
     raw_data:
      { contract: [Array],
        ref_block_bytes: '356b',
        ref_block_hash: '5aad1c587783fe84',
        expiration: 1623501405000,
        timestamp: 1623501348040 },
     raw_data_hex:
      '0a0235XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2caadc4d984697cf6dd3d483dc9fcce310abedee051803500170c8b9e281a02f',
     signature:
      [ '5316c2f0XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbcb8f366184d135dda4c01' ] } }
12.301995 TRX frozon!
Balance of SUN = 12301995 or 12.301995 TRX
{ result: true,
  txid:
   '77c74b7e76XXXXXXXXXXXXe0f6829b74ed',
  transaction:
   { visible: false,
     txID:
      '77c74b7e76cXXXXXXXXXXXXXXXXXXXXXXXXd0656e0f6829b74ed',
     raw_data:
      { contract: [Array],
        ref_block_bytes: '356b',
        ref_block_hash: '5aad1c587783fe84',
        expiration: 1623501405000,
        timestamp: 1623501348040 },
     raw_data_hex:
      '0a0235XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2caadc4d984697cf6dd3d483dc9fcce310abedee051803500170c8b9e281a02f',
     signature:
      [ '5316c2f0XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbcb8f366184d135dda4c01' ] } }
12.301995 TRX frozon!

Stake TRX (Freeze), and then Upvote the SR (Super Representatives) aka the Witnesses/Miners on Tron Blockchain, and then claim the rewards once every 24 hours. And with the earned TRX, you can invest and stake again to earn more.

See also: How to Claim the Witness (Super Representative) Voting Rewards on Tron Blockchain using Node Js (Javascript)?

Tron Blockchain

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
a WordPress rating system
963 words
Last Post: Teaching Kids Programming - Max Product of Two Numbers
Next Post: Teaching Kids Programming - Breadth First Search Algorithm to Find Bottom Left Tree Value

The Permanent URL is: Automate Freeze (Stake) Balance on Tron Blockchain using NodeJs with TronWeb/TronGrid

Leave a Reply