diff --git a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js index f36eff34..6927eca1 100644 --- a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js +++ b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js @@ -57,8 +57,8 @@ class AssetTransfer extends Contract { ]; for (const asset of assets) { - assets.docType = 'asset'; - await ctx.stub.putState(assets.ID, Buffer.from(JSON.stringify(assets))); + asset.docType = 'asset'; + await ctx.stub.putState(asset.ID, Buffer.from(JSON.stringify(asset))); console.info(`Asset ${asset.ID} initialized`); } } @@ -129,8 +129,10 @@ class AssetTransfer extends Contract { 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 (const { key, value } of ctx.stub.getStateByRange('', '')) { - const strValue = Buffer.from(value).toString('utf8'); + const iterator = await ctx.stub.getStateByRange('', ''); + let result = await iterator.next(); + while (!result.done) { + const strValue = Buffer.from(result.value.value.toString()).toString('utf8'); let record; try { record = JSON.parse(strValue); @@ -138,7 +140,8 @@ class AssetTransfer extends Contract { console.log(err); record = strValue; } - allResults.push({ Key: key, Record: record }); + allResults.push({ Key: result.value.key, Record: record }); + result = await iterator.next(); } return JSON.stringify(allResults); } diff --git a/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts b/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts index 0895685d..b21f12ec 100644 --- a/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts +++ b/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts @@ -2,13 +2,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -import {Context, Contract, Returns, Transaction} from 'fabric-contract-api'; +import {Context, Contract, Info, Returns, Transaction} from 'fabric-contract-api'; import {Asset} from './asset'; -export class AssetTransfer extends Contract { +@Info({title: 'AssetTransfer', description: 'Smart contract for trading assets'}) +export class AssetTransferContract extends Contract { @Transaction() - public async InitLedger(ctx: Context) { + public async InitLedger(ctx: Context): Promise { const assets: Asset[] = [ { ID: 'asset1', @@ -63,7 +64,7 @@ export class AssetTransfer extends Contract { // CreateAsset issues a new asset to the world state with given details. @Transaction() - public async CreateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) { + public async CreateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number): Promise { const asset = { ID: id, Color: color, @@ -86,7 +87,7 @@ export class AssetTransfer extends Contract { // UpdateAsset updates an existing asset in the world state with provided parameters. @Transaction() - public async UpdateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) { + public async UpdateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number): Promise { const exists = await this.AssetExists(ctx, id); if (!exists) { throw new Error(`The asset ${id} does not exist`); @@ -105,7 +106,7 @@ export class AssetTransfer extends Contract { // DeleteAsset deletes an given asset from the world state. @Transaction() - public async DeleteAsset(ctx: Context, id: string) { + public async DeleteAsset(ctx: Context, id: string): Promise { const exists = await this.AssetExists(ctx, id); if (!exists) { throw new Error(`The asset ${id} does not exist`); @@ -123,7 +124,7 @@ export class AssetTransfer extends Contract { // TransferAsset updates the owner field of asset with given id in the world state. @Transaction() - public async TransferAsset(ctx: Context, id: string, newOwner: string) { + public async TransferAsset(ctx: Context, id: string, newOwner: string): Promise { const assetString = await this.ReadAsset(ctx, id); const asset = JSON.parse(assetString); asset.Owner = newOwner; @@ -136,8 +137,10 @@ export class AssetTransfer extends Contract { public async GetAllAssets(ctx: Context): Promise { 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('', '')) { - const strValue = Buffer.from(value).toString('utf8'); + const iterator = await ctx.stub.getStateByRange('', ''); + let result = await iterator.next(); + while (!result.done) { + const strValue = Buffer.from(result.value.value.toString()).toString('utf8'); let record; try { record = JSON.parse(strValue); @@ -145,7 +148,8 @@ export class AssetTransfer extends Contract { console.log(err); record = strValue; } - allResults.push({Key: key, Record: record}); + allResults.push({Key: result.value.key, Record: record}); + result = await iterator.next(); } return JSON.stringify(allResults); } diff --git a/asset-transfer-basic/chaincode-typescript/src/index.ts b/asset-transfer-basic/chaincode-typescript/src/index.ts index c018f4b7..cdd7b585 100644 --- a/asset-transfer-basic/chaincode-typescript/src/index.ts +++ b/asset-transfer-basic/chaincode-typescript/src/index.ts @@ -2,8 +2,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -import {AssetTransfer} from './assetTransfer'; +import {AssetTransferContract} from './assetTransfer'; -export {AssetTransfer} from './assetTransfer'; +export {AssetTransferContract} from './assetTransfer'; -export const contracts: any[] = [AssetTransfer]; +export const contracts: any[] = [AssetTransferContract]; diff --git a/asset-transfer-basic/chaincode-typescript/tsconfig.json b/asset-transfer-basic/chaincode-typescript/tsconfig.json index 2eaf5b98..80d8e12d 100644 --- a/asset-transfer-basic/chaincode-typescript/tsconfig.json +++ b/asset-transfer-basic/chaincode-typescript/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "experimentalDecorators": true, + "emitDecoratorMetadata": true, "outDir": "dist", "target": "es2017", "moduleResolution": "node",