问题描述
刚开始在 VS code 上通过 IBM 区块链平台学习区块链,并面临代码问题。我按照教程做了一切,但不知道如何解决它们。
这是代码
/*
* SPDX-License-Identifier: Apache-2.0
*/
import { Context,Contract,Info,Returns,Transaction } from 'fabric-contract-api';
import { MyAsset } from './my-asset';
@Info({title: 'MyAssetContract',description: 'My Smart Contract' })
export class MyAssetContract extends Contract {
@Transaction(false)
@Returns('boolean')
public async myAssetExists(ctx: Context,myAssetId: string): Promise<boolean> {
const data: Uint8Array = await ctx.stub.getState(myAssetId);
return (!!data && data.length > 0);
}
@Transaction()
public async createMyAsset(ctx: Context,myAssetId: string,value: string): Promise<void> {
const exists: boolean = await this.myAssetExists(ctx,myAssetId);
if (exists) {
throw new Error(`The my asset ${myAssetId} already exists`);
}
const myAsset: MyAsset = new MyAsset();
myAsset.value = value;
const buffer: Buffer = Buffer.from(JSON.stringify(myAsset));
await ctx.stub.putState(myAssetId,buffer);
}
@Transaction(false)
@Returns('MyAsset')
public async readMyAsset(ctx: Context,myAssetId: string): Promise<MyAsset> {
const exists: boolean = await this.myAssetExists(ctx,myAssetId);
if (!exists) {
throw new Error(`The my asset ${myAssetId} does not exist`);
}
const data: Uint8Array = await ctx.stub.getState(myAssetId);
const myAsset: MyAsset = JSON.parse(data.toString()) as MyAsset;
return myAsset;
}
@Transaction()
public async updateMyAsset(ctx: Context,newValue: string): Promise<void> {
const exists: boolean = await this.myAssetExists(ctx,myAssetId);
if (!exists) {
throw new Error(`The my asset ${myAssetId} does not exist`);
}
const myAsset: MyAsset = new MyAsset();
myAsset.value = newValue;
const buffer: Buffer = Buffer.from(JSON.stringify(myAsset));
await ctx.stub.putState(myAssetId,buffer);
}
@Transaction()
public async deleteMyAsset(ctx: Context,myAssetId: string): Promise<void> {
const exists: boolean = await this.myAssetExists(ctx,myAssetId);
if (!exists) {
throw new Error(`The my asset ${myAssetId} does not exist`);
}
await ctx.stub.deleteState(myAssetId);
}
}
错误 1
错误 2
错误 3
错误 4
错误 5
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)