How to listen for transactions on Ethereum or Polygon
Listening to transactions on the blockchain from a specific smart contract.
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.
Give users feedback on transactions.
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
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
div in which we can show the events so we don’t have to
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
nonce , and more.
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.