8

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));
})();
Oluwatumbi
  • 1,321
  • 1
  • 12
  • 19

3 Answers3

12

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Oluwatumbi
  • 1,321
  • 1
  • 12
  • 19
  • Is there a way to get all transfer events for TRC20 (example USDT), not only for one particular user (Address)? – s k Sep 24 '22 at 09:28
  • 1
    This is exactly what this answer explains. The `tronGrid.contract.getEvents(contractAddress, {...})` fetches the events. – Oluwatumbi Sep 25 '22 at 16:19
0

I found a API that can take TRC20 transactions, but I haven't found an implementation in webtron.

https://api.shasta.trongrid.io/v1/accounts/address/transactions

Related document: https://developers.tron.network/reference#transaction-information-by-account-address

quita
  • 93
  • 6
0
var request = require('request');
var contract = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; //usdt
var wallet = "TQiC7LJUtnagUcwzcc6ULRmXiHipf4Tbou"; //publicKey
var url = "https://api.trongrid.io/v1/accounts/" + wallet + "/transactions/trc20?limit=100&contract_address=" + contract;
request({
    headers: {
        "TRON-PRO-API-KEY": '63d4cba8-xxx-ab8d4c91834e'
    },
    uri: url,
    method: 'GET'
}, function (err, res, body) {
        var transcol = JSON.parse(body).data;//list of last 100 wallet usdt transactions
        var sentArray = transcol.filter(function (el) {
            return el.to == wallet
        });//list of last recived usdt transactions in to wallet

        var lastReciveValue = (parseInt(sentArray[0].value) / 1000000);
        var lastReciveTransid = sentArray[0].transaction_id;

        console.log('last transaction: ' + lastReciveValue + ' USDT');
        console.log("https://tronscan.org/#/transaction/" + lastReciveTransid);
});
  • transcol : list of latest 100 usdt transactions that related to the wallet
  • sentArray : list of latest recived usdt transactions in to the wallet
  • limit=100 : you can remove this to get all related transactions to the wallet, or change it to custom value.
  • contract_address= : you can remove it to get transactions in all type of currencies.
  • lastReciveValue : the last usdt value that sent to the wallet
  • lastReciveTransid : the last transaction id that sent usdt to the wallet.

sample of return json object:

"transaction_id":"b02049daa9a0de0e10187dfa4d1ce560c3e8dd23625e92d2adfcf242e1af5891",
"token_info":{
"symbol":"USDT",
"address":"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
"decimals":6,
"name":"Tether USD"
},
"block_timestamp":1689029211000,
"from":"TQiC7LJUtnagUcwzcc6ULRmXiHipf4Tbou",
"to":"TAiZ8qT2ggJDCRf6xutDKSnEjiCnFDm22K",
"type":"Transfer",
"value":"5000000"
}
Eyni Kave
  • 1,113
  • 13
  • 23