最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I listen transactions in Tron blockchain? - Stack Overflow

programmeradmin3浏览0评论

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: thiswork.host,
  solidityNode: thiswork.host,
  eventServer: thiswork.event,
});

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

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: thiswork.host,
  solidityNode: thiswork.host,
  eventServer: thiswork.event,
});

this.web3.eventServer.subscribe('transactions', (error, event) => {
  if (error) {
     console.error('Error subscribing to transaction event:', error);
  } else {
     console.log('New transaction:', event);
  }
});
Share Improve this question edited Oct 10, 2024 at 5:31 BeycanDeveloper asked Jun 23, 2023 at 18:02 BeycanDeveloperBeycanDeveloper 751 silver badge9 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 2

You can use tron event query to listen all transactions and filter each belongs to your contract address - https://developers.tronwork/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./ahmadbrainworks/tron-events

you can use Bitquery Tron APIs.

Test out this API on Bitquery IDE, you can easily integrate API into your code. Check out the Bitquery docs.

Bitquery Tron docs - https://docs.bitquery.io/docs/category/tron/

API looks like this-

subscription {
  Tron {
    Transactions{
      Block {
        Hash
        Time
        Number
      }
      Contract {
        Address
      }
      ChainId
      Transaction {
        Fee
        Hash
        FeePayer
        Signatures
        Result {
          Success
          Status
          Message
        }
        Time
      }
    }
  }
}

Its a graphQL format, so remove whatever field you don't need, there are many more fields that you can add just check Bitquery API builder on the left for more fields.

Full Disclosure - I work at Bitquery.

First, the disclaimer, I work with QuickNode.

So you can track transactions ing 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 ing to your wallet, and the tx_from will track transactions ing 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:

  • [Video] Get Started with QuickAlerts
  • [Video] How to track wallet transactions in JavaScript and QuickAlerts REST API
  • [Guide] Get started with QuickAlerts
  • [Guide] QuickAlerts REST API documentation

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);
    });
})();

This was a major shorting for the Tron Blockchain network and there was no easy solution. The only way was to build your own full node, which was a really costly process both in time and money. Based on the problem I had here, I created TronSocket. and now listening to Tron Blockchain events is a piece of cake, as in the example below.

import TronSocket, { Transaction } from "@tronsocket/sdk/node";

const socket = new TronSocket("your_token_here");

await socket.connect().then((connected) => {
    console.log("Connected:", connected);
}).catch((error) => {
    console.error("Error:", error);
});

// All transactions
socket.on("transaction", (tx: Transaction) => {
    const id = tx.transactionId;
});

const usdtAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'

// Only USDT transactions
socket.on("transaction", (tx: Transaction) => {
    const id = tx.transactionId;
}, { toAddress: usdtAddress });
发布评论

评论列表(0)

  1. 暂无评论