[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);
}
相關文章
相關標籤/搜索