{"id":2733,"date":"2018-12-15T23:40:23","date_gmt":"2018-12-15T18:10:23","guid":{"rendered":"http:\/\/navveenbalani.dev\/?p=2733"},"modified":"2019-12-18T17:21:58","modified_gmt":"2019-12-18T11:51:58","slug":"smart-contract-for-trade-finance-application","status":"publish","type":"post","link":"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/","title":{"rendered":"Smart Contract for Trade Finance application"},"content":{"rendered":"\n<p>In Fabric terminology, smart contracts are chaincodes. It has business\nfunctions that are invoked as part of transactions to query and update the\nstate of the ledger. Our chaincode will be a smart contract termed as\n&#8216;tradefinancecc&#8217;. It consists of necessary business methods that are invoked to\ncarry out the trade finance process. Chaincodes are installed on each peer and\nrun as an isolated docker container. The chaincode is typically stored in the <em>GOPATH\/src<\/em> folder of your workspace. In\nthis stage, we will walk you through each method of our smart contract and\nunderstand the trade finance application workflow. We will also look at how one\ncan use the core chaincode interface named <em>ChaincodeStubInterface<\/em>\nto read and update the ledger state. <\/p>\n\n\n\n<p>Before we look at each business method, let&#8217;s look at the core methods of the chaincode. Every chaincode implements Chaincode interface (part of shim package) which has two core methods viz. <em>Init()<\/em> and <em>Invoke().<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\u2026\n\t\"github.com\/hyperledger\/fabric\/core\/chaincode\/shim\"\n\u2026\n)\n<\/code><\/pre>\n\n\n\n<p>As they are part of Chaincode interface, we need\nto import the said interface and thereby implementing <em>Init<\/em> and <em>Invoke<\/em> method.\nLet&#8217;s look closely at the <em>Init<\/em>\nmethod.<\/p>\n\n\n\n<h4><a>The Init()\nmethod<\/a><\/h4>\n\n\n\n<p>The Init method is invoked when the chaincode is instantiated by the client application. The below code shows the Init method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (t *TradeContract) Init(stub shim.ChaincodeStubInterface) pb.Response {\n\treturn setupTrade(stub);\n}\n\nfunc setupTrade(stub shim.ChaincodeStubInterface) pb.Response {\n\t_, args := stub.GetFunctionAndParameters()\n\ttradeId := args[0]\n\tbuyerTaxId := args[1]\n\tsellerTaxId := args[2]\n\tskuid := args[3]\n\ttradePrice,_ := strconv.Atoi(args[4])\n\tshippingPrice,_ := strconv.Atoi(args[5])\n\t\t\n\ttradeContract := trade {\n\t\tTradeId: tradeId, \n\t\tBuyerTaxId: buyerTaxId, \n\t\tSellerTaxId: sellerTaxId, \n\t\tSkuid: skuid,\n\t\tTradePrice: tradePrice,\n\t\tShippingPrice: shippingPrice,\n\t\tStatus: \"Trade initiated\"}\n\n\ttcBytes, _ := json.Marshal(tradeContract)\n\tstub.PutState(tradeContract.TradeId, tcBytes)\n\t\n\treturn shim.Success(nil)\n}\n<\/code><\/pre>\n\n\n\n<p>The Init method is passed a &#8216;stub&#8217; object that\nreferences ChaincodeStubInterface interface. The said interface is typically\nused to query\/update the ledger. If you recall, we mentioned earlier in this\nchapter that our smart contract would focus on trade finance workflow starting\nfrom LOC creation stage. It means our smart contract is already initialized\nwith initial trade details. We have used <em>Init<\/em>\nmethod to initialize our trade contract with predefined values passed from the\nclient application and fetched using <em>stub.GetFunctionAndParameters<\/em>\nmethod call. These values signify that the trade has reached a point where\nbuyer and seller have agreed and accepted on the trade terms. The values\nreflect buyer and seller details, order and product details and more\nimportantly, we now have a trade id. The newly initialized trade contract is\nthen stored in the ledger using <em>ChaincodeStubInterface.PutState<\/em>\nmethod. Once the trade is initialized, we indicate it by returning the success\nstatus as OK.<\/p>\n\n\n\n<h4><a>The\nInvoke() method<\/a><\/h4>\n\n\n\n<p>The next core method is Invoke. The Invoke method as the name suggests\nwill invoke our business methods. It will delegate the call to an appropriate\nbusiness method based on the method name passed in the parameter. Let\u2019s look at\nthe code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (t *TradeContract) Invoke(stub shim.ChaincodeStubInterface) pb.Response {\n\tfunction, args := stub.GetFunctionAndParameters()\n\tif function == \"createLOC\" {\n\t\treturn t.createLOC(stub, args)\n\t} else if function == \"approveLOC\" {\n\t\n\t\treturn t.approveLOC(stub, args)\n\t} else if function == \"initiateShipment\" {\n\t\n\t\treturn t.initiateShipment(stub, args)\n\t} else if function == \"deliverGoods\" {\n\t\n\t\treturn t.deliverGoods(stub, args)\n\t} else if function == \"shipmentDelivered\" {\n\t\n\t\treturn t.shipmentDelivered(stub, args)\n\t} else if function == \"query\" {\n\t\n\t\treturn t.query(stub, args)\n\t}\n\n\treturn shim.Error(\"Invalid function name\")\n}\n<\/code><\/pre>\n\n\n\n<p>First, we get the function name passed as an argument from the client\napplication. This is done using <em>ChaincodeStubInterface.GetFunctionAndParameters()<\/em>\nmethod. Then based on the value retrieved, we invoke the appropriate business\nmethod. To sum up, the below table depicts our business methods that will\nfulfill our trade finance contract.<\/p>\n\n\n\n<table class=\"wp-block-table has-fixed-layout\"><tbody><tr><td>\n  <strong>Method Name<\/strong>\n  <\/td><td>   <strong>Description<\/strong>                    <\/td><td>\n  <strong>Contract State<\/strong>\n  <\/td><\/tr><tr><td>\n  createLOC\n  <\/td><td>Importer bank creates the LOC to be sent to exporter bank. Contract   is updated with the importer bank details.   <\/td><td>LOC <br>created   <\/td><\/tr><tr><td>\n  approveLOC\n  <\/td><td>Exported upon seeing and verifying LOC, approves the same. Contract is updated with the exporter bank details.   <\/td><td> LOC approved   <\/td><\/tr><tr><td>\n  initiateShipment\n  <\/td><td>The seller upon LOC <br>approval will initiate .  the shipment process. <br>The contract will <br>update the delivery <br>date one month from <br>the current date.   <\/td><td>\n  Shipment initiated\n  <\/td><\/tr><tr><td>\n  deliverGoods\n  <\/td><td>Goods are then <br>delivered by the <br>shipper. <br>Contract Is updated <br>with the shipper<br>details.   <\/td><td>\n  BOL created\n  <\/td><\/tr><tr><td>\n  shipmentDelivered\n  <\/td><td>Shipment is finally <br>received by the buyer which signifies the   <br>completion of trade   <\/td><td>Trade completed   <\/td><\/tr><\/tbody><\/table>\n\n\n\n<p> In the <a href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/deploying-trade-finance-application-on-hyperledger\/\">next article<\/a><a href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/end-to-end-test-execution-of-trade-finance-application\/\">,<\/a> we will implement our network topology as defined in stage two of the design and implementation approach <\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8216;tradefinancecc&#8217;. It consists of necessary business methods that are invoked to carry out the trade finance process. Chaincodes are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,174],"tags":[286],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.0.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Smart Contract for Trade Finance application - Current and Future Technology Trends by Navveen Balani<\/title>\n<meta name=\"description\" content=\"Smart Contract for Trade Finance application - Articles\" \/>\n<link rel=\"canonical\" href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Smart Contract for Trade Finance application - Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"og:description\" content=\"Smart Contract for Trade Finance application - Articles\" \/>\n<meta property=\"og:url\" content=\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/\" \/>\n<meta property=\"og:site_name\" content=\"Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-15T18:10:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-18T11:51:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"4 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/navveenbalani.dev\/#website\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Current and Future Technology Trends by Navveen Balani\",\"description\":\"Current and Future Technology Trends by Navveen Balani\",\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/navveenbalani.dev\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/#webpage\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/\",\"name\":\"Smart Contract for Trade Finance application - Current and Future Technology Trends by Navveen Balani\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/#website\"},\"datePublished\":\"2018-12-15T18:10:23+00:00\",\"dateModified\":\"2019-12-18T11:51:58+00:00\",\"description\":\"Smart Contract for Trade Finance application - Articles\",\"breadcrumb\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/\",\"name\":\"Smart Contract for Trade Finance application\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/#webpage\"},\"author\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"headline\":\"Smart Contract for Trade Finance application\",\"datePublished\":\"2018-12-15T18:10:23+00:00\",\"dateModified\":\"2019-12-18T11:51:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/smart-contract-for-trade-finance-application\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"keywords\":\"blockchain-guide\",\"articleSection\":\"Articles,Blockchain\",\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\",\"name\":\"Navveen\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/07\/navveen_balani.jpeg\",\"width\":200,\"height\":200,\"caption\":\"Navveen\"},\"logo\":{\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\"},\"sameAs\":[\"http:\/\/naveenbalani.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2733"}],"collection":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/comments?post=2733"}],"version-history":[{"count":5,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2733\/revisions"}],"predecessor-version":[{"id":2768,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2733\/revisions\/2768"}],"wp:attachment":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media?parent=2733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/categories?post=2733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/tags?post=2733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}