Search code examples
javascripthyperledgerhyperledger-composer

Hyperledger Composer check array


I have defined an array Account[] family in my model.cto file and I want to access it from my logic.js. In particular I want to perform the transaction only if the receiver is in the family array of the sender.

My 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
}

My 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");
    }

}

Solution

  • Ok so basically you want to first get all the accounts of a Family asset, and then check if the Customer participant is included in it? Correct me if I am wrong. A logical set of steps would be -

    1. Retrieve the Account based on the to and from inputs
    2. Retrieve each Customer for each Account using the owner variable
    3. Get the family variable from each 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");
        }
    
    }
    
    

    Due to the syntax changes in the last few Composer versions may not work depending on the version you use in your project. If this does not work and you are using an older version, let me know and I will update the answer accordingly.