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
- TRON Blockchain: How to Send the USDT (TRC-20) Transacton using Python tronpy?
- How to Get Balance of TRX or USDT/USDD/USDC (TRC-20) Smart Contract Tokens on TRON Blockchain?
- Function to Return the USDT/USDD/USDC Contract Address on Tron Blockchain (Main Net, Nile, Shasta)
- How to Send/Transfer USDT/USDD/USDC on Tron Blockchain using Tronweb?
- Javascript Function to Send Trx on Tron Blockchain based on TronWeb
- How to Generate an Account on Tron Blockchain using Python SDK?
- How to Claim the Witness (Super Representative) Voting Rewards on Tron Blockchain using Node Js (Javascript)?
- Automate Freeze Balance on Tron Blockchain using NodeJs with TronWeb/TronGrid
- Python Tool of Base58 Decode Check
- TRON Blockchain: How to Check the Number of Peers the Tron Node is Connected to?
- TRON Blockchain: How to Check If a Node is synchronized and Fully Functional?
- How to Activate a TRON (TRX) Blockchain Wallet Address?
- Delayed Swap due to Numeric Underflow Bug by using Tron’s triggerSmartContract Method
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
959 wordsloading...
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
Hello, can you update this script for work with new version of tronweb and nodejs?
What errors do you see? It should work.
debi@DESKTOP-C4RTQTD:~/tronweb$ node worker.js
/home/debi/tronweb/worker.js:33
if (tx.Error) {
^
TypeError: Cannot read properties of undefined (reading ‘Error’)
at /home/debi/tronweb/worker.js:33:12
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.13.0
debi@DESKTOP-C4RTQTD:~/tronweb$
Can you share your full source code ?
i copy ur code and replace:
const TrxAccount = “Tron Account”; to trx address
const AppKey = ‘Your APP Key at Tron Grid’ to app key
const privateKey = ‘Your Private Key (Active Key)’; to hex private key
Have you installed tron using “npm install -g tronweb”?
Excuse me, but don’t you need to vote for the Super-Representative in order to receive a reward? Do you just freeze and claim reward every 24 hours?
Btw, you can get the next voting time with this method: https://developers.tron.network/reference/walletgetnextmaintenancetime
I believe, you have to stake and vote in order to earn rewards.