[hyperledger-composer]实现一个最简单的转帐功能

 

namespace org.acme.shipping.perishable
abstract participant Person identified by id {
  o String id
  o String name
}
participant Man extends Person {
  o Double account
}


transaction SetupDemo {
}
transaction P2P {
  o Double account
/*   地址引用 
  引用传递:将修改的值更新到区块链上,没有操做的值不会更新到区块链上
  {
    "$class": "org.acme.shipping.perishable.P2P",
    "account": "12312",
    "a": "resource:org.acme.shipping.perishable.Man#1",
    "b": "resource:org.acme.shipping.perishable.Man#2"
  } 
*/
  -->Man a 
  -->Man b 


/** 
值传递方式:会将入参所有的更新到区块链上
{
  "$class": "org.acme.shipping.perishable.P2P",
  "account": 0,
  "a": {
    "$class": "org.acme.shipping.perishable.Man",
    "account": 0,
    "id": "6520",
    "name": ""
  },
  "b": {
    "$class": "org.acme.shipping.perishable.Man",
    "account": 0,
    "id": "6560",
    "name": ""
  }
}
 */
  // o Man a 
  // o Man b 

}

 

/* global getParticipantRegistry getAssetRegistry getFactory */

/**
 * 执行一个转帐的操做,没有进行历史留痕
 * 注意:
 *  一、P2P交易中若是要进行参数传递,须要指定在cto的P2P定义中指定参数类型
 *      好比:1)传递的是参与者Man,则要用"-->Man a"指定类型
 *           2)转帐的金额account,涉及到计算操做,Man、P2P中应该定义为double类型
 * 二、getParticipantRegistry中的update方法是将最新的值更新到对应的参与者数据中
 *  * Initialize some test assets and participants useful for running a demo.
 * @param {org.acme.shipping.perishable.P2P} p2p2 - the p2p transaction
 * @transaction
 */
async function p2p(p2p2){
    const a = p2p2.a
    const b = p2p2.b
    const account = p2p2.account
    a.account = a.account - p2p2.account
    b.account = b.account + p2p2.account

    const growerRegistry = await getParticipantRegistry('org.acme.shipping.perishable.Man');
    await growerRegistry.update(a)
    await growerRegistry.update(b)
}

/**
 * Initialize some test assets and participants useful for running a demo.
 * @param {org.acme.shipping.perishable.SetupDemo} setupDemo - the SetupDemo transaction
 * @transaction
 */
async function setupDemo(setupDemo) { 
    // eslint-disable-line no-unused-vars
   const factory = getFactory();
   const NS = 'org.acme.shipping.perishable';
   var manes = []
   // const shipperRegistry = [10]
   debugger
   try {
       for (var i = 0; i < 10; i++) {
           var man = factory.newResource(NS, "Man", i+'')
           man.name = "左龙龙" + i
           man.account = i +''
           manes.push(man)

       }
   } catch (error) {
       console.error(error)
   }
   var shipperRegistry = await getParticipantRegistry(NS + '.Man');
   await shipperRegistry.addAll(manes);
}
相关文章
相关标签/搜索