How can I listen transactions in Tron blockchain?

1k views Asked by At

I want to track transactions on the Tron blockchain network. More precisely, I want to follow the transactions made through my own address, but I could not find any way. I tried the following way but it says "subscribe" method is an undefined method. I found some content about Websocket I got Websocket API via QuickNode but it didn't work either. Is there a way I can track transactions on the Tron network? But I don't want to do this by constantly making HTTP requests. I want to be notified when there is a transaction.

I tried the code below and the Websocket method, but no results.

this.web3 = new TronWeb({
  fullHost: this.network.host,
  solidityNode: this.network.host,
  eventServer: this.network.event,
});

this.web3.eventServer.subscribe('transactions', (error, event) => {
  if (error) {
     console.error('Error subscribing to transaction event:', error);
  } else {
     console.log('New transaction:', event);
  }
});
3

There are 3 answers

0
Sahil - QuickNode On

First, the disclaimer, I work with QuickNode.

So you can track transactions coming in and out of your wallet using QuickAlerts from QuickNode.

You have to create an alert using the following expression:

(tx_to == 'your_wallet_address') || (tx_from == 'your_wallet_address')

The tx_to will track transactions coming to your wallet, and the tx_from will track transactions coming from your wallet.

You can set up your desired WebHook location as the destination to receive the transaction information.

To use QuickAlerts with JavaScript, you can use the API of QuickAlerts; here are a few resources:

0
BeycanDeveloper On

When I try the code below for tokens it works. But unfortunately, I still haven't found anything that can capture TRX transactions or all transactions.

(async() => {
    let contract = await 
    tronWeb.contract().at("TXLAQ63Xg1NAzckPwKHvzw7CSEmLMEqcdj");

    contract.Transfer().watch((err, event) => {
        if(err)
            return console.error('Error with "Message" event:', err); 
    
        console.log(event);
    });
})();
0
skoniks On

You can use tron event query to listen all transactions and filter each belongs to your contract address - https://developers.tron.network/docs/use-java-trons-built-in-message-queue-for-event-subscription

var zmq = require("zeromq"),
var sock = zmq.socket("sub");

sock.connect("tcp://127.0.0.1:5555");
sock.subscribe("block");
console.log("Subscriber connected to port 5555");

sock.on("message", function(topic, message) {
  console.log(
    "received a message related to:",
    Buffer.from(topic).toString(),
    ", containing message:",
    Buffer.from(message).toString()
  );
});

Filtering example (without event query) - https://github.com/ahmadbrainworks/tron-events