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


tron-blockchain How to Claim the Witness (Super Representative) Voting Rewards on Tron Blockchain using Node Js (Javascript)? blockchain Cryptocurrency Cryptocurrency javascript nodejs Tron Tron Blockchain

tron-blockchain

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.

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
35
36
37
38
39
40
41
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);
    }
})();
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:

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
$ 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' }
$ 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' }

Tron Blockchain

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
959 words
Last Post: Teaching Kids Programming - Breadth First Search Algorithm to Find Bottom Left Tree Value
Next Post: GoLang: Find Bottom Left Tree Value via Depth First Search or Breadth First Search Algorithm

The Permanent URL is: How to Claim the Witness (Super Representative) Voting Rewards on Tron Blockchain using Node Js (Javascript)?

8 Comments

  1. Tronex
      • Tronex
          • Tronex
  2. Boris

Leave a Reply