Logo

dev-resources.site

for different kinds of informations.

Tutorial: Play with Truffle & Ganache

Published at
4/11/2022
Categories
blockchain
truffle
tutorial
ganache
Author
yongchanghe
Author
11 person written this
yongchanghe
open
Tutorial: Play with Truffle & Ganache

Take you through the basics of creating a local blockchain, deploying a smart contract and interacting with it. (MacOS)

This tutorial is meant for those with a basic knowledge of Ethereum and smart contracts, who have some knowledge of HTML and JavaScript, but who are new to dApps.
The purpose of building this blog is to write down the detailed operation history and my memo for learning the dApps.
If you are also interested and want to get hands dirty, just follow these steps below and have fun!~

Prerequisites

Intro & Review

In this tutorial we will be covering:

  1. Create a truffle project
  2. Launch a local blockchain
  3. Test the smart contract
  4. Compile and deploy the smart contracts
  5. Interact with a smart contract

What are Truffle and Ganache?

Truffle is a world-class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier. With Truffle, you get:

  • Built-in smart contract compilation
  • Automated contract testing
  • extensible deployment & migrations framework
  • Network management for deploying to public & private networks
  • Package management using the ERC190 standard
  • Interactive console for direct contract communication
  • Configurable build pipeline
  • External script runner

Ganache is a personal blockchain server for rapid Ethereum and Corda distributed application development. You can use Ganache across the entire development cycle; enabling you to develop, deploy, and test your dApps in a safe and deterministic environment. Ganache comes in two flavours: a UI and CLI. In this blog, we choose to use Ganache UI.

Getting started

Create a truffle project

First, we create a new directory for our Truffle project using CLI, and navigate to the root directory of our project:

mkdir meta-coin
cd meta-coin
Enter fullscreen mode Exit fullscreen mode

Image description

Let's open the project folder using VS Code:

Image description

Next, we download the meta-coin box using:

truffle unbox metacoin
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Once this operation is completed, you'll now have a project structure:

contracts/: Directory for Solidity contracts
migrations/: Directory for scriptable deployment files
test/: Directory for test files for testing your application and contracts
truffle-config.js: Truffle configuration file
LICENSE: License for your project

Launch a local blockchain

Now, we need to launch a local blockchain, so that we can connect to it and deploy our smart contract to it.
Truffle has a built-in personal blockchain that can be used. This blockchain is local to your system and does not interact with the main Ethereum network.

We have two options to connect to a local blockchain:
Option 1: using command truffle develop
Option 2: using Ganache UI to visualize.
In this blog we use option2 to demonstrate.

We open truffle-config.js, and replace the content with the following:

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

Image description

Next, we open Ganache UI, click QUICKSTART, and ADD PROJECT by selecting truffle-config.js, and click restart:

Image description

Image description

Image description

By finishing the step above, we have launched a local blockchain.

Test the smart contract

We will find the test code has already been done for us at /meta-coin/test.

Image description
Let's run the solidity test in the CLI:

truffle test ./test/metacoin.js
Enter fullscreen mode Exit fullscreen mode

Image description

Compile and deploy the smart contract

Now we compile our smart contract:

truffle compile
Enter fullscreen mode Exit fullscreen mode

Image description

We can now migrate and deploy our smart contracts to the blockchain using the following command:

truffle migrate
Enter fullscreen mode Exit fullscreen mode

This shows the transaction IDs and addresses of the deployed contracts. It also includes a cost summary and real-time status updates.

Image description

In Ganache, click the “BLOCKS”, “TRANSACTIONS”, and “CONTRACTS” to see the transactions that have been processed.

Image description

Image description

Image description

Image description

The MetaMask extension also provides us ETH balance if the first account in Ganache is imported.

Image description

Image description

Image description
Image description

Interact with the smart contract

We use the Truffle Console to interact with the smart contract. Let's run the following command to launch the console:

truffle console
Enter fullscreen mode Exit fullscreen mode

Image description

First, we establish the deployed meta-coin instance and the accounts created by Ganache:

NOTE: > means we are currently working in Truffle Console

> let instance = await MetaCoin.deployed()
> let accounts = await web3.eth.getAccounts()
Enter fullscreen mode Exit fullscreen mode

Image description

We can check the meta-coin balance of the account which has deployed the contract:

> let balance = await instance.getBalance(accounts[0])
> balance.toNumber()
Enter fullscreen mode Exit fullscreen mode

Image description

We can transfer some meta-coin from one account to another:

> instance.sendCoin(accounts[1], 500)
Enter fullscreen mode Exit fullscreen mode

Image description

Check the balance of the meta-coin receiving account:

> let received = await instance.getBalance(accounts[1])
> received.toNumber()
Enter fullscreen mode Exit fullscreen mode

Image description

Check the balance of the meta-coin sending account:

> let newBalance = await instance.getBalance(accounts[0])
> newBalance.toNumber()
Enter fullscreen mode Exit fullscreen mode

Image description

Really COOL isn't it!

References

https://trufflesuite.com/docs/truffle/index.html

truffle Article's
30 articles in total
Favicon
Hardhat vs Truffle: Which One Is the Best for Developing Ethereum dApps?
Favicon
Help Me With Ubuntu Terminal...
Favicon
How to write, test and deploy Ethereum smart contracts using Truffle
Favicon
Create & deploy an ERC-20 token in 15 minutes (Truffle, OpenZeppelin, Goerli)
Favicon
How to Run Ganache in a Browser
Favicon
121 ethereum truffle : Writing automated smart contract tests
Favicon
11Z ethereum truffle : Deploying and interacting with smart contracts
Favicon
11X ethereum truffle : Developing smart contracts
Favicon
how to use web3.js instead of Ethers in react Dapp and connect to hardhat node
Favicon
Truffle React box using functional Components
Favicon
Build a simple dApp using truffle, ganache, Ethers.js and React(1)
Favicon
How To Mint an NFT on Polygon
Favicon
Hardhat vs Truffle: Which one is better for writing Smart Contracts?
Favicon
Interactuar con contratos en Ethereum
Favicon
Tutorial: Play with Truffle & Ganache
Favicon
has no network configuration for its current network id (5777).
Favicon
Roadmap to become a Blockchain developer
Favicon
How to Fork Ethereum and Replay Historical Transactions with Ganache 7 Archive Support
Favicon
Deploy your smart contracts on any Ethereum network
Favicon
TruffleでGoerliネットワークにコントラクトをデプロイする
Favicon
A Guide to Building, Testing, and Deploying your First DApp with Truffle, Ethers.js, Ganache, and React.
Favicon
Introduciendo Web3 con Angular usando Truffle Box — Aplicaciones Descentralizadas al alcance de todos
Favicon
HardHat: "Hola Mundo!" en un Blockchain de Prueba
Favicon
Using React with Truffle
Favicon
Interacting with Truffle
Favicon
Introducing Truffle
Favicon
How to configure Truffle to connect to RSK
Favicon
Avoiding Call Revert Exception Error when accessing Truffle Ganache via Ethers in Node
Favicon
Short, sharp Solidity: pure vs view vs call
Favicon
Decomposing a BigNumber in Truffle Console

Featured ones: