From 73a62a678011bebe4737159ed11e6ad8b57dee4e Mon Sep 17 00:00:00 2001 From: Brett Logan Date: Fri, 24 Jul 2020 12:51:34 -0400 Subject: [PATCH] Clean up Node Chaincode and Address Linting Issues Signed-off-by: Brett Logan --- .../chaincode-javascript/lib/assetTransfer.js | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js index f3f3fc77..7ce14c10 100644 --- a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js +++ b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js @@ -56,10 +56,10 @@ class AssetTransfer extends Contract { }, ]; - for (let i = 0; i < assets.length; i++) { - assets[i].docType = 'asset'; - await ctx.stub.putState(assets[i].ID, Buffer.from(JSON.stringify(assets[i]))); - console.info('Added <--> ', assets[i]); + for (const asset of assets) { + assets.docType = 'asset'; + await ctx.stub.putState(assets.ID, Buffer.from(JSON.stringify(assets))); + console.info(`Asset ${asset.ID} initialized`); } } @@ -72,8 +72,7 @@ class AssetTransfer extends Contract { Owner: owner, AppraisedValue: appraisedValue, }; - - await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset))); + return ctx.stub.putState(id, Buffer.from(JSON.stringify(asset))); } // readAsset returns the asset stored in the world state with given id. @@ -82,7 +81,6 @@ class AssetTransfer extends Contract { if (!assetJSON || assetJSON.length === 0) { throw new Error(`The asset ${id} does not exist`); } - return assetJSON.toString(); } @@ -93,15 +91,14 @@ class AssetTransfer extends Contract { throw new Error(`The asset ${id} does not exist`); } - // overwritting original asset with new asset - let updatedAsset = { + // overwriting original asset with new asset + const updatedAsset = { ID: id, Color: color, Size: size, Owner: owner, AppraisedValue: appraisedValue, }; - return ctx.stub.putState(id, Buffer.from(JSON.stringify(updatedAsset))); } @@ -111,34 +108,28 @@ class AssetTransfer extends Contract { if (!exists) { throw new Error(`The asset ${id} does not exist`); } - return ctx.stub.deleteState(id); } // assetExists returns true when asset with given ID exists in world state. async assetExists(ctx, id) { const assetJSON = await ctx.stub.getState(id); - if (!assetJSON || assetJSON.length === 0) { - return false; - } - return true; + return assetJSON && assetJSON.length > 0; } // transferAsset updates the owner field of asset with given id in the world state. async transferAsset(ctx, id, newOwner) { - let assetString = await this.readAsset(ctx, id); - - let asset = JSON.parse(assetString); + const assetString = await this.readAsset(ctx, id); + const asset = JSON.parse(assetString); asset.Owner = newOwner; - - await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset))); + return ctx.stub.putState(id, Buffer.from(JSON.stringify(asset))); } // getAllAssets returns all assets found in the world state. async getAllAssets(ctx) { const allResults = []; // range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace. - for await (const { key, value } of ctx.stub.getStateByRange("", "")) { + for (const { key, value } of ctx.stub.getStateByRange('', '')) { const strValue = Buffer.from(value).toString('utf8'); let record; try { @@ -149,7 +140,6 @@ class AssetTransfer extends Contract { } allResults.push({ Key: key, Record: record }); } - console.info(allResults); return JSON.stringify(allResults); }