2

I want to set watch for my public address. So i can get all transaction for that address.

My code is:

var filter = web3.eth.filter({ address: ["0x2A7146700BCe4A574eE36D5e9cd6CF7d8871aEB6"] });
    filter.watch(function (error, result) {
    console.log(error)
    console.log(result);
});

When any transaction occur from ethereum blockchain, i need to call my event.

2
  • Hi there. What's your actual question? :-) Commented Sep 30, 2017 at 10:52
  • When anyone deposite ether in my address. I want to call my function at that time. I am using web3js. Commented Sep 30, 2017 at 13:58

1 Answer 1

9

No, currently there's no event triggered when the balance of an address has been modified.

Your account balance can change without a transaction, if your address is used in the miner field of a block or uncle it will be credited with the reward, but no transaction is involved. The balance can be credited in an "internal" transaction.

The closest thing you can do is setup a block filter event and monitor for each new block if your balance has changed.

const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

const address = "0x....";

let balance = web3.eth.getBalance(address);

const filter = web3.eth.filter('latest');
filter.watch((err, res) => {
  if (err) {
    console.log(`Watch error: ${err}`);
  } else {
    // Update balance
    web3.eth.getBalance(address, (err, bal) => {
      if (err) {
        console.log(`getBalance error: ${err}`);
      } else {
        balance = bal;
        console.log(`Balance [${address}]: ${web3.fromWei(balance, "ether")}`);
      }
    });
  }
});
2
  • 1
    Is this still the best approach?
    – minus1
    Commented Mar 23, 2018 at 15:12
  • 1
    Since geth v1.7.3 there is debug_getModifiedAccountsByNumber but it is not documented in the wiki yet, you have to take a look at the pull request github.com/ethereum/go-ethereum/pull/15512.
    – Ismael
    Commented Mar 23, 2018 at 15:44

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.