Introducing Ethereum Development - Part 3 - TestRPC and Truffle
In this tutorial we continue following Alice in her attempts to monetise her blog posts. Alice has realised that she needs some help with her writing and decides to enlist Edward the editor. Ed has agreed to accept 20% of each contribution Alice receives.
In an earlier tutorial, Alice created a wallet and a very basic app which allowed her readers to transfer Ether via an embedded button, however this will no longer suffice since incoming funds need to be forwarded to multiple accounts. Alice thus endeavours to build a Smart Contract.
Before deploying an actual contract on the Ethereum blockchain, she decides to experiment - aware that mistakes now, may cause problems later.
npm install -g ethereumjs-testrpc
npm install -g truffle
Ethereum TestRPC is a fast and flexible way to emulate calls to the blockchain without the overheads of running an actual Ethereum node. Features include:
- Accounts can be re-used and reset.
- The live blockchain can be forked at a given block to test existing in-the-wild contracts.
- Specific accounts can be instantiated with a fixed amount of Ether (no need for faucets or mining).
- Gas price and mining speed can be modifed.
To launch TestRPC run:
testrpc -m "warrior minimum breeze raven garden express solar flavor obvious twenty alpha actress"
This will create a running instance of TestRPC with accounts generated from the "brain wallet" passed in with the -m switch.
truffle init will create an example project with folder structure
Truffle ├── build/ ├── contracts/ ├── migrations/ └── test/
buildcontains the compiled version of contracts after running truffle compile
contractsis where we will build our Solidity based contracts
migrationshelps us manage migration of contracts as we make changes over the contracts lifetime
testcontains Mocha based tests
Alice's contract code (
truffle create contract AuthorDonationExampleadds AuthorDonationExample.js file in /contracts directory)
Alice's test code (
truffle create test adds AuthorDonationsTestadds AuthorDonationsTest.js file to /test drectory)
After writing this draft of her contract Alice realises there are a few problems that need resolved. The contract will live forever (at least until Serenity is released) and she cannot amend or update it. In her next revision, she will deal with this problem and abstract her solution a little further to create a generic payment button combined with a dynamic smart contract.