Deploy your own smart contract with Truffle and Ganache CLI — Beginner Tutorial (Part II)

HaloBlock Official
Jul 2, 2018 · 5 min read

In PwC’s latest report of ICO (Initial Coin Offerings), there are 537 successful ICOs with a volume of $13.7 billion in the first 5 months of 2018, which is more than all pre-2018 ICOs combined. Almost all of ICOs are based on smart contracts of Ethereum.

Our last article shows how to create a simple virtual token on the browser based IDE. In this article, we will show you a step-by-step instruction on setting up a smart contract on your local environment.

We need two tools for building local smart contract environments: Truffle and Ganache CLI.

Truffle

As one of the most popular Ethereum development frameworks, Truffle helps you create basic solidity templates to deploy.

Ganache CLI

It’s the command line version of Ganache. It’s used to simulate full client behavior and make developing Ethereum applications faster, easier, and safer.

In this article, we will explain in details on how to set up Truffle and Ganache CLI in Ubuntu 16.04.

Prerequisites

  • Operating system: any version of Ubuntu 16.04.
  • Software: npm.
$ sudo apt-get update$ sudo apt-get install nodejs$ sudo apt-get install npm
$ npm -v

If you can see the version number, then you are ready to go! Please make sure that the version number is 5.0+. If it’s not, try to run the following:

$ sudo apt-get install npm@5.0.0

Install Truffle and Ganache CLI

  1. Ensure that you have installed npm with version 5.0+ on your system.
  2. Run the following command:
$ sudo npm install -g ganache-cli truffle

This is a global installation, so no need to worry about the installed location.

Run Ganache CLI

After installation, you can start to develop your own smart contracts.

First, you need to create a simulated environment for running smart contracts on your local computer:

$ ganache-cli

Then, you will see results like below:

Ganache CLI automatically creates 10 accounts associated with 10 private keys. Each account has 100 ethers for testing purpose.

Note: Ganache CLI is running on the memory, so everything will be back to fresh if restart occurs.

Build a “HelloWorld” smart contract

Step 1: With Ganache CLI running behind, let’s start with a new terminal, and run the commands below:

$ mkdir SmartContractDemo$ cd SmartContractDemo/$ mkdir HelloWorld$ cd HelloWorld/$ truffle init
  1. /contracts: store original codes of the smart contract. We will place our HelloWorld.sol file here.
  2. /migrations: deploy the smart contract in the “contracts” folder.
  3. /test: test codes for your smart contract, support both JavaScript and Solidity.
  4. truffle.js: configuration document.
  5. truffle-config.js: configuration document for windows user. We can just leave it alone. (However, if your Ubuntu 16.04 is running on a virtual machine under Windows, then your configuration should also go here.)

Step 2: Create “HelloWorld” contract.

There are two ways to create a new contract:

  1. Directly place “HelloWorld.sol” file under “contracts” folder.
  2. In the “HelloWorld” folder, run command:
$ truffle create contract HelloWorld

Copy the following codes into “HelloWorld.sol”:

pragma solidity ^0.4.18;Contract HelloWorld {    function hi() public pure returns (string) {        return (“Hello World”);    }}

Step 3: Compile “HelloWorld” with the following command.

$ truffle compile

This compiles the original code into Ethereum bytecode. If everything goes well, it will create .json file under build/contracts folder.

Step 4: Deploy “HelloWorld” contract.

Step 4.1: Create a js file under migrations, name it “2_deploy_contracts.js”.

Step 4.2: Copy and past the following deploying content into the “2_deploy_contracts.js”.

var HelloWorld=artifacts.require (“./HelloWorld.sol”);module.exports = function(deployer) {      deployer.deploy(HelloWorld);}

Step 4.3: Modify truffle.js file (or truffle-config.js if you are a windows user), like below:

Step 4.4: Run the following command:

$ truffle migrate

In the meantime, there are a lot of logs in the terminal running Ganache CLI, like below:

🎉Congrats! The “HelloWorld” smart contract has been successfully deployed to Ganache.

Conclusion

As you can see, it’s easy to set up the working environment for a smart contract. This is just the first step. In our future blogs, we will share more technical details about how to modify contracts, how to add more functionalities to contracts, and how to secure contracts.

By Quan Sun, Security Researcher from HaloBlock.io

HaloBlock

Security audits for smart contracts and cryptocurrency exchanges

HaloBlock Official

Written by

Security Audits for Smart Contracts and Crypto Exchanges

HaloBlock

HaloBlock

Security audits for smart contracts and cryptocurrency exchanges

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade