Skip to content
ReferenceTestnet: see pageMainnet: Not availableSoftware: Core bad4a06ea · protocol pqwerai-v8-testnet/1Updated

Your first WASM contract

For a fixed-supply ERC-20 instead of a counter, use the tested C++ and C# token examples.

Result: a counter with get(), increment() and an Incremented(uint256) event, written in C++ or C#, deployed as a WASM contract and called using the Solidity ABI.

Prerequisites: the Windows Wallet with C++/C# Studio support, a compatible 20778 node, a locally controlled account funded with test tPQAI, and the compiler for your chosen language. Install it using the C++ setup or C# setup.

1. Create a project and check the chain

Enable Advanced mode, open Contract Studio and choose Custom contract. Select C++ or C# as the source language. Use Check connected chain and confirm WASM VM yes, the expected chain ID and the approved genesis identity. A chain ID alone does not authenticate a network.

If the language is absent, use a wallet build containing its Studio adapter. If WASM support is absent, connect to the correct compatible node; editing a project's language does not activate a VM on that node.

2. Add the source

Paste the corresponding source below. Studio supplies pqwerai.h or PqwerAI.Contracts.cs when compiling; you do not need to fetch a package with that name.

Counter.cpp
// Tutorial counter for PQWERAI WASM VM v1. Unaudited; arithmetic wraps at 2^256.
// @abi function increment() nonpayable returns ()
// @abi function get() view returns (uint256)
// @abi event Incremented(uint256 value)
#include "pqwerai.h"

static uint8_t SLOT[32];
static uint8_t VALUE[32];

PQW_EXPORT("call") void call() {
    pqw::require_no_value();
    switch (pqw::selector()) {
    case 0xd09de08a: { // increment()
        pqw_storage_load(SLOT, VALUE);
        pqw::inc256(VALUE);
        pqw_storage_store(SLOT, VALUE);
        uint8_t topic[32];
        const char sig[] = "Incremented(uint256)";
        pqw_keccak256((const uint8_t*)sig, sizeof(sig) - 1, topic);
        pqw_emit_log(topic, 1, VALUE, 32);
        pqw_finish(nullptr, 0);
        return;
    }
    case 0x6d4ce63c: // get()
        pqw_storage_load(SLOT, VALUE);
        pqw::return_word(VALUE);
        return;
    default:
        pqw_revert(nullptr, 0);
    }
}
Counter.cs
// Tutorial counter for PQWERAI WASM VM v1. Unaudited; arithmetic wraps at 2^256.
// @abi function increment() nonpayable returns ()
// @abi function get() view returns (uint256)
// @abi event Incremented(uint256 value)
using PqwerAI.Contracts;

public static unsafe class Counter
{
    [Export("call")]
    public static void Call()
    {
        Sdk.RequireNoValue();
        byte* slot = stackalloc byte[32];
        byte* value = stackalloc byte[32];
        Sdk.Zero(slot, 32);
        switch (Sdk.Selector())
        {
            case 0xd09de08a: // increment()
                Host.StorageLoad(slot, value);
                for (int i = 31; i >= 0; i--) { value[i]++; if (value[i] != 0) break; }
                Host.StorageStore(slot, value);
                byte* topic = stackalloc byte[32];
                Host.Keccak256(Sdk.Utf8("Incremented(uint256)"), Sdk.Utf8Len("Incremented(uint256)"), topic);
                Host.EmitLog(topic, 1, value, 32);
                Host.Finish(null, 0);
                return;
            case 0x6d4ce63c: // get()
                Host.StorageLoad(slot, value);
                Sdk.ReturnWord(value);
                return;
            default:
                Host.Revert(null, 0);
                return;
        }
    }
}

Both versions use storage slot 0 and a full 32-byte, big-endian counter. The increment wraps modulo 2²⁵⁶; this is a small tutorial, not a checked financial arithmetic library. Both reject attached tPQAI and unknown selectors.

The // @abi comments populate the wallet's function and event list. They do not generate function bodies, argument validation or a dispatcher. The call export handles the selectors explicitly.

3. Compile and inspect

Choose the compiler file, then Compile. For C++ choose the wasi-sdk clang++.exe; for C# choose the pinned SDK's Roslyn/bincore/csc.dll. Review errors and warnings before proceeding.

Confirm that the artifact has the wasm-vm/v1 target, lists get() and increment(), and contains deployment bytecode. There are no constructor arguments in this counter. A raw Roslyn DLL is an intermediate file; the Studio translator must produce the WASM module before it can be deployed.

4. Simulate and deploy

Select your test account and send zero tPQAI value with the deployment. Simulate the deployment and review the estimated gas, fees and WASM module summary. Deploy signs a normal contract-creation transaction locally; Studio also simulates before signing.

Wait for a successful receipt (status = 0x1) and record the contract address, transaction hash and block. Check the post-deployment module verification result. An address by itself does not establish a successful, verified deployment. See how WASM verification works.

5. Read, write and read again

Open the contract in Studio's console with its generated ABI:

Action How to execute Expected result for a fresh counter
get() Read with eth_call ABI word containing 0
increment() Send a signed transaction with value 0 Successful receipt with one Incremented event whose value is 1
get() Read again after the receipt ABI word containing 1

An eth_call to increment() only simulates the write. Send a transaction to persist it. Before treating an application action as final, use the application finality guidance.

Ordinary ABI clients can use the same address and signatures: Foundry, ethers.js, viem and web3.py do not need to understand the contract's source language. Their deployment tooling must use the wrapped WASM creation payload rather than compiling this source as Solidity.

6. Save the project

Use Save project to retain source, ABI, artifact, compiler details, constructor arguments and deployment records in a pqwerai-studio-project/2 file. Keep attached sources and exact toolchain identities so you can rebuild the module. Changing source after deployment creates a new build; it does not update the contract.

Next: ABI, storage and host functions · Deployment errors.


Sources at 83ff5b69f0 (project repository, not public yet): developers/examples/wasm/Counter.cpp · developers/examples/wasm/Counter.cs · apps/pqwerai-wallet-windows/docs/STUDIO_DEVELOPER_GUIDE.md · apps/pqwerai-wallet-windows/src/PqwerAI.Wallet.Core/Studio/StudioEngine.cs

Found a problem? Report a documentation issue · Build testnet-20778+83ff5b69f0