Hyperledger Composer检查数组

问题描述 投票:0回答:1

我在我的model.cto文件中定义了一个数组Account[] family,我想从我的logic.js中访问它。特别是我想只在接收者位于发送者的族阵列中时才执行事务。

我的model.cto:

namespace org.digitalpayment

asset Account identified by accountId {
  o String accountId
  --> Customer owner
  o Double balance
}

participant Customer identified by customerId {
  o String customerId
  o String firstname
  o String lastname
  --> Account[] family optional
}

transaction AccountTransfer {
--> Account from
--> Account to
o Double amount
}

我的logic.js:

/**
* Account transaction
* @param {org.digitalpayment.AccountTransfer} accountTransfer
* @transaction
*/
async function accountTransfer(accountTransfer) {
    if (accountTransfer.from.balance < accountTransfer.amount) {
        throw new Error("Insufficient funds");
    }

    if (/*TODO check if the family array contains the receiver account*/) {        

        // perform transaction
        accountTransfer.from.balance -= accountTransfer.amount;
        accountTransfer.to.balance += accountTransfer.amount;

        let assetRegistry = await getAssetRegistry('org.digitalpayment.Account');

        await assetRegistry.update(accountTransfer.from);
        await assetRegistry.update(accountTransfer.to);

    } else {
        throw new Error("Receiver is not part of the family");
    }

}
javascript hyperledger hyperledger-composer
1个回答
1
投票

好吧基本上你想首先获得Family资产的所有账户,然后检查Customer参与者是否包含在其中?如果我错了,请纠正我。一套合乎逻辑的步骤是 -

  1. 根据Accountto输入检索from
  2. 使用Customer变量检索每个Account的每个owner
  3. 从每个family获取Customer变量
/**
* Account transaction
* @param {org.digitalpayment.AccountTransfer} accountTransfer
* @transaction
*/
async function accountTransfer(accountTransfer) {
    if (accountTransfer.from.balance < accountTransfer.amount) {
        throw new Error("Insufficient funds");
    };

    var from = accountTransfer.from;
    var to = accountTransfer.to;
    var fromCustomer = from.owner;
    var toCustomer = to.owner;
    var fromCustomerFamily = fromCustomer.family;

    if (fromCustomerFamily && fromCustomerFamily.includes(to)) {        

        // perform transaction
        accountTransfer.from.balance -= accountTransfer.amount;
        accountTransfer.to.balance += accountTransfer.amount;

        let assetRegistry = await getAssetRegistry('org.digitalpayment.Account');

        await assetRegistry.update(accountTransfer.from);
        await assetRegistry.update(accountTransfer.to);

    } else {
        throw new Error("Receiver is not part of the family");
    }

}

由于最后几个Composer版本中的语法更改可能不起作用,具体取决于您在项目中使用的版本。如果这不起作用并且您使用的是旧版本,请告诉我,我会相应地更新答案。

© www.soinside.com 2019 - 2024. All rights reserved.