Blockchain & Solidity Program Lab Manual
ISBN 9788119221646

Highlights

Notes

  

Prog. 13:: How to Develop Ethereum Smart Contract with Truffle and Ganache

Pre-requisites

Truffle Framework: npm install -g truffle@4.1.13

Ganache: Download and install the GUI Client

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 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.

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:

You should see a couple of files being created 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:

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 the 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:

Step 5: Update the Configurations

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

Step 6: Deploy!

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

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 Democontract, 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:

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:

Here, the 0x85365158Ed31cF2d8D9E8c00708981d9419eA5B6E is the contract address that you needed to copy in Step 6. We assigned this instance of the Demo contract 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:

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:

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

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 = 1018 wei. See: https://etherconverter.online/

Step 7c: Check the Balance Again

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

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 using truffle

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.