March 15, 2019 Abdul Sami

Guide to setting up your first Hyperledger Fabric network (Part 2)


In this part, we are going to write a chaincode (smart contract), install and instantiate it, and interact with it. We are going to use the repository that we developed in the previous guide. The repository contains both the network and chaincode as well as useful scripts to automate crucial tasks.

What is chaincode?

Prerequisites to writing chaincode

  • Install Typescript.
npm install typescript -g
  • Create a new directory at the root of the project called chaincode and set up a new Typescript project.
mkdir chaincode/node
cd chaincode/node
tsc --init
  • Create a package.json file inside the chaincode/node directory

  • Create tsconfig.json file.

  • Create yarn.lock file to ensure that you have the right versions of packages being installed.

  • Install all the npm packages.
npm install

Writing chaincode

If you have experience writing smart contracts using Solidity in Ethereum or other public blockchain, you might notice similarities. We have a chaincode class named FirstChaincode which has two functions that are available to be executed from outside the ledger. initLedger function adds car entries into the ledger. The second function queryAllCars returns all the car objects that have been initialized.

We use the chaincode utils developed by the TheLedger. You will find the chaincode Node.js SDK to be quite different if you look at the official chaincode samples developed by Hyperledger Fabric. These utilities are intended to make writing chaincode easier.

We are going to start the network that we built in the previous article, create and join channels, install and instantiate the chaincode, and finally call functions on the chaincode.

Building the network

Starting the network

docker-compose -f docker-compose.yml up -d

Creating and joining the channel

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/" peer channel create -o -c mychannel -f /etc/hyperledger/configtx/channel.txdocker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/" peer channel join -b mychannel.block

Installing the chaincode

  • Building; builds the source code to be installed on peers.
  • Installing; installs the chaincode on peers.
  • Instantiating; instantiates the chaincode interface to become public in the scope of the channel.

Go to the chaincode/node directory.


yarn run clean
yarn run build


docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode install -n "firstchaincode" -v "1.0" -p "/opt/gopath/src/" -l "node"


docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode instantiate -o -C mychannel -n "firstchaincode" -l "node" -v "1.0" -c '{"function":"init","Args":["'1.0'"]}'

Now, that we have the network deployed and chaincode installed and instantiated. We will interact with the chaincode.

Executing a transaction

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode invoke -o -C mychannel -n "firstchaincode" -c '{"function":"initLedger","Args":[""]}'

You should see the following message.

2019-03-14 08:56:24.608 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 04f Chaincode invoke successful. result: status:200

Querying the chaincode

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/" cli peer chaincode query -o -C mychannel -n "firstchaincode" -c '{"function":"queryAllCars","Args":[""]}'

If you see an output like this, your invoke function was successful.



If you want to get your hands on the code, you can get it here.

Get in touch

, , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *