Getting started with Solidity development using Truffle

Joseph Delong
Apr 9, 2018 · 2 min read
Image for post
Image for post

Presented by Yorke Rhodes January 31, 2017

Updated by Joseph Delong April 9, 2018

Prerequisites

Walkthrough

Installation

  1. We need to install the Truffle package first sudo npm install -g truffle

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.

Further Links

Get Best Software Deals Directly In Your Inbox

Image for post
Image for post

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Coinmonks

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Joseph Delong

Written by

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Joseph Delong

Written by

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store