创建新参与者并通过引用添加资产数组

时间:2018-06-12 15:13:54

标签: hyperledger-composer

我在尝试将新资源添加到资产数组时遇到问题,这些资产属于参与者参考

在这里,我有SharedAccount参与者由其成员控制,这些成员通过他们在帐户中的共享进行连接。 我想编写一个用于由一个人创建新的SharedAccount的事务。当某人提交交易时,如果该人创建了一个共享资产并将其添加到SharedAccount的共享数组中。

以下是我的代码的样子

.cto:

...
participant SharedAccount identified by sharedAccountId {
  o String sharedAccountId
  --> Share[] shares
  o Double balance
  o Double originalBalance
}

asset Share identified by shareId {
  o String shareId
  --> Person shareHolder
  o Double amount
}

transaction CreateSharedAccount {
  --> Person creator
  o String accountName
  o Integer amount
}
...

的.js:

...
/**
 * @param {org.mistral.bulliongrower.CreateSharedAccount} createSharedAccount
 * @transaction
 */
async function CreateSharedAccount(createSharedAccount) {

  const factory = getFactory();
  const NS = 'org.mistral.bulliongrower';

  // create share

  const share = factory.newResource(NS, 'Share', createSharedAccount.creator.personId + 'SHARE');
  share.amount = createSharedAccount.amount;
  share.shareHolder = createSharedAccount.creator;
  share.shareHolder.balance -= createSharedAccount.amount;

  const sharesRegistry = await getAssetRegistry(NS + '.Share');
  await sharesRegistry.add(share);

  const personRegistry = await getParticipantRegistry(NS + '.Person');
  await personRegistry.update(share.shareHolder);

  // create sharedAccount

  const sharedAcc = factory.newResource(NS, 'SharedAccount', createSharedAccount.accountName);
  sharedAcc.shares.push(share);
  sharedAcc.balance = createSharedAccount.amount;
  sharedAcc.originalBalance = createSharedAccount.amount;

  const sharedAccRegistry = await getAssetRegistry(NS + '.SharedAccount');
  await sharedAccRegistry.add(sharedAcc);

}
...

我不确定是否应该使用factory.newRelationship以及在向SharedAccount添加共享资产时如何使用。 尝试执行事务时我在操场上遇到的错误是

  

TypeError:无法读取未定义

的属性'push'

2 个答案:

答案 0 :(得分:0)

您的交易代码应如下所示 - 您的某些参考文献并不正确(需要很长时间才能指出所有更改,因此您可以参考下文)。

我添加了一个测试字符串(对于' Person')只是为了显示你会做什么(有理由更新该特定参与者注册表)。

我觉得SharedAccount是资产而不是参与者。并且您将使用适当的JS API来更新该类型的注册表。

balance不是Person上的字段(它位于SharedAccount上),但您的代码试图引用它。

我已经留下了关于其他方式的评论'声明等等 - 只是通过信息。

/**
 * @param {org.mistral.bulliongrower.CreateSharedAccount} createSharedAccount
 * @transaction
 */
async function CreateSharedAccount(createSharedAccount) {

  const factory = getFactory();
  const NS = 'org.example.trading';

  // create share

  const share = factory.newResource(NS, 'Share', createSharedAccount.creator.personId + 'SHARE');
  share.amount = createSharedAccount.amount;
  console.log("amount is " + share.amount);
  share.shareHolder = createSharedAccount.creator;
 // share.shareHolder.balance -= createSharedAccount.amount;  // won't work - balance is a field on SharedAccount not Person - moved it below


  const sharesRegistry = await getAssetRegistry(NS + '.Share');
  await sharesRegistry.add(share);

  share.shareHolder.newstr = "123"; // setting 'SOME' field (I added 'newstr' in my model, see below - so as to have a reason to update / give an example
  const personRegistry = await getParticipantRegistry(NS + '.Person');
  await personRegistry.update(share.shareHolder);

  // create sharedAccount

  const sharedAcc = factory.newResource(NS, 'SharedAccount', createSharedAccount.accountName);

  //let idsArray = new Array(); // alternative, or use implicit below.
  let idsArray = [] ;
  let shareAssetRelationship = factory.newRelationship(NS, 'Share', share.getIdentifier());
  idsArray.push(shareAssetRelationship); // only one element anyway

  sharedAcc.shares = idsArray;
  sharedAcc.balance = createSharedAccount.amount; // this is a new resource - so balance is eq to trxn amount ?
  sharedAcc.originalBalance = createSharedAccount.amount; // original balance is nothing or 'balance' ?....anyway....

  const sharedAccRegistry = await getAssetRegistry(NS + '.SharedAccount');
  await sharedAccRegistry.add(sharedAcc);

}

我使用的模型是:

participant Person identified by personId {
  o String personId
  o String newstr
}

asset SharedAccount identified by sharedAccountId {
  o String sharedAccountId
  --> Share[] shares
  o Double balance
  o Double originalBalance
}

asset Share identified by shareId {
  o String shareId
  --> Person shareHolder
  o Double amount
}

transaction CreateSharedAccount {
  --> Person creator
  o String accountName
  o Integer amount
}

答案 1 :(得分:0)

尝试这样做:

/**
* @param {org.mistral.bulliongrower.CreateSharedAccount} createSharedAccount
* @transaction
*/
async function CreateSharedAccount(createSharedAccount) {

const factory = getFactory();
const NS = 'org.mistral.bulliongrower';

// create share

const share = factory.newResource(NS, 'Share', createSharedAccount.creator.personId + 'SHARE');
//const share = factory.newRelationship(NS, 'Share', createSharedAccount.creator.personId + 'SHARE');
share.amount = createSharedAccount.amount;
//share.shareHolder = factory.newRelationship(NS, 'Share', createSharedAccount.creator.personId);
share.shareHolder = createSharedAccount.creator;
share.shareHolder.balance -= createSharedAccount.amount;


const sharesRegistry = await getAssetRegistry(NS + '.Share');
await sharesRegistry.add(share);

const personRegistry = await getParticipantRegistry(NS + '.Person');
await personRegistry.update(share.shareHolder);

// create sharedacc

const sharedAcc = factory.newResource(NS, 'SharedAccount', createSharedAccount.accountName);
//sharedAcc.shares = factory.newRelationship(NS, 'Share', createSharedAccount.creator.personId);
//sharedAcc.shares[0] = factory.newRelationship(NS, 'Share', share.shareId);

// define an array  
let  sharesArray = [];
sharesArray.push(share);

sharedAcc.shares = sharesArray;
sharedAcc.balance = createSharedAccount.amount;
sharedAcc.originalBalance = createSharedAccount.amount;

// use getParticipantRegistry not getAssetRegistry
const sharedAccRegistry = await getParticipantRegistry(NS + '.SharedAccount');
await sharedAccRegistry.add(sharedAcc);

}