7

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.

4 Answers 4

6

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.

7
  • 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, 2018 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 Jul 3, 2018 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, 2018 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. Jul 3, 2018 at 23:40
  • 2
    @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, 2018 at 1:57
3

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).

1
  • 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, 2018 at 4:09
1

The accepted answer will only work for txn to a contract.

For detecting ether transfers ( txn to EOAs ) you'll have to setup a listener for getting latest blocks & query on the transactions array of the block to find a match for a particular requirement.

0

This will monitor the blockchain for incoming transaction for an address

const monitorTransaction = async (addressss)=>{
    
        const block = await web3.eth.getBlock('latest');
    
        console.log(`Checking new block ${block.number}`)
    
        for(let txHash of block.transactions){
            // console.log(txHash);
            const tx = await web3.eth.getTransaction(txHash)
            // console.log(JSON.stringify(tx));
            const tranx = typeof tx === 'string' ? tx.to.toLowerCase() : '';
    
                if(addressss === tranx){
                    console.log(`New transaction found - Block ${block.number}`);
                    console.log(`Transaction From: ${tx.from}`);
                    console.log(`Transaction To: ${tx.to}`);
                    console.log(JSON.stringify(tx));
                }
        }
    }
3
  • The code doesn't do what is implied in the answer. It only queries the latest block, it doesn't monitor for changes. It has other issues like using the variable str without defining it, doesn't consider internal transactions.
    – Ismael
    Nov 26, 2022 at 19:44
  • Thanks for that str has been updated to tx. You can use SetInterval in javascript to constantly query the block every xxx seconds(12 to 15 seconds as the default block time). Sometimes you need to use your technical skills to solves problems. You don't need to depend on full solution from people. Nov 27, 2022 at 16:52
  • At least the most important logic is written. You can write a code to run the monitorTransaction function to monitor for an incoming transactions Nov 27, 2022 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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