19. 编写nodejs链码

  1. const shim = require('fabric-shim');
  2. const Chaincode = class{
  3. //链码初始化操作
  4. async Init(stub){
  5. var ret = stub.getFunctionAndParameters();
  6. var args = ret.params;
  7. var a = args[0];
  8. var aValue = args[1];
  9. var b = args[2];
  10. var bValue = args[3];
  11. await stub.putState(a,Buffer.from(aValue));
  12. await stub.putState(b,Buffer.from(bValue));
  13. return shim.success(Buffer.from('heima chaincodinit successs'));
  14. }
  15.  
  16. async Invoke(stub){
  17. let ret = stub.getFunctionAndParameters();
  18. let fcn = this[ret.fcn];
  19. return fcn(stub,ret.params);
  20. }
  21. //查询操作
  22. async query(stub,args){
  23. let a = args[0];
  24. let balance = await stub.getState(a);
  25. return shim.success(balance);
  26. }
  27.  
  28. };
  29. shim.start(new Chaincode());