Blockchain Development Tips

How to start using the Truffle framework?

How can we simplify the smart contract process with the Truffle framework? A complete guide to using it with an example.

Arnish Gupta
Web3 Magazine

--

Truffle is the most popular framework for solidity language. We can compile the smart contract and deploy it to the blockchain. We can write testing logic and execute it after compilation. It makes the developer’s life very easy.

Photo by Tolga Ulkan on Unsplash

Here we will learn the below points about the Truffle framework.

  • Prerequisites
  • How to Setup Truffle Framework?
  • How to write a Smart Contract?
  • How to write Test cases?
  • How to compile a contract and deploy it?
  • What is Ganache and how to set up?
  • How to deploy the smart contract on Ganache?

So gird up and come with me on this journey.

Prerequisites

How to Setup Truffle Framework?

  • Installation: npm install -g truffle (If it gives an error then you should try npm install -g truffle@4.1.17
  • To create a basic structure: truffle init (Note: Execute this command in the empty directory) It will create the structure automatically.
  • Truffle framework gives many templates for projects. You can check it on the below link.
https://github.com/truffle-box

Let’s take a sample coin project: truffle unbox metacoin It creates three directories and one configuration file.

  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment files
  • test/: Directory for test files for testing your application and contracts
  • truffle.js: Truffle configuration file

How to write a Smart Contract?

  • Once the truffle commands are finished, A contract directory is created for smart contracts. We can write the contract in this folder. In the metacoin project MetaCoin.sol file exists and the contract is written in this file.
  • We will create a public variable and a setter method for changing the variable value. So we modify this contract with the below changes.
string public name; event NameChanged(string name);constructor() { …   name = “Arnish Gupta”; 
}
function setName(string memory _name) public {
name = _name;
emit NameChanged(_name);
}

After these changes MetaCoin.sol file looks like this.

How to write Test cases?

  • Test cases will be written in the test folder. Here we have a metacoin.js file that has to assert conditions.
  • So as per we modified MetaCoin.sol file so we write the test condition that is first we change the variable value using the setter method then compare the variable value.
it('should value of name is Arnish Blockchain World ', async () => {const _name = "Arnish Blockchain World";const metaCoinInstance = await MetaCoin.deployed();await metaCoinInstance.setName(_name);const newName = await metaCoinInstance.name.call()assert.equal(newName, _name, "Name variable was not as expected " + _name);});
  • We are done and the time is now to run the test cases and feel the happiness.
// for running all the test casestruffle test
  • See the below image to verify our test case.
Arnish Gupta (Truffle test case)

How to compile a contract and deploy it?

  • Compilation of the contract is to convert the solidity code into EVM support language.
// Truffle compile commandtruffle compile

The first time we compile the project then all the contracts are compiled and Upon subsequent runs, the truffle will compile only changed contracts, If we want to compile the project then run the compile command with the --all option.

  • To deploy the contract into a blockchain we will have the command and you will see the output as per the below image.
truffle migrate
  • Yes, our MetaCoin contract deployed and we get the contract address: 0xebde8ff3a9b561e23aa2ab2638d3e9bccaca71ef

What is Ganache and how to set up?

  • Ganache is a tool that provides the local ethereum blockchain environment. It gives addresses and private keys with ETH amount.
  • Ganache is available on UI and CLI commands. It has the same parameter as RPC URL, Network Id, etc.
  • We can customize all the things as per our requirements and test all the contracts locally without wasting time.

Setup Ganache

  • You can download the Ganache tool from here.
  • It will look like the below image. Select the quickstart button (It takes some time)and you will see 10 Addresses with a 100 ETH Balance.
Arnish Gupta Ganache
  • You can see the RPC server detail, network id, etc. You can change this in the setting tab.

How to deploy the smart contract on Ganache?

  • Once the ganache is ready then you have to know about the three parameters.
    1. RPC URL (Default: 127.0.0.1)
    2. Port (Default: 7545)
    3. Network Id (Default: 5777)
  • Go to the truffle folder, Open the truffle-config.js file. Uncomment the network object and change the value as per the ganache tool.
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: 5777
},
test: {
host: "127.0.0.1",
port: 9545,
network_id: 5777
}
  • Run the truffle migrate command. The contract will be deployed and you can see the Ganache tool there was ETH deducted from the first address.

That’s it. Congratulation!! you have achieved first milestone on the land of blockchain. Stay tuned with me for more about blockchain learning.

If you are facing any challenges then don’t worry I’m your umbrella :) We will solve the problem together.

Thank you for reading.. !!

Happy Learning.

--

--

Arnish Gupta
Web3 Magazine

Learn Blockchain with me every Monday at 9AM (GMT+5:30).