Intro
I’ve been following the Status project for some time and recently they announced their virtual hackathon. I’d been planning on diving into the Ethereum/Decentralised App development world and thought this would be an ideal time to start and I pretty quickly saw how powerful and relatively simple this can be.
As a complete beginner to working with the blockchain I found it better to begin by forgetting about the Status part and even the Dapp part and just see how to interact with a blockchain. Basically you can send some Eth between accounts using one function – that’s impressive.
Setup
I figured you need to get set-up with three things to get going:
1. A blockchain
Normally this would be the Ethereum blockchain but whenever you run a smart contract on Ethereum you must pay for the computation using gas. Testnets simulate the Ethereum network on you local machine and allow you to experiment for free. I installed testrpc.
2. web3.js
web3.js is a Javascript library which runs in a browser, connects to a local blockchain node and allows you to make calls on the blockchain. It does this using remote procedure calls (RPC) which basically means passing messages in JSON format.
3. Truffle
Truffle is a development environment for Ethereum that makes development a lot easier. For this exercise I’m just using the Truffle console to interact with web3.js manually. It’s way more powerful than that, which I hope show during some more posts in the future.
The Action
Start the testnet
To begin the testrpc testnet needs to be started by entering the following command:
testrpc -p 8546
Available Accounts ================== (0) 0xc7975434577b0d57248e1de03fcc6f27cd6cc742 (1) 0x74dcfef837aff77d7c43d44d1d69b1eef5d708a6 (2) 0xac63a31fd24d692997e577c02524f81eb223cd1d (3) 0x86fd495d744d13d43d00aa700dc9517a92eb9eae (4) .......
Run web3 commands
Truffle has a console tool that can be used to run web3 commands. To start it just type (keep testrpc running in separate window):
truffle console
Then try:
web3.eth.accounts
you should see a list of the same accounts you saw when you started testrpc.
Send send send
So now the really cool part. To transfer Eth between accounts all you need to enter is:
var sender = web3.eth.accounts[0];
var receiver = web3.eth.accounts[1]
web3.eth.getBalance(sender) web3.eth.getBalance(receiver)
These should both return,
{ [String: '100000000000000000000'] s: 1, e: 20, c: [ 1000000 ] }
var amount = web3.toWei(0.01, 'ether')
web3.eth.sendTransaction({from:sender, to:receiver, value: amount})
{ [String: ‘99989999999999979000’] s: 1, e: 19, c: [ 999899, 99999999979000 ] }
web3.eth.getBalance(receiver) this time shows:
{ [String: ‘100010000000000000000’] s: 1, e: 20, c: [ 1000100 ] }
0x9e9963f4782b4248b2cd42d8af17e85d64d049e052da335e3517f4a8a69aa064 Gas usage: 0x5208 Block Number: 0x01 Block Time: Tue Jun 06 2017 19:34:54 GMT+0100 (BST)
So with as little as one command you can transfer value electronically, cryptographically proven and with minimal fee. From a pure tech geek point of view that’s awesome, no wonder people get excited about this stuff!