Introduction to Truffle Framework
Written by Dr. Amol Dhumane
-----------------------------------------------------------------------------------------------------------------------------
Install truffle:
- Go to terminal
- Write a command ‘npm install –g truffle’ (g is used for global installation)
- Check truffle version using ‘truffle version’ command
----------------------------------------------------------------------------------------------------------------------
- ‘truffle init’ command is used to setup the default structure of the truffle project
- ‘truffle init’ command creates 3 folders: ‘contracts’, ‘migrations’ and ‘test’ folder along with them two javascript files: truffle-config.js and truffle.js
- ‘contract’ folder is used to place the smart contracts that you have written using solidity language.
- ‘migration’ folder contains the information (javascript file) about how to deploy the smart contract on the blockchain network. (We need to create this file before migrating smart contract on the testnet)
- ‘test’ folder is used to keep the smart contract test files that you are going to use. You can write these test files using ‘javascript’ or ‘solidity’ language. (In this blog we will write it using javascript)
- To check all the commands of truffle, use the help command ‘truffle’. It will list all the truffle commands with proper descriptions on the command prompt as given below

- Create truffle contract ‘HelloWorld.sol’ using following command
- > truffle create contract HelloWorld
- This will create a solidity file ‘HelloWorld.sol’ in the contract folder of truffle (‘.sol’ files are the smart contract files written using solidity language)
- Open the MyContract.sol using appropriate editor (e.g. sublime editor) and write the following code into file
- Now let’s deploy your smart contract
- Go to migration folder and create a deployment script ‘1_HelloWorld.js’ with the following code. (You may use a command ‘truffle create migration 1_HelloWorld.js’ on the command prompt)
- At the beginning of the migration, we tell Truffle which contracts we'd like to interact with via the artifacts.require() method. Here ‘artifacts.require()’ method is provided by Truffle that allows us to request a usable contract abstraction for a specific Solidity contract.
- Now lets compile our smart contract in to byte code which is understandable to EVM (Ethereum Virtual Machine)
- > truffle compile
- After compilation 2 json files (HelloWorld.json and Migrations.json) will be created in the build folder. These json files are further needed by the artifacts.
- The last phase is to deploy our contract on the testnet and interact with it.
- Use ‘truffle develop’ command. It will first start the local testnet as given below.
The local testnet will be available at port number 9545 with 10 accounts and their private keys. It opens an interactive prompt for interacting with the smart contract.
1. Now run the migrations that we prepared before (as shown in below image)
With this we can say that our smart contract is live on the local testnet. Now we can interact with the smart contract.
- Now create an instance to HelloWorld smart contract using
- truffle(develop)> const instance = await HelloWorld.deployed();
- Check the address of the instance using
- truffle(develop)>instance.address
- Now call the hello method from the contract using
- truffle(develop)> instance.hello()
- This will generate the output as 'Hello World'. (refer the following image)






Comments
Post a Comment