Dr. Blocklove or: How I learned to stop worrying and Love the Ðapps a.k.a. getting started with Ðapps (Pt. 2)

Ahoy! Navigators of far and wide! Hope you’re still pumping Ethereum. In the first half of this ‘starter manual’ with Ethereum, we covered how to set things up – installing testrpc (complete test network), using Ethereum development web-framework Truffle and a special Javascript object, ‘web3’ to interact with Ethereum’s EVM, but only using testrpc and not the ‘world computer’.

Note: For supporting codes — github

We deployed a simplistic ‘helloWorld’ contract, which did nothing other than setting up the balance of a contract, i.e., numbers of tokens underlying a contract as 1000. This 1000 is not ETH, please note, it is just a figure — a simplistic token, if you may.

Possibly you can release it out in the wild on the real Ethereum Virtual Machine — World Computer, and do an ICO as a lot of people are doing these days and then see your token’s value sore to $5000, chi ching! Or so you would have thought. This ain’t so easy — Unforgiven (1992).

Anyway, I will show you today how to move a step further from testrpc which sits on your local computer to an actual testnet — which people from across the globe use for testing out Ethereum. We are going to use a provider — Ropsten, or overall Ropsten testnet. It will work the same way as Ethereum works. You would have mining which you can do — and the best part it you can do it on your computer. Then, you would have wallets and the whole shebang. The bummer is your pseudo-Ethers won’t mean anything, other than facilitating testing on this network. But then, we are testing and no body’s gonna use Ethers for that, thankfully.

You might ask at this point, why not then stick with testrpc. You definitely can use testrpc, but there might be several cases why you can think of using Ropsten test-network, out of which for me the reasons are:

  1. I like my contracts to be there and deployed when I switch off and on my computer. Testrpc creates 10 new accounts each time it’s closed and started.
  2. I love the UI of Ethereum wallet, they have created. Plain and simple console is also pretty powerful, but UI gives a bit of excitement. You actually can do create transactions as it would happen on live Ethereum. There are several commands in console the result of which is already present in the Ethereum Wallet UI, such as how much is the balance in an accounts. (I know this is a childish logic, but I like the idea of being a child at times, makes me stay young at heart).
  3. And the lovely Etherscan is also created for Ropsten test-network. You can track all your transactions and contracts on Etherscan if you don’t know about this.

All right, let’s do this!

Nothing fancy. Head over to Ethereum’s link and download the wallet.

Open you wallet application and install it. It would prompt you to use test-network. Check that you use the test network. (Don’t worry, even if you didn’t do that initially, you can do it later). You will be asked to set up your password for one ‘main account’ which you start off with. (You can create as many number of accounts as you want later). After these formalities, your Ethereum will sync and download the Ethereum test-net blockchain, as shown in the next image, which basically means, it will download all the blocks starting from genesis block.

Downloading whole of testnet blockchain will take some hours (Make sure you have around 20 Gigs of space in your system). Every time you close Ethereum wallet application, which keeps you in sync with Ethereum test-network blockchain, same ‘downloading block’ stuff will play up for some time, meaning your node will sync with latest blockchain (it will download the blockchain).

The wallet looks somewhat like:

You can also choose to start mining on Ropsten test-net, if you want to get some pseudo-ethers. You’d have to anyway if you want to continue experiments on Ropsten test-net, sigh!

Start mining with Shift + Command + M or go to View -> Develop -> Start Mining (test-net only). In around 8–10 hours you’d have some pseudo-ethers, either around 3 or 5, which is enough to get started. There are some ‘faucets’ available on the net, by the way, which gives some pseudo-ethers after some time interval if you ask politely enough ;), but they’ve never worked for me, hence I rely on plain and simple mining on test-net.

This is all fine, in your wallet application you can deploy contracts and all, by going to Contract -> Deploy New Contract, as shown below.

As you see, I have pasted my ‘helloWorld’ contract as below onto the SOLIDITY CONTRACT SOURCE CODE on the wallet and it given me an option to pick a contract to deploy (Since we only have one contract, there will be one option, I am sure you get the gist). Interestingly, we also have an estimate of how much pseudo-Ethers would this contract take to deploy. You have option to adjust the fee owing to which when that contract would be picked up by the miners to be mined will be decided by them. (Default option is generally the most optimum one).

As soon as you hit deploy, after entering your password (which you have to, every time you want to deploy a contract — I like that it’s secure, though it’s pseudo), steps which the contract will go through are below:

Great, you can go to Ropsten Etherscan to see this contract right now! This is the ‘helloWorld’ contract for you — deployed! And reading the balance from contract, which we did in the last part, that is already there in the Wallet application.

We are effectively done actually, take the blue pill and the story will end.

Or, take the red pill and I will show you how deep the rabbit hole goes.

