Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetHistoryForKey added to the chaincode-typescript #1226

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,34 @@ export class AssetTransferContract extends Contract {
return JSON.stringify(allResults);
}

// GetHistoryForKey returns all assets history in the world state.
@Transaction(false)
@Returns("string")
public async GetHistoryForKey(ctx: Context, id: string): Promise<string> {
const results: string[] = [];
try {
const historyIterator = await ctx.stub.getHistoryForKey(id);
let result = await historyIterator.next();
if (result.done) {
const errorMessage = `Asset ${id} does not exist`;
console.log(errorMessage);
throw new Error(errorMessage);
}

while (!result.done) {
const iteratorValue = Buffer.from(
result.value.value.toString()
).toString("utf8");
results.push(iteratorValue);
console.log(iteratorValue);
result = await historyIterator.next();
}

await historyIterator.close();
} catch (error) {
console.log(error);
}
return JSON.stringify(results);
}

}
Loading