In Fabric terminology, smart contracts are chaincodes. It has business functions that are invoked as part of transactions to query and update the state of the ledger. Our chaincode will be a smart contract termed as ‘tradefinancecc’. It consists of necessary business methods that are invoked to carry out the trade finance process. Chaincodes are installed on each peer and run as an isolated docker container. The chaincode is typically stored in the GOPATH/src folder of your workspace. In this stage, we will walk you through each method of our smart contract and understand the trade finance application workflow. We will also look at how one can use the core chaincode interface named ChaincodeStubInterface to read and update the ledger state.
Before we look at each business method, let’s look at the core methods of the chaincode. Every chaincode implements Chaincode interface (part of shim package) which has two core methods viz. Init() and Invoke().
package main
import (
…
"github.com/hyperledger/fabric/core/chaincode/shim"
…
)
As they are part of Chaincode interface, we need to import the said interface and thereby implementing Init and Invoke method. Let’s look closely at the Init method.
The Init() method
The Init method is invoked when the chaincode is instantiated by the client application. The below code shows the Init method:
func (t *TradeContract) Init(stub shim.ChaincodeStubInterface) pb.Response {
return setupTrade(stub);
}
func setupTrade(stub shim.ChaincodeStubInterface) pb.Response {
_, args := stub.GetFunctionAndParameters()
tradeId := args[0]
buyerTaxId := args[1]
sellerTaxId := args[2]
skuid := args[3]
tradePrice,_ := strconv.Atoi(args[4])
shippingPrice,_ := strconv.Atoi(args[5])
tradeContract := trade {
TradeId: tradeId,
BuyerTaxId: buyerTaxId,
SellerTaxId: sellerTaxId,
Skuid: skuid,
TradePrice: tradePrice,
ShippingPrice: shippingPrice,
Status: "Trade initiated"}
tcBytes, _ := json.Marshal(tradeContract)
stub.PutState(tradeContract.TradeId, tcBytes)
return shim.Success(nil)
}
The Init method is passed a ‘stub’ object that references ChaincodeStubInterface interface. The said interface is typically used to query/update the ledger. If you recall, we mentioned earlier in this chapter that our smart contract would focus on trade finance workflow starting from LOC creation stage. It means our smart contract is already initialized with initial trade details. We have used Init method to initialize our trade contract with predefined values passed from the client application and fetched using stub.GetFunctionAndParameters method call. These values signify that the trade has reached a point where buyer and seller have agreed and accepted on the trade terms. The values reflect buyer and seller details, order and product details and more importantly, we now have a trade id. The newly initialized trade contract is then stored in the ledger using ChaincodeStubInterface.PutState method. Once the trade is initialized, we indicate it by returning the success status as OK.
The Invoke() method
The next core method is Invoke. The Invoke method as the name suggests will invoke our business methods. It will delegate the call to an appropriate business method based on the method name passed in the parameter. Let’s look at the code below:
func (t *TradeContract) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if function == "createLOC" {
return t.createLOC(stub, args)
} else if function == "approveLOC" {
return t.approveLOC(stub, args)
} else if function == "initiateShipment" {
return t.initiateShipment(stub, args)
} else if function == "deliverGoods" {
return t.deliverGoods(stub, args)
} else if function == "shipmentDelivered" {
return t.shipmentDelivered(stub, args)
} else if function == "query" {
return t.query(stub, args)
}
return shim.Error("Invalid function name")
}
First, we get the function name passed as an argument from the client application. This is done using ChaincodeStubInterface.GetFunctionAndParameters() method. Then based on the value retrieved, we invoke the appropriate business method. To sum up, the below table depicts our business methods that will fulfill our trade finance contract.
Method Name | Description | Contract State |
createLOC | Importer bank creates the LOC to be sent to exporter bank. Contract is updated with the importer bank details. | LOC created |
approveLOC | Exported upon seeing and verifying LOC, approves the same. Contract is updated with the exporter bank details. | LOC approved |
initiateShipment | The seller upon LOC approval will initiate . the shipment process. The contract will update the delivery date one month from the current date. | Shipment initiated |
deliverGoods | Goods are then delivered by the shipper. Contract Is updated with the shipper details. | BOL created |
shipmentDelivered | Shipment is finally received by the buyer which signifies the completion of trade | Trade completed |
In the next article, we will implement our network topology as defined in stage two of the design and implementation approach