In order to talk to Ethereum (or Ethereum Virtual Machine), we need some kind of client. That’s effectively what we were doing in the last part — the only difference was we were interacting with a blockchain sitting in our own computer. That’s also effectively what we did when we took the blue pill and deployed our contract using the Ethereum Wallet itself. Let’s do this with Truffle and let’s not even involve Ethereum wallet.

Download and install geth cli — a command line interface lying underneath the wallet which you saw when you took a blue pill.

$ brew update    //updating homebrew
$ brew upgrade //upgrading homebrew
$ brew tap ethereum/ethereum
$ brew install ethereum

Close off your Ethereum Wallet, if you have it running. It will otherwise interfere with geth console and you will have to use a workaround to start geth console.

$ geth --testnet console

Go into the geth console and all the things you saw earlier on wallet can be retrieved on geth console. Let’s try a few things. You can use it almost like we used truffle console (you can do a web3, since we already have it installed). Refer to link for more geth console commands

> eth.accounts
["0x01eeb4c8e7d8b6bcfef1d9d5f3f07b9e3b00d511", //my main account
"0x72db6fba111617139dec7e694fd400a4f8c3cc85"] //another account I created
> eth.getBalance(eth.accounts[0])
339703572760000000000 //balance in my main account in wei (1 Eth = 10^18 wei)
> web3.eth.accounts
["0x01eeb4c8e7d8b6bcfef1d9d5f3f07b9e3b00d511", "0x72db6fba111617139dec7e694fd400a4f8c3cc85"]
> web3.eth.getBalance(web3.eth.accounts[0])

We will be deploying our ‘helloWorld’ contract on Ropsten testnet. First off, we need to unlock our account so that transaction (i.e., first transaction — deployment of our contract) can happen through that account, since for any transaction some Ethers (in our case pseudo-Ethers) will be expended.

> personal.unlockAccount(eth.coinbase, '<password>', 600)

Now the tricky part. Any transaction requires some gas. For Ropsten testnet, the default gasLimit is 4712388 which you can see as:

> eth.getBlock("pending").gasLimit

Whenever you will try and deploy any contract, if you choose to use defaul configurations, Ropsten testnet will use all gas it makes available (as default, i.e., 4712388). Then, whatever gas is not expended will return back to you.

Problem is truffle has default gas as 3141592. Due to this issue, you’ll always run into errors such as ‘Error encountered, bailing. Network state unknown. Review successful transactions manually.’

Anyway, the workaround is to change gas limit for your deploys in your truffle.js and work with that. Hence, In your truffle.js

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*", // Match any network id
gas: 4612388 // add this line (don't forget ',' in the above line, value less than 4712388


Remember, when I was initialising geth console, I mentioned that if you have Ethereum running separately (through wallet or console), the command geth --testnet console might interfere with that. Hence, this is how I generally initialise Ropsten testnet without recourse to Ethereum wallet.

$ geth --testnet --mine --rpc    //to start RPC and mining together on Ropsten testnetConsole will start logging your activity now. Open another terminal and:$ geth --testnet attach '/Users/<name>/Library/testnet/geth.ipc' 
//Make sure that you are directing it to correct folder containing geth.ipc. By default, when you install Ethereum, your geth instance, logging, keypairs are stored under Library/ Ethereum/testnet/ in case you change, folder containing geth.ipc where the console needs to be attached would have to be given in the command.

All right, we are ready to deploy our contract, we have unlocked the account for 600 seconds which is enough for you to deploy the contract. Go to the folder ‘I_am_so_hot_that_I_am_cool’ and start truffle console:

$ truffle console
$ truffle compile && truffle migrate --reset
Running migration: 1_initial_migration.js
Deploying Migrations...
Migrations: 0x87c1b79c938fa103b6d7c512b6cd35295d5145da
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying ConvertLib...
ConvertLib: 0xf78a5bc5cf332e446bfeb633f578dcf69c945b24
Linking ConvertLib to MetaCoin
Deploying MetaCoin...
MetaCoin: 0x655187342029881465053e5e2214b1b834a221ec
Deploying helloWorld...
helloWorld: 0xbf01aef4163fefde175058a91148c4677249b8b6
Saving successful migration to network...
Saving artifacts...

Done! Now you can use truffle console in a similar way to query balance which was set by this contract.

truffle(development)> helloWorld.deployed().then(function(instance){HW = helloWorld.at(instance.address)})truffle(development)> HW.balance()
{ [String: '1000'] s: 1, e: 3, c: [ 1000 ] }

A great wrap! Let me know whether it all makes sense. We are now actually ready to use Ethereum main network since almost all of the things we had to do on test network would be the same there. I would strictly suggest creating good contract, secure ones (alluding to Parity mishap) and using standard contracts, such as ERC20, etc. as best practice.