[Ethereum] Listen to ethereum transactions on a specific address

addressesgo-ethereumjavascriptweb3js

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.

Best Answer

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.

Related Topic