WikiGetting hands onBuilding a Smart Contract using TypeScript

Building a Smart Contract using Ask! (TypeScript)

What is Ask! ?

It’s AssemblyScript-based framework. AssemblyScript is a TypeScript-like language for WebAssembly. Ask! allows writing smart contracts in a familiar syntax while compiling to WebAssembly (WASM) for Polkadot compatibility.

Ink!! vs. Ask!

AspectInk! (Rust)Ask! (TypeScript)
LanguageRust (statically typed, systems)TypeScript (JS superset, dynamic)
FamiliaritySteeper learning curveEasier for JS/TS developers
SafetyCompile-time guaranteesRuntime checks (no borrow checker)
AdoptionDefault choiceExperimental (discontinued?)
Use CaseProduction-gradePrototyping, JS/TS devs
Virtual MachineAny Wasm VMAny Wasm VM
EncodingWasmWasm
LanguageRustAssemblyScript
Overflow ProtectionEnabled by defaultNone
Constructor FunctionsMultipleMultiple
VersioningSemanticSemantic
Has Metadata?YesYes
Multi-File ProjectPlannedYes
Storage EntriesVariableVariable
Supported TypesDocsDocs
Has Interfaces?Yes (Rust Traits)Yes

A simple example, the Flipper

/* eslint-disable @typescript-eslint/no-inferrable-types */
import { env, Pack } from "ask-lang";
 
@event({ id: 1 })
export class FlipEvent {
    flag: bool;
 
    constructor(flag: bool) {
        this.flag = flag;
    }
}
 
@spreadLayout
@packedLayout
export class Flipper {
    flag: bool;
    constructor(flag: bool = false) {
        this.flag = flag;
    }
}
 
@contract
export class Contract {
    _data: Pack<Flipper>;
 
    constructor() {
        this._data = instantiate<Pack<Flipper>>(new Flipper(false));
    }
 
    get data(): Flipper {
        return this._data.unwrap();
    }
 
    set data(data: Flipper) {
        this._data = new Pack(data);
    }
 
    @constructor()
    default(flag: bool): void {
        this.data.flag = flag;
    }
 
    @message({ mutates: true })
    flip(): void {
        this.data.flag = !this.data.flag;
        let event = new FlipEvent(this.data.flag);
        // @ts-ignore
        env().emitEvent(event);
    }
 
    @message()
    get(): bool {
        return this.data.flag;
    }
}

Download from ask-template and build it

yarn build flipper.ts

After building we tried to upload the wasm on test net but it returns the error This contract file is not in a valid format.

Why Ask! still has potential

  • Lower Barrier: TypeScript devs can onboard to Polkadot faster.
  • Wasm Backend: Compiles to the same target as ink!, so theoretically interoperable.

Proceed with Caution:

While Ask! offers a friendlier syntax, its uncertain future and lack of tooling make it risky for mission-critical projects.