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

in #programming3 years ago (edited)

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 on the given Tron wallet - you can do this every 24 hours.

const TrxAccount = "TL3SEY2dn4VfXXXXXXXXXXXXXXXXXXXXX5nte";
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:

Balance of SUN = 12301995 or 12.301995 TRX
{ result: true,
  txid:
   '77c74b7e76c12a61f10XXXXXXXXXXXXXXXXXXXXXXXXXX0f6829b74ed',
  transaction:
   { visible: false,
     txID:
      '77c74b7e76c12a61f10XXXXXXXXXXXXX5d0656e0f6829b74ed',
     raw_data:
      { contract: [Array],
        ref_block_bytes: '356b',
        ref_block_hash: '5aad1c587783fe84',
        expiration: 1623501405000,
        timestamp: 1623501348040 },
     raw_data_hex:
      '0a023XXXXXXXXXXXXXXXXXXXXXXXXXXXX1a02f',
     signature:
      [ '5316c2f09XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXda4c01' ] } }
12.301995 TRX frozon!

Reposted to Blog

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^

NEW! Following my Trail (Upvote or/and Downvote)

Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com

My contributions

Delegation Service

  1. Voting Algorithm Updated to Favor those High Delegations!
  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
    Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

Sort:  

Your original post was deleted or renamed thus the voting were failed . Anyway, giving you a upvote here.

I see. Thanks for the reply.