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

0 47


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.

$ npm install -g truffle

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

$ truffle unbox webpack
or
$ truffle init

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

$ Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile:              truffle compile
Migrate:              truffle migrate
Test contracts:       truffle test
Run linter:           npm run lint
Run dev server:       npm run dev
Build for production: npm run build

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.

$ truffle compile
$ truffle migrate --network development
or
$ truffle migrate

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

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'development'
> Network id: 1557978469538
> Block gas limit: 0x6691b7
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x9e9d9d02b09913c18b5973c20ebd25386b647bc7e837a2d2a1486fd125ebd3e7
> Blocks: 0 Seconds: 0
> contract address: 0x7B64768252203353c47DE3a35F157E7d1Be1C182
> block number: 1
> block timestamp: 1557979380
> account: 0xce599E446B1a8FEA3905df54224ebCACCbb03D41
> balance: 99.99430312
> gas used: 284844
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00569688 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00569688 ETH
2_deploy_contracts.js
=====================
Deploying 'NotarizedDocument'
-----------------------------
> transaction hash: 0x720404200b6882e65552d5e108b31b93f0427484ebbd056fd33565a1fe10a5f9
> Blocks: 0 Seconds: 0
> contract address: 0x2cD3d3090b4C62840B7B46E5237Fe8E951De78b2
> block number: 3
> block timestamp: 1557979380
> account: 0xce599E446B1a8FEA3905df54224ebCACCbb03D41
> balance: 99.98727016
> gas used: 309614
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00619228 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00619228 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.01188916 ETH

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.

$ truffle console
truffle(development)> NotarizedDocument.deployed()
truffle(development)> let instance = NotarizedDocument.deployed()

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

truffle(development)> instance.notarize("Hello Romil!!")

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.

{ tx:  '0x6da9b7c2509118233f564c86786d4c7e9206523d9bcecc3e80ec22b9edcef58e',
receipt:
{ transactionHash: '0x6da9b7c2509118233f564c86786d4c7e9206523d9bcecc3e80ec22b9edcef58e',
transactionIndex: 0,
blockHash: '0xcc169d76a0098aa7b859ae1d6d6ed67920a6d15d0281c26ae0507446b6b16bee',
blockNumber: 5,
from: '0xce599e446b1a8fea3905df54224ebcaccbb03d41',
to: '0x2cd3d3090b4c62840b7b46e5237fe8e951de78b2',
gasUsed: 45005,
cumulativeGasUsed: 45005,
contractAddress: null,
logs: [],
status: true,
logsBloom:'',
v: '0x1c',
r:'',
s:'',
rawLogs: [] },
logs: [] }

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

truffle(development)> instance.checkDocument("Hello Romil!!")

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.

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

You might also like

Pin It on Pinterest

Share This

Share this post with your friends!

WhatsApp chat