Install Truffle 3 & make your first truffle project
$ sudo npm install -g truffle $ mkdir myproject $ cd myproject $ truffle init
Set the default account for truffle to use
We are going to use the default files to test the compile, migration and test functions, further details can be found on Truffle’s website.
Truffle default’s to using the accounts, which is not necessarily the codebase. You can set the default account for Truffle to use by adding the ‘from’ into the ./truffle.js file.
Compile and deploy your first truffle project
$ truffle compile $ truffle migrate
Truffle will hang during the deployment because it is waiting for you to authorise the use of gas for the transaction. The below shows that the transaction needs to be confirmed in Purity browser before it can be sent.
Speed contract deployment up – switch to Dev network and unlock your account
You’ll see that things being to get very slow, waiting for a contract to be deployed before deploying the next contract. At this stage it is worth switching to the new private dev network from Parity to use for developing and initial testing instead of the public ropsten network.
parity --geth --chain dev --force-ui --reseal-min-period 0 --jsonrpc-cors http://localhost
[There is a bug at the moment that requires –reseal-min-period 0 to be set].
Let’s create an account and get some dev ETH in it. Open your browser and go to the parity web app http://localhost:8080
Create an account by “recover from owner recovery phrase”, enter a password and name but leave the phrase blank. Partity has made this ‘hack’ on their Dev env to create a new account with plenty of dev ETH in it.
You can also configure Parity to unlock the main account that was created with the ETH by default so that you are not prompted to confirm the transaction in the Parity web app each time.
Use the –unlock flag when starting Parity to allow any transactions to use the account without confirmation. You can put the password into a file rather than typing it on the command line.
parity --geth --chain dev --force-ui --reseal-min-period 0 --jsonrpc-cors http://localhost --unlock 0xda9b1a939350dc7198165ff84c43ce77a723ef73 --password ./.ethereum/password
Try running the truffle migrate again to check that you do not have to confirm transactions. If you have made no changes to your contracts you’ll need to use the –refresh flag.
$ truffle migrate --refresh
Testing your first truffle contract
Now you can test the deployment. If your default account is not account, you should edit /test/metacoin.js to set the default account (mine was account).
$ truffle test
Accessing your Truffle contract from a web app
Create a new directory called truffleBasic, install web3 and jQuery using bower. You can refer to Your first Ethereum web page (Blockchain stack #3) post to understand why web3 and jQuery are required.
$ cd /var/www/html $ mkdir truffleBasic $ cd truffleBasic $ bower install jquery $ bower install web3
Inside the truffleBasic directory, create a directory to contain the JSON files produced by truffle and copy the compiled files produced when $truffle compile was executed.
$ mkdir contracts $ cp ~/Truffle/myproject/build/contracts/* ./contracts/
Create the file index.html in the truffleBasic directory and copy this into it:
Update this line to use the address that your contract was deployed to.
var MyContract = web3.eth.contract( MetaCoin_json.abi ).at("0xc208fd1234f928dbd1dd2006b9cf74f4f14f6c84");
You can find the address from the contract file MetaCoin.json.
Check Parity dev network is running and allow cross browser queries (–jsonrpc-cors http://localhost).
parity --chain dev --force-ui --jsonrpc-cors http://localhost --unlock 0x00a329c0648769A73afAc7F9381E08FB43dBEA72 --password ./.ethereum/password --reseal-min-period 0 --geth
Point your browser to http://localhost/tuffleBasic
Summary of code:
- The Ethereum node is set. The if statement checks if the web page has been opened in an Ethereum type browser like Mist and doesnt change it if it has, otherwise defaults to a node running on localhost:8545.
- The JSON file MetaCoin.json containing the ABI (interface) details is loaded. This was the file produced by the Truffle compile. It requires the jQuery getJSON function to be called asynchronously.
- The address of the contract is hard coded into the HTML and the getBalance function is called on it. This asynchronously updates the HTML when the call back is made.
Next in the Blockchain stack series
The next post in the series, Truffle 3 – Using Truffle Contract (formerly EtherPudding), looks at use Truffle Contract to simplify your HTML code and remove the need to hard code the contract address in the HTML. This is then followed by a post showing you how to make your Ethereum DevOps more efficient by using the Truffle Default Builder.
There’s a team of us that have come together at Ziggify to work on a Blockchain play. We are in the exploration phase, so if you want to get involved or have an idea please get in touch. I’m based in London and enjoy chewing the Blockchain fat over a cuppa or a beer.