Storage and data strucutures
#
Basic TypesAsk! provides its own data types implmenting scale codec corresponding to basic data types in AssemblyScript and standard library collections.
AssemblyScript | ask! | ask! convert |
---|---|---|
bool | Bool | true |
i{8, 16, 32, 64, 128} | Int{8, 16, 32, 64, 128} | true |
u{8, 16, 32, 64, 128} | UInt{8, 16, 32, 64, 128} | true |
string(String) | ScaleString | true |
Array | ScaleArray | false |
Set | ScaleSet | false |
Map | ScaleMap | false |
When the basic assemblyscript types in the table above are used in the contract code (not including container types such as Array
/Set
/Map
), Ask! It will be automatically converted to the corresponding Scale type (that is, automatic boxing/unboxing) for storage during compile time.
#
Contract Parameter TypesContract Parameter Types are types defined to communicate Frame pallet-contract
Contract Parameter Types | ask! types |
---|---|
AccountId | Array<u8>(32) |
Hash | Array<u8>(32) |
Balance | UInt128 |
BlockNumber | UInt32 |
You can customize them in assembly/env/CustomTypes.ts
as long as the correct Codec is implementd.
#
Usage#
storageYou can import Bool
via ask-lang
.
import { Bool } from 'ask-lang';
If you need to store this data for blockchain storage
class Flipper { @state flag: Bool;}
You can also directly use bool
and ask! will convert it automatically during compilation
class Flipper { @state flag: bool;}
#
Opt out of storageIf you does not need to sync the data to blockchain storage but just declare it as a normal class property
class Flipper { @state flag: bool; flag1: bool;}
#
LazyWhen a data is decorated with @state
, it means it will sync with the blockchain database when its value gets changed which involves calling host function seal_set_storage
or seal_get_storage
. However, calling host function is expensive in terms of wasm performance.
Therefore @state
introduces lazy option as: @state({"lazy": false})
While lazy is true, that means while a state variable gets changed multiple times in a contract call, only the last change will be synced to blockchain.
By default, lazy
is set to true
. While lazy is false, then every change made to the state variable will be synced to blockchain.
Basic principle of implmentation: For every state varible with lazy set as true, the setter function generated by compiler will only updates the value changed in memory; Meanwhile, compiler also creates a commit function. If the state variables within this function ever gets changed before the contract call is done, the updated values will be synced to blockchain.