Skip to main content

Storage and data strucutures

Basic Types#

Ask! provides its own data types implmenting scale codec corresponding to basic data types in AssemblyScript and standard library collections.

AssemblyScriptask!ask! convert
boolBooltrue
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)ScaleStringtrue
ArrayScaleArrayfalse
SetScaleSetfalse
MapScaleMapfalse

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 Types#

Contract Parameter Types are types defined to communicate Frame pallet-contract

Contract Parameter Typesask! types
AccountIdArray<u8>(32)
HashArray<u8>(32)
BalanceUInt128
BlockNumberUInt32

You can customize them in assembly/env/CustomTypes.ts as long as the correct Codec is implementd.

Usage#

storage#

You 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 storage#

If 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;}

Lazy#

When 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.