Reputation: 345
I'm new to blockchain development. I have a requirement where for one hour, I need to monitor an Eth address and detect any transactions, upon which I will do some logic.
The simplest way is to brute force it, like for every minute, get the transactions for this address and see the difference. But for hundreds of addresses at once this is a very bad idea.
I've been looking for a more elegant way of handling this. I've looked into web hooks, subscriptions, web sockets, but can't find anything that includes Polygon Mainnet and Mumbai.
This monitoring service would need to be able to interact with a database. The idea is that for one hour, we monitor an address, we see a new transaction, and we send something to the database and stop the monitoring. What could be a solution for this?
Thanks.
Upvotes: 0
Views: 953
Reputation: 345
I ended up using Nethereum's API for this. I set up a Web3, an EventDTO to model the Smart Contract function that I was trying to track, and a FilterInput just for the address I was concerned about. I used a StreamingWebSocketClient with a public web socket URL for Matic Testnet, and from there I just tracked the logs with a EthLogsObservableSubscription, decoding the event into my EventDTO.
Upvotes: 1
Reputation: 79
Seems like the most performant way of solving this would be having a listener on your polygon node. I suggest going with alchemy and using ethers js to listen on new blocks.
https://docs.ethers.io/v5/single-page/#/v5/api/providers/provider/
https://docs.ethers.io/v5/single-page/#/v5/api/providers/provider/-%23-Provider--events
provider.on("block",(blocknumber) => {
// this function would work every time a new block is mined
});
Upvotes: 1