Creating & Deploying a Smart Contract using Truffle framework & Ganache-CLI — Part 2

Romil Jain
Coinmonks
4 min readMay 16, 2019

--

In Part-1, we created and deployed a smart contract using web3js and ganache-cli. In this section, we will deploy the same contract using Truffle framework and Ganache-CLI. Truffle framework has made a lot of things easy and hides the boilerplate code. So let's start —

Ethereum’ Smart Contract using Truffle Framework & Ganache-CLI

Deploying using Truffle Framework and Ganache-CLI

First, we need to install truffle. Let’s install the truffle globally.

In the same terminal window, Create a new project/folder ‘ethereumdapp’ and run below command.

If using the first command, you will see below in the command line

This will set up the complete project. You can see a few more contracts are created under contracts folder. Also, 1_initial_migration.js and 2_deploy_contracts.js are created under the migrations folder. In addition to it, there is truffle-config.js created. We should uncomment the development section under network to connect to Ganache as shown below.

Once this setup is done, then copy the NotarizedDocument.sol file created earlier, into the folder “contracts”. We can delete ‘ConvertLib.sol’ & ‘MetaCoin.sol’ as these are of no use to us.

Next, open the folder “migrations” and in the file named “2_deploy_contracts.js” just add below line of code, remove rest of the lines and save.

Migrations are simply the scripts that’ll help us deploy our contracts to a blockchain.

Let’s compile all the contracts and migrate to deploy them.

Once the contract is deployed you can see block/s have been created in a ganache console. Note the contract address to interact with it. In this case it is 0xc0a80b11dae06421fe2ad2f5a1554c871f72383d

Generally, truffle deploys the contract to the first account provided by ganache. To interact with contract lets go to truffle console and type below commands one by one and keep checking ganache console in another window.

When you get the instance, let’s invoke the first method of NotarizeDocument contract which is notarized and pass some string value.

As soon as you run this in the truffle console, you can see another block created in the ganache console and below in the truffle console.

To check if this document is created, call the checkDocument method of the contract.

This will return true in the console.

Now its time for you to play around with Truffle framework. For sure Truffle has made the things easy by hiding lots of execution which we were able to see using web3js. The complete code can be found here.

So let’s create more contracts and deploy them over the ganache before deploying over the main net. It’s time to BUIDL #buidl

Please share your thoughts, feedback, comments, and clarifications. It will help me to improvise.

If you liked this post, you can help me share it by recommending it below ❤

Follow me on Linkedin and Twitter

Get Best Software Deals Directly In Your Inbox

--

--