mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-21 00:55:10 +00:00
Refactor GetAllAssets
GetAllAssets uses getStateByRange which returns an iterator. Refactored code to make use of the iterator as this code did not function in its previous state Signed-off-by: Brett Logan <brett.t.logan@ibm.com>
This commit is contained in:
parent
bd38f9220b
commit
4c3fe17310
4 changed files with 26 additions and 18 deletions
|
|
@ -57,8 +57,8 @@ class AssetTransfer extends Contract {
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const asset of assets) {
|
for (const asset of assets) {
|
||||||
assets.docType = 'asset';
|
asset.docType = 'asset';
|
||||||
await ctx.stub.putState(assets.ID, Buffer.from(JSON.stringify(assets)));
|
await ctx.stub.putState(asset.ID, Buffer.from(JSON.stringify(asset)));
|
||||||
console.info(`Asset ${asset.ID} initialized`);
|
console.info(`Asset ${asset.ID} initialized`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -129,8 +129,10 @@ class AssetTransfer extends Contract {
|
||||||
async GetAllAssets(ctx) {
|
async GetAllAssets(ctx) {
|
||||||
const allResults = [];
|
const allResults = [];
|
||||||
// range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace.
|
// 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 iterator = await ctx.stub.getStateByRange('', '');
|
||||||
const strValue = Buffer.from(value).toString('utf8');
|
let result = await iterator.next();
|
||||||
|
while (!result.done) {
|
||||||
|
const strValue = Buffer.from(result.value.value.toString()).toString('utf8');
|
||||||
let record;
|
let record;
|
||||||
try {
|
try {
|
||||||
record = JSON.parse(strValue);
|
record = JSON.parse(strValue);
|
||||||
|
|
@ -138,7 +140,8 @@ class AssetTransfer extends Contract {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
record = strValue;
|
record = strValue;
|
||||||
}
|
}
|
||||||
allResults.push({ Key: key, Record: record });
|
allResults.push({ Key: result.value.key, Record: record });
|
||||||
|
result = await iterator.next();
|
||||||
}
|
}
|
||||||
return JSON.stringify(allResults);
|
return JSON.stringify(allResults);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,14 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* 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';
|
import {Asset} from './asset';
|
||||||
|
|
||||||
export class AssetTransfer extends Contract {
|
@Info({title: 'AssetTransfer', description: 'Smart contract for trading assets'})
|
||||||
|
export class AssetTransferContract extends Contract {
|
||||||
|
|
||||||
@Transaction()
|
@Transaction()
|
||||||
public async InitLedger(ctx: Context) {
|
public async InitLedger(ctx: Context): Promise<void> {
|
||||||
const assets: Asset[] = [
|
const assets: Asset[] = [
|
||||||
{
|
{
|
||||||
ID: 'asset1',
|
ID: 'asset1',
|
||||||
|
|
@ -63,7 +64,7 @@ export class AssetTransfer extends Contract {
|
||||||
|
|
||||||
// CreateAsset issues a new asset to the world state with given details.
|
// CreateAsset issues a new asset to the world state with given details.
|
||||||
@Transaction()
|
@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<void> {
|
||||||
const asset = {
|
const asset = {
|
||||||
ID: id,
|
ID: id,
|
||||||
Color: color,
|
Color: color,
|
||||||
|
|
@ -86,7 +87,7 @@ export class AssetTransfer extends Contract {
|
||||||
|
|
||||||
// UpdateAsset updates an existing asset in the world state with provided parameters.
|
// UpdateAsset updates an existing asset in the world state with provided parameters.
|
||||||
@Transaction()
|
@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<void> {
|
||||||
const exists = await this.AssetExists(ctx, id);
|
const exists = await this.AssetExists(ctx, id);
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
throw new Error(`The asset ${id} does not exist`);
|
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.
|
// DeleteAsset deletes an given asset from the world state.
|
||||||
@Transaction()
|
@Transaction()
|
||||||
public async DeleteAsset(ctx: Context, id: string) {
|
public async DeleteAsset(ctx: Context, id: string): Promise<void> {
|
||||||
const exists = await this.AssetExists(ctx, id);
|
const exists = await this.AssetExists(ctx, id);
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
throw new Error(`The asset ${id} does not exist`);
|
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.
|
// TransferAsset updates the owner field of asset with given id in the world state.
|
||||||
@Transaction()
|
@Transaction()
|
||||||
public async TransferAsset(ctx: Context, id: string, newOwner: string) {
|
public async TransferAsset(ctx: Context, id: string, newOwner: string): Promise<void> {
|
||||||
const assetString = await this.ReadAsset(ctx, id);
|
const assetString = await this.ReadAsset(ctx, id);
|
||||||
const asset = JSON.parse(assetString);
|
const asset = JSON.parse(assetString);
|
||||||
asset.Owner = newOwner;
|
asset.Owner = newOwner;
|
||||||
|
|
@ -136,8 +137,10 @@ export class AssetTransfer extends Contract {
|
||||||
public async GetAllAssets(ctx: Context): Promise<string> {
|
public async GetAllAssets(ctx: Context): Promise<string> {
|
||||||
const allResults = [];
|
const allResults = [];
|
||||||
// range query with empty string for startKey and endKey does an open-ended query of all assets in the chaincode namespace.
|
// 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 iterator = await ctx.stub.getStateByRange('', '');
|
||||||
const strValue = Buffer.from(value).toString('utf8');
|
let result = await iterator.next();
|
||||||
|
while (!result.done) {
|
||||||
|
const strValue = Buffer.from(result.value.value.toString()).toString('utf8');
|
||||||
let record;
|
let record;
|
||||||
try {
|
try {
|
||||||
record = JSON.parse(strValue);
|
record = JSON.parse(strValue);
|
||||||
|
|
@ -145,7 +148,8 @@ export class AssetTransfer extends Contract {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
record = strValue;
|
record = strValue;
|
||||||
}
|
}
|
||||||
allResults.push({Key: key, Record: record});
|
allResults.push({Key: result.value.key, Record: record});
|
||||||
|
result = await iterator.next();
|
||||||
}
|
}
|
||||||
return JSON.stringify(allResults);
|
return JSON.stringify(allResults);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* 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];
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
|
"emitDecoratorMetadata": true,
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"target": "es2017",
|
"target": "es2017",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue