mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 07:25:10 +00:00
Clean up Node Chaincode and Address Linting Issues
Signed-off-by: Brett Logan <brett.t.logan@ibm.com>
This commit is contained in:
parent
e31ecf3165
commit
73a62a6780
1 changed files with 12 additions and 22 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue