Getting started with Solidity development using Truffle
Presented by Yorke Rhodes January 31, 2017
Updated by Joseph Delong April 9, 2018
Prerequisites
Walkthrough
Installation
- We need to install the Truffle package first
sudo npm install -g truffle
- Verify that it has been downloaded successfully
truffle
- We also need to install TestRPC
sudo npm install -g ethereumjs-testrpc
- Verify that it has been downloaded successfully
testrpc
Setup to initialize a default truffle project, navigate to the desired directory and run truffle init
To configure truffle to use our testRPC client, populate truffle.js
with:
Contract
Let’s write our first contract. Create a new file in the contracts directory called SimpleStorage.sol
Populate SimpleStorage.sol
with:
Let’s compile our contract. Run truffle compile
in our truffle project's root directory. You should receive some helpful warnings. You should also be notified that a new artifact has been written.
Deploy
We now have to configure truffle to deploy this artifact to our network. Create a new file in the migrations directory called 2_deploy_contracts.js
.
Populate 2_deploy_contracts.js
with:
Now if we run truffle deploy
we should see our migration script running, and eventually truffle will tell us the address that the contract account has been created at.
Tests
What can we do with our contract now? We should test it before we consider deploying it permanently (on a live ethereum network)!
Let’s write our first tests. Create a new file in the test directory called simpleStorage.js
.
Populate simpleStorage.js
with:
Before running our tests, we need to make sure our Ethereum client is running. Open up another terminal window and run testrpc
if you haven't already.
Now go back to your truffle project and run truffle test
. One of our tests should fail. Find the error and fix it.
Troubleshooting
If you encounter problems I have GitHub repository with a working version of this guide. In your terminal run:
git clone https://github.com/dangerousfood/hello_contract.git
If truffle compile
or truffle test
fails in the newly cloned folder then there may be an environmental issue. Ensure that NodeJS and NPM are installed correctly.