From 1482e8f3bd475c7a86cb655bac30ed02bd15778e Mon Sep 17 00:00:00 2001 From: Liangqin Fan Date: Thu, 3 Jun 2021 11:49:27 +0800 Subject: [PATCH] remove useless files and correct example erc20 --- examples/composite/embedobj.ts | 165 ------------------- examples/composite/extension/embedobj.ts | 197 ----------------------- examples/composite/target.ts | 173 -------------------- examples/composite/target/metadata.json | 188 --------------------- examples/erc20/ERC20.ts | 4 +- examples/incrementer/incrementer.ts | 2 +- 6 files changed, 2 insertions(+), 727 deletions(-) delete mode 100644 examples/composite/embedobj.ts delete mode 100644 examples/composite/extension/embedobj.ts delete mode 100644 examples/composite/target.ts delete mode 100644 examples/composite/target/metadata.json diff --git a/examples/composite/embedobj.ts b/examples/composite/embedobj.ts deleted file mode 100644 index 23089a3d..00000000 --- a/examples/composite/embedobj.ts +++ /dev/null @@ -1,165 +0,0 @@ -/** - * All Rights Reserved by Patract Labs. - * @author liangqin.fan@gmail.com - */ -import {Int8, Codec, ScaleString, u128, UInt128, PackedStorableArray, PackedStorableMap } from "ask-lang"; - -class Car { - - name: string; - age: i8; - - constructor(name : string= "", age: i8 = 0) { - this.name = name; - this.age = age; - } - - toU8a(): u8[] { - let bytes = new Array(); - let name = new ScaleString(this.name); - let age = new Int8(this.age); - bytes = bytes.concat(name.toU8a()).concat(age.toU8a()); - return bytes; - } - - encodedLength(): i32 { - - let bWrap = new ScaleString(this.name); - let cWrap = new Int8(this.age); - - return bWrap.encodedLength() + cWrap.encodedLength(); - } - - populateFromBytes(bytes: u8[], index: i32 = 0): void { - - let name = new ScaleString(); - name.populateFromBytes(bytes, index); - this.name = name.toString(); - index += name.encodedLength(); - - - - let age = new Int8(); - age.populateFromBytes(bytes, index); - - this.age = age.unwrap(); - } - - eq(other: Car): bool { - return this.age == other.age && this.name == other.name; - } - - notEq(other: Car): bool { - return !this.eq(other); - } -} -class EmbedObj implements Codec { - - a: i8; - b: string; - c: u128; - car: Car; - - constructor(a: i8 = 0, b: string = "", c: u128 = u128.Zero, car: Car = new Car) { - this.a = a; - this.b = b; - this.c = c; - this.car = car; - } - - toU8a(): u8[] { - let bytes = new Array(); - let aWrap = new Int8(this.a); - let bWrap = new ScaleString(this.b); - let cWrap = new UInt128(this.c); - let car = this.car; - - bytes = bytes.concat(aWrap.toU8a()) - .concat(bWrap.toU8a()) - .concat(cWrap.toU8a()) - .concat(car.toU8a()); - return bytes; - } - - encodedLength(): i32 { - let aWrap = new Int8(this.a); - let bWrap = new ScaleString(this.b); - let cWrap = new UInt128(this.c); - let car = this.car; - - return aWrap.encodedLength() + bWrap.encodedLength() + cWrap.encodedLength() + car.encodedLength(); - } - - populateFromBytes(bytes: u8[], index: i32 = 0): void { - let aWrap = new Int8(); - aWrap.populateFromBytes(bytes, index); - index += aWrap.encodedLength(); - - let bWrap = new ScaleString(); - bWrap.populateFromBytes(bytes, index); - index += bWrap.encodedLength(); - - let cWrap = new UInt128(); - cWrap.populateFromBytes(bytes, index); - index += cWrap.encodedLength(); - let car = new Car(); - car.populateFromBytes(bytes, index); - - this.a = aWrap.unwrap(); - this.b = bWrap.toString(); - this.c = cWrap.unwrap(); - this.car = car; - } - - eq(other: EmbedObj): bool { - return this.a == other.a && this.b == other.b && this.c == other.c && this.car == other.car; - } - - notEq(other: EmbedObj): bool { - return !this.eq(other); - } -} - -@storage -class CollectionTypes { - emObj: EmbedObj; - emObjArr: PackedStorableArray; - emObjMap: PackedStorableMap; -} - -@contract -@doc(desc = "The contract about embed object") -class EmbedObject { - protected types: CollectionTypes; - - constructor() { - this.types = new CollectionTypes(); - } - @constructor - default(emObja: i8, emObjb: string): void { - this.types.emObj.a = emObja; - this.types.emObj.b = emObjb; - } - - @message - setEmbedObjCarAge(age: i8): void { - this.types.emObj.car.age = age; - } - - @message - setEmbedObja(a: i8): void { - this.types.emObj.a = a; - } - - @message(mutates = false) - readEmbendObja(): i8 { - return this.types.emObj.a; - } - - @message(mutates = false) - @doc(desc = "doc about getEmbedObjCarAge") - getEmbedObjCarAge(): i8 { - return this.types.emObj.car.age; - } -} - diff --git a/examples/composite/extension/embedobj.ts b/examples/composite/extension/embedobj.ts deleted file mode 100644 index d0482d12..00000000 --- a/examples/composite/extension/embedobj.ts +++ /dev/null @@ -1,197 +0,0 @@ -import * as _lang from "ask-lang"; - /** - * All Rights Reserved by Patract Labs. - * @author liangqin.fan@gmail.com - */ -import { Storage, StoreMode, Int8, Codec, ScaleString, u128, UInt128, PackedStorableArray, PackedStorableMap } from "ask-lang"; - -class Car implements Codec { - - name: string; - age: i8; - - constructor(name : string= "", age: i8 = 0) { - this.name = name; - this.age = age; - } - - toU8a(): u8[] { - let bytes = new Array(); - let name = new ScaleString(this.name); - let age = new Int8(this.age); - bytes = bytes.concat(name.toU8a()).concat(age.toU8a()); - return bytes; - } - - encodedLength(): i32 { - - let bWrap = new ScaleString(this.name); - let cWrap = new Int8(this.age); - - return bWrap.encodedLength() + cWrap.encodedLength(); - } - - populateFromBytes(bytes: u8[], index: i32 = 0): void { - - let name = new ScaleString(); - name.populateFromBytes(bytes, index); - this.name = name.toString(); - index += name.encodedLength(); - - - - let age = new Int8(); - age.populateFromBytes(bytes, index); - - this.age = age.unwrap(); - } - - eq(other: Car): bool { - return this.age == other.age && this.name == other.name; - } - - notEq(other: Car): bool { - return !this.eq(other); - } - -} - - -class EmbedObj implements Codec { - - a: i8; - b: string; - c: u128; - - constructor(a: i8 = 0, b: string = "", c: u128 = u128.Zero) { - this.a = a; - this.b = b; - this.c = c; - } - - toU8a(): u8[] { - let bytes = new Array(); - let aWrap = new Int8(this.a); - let bWrap = new ScaleString(this.b); - let cWrap = new UInt128(this.c); - - bytes = bytes.concat(aWrap.toU8a()) - .concat(bWrap.toU8a()) - .concat(cWrap.toU8a()); - return bytes; - } - - encodedLength(): i32 { - let aWrap = new Int8(this.a); - let bWrap = new ScaleString(this.b); - let cWrap = new UInt128(this.c); - - return aWrap.encodedLength() + bWrap.encodedLength() + cWrap.encodedLength(); - } - - populateFromBytes(bytes: u8[], index: i32 = 0): void { - let aWrap = new Int8(); - aWrap.populateFromBytes(bytes, index); - index += aWrap.encodedLength(); - - let bWrap = new ScaleString(); - bWrap.populateFromBytes(bytes, index); - index += bWrap.encodedLength(); - - let cWrap = new UInt128(); - cWrap.populateFromBytes(bytes, index); - - this.a = aWrap.unwrap(); - this.b = bWrap.toString(); - this.c = cWrap.unwrap(); - } - - eq(other: EmbedObj): bool { - return this.a == other.a && this.b == other.b && this.c == other.c; - } - - notEq(other: EmbedObj): bool { - return !this.eq(other); - } -} - -class CollectionTypes { - private _emObj: EmbedObj | null = null; - private _emObjArr: PackedStorableArray | null = null; - private _emObjMap: PackedStorableMap | null = null; - - get emObj(): EmbedObj { - if (this._emObj === null) { - const st = new _lang.Storage(new _lang.Hash([0xd8,0x97,0xfc,0x54,0xa3,0xc5,0x0f,0x6c,0x34,0xd5,0x38,0xb0,0x9b,0xca,0x50,0x2c,0xa9,0x37,0x71,0xac,0x3f,0x68,0xa6,0xe9,0x09,0x8f,0x32,0xf9])); - let val = st.load(); - if (!val) this._emObj = new EmbedObj(); - else this._emObj = val; - } - return this._emObj!; - } - set emObj(v: EmbedObj) { - this._emObj = new EmbedObj(v); - const st = new _lang.Storage(new _lang.Hash([0x42,0x03,0xcf,0x55])); - st.store(this._emObj!); - } - get emObjArr(): PackedStorableArray { - if (this._emObjArr === null) { - this._emObjArr = new _lang.PackedStorableArray("examples/composite/embedobj/CollectionTypes#emObjArremObjArr", 0); - } - return this._emObjArr!; - } - - get emObjMap(): PackedStorableMap { - if (this._emObjMap === null) { - this._emObjMap = new _lang.PackedStorableMap("examples/composite/embedobj/CollectionTypes#emObjMapemObjMap"); - } - return this._emObjMap!; - } - -} - - -@contract -class ArrayUsages { - protected types: CollectionTypes; - - constructor() { - this.types = new CollectionTypes(); - } - @constructor - default(capacity: i32): void { - - } - @message(mutates = false) - get(index: i32): i8 { - _lang.Storage.mode = _lang.StoreMode.R; - Storage.mode = StoreMode.R; - return this.types.emObj.a; - } -} - - -export function deploy(): i32 { - let _arrayusages = new ArrayUsages(); - - const defaultSelector: u8[] = [0xed,0x4b,0x9d,0x1b]; - if (_lang.msg.isSelector(defaultSelector)) { - const fnParameters = new _lang.FnParameters(_lang.msg.data); - let p0 = fnParameters.get<_lang.Int32>(); - _arrayusages.default(p0.unwrap()); - } - return 0; -} - -export function call(): i32 { - const _arrayusages = new ArrayUsages(); - const getSelector: u8[] = [0x2f,0x86,0x5b,0xd9]; - if (_lang.msg.isSelector(getSelector)) { - const fnParameters = new _lang.FnParameters(_lang.msg.data); - let p0 = fnParameters.get<_lang.Int32>(); - let rs = _arrayusages.get(p0.unwrap()); - - _lang.ReturnData.set<_lang.Int8>(new _lang.Int8(rs)); - } - return 0; -} \ No newline at end of file diff --git a/examples/composite/target.ts b/examples/composite/target.ts deleted file mode 100644 index 9b3921d1..00000000 --- a/examples/composite/target.ts +++ /dev/null @@ -1,173 +0,0 @@ -/** - * All Rights Reserved by Patract Labs. - * @author liangqin.fan@gmail.com - */ -import { FnParameters, Storage, ReturnData, StoreMode, Int8, StorableArray, Int32, SpreadStorableArray, Bool, Codec, StorableMap, ScaleString, Crypto, SpreadStorableMap, UInt128, u128 } from "ask-lang"; - -class EmbedObj implements Codec { - - a: i8; - b: string; - c: u128; - - constructor(a: i8 = 0, b: string = "", c: u128 = u128.Zero) { - this.a = a; - this.b = b; - this.c = c; - } - - toU8a(): u8[] { - let bytes = new Array(); - let aWrap = new Int8(this.a); - let bWrap = new ScaleString(this.b); - let cWrap = new UInt128(this.c); - - bytes = bytes.concat(aWrap.toU8a()) - .concat(bWrap.toU8a()) - .concat(cWrap.toU8a()); - return bytes; - } - - encodedLength(): i32 { - let aWrap = new Int8(this.a); - let bWrap = new ScaleString(this.b); - let cWrap = new UInt128(this.c); - - return aWrap.encodedLength() + bWrap.encodedLength() + cWrap.encodedLength(); - } - - populateFromBytes(bytes: u8[], index: i32 = 0): void { - let aWrap = new Int8(); - aWrap.populateFromBytes(bytes, index); - index += aWrap.encodedLength(); - - let bWrap = new ScaleString(); - bWrap.populateFromBytes(bytes, index); - index += bWrap.encodedLength(); - - let cWrap = new UInt128(); - cWrap.populateFromBytes(bytes, index); - - this.a = aWrap.unwrap(); - this.b = bWrap.toString(); - this.c = cWrap.unwrap(); - } - - eq(other: EmbedObj): bool { - return this.a == other.a && this.b == other.b && this.c == other.c; - } - - notEq(other: EmbedObj): bool { - return !this.eq(other); - } -} - -class CollectionTypes { - _emObj: EmbedObj | null; - _emObjArr: StorableArray | null; - _emObjMap: StorableMap | null; - - get emObj(): EmbedObj { - if (this._emObj == null) { - let strg = new Storage(Crypto.blake256s("embedObj.0")); - this._emObj = strg.load(); - } - return this._emObj!; - } - - set emObj(obj: EmbedObj) { - this._emObj = obj; - let strg = new Storage(Crypto.blake256s("embedObj.0")); - strg.store(this._emObj!); - } - - get emObjArr(): StorableArray { - if (this._emObjArr == null) { - this._emObjArr = new SpreadStorableArray("StorableArray.EmbedObj.0"); - } - - return this._emObjArr!; - } - - - get emObjMap(): StorableMap { - if (this._emObjMap == null) { - this._emObjMap = new SpreadStorableMap("StorableMap.EmbedObj.0"); - } - - return this._emObjMap!; - } -} - - -@contract -class ArrayUsages { - protected types: CollectionTypes; - - constructor() { - this.types = new CollectionTypes(); - } - - @constructor - default(capacity: i32): void { - - } - - @message - push(value: i8): void { - } - - @message - remove(index: i32): bool { - } - - @message(mutates = false) - get(index: i32): i8 { - Storage.mode = StoreMode.R; - } -} - -var msg: Msg = new Msg(); - -export function deploy(): i32 { - let mspUsages = new ArrayUsages(); - - const defaultSelector: u8[] = [0xed, 0x4b, 0x9d, 0x1b]; - if (msg.isSelector(defaultSelector)) { - const fnParameters = new FnParameters(msg.data); - let p0 = fnParameters.get(); - mspUsages.default(p0.unwrap()); - } - return 0; -} - -export function call(): i32 { - const arrUsages = new ArrayUsages(); - - // push - const setSelector: u8[] = [0xe8, 0xc4, 0x5e, 0xb6]; - if (msg.isSelector(setSelector)) { - const fnParameters = new FnParameters(msg.data); - let p0 = fnParameters.get(); - arrUsages.push(p0.unwrap()); - } - - // get - const getType128Selector: u8[] = [0x6a, 0x01, 0xaf, 0x21]; - if (msg.isSelector(getType128Selector)) { - const fnParameters = new FnParameters(msg.data); - let p0 = fnParameters.get(); - let rs = arrUsages.get(p0.unwrap()); - ReturnData.set(new Int8(rs)); - } - // remove - const removeSelector: u8[] = [0x7a, 0x01, 0xaf, 0x21]; - if (msg.isSelector(removeSelector)) { - const fnParameters = new FnParameters(msg.data); - let p0 = fnParameters.get(); - let rs = arrUsages.remove(p0.unwrap()); - ReturnData.set(new Bool(rs)); - } - - return 0; -} \ No newline at end of file diff --git a/examples/composite/target/metadata.json b/examples/composite/target/metadata.json deleted file mode 100644 index 3e701c0f..00000000 --- a/examples/composite/target/metadata.json +++ /dev/null @@ -1,188 +0,0 @@ -{ - "metadataVersion": "0.1.0", - "source": { - "hash": "", - "language": "ask v0.1", - "compiler": "ask v0.1" - }, - "contract": { - "name": "arrayUsages", - "version": "1.0", - "authors": [], - "description": null, - "documentation": null, - "repository": null, - "homepage": null, - "license": null - }, - "spec": { - "constructors": [ - { - "args": [ - { - "type": { - "type": 1, - "displayName": [ - "i32" - ] - }, - "name": "capacity" - } - ], - "docs": [ - "" - ], - "name": [ - "default" - ], - "selector": "0xed4b9d1b" - }, - { - "args": [], - "docs": [ - "" - ], - "name": [ - "new" - ], - "selector": "0x9bae9d5e" - } - ], - "messages": [ - { - "mutates": false, - "payable": false, - "args": [ - { - "type": { - "type": 1, - "displayName": [ - "i32" - ] - }, - "name": "index" - } - ], - "returnType": { - "type": 2, - "displayName": [ - "i8" - ] - }, - "docs": [ - "" - ], - "name": [ - "get" - ], - "selector": "0x2f865bd9" - } - ], - "events": [], - "docs": [] - }, - "types": [ - { - "def": { - "primitive": "i32" - } - }, - { - "def": { - "primitive": "i8" - } - }, - { - "def": { - "composite": { - "fields": [ - { - "name": "a", - "type": 2 - }, - { - "name": "b", - "type": 4 - }, - { - "name": "c", - "type": 5 - } - ] - } - } - }, - { - "def": { - "primitive": "str" - } - }, - { - "def": { - "primitive": "u128" - } - }, - { - "def": { - "sequence": { - "type": 3 - } - } - }, - { - "def": { - "composite": { - "fields": [ - { - "name": "key_index", - "type": 8 - }, - { - "name": "value", - "type": 3 - } - ] - } - } - }, - { - "def": { - "composite": { - "fields": [ - { - "name": "_valueStr", - "type": 4 - } - ] - } - } - } - ], - "storage": { - "struct": { - "fields": [ - { - "name": "emObj", - "layout": { - "key": "0x4203cf55d897fc54a3c50f6c34d538b09bca502ca93771ac3f68a6e9098f32f9", - "ty": 3 - } - }, - { - "name": "emObjArr", - "layout": { - "key": "0x01855729e3732b730c21faaee7f12834a3a9b5f35e23d92830e07c582e3554ec", - "ty": 6 - } - }, - { - "name": "emObjMap", - "layout": { - "key": "0x487bbfd0fce11d67eb33ab1b59afc228d264a7ffdf3f95124fb279b1852a5fe3", - "ty": 7 - } - } - ] - } - } -} \ No newline at end of file diff --git a/examples/erc20/ERC20.ts b/examples/erc20/ERC20.ts index fea1f2f3..87d55958 100644 --- a/examples/erc20/ERC20.ts +++ b/examples/erc20/ERC20.ts @@ -1,4 +1,4 @@ -import { AccountId, AccountId0, SpreadStorableMap, u128, UInt128, Log, msg } from "ask-lang"; +import { AccountId, AccountId0, SpreadStorableMap, u128, UInt128, msg } from "ask-lang"; @storage class ERC20Storage { @@ -102,12 +102,10 @@ export class ERC20 { @message transferFrom(sender: AccountId, recipient: AccountId, amount: u128): bool { this._transfer(sender, recipient, amount); - Log.println("transfer finished."); let allow = this.getAllowanceItem(sender); let leftAllowance: u128 = allow.get(msg.sender).unwrap(); assert(leftAllowance >= amount, "allowance overflow"); leftAllowance = leftAllowance - amount; - Log.println("calculate finished."); this._approve(sender, msg.sender, leftAllowance); return true; } diff --git a/examples/incrementer/incrementer.ts b/examples/incrementer/incrementer.ts index a5b950c5..be98e0d3 100644 --- a/examples/incrementer/incrementer.ts +++ b/examples/incrementer/incrementer.ts @@ -27,7 +27,7 @@ class Incrementer { this.stored.value = ++v; } - @message((mutates = false)) + @message(mutates = false) get(): u32 { return this.stored.value; }