Writing Fabric Chaincode in Javascript

We make blockchain happen

Writing Fabric Chaincode in Javascript

With the release of Fabric 1.1 you may now write your chaincode in javascript. Let’s

have a look how it’s done.

  1. Setup
  2. Code chaincode
  3. Deploy chaincode
  4. Run chaincode
  5. Update chaincode

Prerequisites

  • cURL
  • Docker & Docker Compose
  • git

Setup

Setup a fabric, e.g. our minimal fabric setup.

Code chaincode

Your chaincode will always include the two methods.

  • Init(): called when the code is instantiated in your blockchain network
  • Invoke(): Invoke handles all calls by your application

Version 0.0.1 – Basic example

This class contains the basic init and invoke methods as well as simple methods to initalize and query data. Download the example

let Example = class {

async Init(stub) {
.....
}

async Invoke(stub) {
....
}

async initLedger(stub, args) {
.....
}

async queryAll(stub, args) {
.....
}
};

shim.start(new Example());

Deploy chaincode

If you’re using our minimal network you’ll execute the commands directely in the one peer. If you e.g. use multiple peers run the commands in your cli-container.

1. Install

docker exec peer0.org1.fabric.basic.network.com peer chaincode install -n example -v 0.0.1 -l node -p /chaincode/node/examples/0.0.1

Expected result:

DEBU 00d Installed remotely response:<status:200 payload:"OK" >

2. Instantiate

docker exec peer0.org1.fabric.basic.network.com peer chaincode instantiate -l node -n example -v 0.0.1 -C fabricchannel -c '{"Args":["init"]}'

Instantiate will generate a chaincode runtime environment, e.g. in the form of a docker container.
Expected output in the console of the runtime:

=========== Instantiated example chaincode ===========

Run chaincode

Example: Initialize data

docker exec peer0.org1.fabric.basic.network.com peer chaincode invoke -C fabricchannel -n example -c '{"function":"initLedger","Args":[""]}'

Expected output in the terminal:

Chaincode invoke successful. result: status:200

Expected output in the runtime container (see your container logs):

============= START : Initialize Ledger ===========
Added <-->  { name: 'Example 0', value: '101', docType: 'example' }
Added <-->  { name: 'Example 1', value: '202', docType: 'example' }
Added <-->  { name: 'Example 2', value: '303', docType: 'example' }
============= END : Initialize Ledger ===========

Example: Query data

docker exec peer0.org1.fabric.basic.network.com peer chaincode query -C fabricchannel -n example -c '{"function":"queryAll","Args":[""]}'

Expected output in the runtime container (see your container logs):

[ { Key: 'EXAMPLE0',
    Record: { name: 'Example 0', value: '101', docType: 'example' } },
  { Key: 'EXAMPLE1',
    Record: { name: 'Example 1', value: '202', docType: 'example' } },
  { Key: 'EXAMPLE2',
    Record: { name: 'Example 2', value: '303', docType: 'example' } } ]

Update chaincode

Version 0.0.2 – Extended example

Let’s add a queryExample method to request a single record. [Download the example](https://github.com)

async queryExample(stub, args) {
....
}

Deploy the update

1. Install the new version

docker exec peer0.org1.fabric.basic.network.com peer chaincode install -n example -v 0.0.2 -l node -p /chaincode/node/examples/0.0.2

2. Upgrade to the new version

docker exec peer0.org1.fabric.basic.network.com peer chaincode upgrade -n example -v 0.0.2 -C fabricchannel -l node -c '{"Args":[""]}'

Run the new code

docker exec peer0.org1.fabric.basic.network.com peer chaincode query -C fabricchannel -n example -c '{"function":"queryExample","Args":["EXAMPLE1"]}'

Output (see your container logs):

{ fcn: 'queryExample', params: [ 'EXAMPLE1' ] }
Example {}
queryExample
{"name":"Example 1","value":"202","docType":"example"}

This example is based on the Hyperledger fabric-samples

 

One Response

  1. Don Li says:

    AWESOME!

    This one rocks.

    The fabric-samples is a good start to get you familiar with some of the core aspects of Hyperledger Fabric.

    This piece is a perfect drill to apply such learning (via fabric-samples) to practice and very fitting for blockchain specialists coming from web application development background (familiarity with JavaScript among others).

    Keep up your superlative work.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *