Legacy Contract to Smart Contract -Part1

Sunil Suseelan
2 min readNov 26, 2019

--

Understanding HyperLedger Go lang Chaincode — for Distributed Ledger

Hey Reader,

I have been working on with BlockChain for sometime and have written many Smart contracts for various use-case’s and often I come across people who ask about how to write a SmartContract or even questions like what is a smart contract and there are lot of articles on internet that helps one to write BlockChain contracts but they also make it look to difficult thing to do or understand so I thought to write an article about what exactly is a smart contract and how one can be comfortable on not just writing a SmartContract but to have the confidence that he/she are aware of what they are writing.

Defining SmartContract

As you can see the SmartContract can be considered as the transformation of real world business contracts where we can include all the norms, conditions and validations of any business related transactions/events into our chaincodes

In Hyperledger Fabric, chaincode is the SmartContract that runs on the peers and creates transactions. It simply enables users to create transactions in the Hyperledger Fabric network’s shared ledger, update the world state of the assets and can be considered as the only way to interact with the ledger.

Basic API’s & Packages

shim package: “github.com/hyperledger/fabric/core/chaincode/shim

Hyperledger Fabric, Shim provides APIs for the chaincode to access its state variables, transaction context and call other chaincodes. Below are the basic list of API’s that we use to operate on the ledger

  • func (stub *ChaincodeStub) GetState(key string) ([]byte, error) — Simply gets the current state of Key stored in the ledger
  • func (stub *ChaincodeStub) PutState(key string, value []byte) error — Adds a new key or if key already exits then updates the value for that key
  • func (stub *ChaincodeStub) DelState(key string) error — Just updates the IsDelete flag for the key to true and removes the entry only from worldstate.

Key methods for Chaincode

  • Init: Called when a chaincode receives an instantiate or upgrade transaction. This is where you will initialize any application state.
  • Invoke: Called when the invoke transaction is received to process any transaction proposals.

This were the Basics one should know to start writing a contract in the continued article we will see a sample and take next step towards expertise.

Thanks for Reading.

Published By

Suneel Suseelan

https://www.linkedin.com/in/suneel-suseelan/

Originally published at https://www.linkedin.com.

--

--

Sunil Suseelan

Let me Brag about myself, something that no one does on social platform ;). Well to be frank am not a super technical guy, but I strive to do my best