[OBP]
This post is to show how to write a chain code in NodeJS. Since version 1.2, the opensource Hyperledger fabric blockchain framework supports nodeJS to write chain codes. As Oracle Blockchain Platform (OBP) built on Hyperledger fabric and it upgrades to Hyperledger fabric 1.3 (latest version), we can also write chain codes using NodeJS in OBP.
Let's see a sample chain code. Below is the procedure to write and deploy a chain code into OBP written in Node JS.
This post is to show how to write a chain code in NodeJS. Since version 1.2, the opensource Hyperledger fabric blockchain framework supports nodeJS to write chain codes. As Oracle Blockchain Platform (OBP) built on Hyperledger fabric and it upgrades to Hyperledger fabric 1.3 (latest version), we can also write chain codes using NodeJS in OBP.
Let's see a sample chain code. Below is the procedure to write and deploy a chain code into OBP written in Node JS.
- Create a package.json file with the basic details and dependencies of the libraries
- Create node js file/files (.js) and configure them to the above package.json
- Zip the folder with both js and package.json files
- Click on "Deploy chain code" in OBP under "Chaincodes" tab and select the zip file
Note: You just need to create package.json, not required to run npm commands to install the dependent libraries in the folder before packaging(zip) the project as the OBP will run internally once you deploy the code.
Below is the sample package.json file.
{
"name": "reviews",
"version": "1.0.0",
"description": "Chaincode implemented in node.js",
"engines": {
"node": ">=8.4.0",
"npm": ">=5.3.0"
},
"scripts": { "start" : "node reviews.js" },
"engine-strict": true,
"license": "Apache-2.0",
"dependencies": {
"fabric-shim": "~1.1.0"
}
}
Below is the sample Node js file.
const shim = require('fabric-shim');
const Chaincode = class {
async Init(stub) {
return shim.success();
}
async Invoke(stub) {
let ret = stub.getFunctionAndParameters();
let method = this[ret.fcn];
console.log("Inside invoke. Calling method: " + ret.fcn);
if (!method) {
shim.error(Buffer.from('Received unknown function ' + ret.fcn + ' invocation'));
}
try {
let payload = await method(stub, ret.params);
return shim.success(payload);
} catch (err) {
console.log(err);
return shim.error(err);
}
}
//Method to save or update a user review to a product
async saveReview(stub, args) {
console.log("inside saveReview: " + JSON.stringify(args));
if (args.length < 3) {
throw 'Incorrect number of arguments. Expecting productid,userid and rating.';
}
var assetReview = {};
assetReview.productid = args[0];//String
assetReview.userid = args[1];//String
assetReview.rating = args[2];//Number
assetReview.comment = args[3];//String
await stub.putState(assetReview.userid, Buffer.from(JSON.stringify(assetReview)));
}//End of method
}
shim.start(new Chaincode());
Note: In the above js sample, the first statement requires fabric-shim package which is mandatory to execute the chain code.
Nice Article you have posted here. Thank you for giving this innovative information and please add more in future.
ReplyDeleteSpoken English Classes in Chennai
Best Spoken English Class in Chennai
English Coaching Classes in Chennai
french courses in chennai
spoken english class
spoken english course
TOEFL Classes in Chennai
spoken english class in vadapalani
spoken english class in thiruvanmiyur
spoken english class in t nagar
Great Article blockchain projects for students
ReplyDeleteIEEE Projects for Engineering Students
JavaScript Training in Chennai
Networking Projects
JavaScript Training in Chennai
Excellent blog thanks for sharing the valuable information..it becomes easy to read and easily understand the information.
ReplyDeleteUseful article which was very helpful. also interesting and contains good information.
to know about python training course , use the below link.
Python Training in chennai
Python Course in chennai
Article is so convincing that I never stop myself to say something about it. You’re doing a great job Man, Keep it up.
ReplyDeleteJava Training in Chennai
Java Course in Chennai