5-min Tutorial to Ethereum Smart Contract with Truffle and Ganache

Jian Jye
5 min readJul 10, 2018

--

You have read about Ethereum, you know the gist of how blockchains work, now you want to start playing with the development. Where do you start?

Without further ado, here comes the step-by-step guide.

While this article is primarily for Mac users, it should be similar on Windows.

Pre-requisites

You’ll need these:

Truffle is the framework we’ll use to help us easily manage and deploy our Solidity codes. Ganache runs a local instance of the Ethereum network for our testing purposes.

Note that since Truffle and Solidity are quite version sensitive, doing this tutorial with a different Truffle version other than 4.1.13 might break.

Step 1: Start Ganache

When Ganache is started, you will see 10 Ethereum addresses automatically generated for you. These are the wallets that we can test against later.

By default, Ganache starts at http://127.0.0.1:7545. Take note of this address.

Ganache GUI Client

Step 2: Initialize Truffle

Let’s create a new folder for our demo project and initialize Truffle Framework inside. Open up your terminal and type:

cd ~/Ethereum
mkdir demo
cd demo
truffle init

You should see a couple of files being injected by Truffle in the demo folder.

Step 3: Create Demo Contract

Open up the demo project in your favorite editor, create a new file called Demo.sol inside the contracts folder. Paste the following codes into Demo.sol:

pragma solidity ^0.4.23;contract Demo {
uint public balance;
// Initialize global variables
constructor() public
{
balance = 0;
}
// The payable keyword allows this function to accept Ether
function contribute() public payable
{
// msg.value is the value of Ether sent in a transaction
balance += msg.value;
}
}

Here, we have a very simple smart contract where it has a contribute function that takes in Ether and update its balance.

Step 4: Add a New Migration

In order to deploy our Demo contract to our test Ethereum network on Ganache, we need a migration script.

Inside migrations folder, create a new file called 2_demo_migration.js. The 2_ prefix is important so that Truffle knows it’s the step 2 of all the migration steps for this project.

Paste the following codes:

var Demo = artifacts.require("./Demo.sol");module.exports = function(deployer) {
// Demo is the contract's name
deployer.deploy(Demo);
};

Step 5: Update the Configurations

Open up truffle-config.js , paste the following codes:

module.exports = {
networks: {
development: {
// from: "", // Defaults to first address from Ganache
host: "127.0.0.1",
port: 7545,
network_id: "*"
}
}
};

Step 6: Deploy!

Now we are ready to deploy to Ganache for testing. Let’s do it! Open up your terminal and type:

truffle deploy

Once it’s done, let’s check our Ganache. You should see a few transactions being recorded:

2 contracts are generated by our deployment due to us having 2 migrations, with 1 being the default Truffle Framework’s migration.

For our Demo contract, we need to check the second transaction from the top (circled in blue). Click on it, and copy the contract address (circled below) for our next step.

Step 7: Interact with Our Demo Contract

Now that the Demo contract is deployed, we want to play around with it. Truffle provides a nice console tool for this purpose. Open up your terminal and type:

truffle console

Once you are inside the console, type Demo (the name of our contract) and you should see some data dump on our contract. But that’s not very useful, let’s type these instead into your terminal:

var dm; Demo.at("0x85365158Ed31cF2d8D9E8c070898d19419eA5B6E").then( function(x) { dm = x });

Here, the 0x85365158Ed31cF2d8D9E8c00708981d9419eA5B6Eis the contract address that you needed to copy in Step 6.

We assigned this instance of the Democontract to the variable dm so that we can manipulate the contract easily afterwards.

Step 7a: Check the Balance

Now let’s check our balance, which supposed to be 0:

dm.balance().then( function(x) { return x.toString(); });

Step 7b: Let’s Add Some Ether

First you’ll need to copy one of the wallet address in Ganache. Let’s take the third wallet for this:

Accounts screen in Ganache

Now to transfer the money from our wallet to our Demo smart contract, we will need to run this command in the console:

dm.contribute({from:"0x3e08faaf30F2B04B1AD7851Eaac01534d8Cda1bB", value: 10000000000000000000});

Here we are sending 10000000000000000000 wei (or 10 ether) from our third wallet to the smart contract. You should see some data dump on screen if it’s successful.

Note: 1 ether = 10¹⁸ wei. See: https://etherconverter.online/

Step 7c: Check the Balance Again

Next let’s check our balance, do you see 10000000000000000000?

dm.balance().then( function(x) { return x.toString(); });

Closing

Now that you have just created your first smart contract, deployed it to a local testnet and even made some transaction, hopefully this quick example has provided you some foundation to work on the more advance topics in the future.

A few things to note before we go:

  • Ganache is not persistent. Once you restart the app, all the addresses and transactions will be reset.
  • You need to re-deploy every change you make in your contract. The simplest way to do so in the local testnet is by usingtruffle deploy --reset. This will also create a new instance of your smart contract.
  • All the codes used for this demo is available at Github. Help yourselves.

Next Step

Creating your first smart contract is only the first step. Next, let’s look into how you can deploy your codes online with our next tutorial: 5-min Tutorial to Deploy Ethereum Smart Contract to Ropsten Testnet via Infura!

Was it helpful?

If you find this article helpful, do give me a little clap on Medium. 😄 It really makes my day to know that I have helped a fellow developer out there. Thanks!

--

--

Jian Jye
Jian Jye

Written by Jian Jye

I write about Laravel, PHP, and web development related articles.

No responses yet