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


Step 1: Start Ganache

Ganache GUI Client

Step 2: Initialize Truffle

cd ~/Ethereum
mkdir demo
cd demo
truffle init

Step 3: Create Demo Contract

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;

Step 4: Add a New Migration

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

Step 5: Update the Configurations

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

Step 6: Deploy!

truffle deploy

Step 7: Interact with Our Demo Contract

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

Step 7a: Check the Balance

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

Step 7b: Let’s Add Some Ether

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

Step 7c: Check the Balance Again

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


  • 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

Was it helpful?



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
Jian Jye

Jian Jye


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