0

I'm stuck on how to subscribe to events to non-contract addresses. Is this possible? All I'm seeing is subscription to eth contracts only.

2
  • What exactly would you like to listen? There's no event emitted from an EOA (or as you said, non-contract address). Are you more looking for getting all the transactions related to one account? Or getting all the events related to one specific account from a particular smart contract? Jul 25, 2019 at 13:21
  • @ChenchenYo I essentially want to listen to all transactions that are received by a certain EOA in the end. I want to create an alert any time money has hit that address.
    – b3dog
    Jul 25, 2019 at 17:24

1 Answer 1

0

Try with web3.eth.subscribe, where the documentation is here.

function subscribeToTxs(address) {
return web3.eth.subscribe('pendingTransactions', (err, txHash) => {
    if (err) {
        throw(err);
    }
})
.on("data", function(txHash){
    return web3.eth.getTransaction(txHash, (err, returnedValue) => {
        if (err) {
            // error handling
        }
        if (returnedValue && (returnedValue.from === address || returnedValue.to === address)) {
            console.log(`TransactionHash: ${returnedValue.hash}`);
        }
    })
});

}

Please pay attention that here the returned hash for pending transactions, but not mined transactions. You could do further status check or subscribe to 'newBlockHeaders' (see docs) for mined transactions.

6
  • Getting the returnedValue as null.
    – b3dog
    Jul 26, 2019 at 20:44
  • Yep. It's related to the error handling part. If an error returned with getTransaction, then there's no returnedValue. I edit the code and you could try again. Jul 28, 2019 at 9:55
  • This ignores internal transactions. If a smart contract sends eth to the EOA address from inside a method call, for example, that would not be captured. The to address, in that case, is the smart contract. The from is the caller of the smart contract. Jul 28, 2019 at 12:00
  • What @ThomasJayRush mentioned is correct. Unfortunately, there's no out of the box solution in web3js library to deal with internal transactions. One possible way to extract internal transactions is through ethereum clients, such as geth and parity Jul 28, 2019 at 15:58
  • TrueBlocks (my project) is building an out of the box solution to this exact problem. We have a long way to go, but making great progress. Jul 28, 2019 at 23:46

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.