Creating new participant and adding array of asset

2019-07-31 19:20发布

I have a problem when trying to add a new asset to an array of assets which are part of the participant as a reference.

Here I have SharedAccount participant controlled by its members who are connected via their share in the account. I want to write a transaction for creating a new SharedAccount by one person. When a person submits a transaction, it should create a share asset if that person and add it to SharedAccount's shares array.

Here's how my code looks like

.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);

}
...

I'm not sure if I should use factory.newRelationship and how, when adding a share Asset to SharedAccount. The error I get in the playground when trying to execute the transaction is

TypeError: Cannot read property 'push' of undefined

2条回答
淡お忘
2楼-- · 2019-07-31 19:51

try to do this:

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

}
查看更多
等我变得足够好
3楼-- · 2019-07-31 19:54

your transaction code should be something like below - some of your references weren't right (take too long to point out all the changes, so you can refer below).

I added a test string (for 'Person') just to show what you would do (to have a reason to update that particular participant registry).

Seems to me that SharedAccount is an asset not a participant. And you would use the appropriate JS API to update that type of registry.

balance is not a field on Person (it is on SharedAccount), but your code was trying to refer to it.

I've left comments for 'alternative ways' for declarations and such like - just by way of info.

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

}

The model I used is this:

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
}
查看更多
登录 后发表回答