2

Is it possible to listen to transactions on a specific ethereum address? For example, if someone sends a certain amount of ETH to that address, the callback function would be triggered.

I read this: How to explore all transactions for a given account? but it seems to be about past transactions and not listening live.

3

You can use Web3.js 1.0 and WebSockets to accomplish this.

var subscription = web3.eth.subscribe('logs', {
address: '0x123456..',
}, function(error, result){
if (!error)
console.log(result);
})
.on("data", function(log){
console.log(log);
})
.on("changed", function(log){
});

If you use the above code, simply update the address property.

Working sample here: https://jsfiddle.net/h7nskoyu/11/

This looks at the CryptoKitties contract, and when looking at the console you see this: enter image description here

The expanded transaction maps to this: https://etherscan.io/tx/0x3ccce0b8072649ca087a91f00bdbb475b36d03a9a8dd2cc54b9a03ac5826c255

Learn more about web3.eth.subscribe here.

  • Thank you Shawn! This is what I expected earlier, but hadn't been able to get it to work. I had made a GitHub issue about it here: github.com/ethereum/web3.js/issues/1752. Not sure if the problem is in web3 or my code. – Yousuf Jul 3 '18 at 22:47
  • In your code to connect to an Ethereum Provider, are you making sure that the connection is to a WebSocket? For example, you cannot use Metamask in this situation, since Metamask does not currently support websockets – Shawn Tabrizi Jul 3 '18 at 23:09
  • Trying that, but getting this error now. Used --ws when running the miner and hard-coded the websocket address into web3. connection not open on send() Error when watching incoming transactions: connection not open connection not open on send() Error when watching incoming transactions: connection not open connection not open on send() Error when watching incoming transactions: connection not open connection not open on send() Error when watching incoming transactions: connection not open – Yousuf Jul 3 '18 at 23:33
  • I would say try using var web3 = new Web3('wss://mainnet.infura.io/ws'); to simply test your code is working. And if it is, then open up another thread to debug issues accessing your Geth WebSocket. – Shawn Tabrizi Jul 3 '18 at 23:40
  • @ShawnTabrizi This only works if address is a contract and it generates events. This will not work for EOA or if the contract do not generate events. – Ismael Jul 4 '18 at 1:57
1

It's called Continuous monitoring concept. You can also do it using RPC Call. Using Etherscan API (There are many platform which provides ETH API)

Step 1 : Get Latest Block Height using https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey=YourApiKeyToken

Step 2 : Get all details of that block using https://api.etherscan.io/api?module=proxy&action=eth_getBlockByNumber&tag=0x5A0F60&boolean=true&apikey=YourApiKeyToken (You will get all transaction under this block)

Step 3 : Now store all that transaction's data from json to your list and run for loop for your address which you want to monitor continuous.

So now if your address will be in that block's transaction list then you can use details as per your requirement else you can check next generated block after 10 to 15 seconds (as per your requirement).

  • Thanks for your reply! This won't work right now as I want to do testing on a private network. But I'll keep it in mind for the future. – Yousuf Jul 6 '18 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.