7

I'd like to get notification from geth when my account receives ether. I'm thinking to type api command eth_getBalance in every minute, in order to check if my account receive ether or not. However, I don't think it's a good way.

Could you tell me better way in order to get notification from geth?

8

personally I registered my addresses on https://etherscan.io that provides such a service, by email.

If you really want to do it by yourself, the simplest I can think would be to register a cron job that start a getBalance.sh script containing a curl eth_getbalance call.

So enter crontab -e and edit the file at the end with :

*/5 * * * * /home/youruser/getBalance.sh

This would fire the getBalance.sh script every 5 minutes, change it according to your needs.

Create the /home/youruser/getBalance.sh file like this, edit YOURADDRESS and PORT according to what client you use, don't forget to chmod +x it:

#!/bin/bash
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["YOURADDRESS", "latest"],"id":1}' 'http://localhost:PORT'

edit: this is completely untested !

edit2: tested working python script called getBalance.py. It's a very rough script that could be inserted in your cron job instead of getBalance.sh, it just prints 'toto' but you can mail / sms / whatever you feel appropriate with it.

import requests
import json
def balance(address, url):

        postData = {"jsonrpc":"2.0","method":"eth_getBalance","params":[address, "latest"],"id":1}
        #print('p: {}'.format(postData))
        response = requests.post(url, data=json.dumps(postData))
        print(response.json())
        result = response.json()
        return result['result']

def writebalance(balance):
        with open('getBalance.txt', 'w') as f:
                f.write(balance)

def readbalance():
        with open('getBalance.txt', 'r') as f:
                bal = f.read()
        return bal


if __name__ == '__main__':
        oldbalance = readbalance()
        newbalance = balance("YOURADDRESS", 'http://localhost:8545')
        if int(newbalance, 16) - int(oldbalance, 16) >0:
                print('toto')
        writebalance(newbalance)
  • Just to add to the above, in addition to email notifications Etherscan.io also supports telegram notifications pushed directly to your mobile device. You have the option of choosing either one. – mtbitcoin Mar 2 '16 at 19:10
0

You can use Dagger https://matic.network/dagger to send email/notification or process any contract event.

Here is the simple example of how to watch ERC20 token transfer using Dagger:

// web3 contract
var web3Contract = new web3.eth.Contract(abi, address);

// dagger contract
var contract = dagger.contract(web3Contract);
var filter = contract.events.Transfer({room: 'latest'});

// watch
filter.watch(function(data, removed){
    // data.returnValues.to : address to which it has been transferred to
    // data.returnValues.value : value which has been transferred
});

// watch only once
filter.watchOnce(function(data, removed){
    // data.returnValues.to : address to which it has been transferred to
    // data.returnValues.value : value which has been transferred
});

// stop watching
filter.stopWatching();

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.