Smart Contract (HelloWorld)

This is my first post on Blockchain and will try to cover briefly terms most commonly used when talking about Blockchain and will build, deploy and query HelloWorld smart contract.

So what is Blockchain, is it Crypto currency like Bitcoin or some thing else. Blockchain is technology that is decentralized nodes running peer to peer across the globe that have copy of transactions. Blockchain can be public and private. There are many good resources available that go into detail how Blockchain works. I will not go into workings of Blockchain.

For this write up my focus is on Smart Contracts. So what are smart contracts. They are reusable code that resides on decentralised blockchain network. When ever there is change in state of data new immutable transaction record is created.

For this topic I will be using Ethereum ( Ethereum is one such decentralized, open-source blockchain that supports smart contracts. Solidity is the programing language of Ethereum in which smart contracts are written.

Lets setup development environment.

  • Visual Studio Code with Solidity extension
  • Node.js®
  • Ganache
  • Truffle
  • Metamask

Create folder helloworld and go to project folder and run following commands

  • npm init
  • npm install truffle -save-dev
  • ./node_modules/.bin/truffle init

After successfully running above commands we will get following folders in out project folder

  • contracts — where our smart contract code will go
  • migrations — will have migration scripts
  • test — test code

Lets create our first smart contract HelloWorld. and save this file under contracts folder as HelloWorld.sol

pragma solidity >=0.4.22 <0.9.0;

// Smart Contract
contract HelloWorld {
    // State variable
    string public message;

    constructor(string memory greeting) public {
        message = greeting;

    function update(string memory greeting) public {
        message = greeting;

To compile this smart contract we will run following command

  • ./node_modules/.bin/truffle compile

We should see following out put when compiled successfully

Compiling your contracts...
> Compiling ./contracts/HelloWorld.sol
> Artifacts written to /usesr/acol/helloworld/build/contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang

Lets deploy this contract now to our Ganache blockchain, make sure Ganache is running before deploying

  • ./node_modules/.bin/truffle migrate –reset

Once deployed we will see transaction on blockchain for deploying smart contract. Now lets start interacting with our smart contract, for that we will execute following command to launch truffle console

  • ./node_modules/.bin/truffle console

On successful completion of command we will have truffle command line console prompt truffle(development)>

Lets get address of out contract

  • truffle(development)>HelloWorld.address
  • This should print some thing like 0x67224dD3AXe6c00dc829D129191B9761eacE55BA

To set new greeting message lets update message on smart contract

  •“0x67224dD3AXe6c00dc829D129191B9761eacE55BA”).then(function(instance) {return instance.update(“Good Morning”)})
  • Above command will set message and return following transaction details
  tx: '0x647fab729e00b4dcb0b825290914d8a54cb4bd705e04479d070e90cd20badcb8',
  receipt: {
    transactionHash: '0x647fab729e00b4dcb0b825290914d8a54cb4bd705e04479d070e90cd20badcb8',
    transactionIndex: 0,
    blockHash: '0x3e308950c47c89c90d68856aabf855d78a2dc880b0da277175c9c3b28f32b915',
    blockNumber: 8,
    from: '0xc10x7ade43bb7584c61d62458905e7dd3cb75d2a',
    to: '0x67224dD3AXe6c00dc829D129191B9761eacE55BA',
    gasUsed: 29157,
    cumulativeGasUsed: 29157,
    contractAddress: null,
    logs: [],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    rawLogs: []
  logs: []

To retrieve our message we will run following command

  •“0x67224dD3AXe6c00dc829D129191B9761eacE55BA”).then(function(instance) {return})

This will print ‘Good Morning’

Now if we want to update this message we will reissue update command with different message

  •“0x67224dD3AXe6c00dc829D129191B9761eacE55BA”).then(function(instance) {return instance.update(“Good Evening”)})

This will update state variable and will create new transaction. If we rerun our command to get message we will get new value ‘Good Evening’.

We can see as we keep changing state of smart contract, new transaction keep getting generated. But what if we want to see chronology of changes that happened to state of contract. We can not issue command to get previous transaction value as Blockchain is not like other databases where you can query previous values this does not mean this cant be done. Ethereum provides us with events in smart contracts I will cover this topic in upcoming posts and how to capture state changes.

What I have not covered in Migration script and and Test scripts.