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 * SPDX-License-Identifier: Apache-2.0
*/ */
import { Object, Property } from 'fabric-contract-api';
@Object()
export class Asset { export class Asset {
@Property()
public docType?: string; public docType?: string;
public ID: string; public ID: string;
public Color: string; public Color: string;

View file

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

View file

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

View file

@ -15,7 +15,10 @@
"no-string-throw": true, "no-string-throw": true,
"no-var-keyword": true, "no-var-keyword": true,
"no-trailing-whitespace": 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": [] "rulesDirectory": []
} }