创建新的参与者,并通过引用将资产的数组分配给它(Creating new participant a

2019-09-29 01:03发布

尝试新的资源添加到它们的参与者作为参考的一部分资产的数组时,我有一个问题。

在这里,我有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时如何。 我在操场上得到的错误,当试图执行交易

类型错误:无法读取的未定义的属性“推”

Answer 1:

试着这样做:

/**
* @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);

}


Answer 2:

您的交易代码应该像下面 - 你的一些引用不正确的(时间太长指出所有的变化,所以你可以参考下图)。

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

在我看来, 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
}


文章来源: Creating new participant and adding array of assets by reference to it