gpt4 book ai didi

javascript - Hyperledger Composer 检查数组

转载 作者:行者123 更新时间:2023-12-02 23:50:18 24 4
gpt4 key购买 nike

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

我的模型.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");
}

}

最佳答案

好吧,基本上您想首先获取Family Assets 的所有帐户,然后检查Customer参与者是否包含在其中?如果我错了请纠正我。一组逻辑步骤是 -

  1. 根据 tofrom 输入检索帐户
  2. 使用 owner 变量检索每个 Account 的每个 Customer
  3. 从每个客户获取family变量
/**
* 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 版本中的语法更改可能无法工作,具体取决于您在项目中使用的版本。如果这不起作用并且您使用的是旧版本,请告诉我,我将相应地更新答案。

关于javascript - Hyperledger Composer 检查数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55676428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com