Creating a new transaction

For the purpose of our tests, we will keep the new transaction relatively simple: our transaction will merge two assets into one, adding their value in the process.

To declare the new transaction, we will edit the model file and add this new declaration:

transaction MergeAssets {
--> Asset mergeFrom
--> Asset mergeTo
}

With the definition created, let's add the logic in the /lib/logic.js file:

/**
* Sample transaction
* @param {org.example.biznet.MergeAssets} tx
* @transaction
*/
function onMergeAssets(tx) {
var assetRegistry;
var mergeFromAsset = tx.mergeFrom;
var mergeToAsset = tx.mergeTo;
mergeToAsset.value += tx.mergeFrom.value;

return getAssetRegistry('org.example.biznet.SampleAsset')
.then(function(ar) {
assetRegistry = ar;
return assetRegistry.update(mergeToAsset);
})
.then(function() {
return assetRegistry.remove(mergeFromAsset);
});
}

That's all there is to it! Of course, some may remark that we are not following a good methodology—where are our unit tests for this code? Let's proceed. Don't worry, it's all part of the plan!

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

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