Skip to main content

Bespoke web development & support


Introducing Ethereum Development - Part 2 - Geth, NodeJS and Web3

Submitted by Paul on 21/03/2017

After publishing a few articles Alice finds herself helpless addicted to watching her wallet -  waiting for Ether to trickle in - and is getting distracted from more meaningful labour. She resolves to fix this by writing a program that will monitor her account and send here messages when a transaction has been processed. After a little research she settles on using NodeJS, web3 library and Geth.

Geth (Go Ethereum) is a command line tool for running a local Ethereum node, interacting with the blockchain and managing user accounts. It is available from source, binaries or common package managers.

Using Geth we can bootstrap a local copy of the Ethereum blockchain. Even in "Fast mode" this may take considerable time. Depending on your network connection and CPU speed, it could take anything from 20 minutes to several hours to get bootstrapped.

geth --testnet --fast --cache=1024 --rpc console

This will start the blockchain fetching the Ropsten Testnet in fast mode (with 1Gb cache), allow incoming RPC http requests on default port 8545 and open an interactive console. Typing eth.syncing will return the current chain sync state, and entering eth alone will show available commands.

Once the blockchain is fully synced we can run our app code to watch for every time the 12th oldest block is updated.

Breaking that down...

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

Create a new instance of a web3 object and instantiate pointing at the RPC endpoint of our local Ethereum node.

let account = '0x05430f5201585C2601bB75d9658007202864c993'.toLowerCase()

Some wallets use mixed case to checksum a valid Ethereum account address but the blockchain itself uses all lowercase. Rather than edit the address pasted from our wallet (in case of introducing an error) we lowercase the string literal with a method.

let filter = web3.eth.filter('latest')

Creates a new filter object to report on the latest block (currently targeted on the live network as one every 10 seconds). Values passed when instantiating this object can not be changed later. Blockheights must be either an integer eg 15534 or a string ie ‘earliest’ or  ‘latest’.  It’s not possible to watch for a relative block height like the 12th block. So we’ll need to do some extra legwork., result)...

Set our filter to watch the blockchain asynchronously
let confirmedBlock = web3.eth.getBlock(web3.eth.blockNumber - 11)

When we get a result (new block) we fetch the 12th oldest block. Note that web3.eth functions support both synchronous and asynchronous implementations.

let confirmedBlock12 = web3.eth.getBlock(web3.eth.blockNumber - 11) confirmedBlock12.transactions.forEach(function(txId) { let transaction = web3.eth.getTransaction(txId) if ( == account) { // Do something useful (eg send a message) console.log(transaction.from) } }) Iterate over each transaction receipt in the block checking for Alice’s account address in the to field. Once a transaction receipt is found Alice could use something like nodemailer to send herself notifications.


Part 1 | Part 2 | Part 3 | Part 4 | Part 5

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.