Hyperledger Composer transaction processor functions

A Hyperledger Composer business network definition is composed of a set of model files and a set of scripts. The scripts may contain transaction processor functions that implement the transactions defined in the business network definition's model files.

Here's an example of a script file with a transaction:

Sample transaction processor function.
* @param {org.example.basic.SampleTransaction} tx The sample transaction instance.
* @transaction
*/
async function sampleExchange(tx) {
// Get the asset registry for the asset.
const assetRegistry = await getAssetRegistry('org.example.basic.SampleAsset');

//Ensure the balance is greather than the amount to be transfered
if(tx.origin.value > tx.txTransferAmount) {

//charge from receiver account
tx.origin.value = (tx.origin.value - tx.txTransferAmount);

//add to receiver account
tx.target.value = (tx.target.value + tx.txTransferAmount);

// Update the asset in the asset registry.
await assetRegistry.update(tx.origin);
await assetRegistry.update(tx.target);

// Emit an event for the modified asset.
let event = getFactory().newEvent('org.example.basic', 'SampleEvent');

event.origin = tx.origin;
event.target = tx.target;
event.txTransferAmount = tx.txTransferAmount;

emit(event);

}else{
throw Error(`You do not have enough balance for this transaction:
Balance US$: ${tx.origin.value}
Transfer Amount: ${tx.txTransferAmount}`);
}
}

As we can see, transaction processor functions are automatically invoked by the runtime when transactions are submitted using the BusinessNetworkConnection API. Decorators within documentation comments are used to annotate the functions with the metadata required for runtime processing, and each transaction type has an associated registry for storing the transactions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.59