Add typescript decorators, fix case, config tslint for typescript chaincode

Signed-off-by: Chris Gabriel <chris_gabriel_98@yahoo.com>
This commit is contained in:
Chris Gabriel 2020-07-25 18:54:10 -05:00
parent fef45be995
commit 02c95d0dd4
4 changed files with 53 additions and 34 deletions

View file

@ -1,8 +1,12 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
import { Object, Property } from 'fabric-contract-api';
@Object()
export class Asset {
@Property()
public docType?: string;
public ID: string;
public Color: string;

View file

@ -2,53 +2,55 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { Context, Contract } from 'fabric-contract-api';
import { Context, Contract, Info, Returns, Transaction } from 'fabric-contract-api';
import { Asset } from './asset';
@Info({ title: 'AssetTransferBasic', description: 'Asset Transfer Basic contract implemented in TypeScript' })
export class AssetTransfer extends Contract {
public async initLedger(ctx: Context) {
@Transaction()
public async InitLedger(ctx: Context) {
const assets: Asset[] = [
{
ID: "asset1",
Color: "blue",
ID: 'asset1',
Color: 'blue',
Size: 5,
Owner: "Tomoko",
Owner: 'Tomoko',
AppraisedValue: 300,
},
{
ID: "asset2",
Color: "red",
ID: 'asset2',
Color: 'red',
Size: 5,
Owner: "Brad",
Owner: 'Brad',
AppraisedValue: 400,
},
{
ID: "asset3",
Color: "green",
ID: 'asset3',
Color: 'green',
Size: 10,
Owner: "Jin Soo",
Owner: 'Jin Soo',
AppraisedValue: 500,
},
{
ID: "asset4",
Color: "yellow",
ID: 'asset4',
Color: 'yellow',
Size: 10,
Owner: "Max",
Owner: 'Max',
AppraisedValue: 600,
},
{
ID: "asset5",
Color: "black",
ID: 'asset5',
Color: 'black',
Size: 15,
Owner: "Adriana",
Owner: 'Adriana',
AppraisedValue: 700,
},
{
ID: "asset6",
Color: "white",
ID: 'asset6',
Color: 'white',
Size: 15,
Owner: "Michel",
Owner: 'Michel',
AppraisedValue: 800,
},
];
@ -61,7 +63,8 @@ export class AssetTransfer extends Contract {
}
// createAsset issues a new asset to the world state with given details.
public async createAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) {
@Transaction()
public async CreateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) {
const asset = {
ID: id,
Color: color,
@ -74,7 +77,8 @@ export class AssetTransfer extends Contract {
}
// readAsset returns the asset stored in the world state with given id.
public async readAsset(ctx: Context, id: string): Promise<string> {
@Transaction(false)
public async ReadAsset(ctx: Context, id: string): Promise<string> {
const assetJSON = await ctx.stub.getState(id); // get the asset from chaincode state
if (!assetJSON || assetJSON.length === 0) {
throw new Error(`The asset ${id} does not exist`);
@ -84,14 +88,15 @@ export class AssetTransfer extends Contract {
}
// updateAsset updates an existing asset in the world state with provided parameters.
public async updateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) {
const exists = await this.assetExists(ctx, id);
@Transaction()
public async UpdateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) {
const exists = await this.AssetExists(ctx, id);
if (!exists) {
throw new Error(`The asset ${id} does not exist`);
}
// overwritting original asset with new asset
let updatedAsset = {
const updatedAsset = {
ID: id,
Color: color,
Size: size,
@ -103,8 +108,9 @@ export class AssetTransfer extends Contract {
}
// deleteAsset deletes an given asset from the world state.
public async deleteAsset(ctx: Context, id: string) {
const exists = await this.assetExists(ctx, id);
@Transaction()
public async DeleteAsset(ctx: Context, id: string) {
const exists = await this.AssetExists(ctx, id);
if (!exists) {
throw new Error(`The asset ${id} does not exist`);
}
@ -113,7 +119,9 @@ export class AssetTransfer extends Contract {
}
// assetExists returns true when asset with given ID exists in world state.
public async assetExists(ctx: Context, id: string): Promise<boolean> {
@Transaction(false)
@Returns('boolean')
public async AssetExists(ctx: Context, id: string): Promise<boolean> {
const assetJSON = await ctx.stub.getState(id);
if (!assetJSON || assetJSON.length === 0) {
return false;
@ -122,20 +130,22 @@ export class AssetTransfer extends Contract {
}
// transferAsset updates the owner field of asset with given id in the world state.
public async transferAsset(ctx: Context, id: string, newOwner: string) {
let assetString = await this.readAsset(ctx, id);
@Transaction()
public async TransferAsset(ctx: Context, id: string, newOwner: string) {
const assetString = await this.ReadAsset(ctx, id);
let asset = JSON.parse(assetString);
const asset = JSON.parse(assetString);
asset.Owner = newOwner;
await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset)));
}
// getAllAssets returns all assets found in the world state.
public async getAllAssets(ctx: Context): Promise<string> {
@Transaction(false)
public async GetAllAssets(ctx: Context): Promise<string> {
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 await (const { key, value } of ctx.stub.getStateByRange('', '')) {
const strValue = Buffer.from(value).toString('utf8');
let record;
try {

View file

@ -5,6 +5,8 @@
"moduleResolution": "node",
"module": "commonjs",
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"sourceMap": true
},
"include": [

View file

@ -15,7 +15,10 @@
"no-string-throw": true,
"no-var-keyword": true,
"no-trailing-whitespace": true,
"object-literal-key-quotes": [true, "as-needed"]
"object-literal-key-quotes": [true, "as-needed"],
"object-literal-sort-keys": false,
"max-line-length": false,
"prefer-for-of": false
},
"rulesDirectory": []
}