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

in #sct3 years ago

In Tron Blockchain, you can freeze your TRX assets as either energy or bandwidth, and then vote for your preferred witnesses (or called SR = Super Representative).

Each SR has a different ratio of voting distribution, and you can claim the rewards for your witness votes.

With the following Javascript code - you can run it at NodeJs e.g. node or nodejs.

It will first make a API call to trongrid to withdraw-balance, and it will return a JSON - which needs to be properly signed with your private key, and then final step is to broadcast the signed transaction.

const TrxAccount = "Tron Account";
const AppKey = 'Your APP Key at Tron Grid' 
const privateKey = 'Your Private Key (Active Key)';
const fetch = require('node-fetch');
const url = 'https://api.trongrid.io/wallet/withdrawbalance';

const TronWeb = require('tronweb');

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

async function claimRewards() {
  const options = {
    method: 'POST',
    headers: {
        Accept: 'application/json', 
        'Content-Type': 'application/json',
        'TRON-PRO-API-KEY': AppKey,
    },
    body: JSON.stringify({owner_address: TrxAccount, visible: true})
  };
  const res = await fetch(url, options);
  if (res.ok) {  
    return await (res.json());
  }
}

(async function() {
    const tx = await claimRewards();    
    if (tx.Error) {
        console.log(tx.Error);
    } else {
        console.log(tx);
        const signedtxn = await tronWeb.trx.sign(tx, privateKey);        
        const receipt = await tronWeb.trx.sendRawTransaction(signedtxn);
        console.log(receipt);
    }
})();

Please note that you can only claim (withdraw) balance/rewards once every 24 hours. For example:

$ node claim-witness-voting-rewards.js 
{ visible: true,
  txID:
   'd0d64fbdc5bbXXXXXXXXXXXXXXXXXXXXXXXX2d6d8afa2d7c4e366ea',
  raw_data:
   { contract: [ [Object] ],
     ref_block_bytes: '37e4',
     ref_block_hash: '94da0dbee6007793',
     expiration: 1623503307000,
     timestamp: 1623503250641 },
  raw_data_hex:
   '0a0237e4220894daXXXXXXXXXXXXXXXXXXXXXXXXXXXXXa15416e7df7ae2caadc4d984697cf6dd3d483dc9fcce370d1c9d682a02f' }
{ result: true,
  txid:
   'd0d64fbdc5bbfd009674490e101a5XXXXXXXXXXXXXd7c4e366ea',
  transaction:
   { visible: true,
     txID:
      'd0d64fbdc5bbfXXXXXXXXXXXXXXXXXXXXXXXXX7c4e366ea',
     raw_data:
      { contract: [Array],
        ref_block_bytes: '37e4',
        ref_block_hash: '94da0dbee6007793',
        expiration: 1623503307000,
        timestamp: 1623503250641 },
     raw_data_hex:
      '0a0237e42208XXXXXXXXXXXXXXXXXXXXXXX416e7df7ae2caadc4d984697cf6dd3d483dc9fcce370d1c9d682a02f',
     signature:
      [ '19e22bbcf0caeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0755ba72c49cf1d8b594e01' ] } }

$ node claim-witness-voting-rewards.js 
{ Error:
   'class org.tron.core.exception.ContractValidateException : The last withdraw time is 1623503250000, less than 24 hours' }

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:  

Mr @justyy, I have delegated my SP to you, and also chose you as my witness, nice to work with you...

IMG_20210615_155926.jpg

IMG_20210615_155939.jpg

thank you.

今天好像没来点赞

点了。

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 58392.35
ETH 2462.57
USDT 1.00
SBD 2.37