PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 22, 2022

[FIXED] How to get TRC20 transactions to an address

 September 22, 2022     blockchain, tron, tronweb     No comments   

Issue

I am using tron web to query transactions of an address but it does not return transactions sent to that address where token transferred is TRC20.

This does not work. I want to get the transactions on an address and get both TRX, trc10 and trc20 transactions.

What am I doing wrong or how to do that?

Here is my code block:

  tronWeb.setDefaultBlock("latest");
  var result = await tronGrid.account.getTransactions(address, {
    only_confirmed: true,
    only_to: true,
    limit: 10
  });
  console.log(JSON.stringify(result));
})();

Solution

After a lot of research, I found out one can easily query contract events at intervals to get transactions on that contract address and you can then filter it for the address you are watching since you can't get a webhook or websocket with your trongrid/tronweb implementation.

Here is a sample file I used to achieve this and it works great for monitoring many address even with different contract addresses.

Note: In my own implementation, this node file is called from another file and other logistics are handled in the other file, but below you see how I queried the transfer events emitted by the specified contract

const TronWeb = require("tronweb");
const TronGrid = require("trongrid");

const tronWeb = new TronWeb({
  fullHost: "https://api.trongrid.io"
});
const tronGrid = new TronGrid(tronWeb);
const argv = require("minimist")(process.argv.slice(2));
var contractAddress = argv.address;
var min_timestamp = Number(argv.last_timestamp) + 1; //this is stored for the last time i ran the query
(async function() {
  tronWeb.setDefaultBlock("latest");
  tronWeb.setAddress("ANY TRON ADDRESS"); // maybe being the one making the query not necessarily the addresses for which you need the transactions

  var result = await tronGrid.contract.getEvents(contractAddress, {
    only_confirmed: true,
    event_name: "Transfer",
    limit: 100,
    min_timestamp: min_timestamp,
    order_by: "timestamp,asc"
  });
  result.data = result.data.map(tx => {
    tx.result.to_address = tronWeb.address.fromHex(tx.result.to); // this makes it easy for me to check the address at the other end
    return tx;
  });
  console.log(JSON.stringify(result));
})();

You are free to customize the config data passed to the tronGrid.contract.getEvents method. Depending on how frequently transactions come on the contract you are monitoring you should DYOR to know at what interval is great for you and what limit value you should pass.

Refer to https://developers.tron.network/docs/trongridjs for details.



Answered By - Oluwatumbi
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing