How to listen for transactions on Ethereum or Polygon

mbvissers.eth
Quick Programming
Published in
4 min readSep 2, 2022

--

Listening to transactions on the blockchain from a specific smart contract.

Photo by Franco Antonio Giovanella on Unsplash

The blockchain is an amazing tool for all sorts of businesses ranging from artists to finance. One of these businesses is the lovely world of DeFi and everything that comes with it. Front running, flash loans, farming arbitrage, and everything else.

And for some of these, or for your Discord bots, Twitter bots, etc. you want to be able to listen to transactions with an unknown ID, but coming from a known smart contract. And this is what we’ll do in this article.

Listening to transactions with a known hash

I have written another article before about how to listen to transaction in which the transaction hash is known, for example waiting for an NFT to mint, or tokens to approved before they can be staked. If you want to know how that works you can click the link below.

But if you want to listen to transaction with an unknown hash, for instance, running a bot that will tweet for each transfer / sale that happens from your NFT project, then you should continue reading.

Listening to transactions with an unknown hash

As with my previous article, I will be using plain JavaScript together with the HTTP Server package from NPM. We don’t need to access MetaMask this time since we will listen to transactions from any well-known project. This can be UniSwap, BAYC, Pixel Pizzas, or whatever. As long as it’s on an EVM chain like Ethereum or Polygon.

I’ll go with Ethereum and UniSwap because it’s getting a fair amount of transactions to test with. Now let’s create a simple webpage to show our transactions on.

Our minimal HTML will look like this.

We import Web3JS from a CDN, we load a scripts.js file that we need to create in which to put our JavaScript code, and we have a div in which we can show the events so we don’t have to console.log everything.

To connect to Web3JS, we need an Ethereum node. I’m using Infura which is free for small projects. You need to create a project to get a node provider URL. Infura offers WebSocket URLs as well, what is what we’ll be using.

If you load your HTML file you will notice loaded and the web3 object in your console. The next step is to actually subscribe to an address to get all transactions from it. We can use web3.eth.subscribe() for this.

When you reload the HTML page now, you should be getting some transaction objects in your console. It might take a few minutes depending on how busy the smart contract is.

The object contains some information, including the tx hash. You can copy this and check the transaction on Etherscan. Then you can already see who sent the transaction, what they did, and the block number of the transaction.

Getting more Information

We now know the txHash of new transactions, but how do we get any more data? Like who sent the transaction, or what action was performed?

We can use the transaction hash to get the transaction receipt. In this there’s a bit more information.

This gives us access to the following data of any mined transaction.

And while some of this is already known to us, we now also have the from field, so we know who sent the transaction and the gas used. But we still don’t know how much value was sent. We can use getTransaction for that.

getTransaction gives us more info, but it gives it for unmined transactions.

This now gives us access to more information like from , to , value , nonce , and more.

Conclusion

I hope that this was useful for some of you! Please check out my other articles on topics ranging from smart contracts to basic programming if you’ve enjoyed this one!

Thank you so much for reading and have an excellent day.

Consider supporting me by getting a Medium membership. It helps me out a lot, it won’t cost you anything extra, and you can read as many Medium articles as you like!

Follow me on Twitter and gm.xyz to keep up with me and my projects.

Check out Pixel Pizzas on the Polygon Blockchain.

--

--

mbvissers.eth
Quick Programming

I occasionally write about programming and smart contracts. Follow me on Twitter @0xmbvissers