Basic token in every language¶
Choose one of seven complete examples below. Each creates an ERC-20 token with 18 decimals, assigns its entire initial supply to the deploying account, and supports transfers and allowances. Supply is fixed after deployment: there is no additional mint, burn, owner privilege, transfer tax or upgrade function.
These are editable, unaudited learning examples. Their names and symbols are examples, not official PQWERAI currencies. Deploying one does not create native tPQAI or a token with monetary value.
Choose a language¶
| Language | VM | Token name / symbol | Compiler | Initial supply |
|---|---|---|---|---|
| Solidity | EVM | Solidity Coin / SOLC | solc 0.8.26 + OpenZeppelin 5.0.2 | Constructor argument |
| Vyper | EVM | Vyper Coin / VYPC | Vyper 0.4.3 | Constructor argument |
| Yul | EVM | Yul Coin / YULC | solc 0.8.26, strict assembly | Constructor argument |
| Huff | EVM | Huff Coin / HUFC | Huff Neo 1.7.1 | Source constant |
| Fe | EVM | Fe Coin / FEC | Fe 26.5.1 | Constructor argument |
| C++ | WASM | Wasm Coin / WASMC | wasi-sdk 34, clang 23.1.0 | Constructor argument |
| C# | WASM | C# Wasm Coin / CSWC | Roslyn 5.9.0, .NET SDK 10.0.403 + Studio translator | Constructor argument |
For 1,000,000 displayed tokens, use this raw supply:
That is 1,000,000 × 10^18 base units. All function amounts and constructor inputs here are raw integers.
Passing 1000000 as the constructor supply creates only 0.000000000001 displayed tokens.
The Huff example has no constructor arguments. Its INITIAL_SUPPLY constant already equals the raw
amount above (0xd3c21bcecceda1000000). Edit it before compiling to choose a different supply.
Complete sources¶
Solidity uses OpenZeppelin's ERC-20 implementation. The other examples implement the same public token interface directly. Yul and Huff expose their ABI through the declarations in the source; keep those declarations when pasting. Names and symbols can be edited before compilation; keep the corresponding string lengths and ABI encoding correct when changing the low-level Yul or Huff versions.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
// ERC-20 token (Solidity 0.8.26, OpenZeppelin Contracts 5.0.2 bundled with the wallet). Unaudited example.
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract SolidityCoin is ERC20 {
constructor(uint256 initialSupply) ERC20("Solidity Coin", "SOLC") {
_mint(msg.sender, initialSupply);
}
}
# pragma version 0.4.3
# ERC-20 token (Vyper 0.4.3). Unaudited example.
event Transfer:
sender: indexed(address)
receiver: indexed(address)
amount: uint256
event Approval:
owner: indexed(address)
spender: indexed(address)
amount: uint256
name: public(constant(String[11])) = "Vyper Coin"
symbol: public(constant(String[4])) = "VYPC"
decimals: public(constant(uint8)) = 18
totalSupply: public(uint256)
balanceOf: public(HashMap[address, uint256])
allowance: public(HashMap[address, HashMap[address, uint256]])
@deploy
def __init__(initialSupply: uint256):
self.totalSupply = initialSupply
self.balanceOf[msg.sender] = initialSupply
log Transfer(sender=empty(address), receiver=msg.sender, amount=initialSupply)
@internal
def _move(sender: address, receiver: address, amount: uint256):
assert sender != empty(address), "ERC20: transfer from the zero address"
assert receiver != empty(address), "ERC20: transfer to the zero address"
assert self.balanceOf[sender] >= amount, "ERC20: insufficient balance"
self.balanceOf[sender] -= amount
self.balanceOf[receiver] += amount
log Transfer(sender=sender, receiver=receiver, amount=amount)
@external
def transfer(receiver: address, amount: uint256) -> bool:
self._move(msg.sender, receiver, amount)
return True
@external
def approve(spender: address, amount: uint256) -> bool:
assert spender != empty(address), "ERC20: approve to the zero address"
self.allowance[msg.sender][spender] = amount
log Approval(owner=msg.sender, spender=spender, amount=amount)
return True
@external
def transferFrom(sender: address, receiver: address, amount: uint256) -> bool:
allowed: uint256 = self.allowance[sender][msg.sender]
assert allowed >= amount, "ERC20: insufficient allowance"
self.allowance[sender][msg.sender] = allowed - amount
self._move(sender, receiver, amount)
return True
// Basic fixed-supply ERC-20. Educational example, not audited.
// Compile with solc 0.8.26. Supply is minted once to the deployer.
// @abi constructor(uint256 initialSupply)
// @abi function name() view returns (string)
// @abi function symbol() view returns (string)
// @abi function decimals() view returns (uint8)
// @abi function totalSupply() view returns (uint256)
// @abi function balanceOf(address owner) view returns (uint256)
// @abi function allowance(address owner, address spender) view returns (uint256)
// @abi function transfer(address to, uint256 amount) nonpayable returns (bool)
// @abi function approve(address spender, uint256 amount) nonpayable returns (bool)
// @abi function transferFrom(address from, address to, uint256 amount) nonpayable returns (bool)
// @abi event Transfer(address indexed from, address indexed to, uint256 value)
// @abi event Approval(address indexed owner, address indexed spender, uint256 value)
object "YulCoin" {
code {
if callvalue() { revert(0, 0) }
// Constructor arguments follow the complete creation object, without a selector.
if iszero(eq(codesize(), add(datasize("YulCoin"), 32))) { revert(0, 0) }
codecopy(0, datasize("YulCoin"), 32)
let supply := mload(0)
sstore(0, supply)
mstore(0, caller())
mstore(32, 1)
sstore(keccak256(0, 64), supply)
mstore(0, supply)
log3(0, 32, 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0, caller())
datacopy(0, dataoffset("Runtime"), datasize("Runtime"))
return(0, datasize("Runtime"))
}
object "Runtime" {
code {
function requireSize(n) { if lt(calldatasize(), n) { revert(0, 0) } }
function addressArg(offset) -> a {
a := calldataload(offset)
if shr(160, a) { revert(0, 0) }
}
function balanceSlot(owner) -> slot {
mstore(0, owner) mstore(32, 1)
slot := keccak256(0, 64)
}
function allowanceSlot(owner, spender) -> slot {
mstore(0, owner) mstore(32, 2)
let inner := keccak256(0, 64)
mstore(0, spender) mstore(32, inner)
slot := keccak256(0, 64)
}
function word(value) { mstore(0, value) return(0, 32) }
function move(from, to, amount) {
if or(iszero(from), iszero(to)) { revert(0, 0) }
let fromSlot := balanceSlot(from)
let have := sload(fromSlot)
if gt(amount, have) { revert(0, 0) }
sstore(fromSlot, sub(have, amount))
// Read after debiting, so self-transfers restore the original balance.
let toSlot := balanceSlot(to)
let received := add(sload(toSlot), amount)
if lt(received, amount) { revert(0, 0) }
sstore(toSlot, received)
mstore(0, amount)
log3(0, 32, 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, from, to)
}
if callvalue() { revert(0, 0) }
requireSize(4)
switch shr(224, calldataload(0))
case 0x06fdde03 { // name()
mstore(0, 32) mstore(32, 8) mstore(64, "Yul Coin") return(0, 96)
}
case 0x95d89b41 { // symbol()
mstore(0, 32) mstore(32, 4) mstore(64, "YULC") return(0, 96)
}
case 0x313ce567 { word(18) }
case 0x18160ddd { word(sload(0)) }
case 0x70a08231 {
requireSize(36)
word(sload(balanceSlot(addressArg(4))))
}
case 0xdd62ed3e {
requireSize(68)
word(sload(allowanceSlot(addressArg(4), addressArg(36))))
}
case 0xa9059cbb {
requireSize(68)
move(caller(), addressArg(4), calldataload(36))
word(1)
}
case 0x095ea7b3 {
requireSize(68)
let spender := addressArg(4)
if iszero(spender) { revert(0, 0) }
let amount := calldataload(36)
sstore(allowanceSlot(caller(), spender), amount)
mstore(0, amount)
log3(0, 32, 0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925, caller(), spender)
word(1)
}
case 0x23b872dd {
requireSize(100)
let from := addressArg(4)
let to := addressArg(36)
let amount := calldataload(68)
let slot := allowanceSlot(from, caller())
let allowed := sload(slot)
if gt(amount, allowed) { revert(0, 0) }
sstore(slot, sub(allowed, amount))
move(from, to, amount)
word(1)
}
default { revert(0, 0) }
}
}
}
/* Basic fixed-supply ERC-20. Huff Neo 1.7.1. Educational, not audited.
* No constructor arguments: edit INITIAL_SUPPLY before compiling.
* 1,000,000 tokens with 18 decimals = 1,000,000 * 10^18 base units.
* Slots: 0 supply, 1 balances mapping, 2 nested allowances mapping.
* Scratch locals: 0x80 from, 0xa0 to, 0xc0 amount, 0xe0 spender.
*/
#define function name() view returns (string)
#define function symbol() view returns (string)
#define function decimals() view returns (uint8)
#define function totalSupply() view returns (uint256)
#define function balanceOf(address) view returns (uint256)
#define function allowance(address,address) view returns (uint256)
#define function transfer(address,uint256) nonpayable returns (bool)
#define function approve(address,uint256) nonpayable returns (bool)
#define function transferFrom(address,address,uint256) nonpayable returns (bool)
#define event Transfer(address indexed from, address indexed to, uint256 value)
#define event Approval(address indexed owner, address indexed spender, uint256 value)
#define constant INITIAL_SUPPLY = 0xd3c21bcecceda1000000
#define macro REQUIRE_SIZE(size) = takes (0) returns (0) {
<size> calldatasize lt bad_call jumpi
}
#define macro ADDRESS_ARG(offset, destination) = takes (0) returns (0) {
<offset> calldataload
dup1 0xa0 shr bad_call jumpi
<destination> mstore
}
#define macro BALANCE_SLOT(address_memory, destination) = takes (0) returns (0) {
<address_memory> mload 0x00 mstore
0x01 0x20 mstore
0x40 0x00 sha3 <destination> mstore
}
#define macro ALLOWANCE_SLOT() = takes (0) returns (0) {
0x80 mload 0x00 mstore 0x02 0x20 mstore
0x40 0x00 sha3 0x20 mstore
0xe0 mload 0x00 mstore
0x40 0x00 sha3 0x160 mstore
}
#define macro TRUE() = takes (0) returns (0) {
0x01 0x00 mstore 0x20 0x00 return
}
#define macro MOVE() = takes (0) returns (0) {
0x80 mload iszero 0xa0 mload iszero or bad_call jumpi
BALANCE_SLOT(0x80, 0x100)
0x100 mload sload 0x140 mstore
0x140 mload 0xc0 mload gt bad_call jumpi
0xc0 mload 0x140 mload sub 0x100 mload sstore
// Read the destination after debiting; this also handles a self-transfer.
BALANCE_SLOT(0xa0, 0x120)
0xc0 mload 0x120 mload sload add 0x140 mstore
0xc0 mload 0x140 mload lt bad_call jumpi
0x140 mload 0x120 mload sstore
0xc0 mload 0x00 mstore
0xa0 mload 0x80 mload __EVENT_HASH(Transfer) 0x20 0x00 log3
}
#define macro CONSTRUCTOR() = takes (0) returns (0) {
callvalue iszero constructor_ok jumpi
0x00 0x00 revert
constructor_ok:
[INITIAL_SUPPLY] dup1 0x00 sstore
caller 0x00 mstore 0x01 0x20 mstore
0x40 0x00 sha3 sstore
[INITIAL_SUPPLY] 0x00 mstore
caller 0x00 __EVENT_HASH(Transfer) 0x20 0x00 log3
// hnc adds the runtime-copy/return bootstrap after this macro.
}
#define macro MAIN() = takes (0) returns (0) {
callvalue bad_call jumpi
REQUIRE_SIZE(0x04)
0x00 calldataload 0xe0 shr
dup1 __FUNC_SIG(name) eq name jumpi
dup1 __FUNC_SIG(symbol) eq symbol jumpi
dup1 __FUNC_SIG(decimals) eq decimals jumpi
dup1 __FUNC_SIG(totalSupply) eq supply jumpi
dup1 __FUNC_SIG(balanceOf) eq get_balance jumpi
dup1 __FUNC_SIG(allowance) eq allowance jumpi
dup1 __FUNC_SIG(transfer) eq transfer jumpi
dup1 __FUNC_SIG(approve) eq approve jumpi
dup1 __FUNC_SIG(transferFrom) eq transfer_from jumpi
0x00 0x00 revert
name:
pop 0x20 0x00 mstore 0x09 0x20 mstore
__RIGHTPAD(0x4875666620436f696e) 0x40 mstore 0x60 0x00 return
symbol:
pop 0x20 0x00 mstore 0x04 0x20 mstore
__RIGHTPAD(0x48554643) 0x40 mstore 0x60 0x00 return
decimals:
pop 0x12 0x00 mstore 0x20 0x00 return
supply:
pop 0x00 sload 0x00 mstore 0x20 0x00 return
get_balance:
pop REQUIRE_SIZE(0x24) ADDRESS_ARG(0x04, 0x80)
BALANCE_SLOT(0x80, 0x100)
0x100 mload sload 0x00 mstore 0x20 0x00 return
allowance:
pop REQUIRE_SIZE(0x44) ADDRESS_ARG(0x04, 0x80) ADDRESS_ARG(0x24, 0xe0)
ALLOWANCE_SLOT()
0x160 mload sload 0x00 mstore 0x20 0x00 return
transfer:
pop REQUIRE_SIZE(0x44) ADDRESS_ARG(0x04, 0xa0)
caller 0x80 mstore 0x24 calldataload 0xc0 mstore
MOVE() TRUE()
approve:
pop REQUIRE_SIZE(0x44) ADDRESS_ARG(0x04, 0xe0)
0xe0 mload iszero bad_call jumpi
caller 0x80 mstore 0x24 calldataload 0xc0 mstore
ALLOWANCE_SLOT()
0xc0 mload 0x160 mload sstore
0xc0 mload 0x00 mstore
0xe0 mload caller __EVENT_HASH(Approval) 0x20 0x00 log3
TRUE()
transfer_from:
pop REQUIRE_SIZE(0x64) ADDRESS_ARG(0x04, 0x80) ADDRESS_ARG(0x24, 0xa0)
caller 0xe0 mstore 0x44 calldataload 0xc0 mstore
ALLOWANCE_SLOT()
0x160 mload sload 0x180 mstore
0x180 mload 0xc0 mload gt bad_call jumpi
0xc0 mload 0x180 mload sub 0x160 mload sstore
MOVE() TRUE()
bad_call:
0x00 0x00 revert
}
// ERC-20 token (Fe 26.5.1). Unaudited example.
use std::abi::sol
use std::evm::{Address, Ctx, Log, StorageMap, revert}
#[event]
struct Transfer {
#[indexed]
from: Address,
#[indexed]
to: Address,
value: u256,
}
#[event]
struct Approval {
#[indexed]
owner: Address,
#[indexed]
spender: Address,
value: u256,
}
msg TokenMsg {
#[selector = sol("name()")]
Name -> String<7>,
#[selector = sol("symbol()")]
Symbol -> String<3>,
#[selector = sol("decimals()")]
Decimals -> u8,
#[selector = sol("totalSupply()")]
TotalSupply -> u256,
#[selector = sol("balanceOf(address)")]
BalanceOf { owner: Address } -> u256,
#[selector = sol("allowance(address,address)")]
Allowance { owner: Address, spender: Address } -> u256,
#[selector = sol("transfer(address,uint256)")]
Send { to: Address, value: u256 } -> bool,
#[selector = sol("approve(address,uint256)")]
Approve { spender: Address, value: u256 } -> bool,
#[selector = sol("transferFrom(address,address,uint256)")]
SendFrom { from: Address, to: Address, value: u256 } -> bool,
}
struct TokenStore {
supply: u256,
balances: StorageMap<Address, u256>,
allowances: StorageMap<(Address, Address), u256>,
}
fn move_tokens(from: Address, to: Address, value: u256) uses (store: mut TokenStore, log: mut Log) {
if from == Address::zero() {
revert("ERC20: transfer from the zero address")
}
if to == Address::zero() {
revert("ERC20: transfer to the zero address")
}
let have = store.balances[from]
if have < value {
revert("ERC20: insufficient balance")
}
store.balances[from] = have - value
store.balances[to] = store.balances[to] + value
log.emit(Transfer { from, to, value })
}
pub contract FeCoin {
mut store: TokenStore
init(initialSupply: u256) uses (mut store, ctx: Ctx, log: mut Log) {
store.supply = initialSupply
store.balances[ctx.caller()] = initialSupply
log.emit(Transfer { from: Address::zero(), to: ctx.caller(), value: initialSupply })
}
recv TokenMsg {
Name -> String<7> {
"Fe Coin"
}
Symbol -> String<3> {
"FEC"
}
Decimals -> u8 {
18
}
TotalSupply -> u256 uses (store) {
store.supply
}
BalanceOf { owner } -> u256 uses (store) {
store.balances[owner]
}
Allowance { owner, spender } -> u256 uses (store) {
store.allowances[(owner, spender)]
}
Send { to, value } -> bool uses (mut store, ctx: Ctx, log: mut Log) {
move_tokens(from: ctx.caller(), to, value)
true
}
Approve { spender, value } -> bool uses (mut store, ctx: Ctx, log: mut Log) {
if spender == Address::zero() {
revert("ERC20: approve to the zero address")
}
store.allowances[(ctx.caller(), spender)] = value
log.emit(Approval { owner: ctx.caller(), spender, value })
true
}
SendFrom { from, to, value } -> bool uses (mut store, ctx: Ctx, log: mut Log) {
let allowed = store.allowances[(from, ctx.caller())]
if allowed < value {
revert("ERC20: insufficient allowance")
}
store.allowances[(from, ctx.caller())] = allowed - value
move_tokens(from, to, value)
true
}
}
}
// ERC-20 token in C++ for the PQWERAI WASM VM (clang, wasi-sdk 34). Unaudited example.
// @abi constructor(uint256 initialSupply)
// @abi function name() view returns (string)
// @abi function symbol() view returns (string)
// @abi function decimals() view returns (uint8)
// @abi function totalSupply() view returns (uint256)
// @abi function balanceOf(address owner) view returns (uint256)
// @abi function allowance(address owner, address spender) view returns (uint256)
// @abi function transfer(address to, uint256 value) nonpayable returns (bool)
// @abi function approve(address spender, uint256 value) nonpayable returns (bool)
// @abi function transferFrom(address from, address to, uint256 value) nonpayable returns (bool)
// @abi event Transfer(address indexed from, address indexed to, uint256 value)
// @abi event Approval(address indexed owner, address indexed spender, uint256 value)
#include "pqwerai.h"
static const char NAME[] = "Wasm Coin";
static const char SYMBOL[] = "WASMC";
static void addr_word(const uint8_t* addr20, uint8_t* w) { pqw::zero(w, 32); pqw::copy(w + 12, addr20, 20); }
static void balance_slot(const uint8_t* addr20, uint8_t* slot) { uint8_t k[32]; addr_word(addr20, k); pqw::mapping_slot(k, 1, slot); }
static void allowance_slot(const uint8_t* owner20, const uint8_t* spender20, uint8_t* slot) {
uint8_t k[32], inner[32]; addr_word(owner20, k); pqw::mapping_slot(k, 2, inner);
uint8_t buf[64]; addr_word(spender20, buf); pqw::copy(buf + 32, inner, 32); pqw_keccak256(buf, 64, slot);
}
static void revert_msg(const char* m, uint32_t n) {
uint8_t buf[4 + 64 + 64]; pqw::zero(buf, sizeof(buf));
buf[0] = 0x08; buf[1] = 0xc3; buf[2] = 0x79; buf[3] = 0xa0; buf[4 + 31] = 0x20; pqw::put_u64(buf + 36, n);
pqw::copy(buf + 68, (const uint8_t*)m, n); pqw_revert(buf, 68 + ((n + 31) / 32) * 32);
}
static void return_string(const char* s, uint32_t n) {
uint8_t buf[96]; pqw::zero(buf, 96); buf[31] = 0x20; pqw::put_u64(buf + 32, n); pqw::copy(buf + 64, (const uint8_t*)s, n);
pqw_finish(buf, 64 + ((n + 31) / 32) * 32);
}
static void emit2(const char* sig, uint32_t n, const uint8_t* a20, const uint8_t* b20, const uint8_t* value) {
uint8_t topics[96]; pqw_keccak256((const uint8_t*)sig, n, topics); addr_word(a20, topics + 32); addr_word(b20, topics + 64);
pqw_emit_log(topics, 3, value, 32);
}
// moves value from -> to; false if the balance is too small
static bool move(const uint8_t* from20, const uint8_t* to20, const uint8_t* value) {
uint8_t fs[32], ts[32], fb[32], tb[32];
if (pqw::is_zero(from20, 20) || pqw::is_zero(to20, 20)) { static const char Z[] = "ERC20: zero address"; revert_msg(Z, sizeof(Z) - 1); }
balance_slot(from20, fs); pqw_storage_load(fs, fb);
if (!pqw::sub256(fb, value, fb)) return false;
pqw_storage_store(fs, fb);
balance_slot(to20, ts); pqw_storage_load(ts, tb); pqw::add256(tb, value, tb); pqw_storage_store(ts, tb);
static const char T[] = "Transfer(address,address,uint256)";
emit2(T, sizeof(T) - 1, from20, to20, value);
return true;
}
PQW_EXPORT("deploy") void deploy() {
uint8_t supply[32], me[20], zero20[20], ts[32], slot0[32];
pqw::require_no_value();
if (pqw_calldata_size() < 32) pqw_revert(nullptr, 0);
pqw_calldata_copy(supply, 0, 32); // constructor argument (no selector)
pqw_caller(me); pqw::zero(zero20, 20); pqw::zero(slot0, 32);
pqw_storage_store(slot0, supply);
balance_slot(me, ts); pqw_storage_store(ts, supply);
static const char T[] = "Transfer(address,address,uint256)";
emit2(T, sizeof(T) - 1, zero20, me, supply);
}
PQW_EXPORT("call") void call() {
uint8_t w[32], a[32], b[32], c[32], slot[32], me[20];
pqw::require_no_value(); // every function is non-payable
switch (pqw::selector()) {
case 0x06fdde03: return_string(NAME, sizeof(NAME) - 1); return; // name()
case 0x95d89b41: return_string(SYMBOL, sizeof(SYMBOL) - 1); return; // symbol()
case 0x313ce567: pqw::put_u64(w, 18); pqw::return_word(w); return; // decimals()
case 0x18160ddd: pqw::zero(slot, 32); pqw_storage_load(slot, w); pqw::return_word(w); return; // totalSupply()
case 0x70a08231: pqw::require_args(1); pqw::arg_address(0, a); balance_slot(a + 12, slot); pqw_storage_load(slot, w); pqw::return_word(w); return;
case 0xdd62ed3e: pqw::require_args(2); pqw::arg_address(0, a); pqw::arg_address(1, b); allowance_slot(a + 12, b + 12, slot); pqw_storage_load(slot, w); pqw::return_word(w); return;
case 0xa9059cbb: // transfer(to, value)
pqw::require_args(2); pqw::arg_address(0, a); pqw::arg_word(1, b); pqw_caller(me);
if (!move(me, a + 12, b)) { static const char M[] = "ERC20: insufficient balance"; revert_msg(M, sizeof(M) - 1); return; }
pqw::return_bool(true); return;
case 0x095ea7b3: // approve(spender, value)
pqw::require_args(2); pqw::arg_address(0, a); pqw::arg_word(1, b); pqw_caller(me);
if (pqw::is_zero(a + 12, 20)) { static const char Z[] = "ERC20: approve to the zero address"; revert_msg(Z, sizeof(Z) - 1); return; }
allowance_slot(me, a + 12, slot); pqw_storage_store(slot, b);
{ static const char A[] = "Approval(address,address,uint256)"; emit2(A, sizeof(A) - 1, me, a + 12, b); }
pqw::return_bool(true); return;
case 0x23b872dd: // transferFrom(from, to, value)
pqw::require_args(3); pqw::arg_address(0, a); pqw::arg_address(1, b); pqw::arg_word(2, c); pqw_caller(me);
allowance_slot(a + 12, me, slot); pqw_storage_load(slot, w);
if (!pqw::sub256(w, c, w)) { static const char M[] = "ERC20: insufficient allowance"; revert_msg(M, sizeof(M) - 1); return; }
pqw_storage_store(slot, w);
if (!move(a + 12, b + 12, c)) { static const char M[] = "ERC20: insufficient balance"; revert_msg(M, sizeof(M) - 1); return; }
pqw::return_bool(true); return;
default:
pqw_revert(nullptr, 0);
}
}
// ERC-20 token in C# for the PQWERAI WASM VM. Unaudited example.
// @abi constructor(uint256 initialSupply)
// @abi function name() view returns (string)
// @abi function symbol() view returns (string)
// @abi function decimals() view returns (uint8)
// @abi function totalSupply() view returns (uint256)
// @abi function balanceOf(address owner) view returns (uint256)
// @abi function allowance(address owner, address spender) view returns (uint256)
// @abi function transfer(address to, uint256 value) nonpayable returns (bool)
// @abi function approve(address spender, uint256 value) nonpayable returns (bool)
// @abi function transferFrom(address from, address to, uint256 value) nonpayable returns (bool)
// @abi event Transfer(address indexed from, address indexed to, uint256 value)
// @abi event Approval(address indexed owner, address indexed spender, uint256 value)
using PqwerAI.Contracts;
public static unsafe class Token
{
static void AddrWord(byte* addr20, byte* w) { Sdk.Zero(w, 32); Sdk.Copy(w + 12, addr20, 20); }
static void BalanceSlot(byte* addr20, byte* slot)
{
byte* k = stackalloc byte[32];
AddrWord(addr20, k);
Sdk.MappingSlot(k, 1, slot);
}
static void AllowanceSlot(byte* owner20, byte* spender20, byte* slot)
{
byte* k = stackalloc byte[32];
byte* buf = stackalloc byte[64];
AddrWord(owner20, k);
Sdk.MappingSlot(k, 2, buf + 32);
AddrWord(spender20, buf);
Host.Keccak256(buf, 64, slot);
}
static void ReturnString(byte* s, uint n)
{
byte* buf = stackalloc byte[96];
Sdk.Zero(buf, 96);
buf[31] = 0x20;
Sdk.PutU64(buf + 32, n);
Sdk.Copy(buf + 64, s, n);
Host.Finish(buf, 64 + (n + 31) / 32 * 32);
}
static void Emit(bool approval, byte* a20, byte* b20, byte* value)
{
byte* topics = stackalloc byte[96];
if (approval) Host.Keccak256(Sdk.Utf8("Approval(address,address,uint256)"), Sdk.Utf8Len("Approval(address,address,uint256)"), topics);
else Host.Keccak256(Sdk.Utf8("Transfer(address,address,uint256)"), Sdk.Utf8Len("Transfer(address,address,uint256)"), topics);
AddrWord(a20, topics + 32);
AddrWord(b20, topics + 64);
Host.EmitLog(topics, 3, value, 32);
}
static bool Move(byte* from20, byte* to20, byte* value)
{
byte* slot = stackalloc byte[32];
byte* bal = stackalloc byte[32];
if (Sdk.IsZero(from20, 20) || Sdk.IsZero(to20, 20)) Sdk.RevertWith(Sdk.Utf8("ERC20: zero address"), Sdk.Utf8Len("ERC20: zero address"));
BalanceSlot(from20, slot);
Host.StorageLoad(slot, bal);
if (!Sdk.SubU256(bal, value, bal)) return false;
Host.StorageStore(slot, bal);
BalanceSlot(to20, slot);
Host.StorageLoad(slot, bal);
Sdk.AddU256(bal, value, bal);
Host.StorageStore(slot, bal);
Emit(false, from20, to20, value);
return true;
}
[Export("deploy")]
public static void Deploy()
{
byte* supply = stackalloc byte[32];
byte* me = stackalloc byte[20];
byte* zero20 = stackalloc byte[20];
byte* slot = stackalloc byte[32];
Sdk.RequireNoValue();
if (Host.CalldataSize() < 32) Host.Revert(null, 0);
Host.CalldataCopy(supply, 0, 32);
Host.Caller(me);
Sdk.Zero(zero20, 20);
Sdk.Zero(slot, 32);
Host.StorageStore(slot, supply);
BalanceSlot(me, slot);
Host.StorageStore(slot, supply);
Emit(false, zero20, me, supply);
}
[Export("call")]
public static void Call()
{
byte* w = stackalloc byte[32];
byte* a = stackalloc byte[32];
byte* b = stackalloc byte[32];
byte* c = stackalloc byte[32];
byte* slot = stackalloc byte[32];
byte* me = stackalloc byte[20];
Sdk.RequireNoValue(); // every function is non-payable
switch (Sdk.Selector())
{
case 0x06fdde03: ReturnString(Sdk.Utf8("C# Wasm Coin"), Sdk.Utf8Len("C# Wasm Coin")); return;
case 0x95d89b41: ReturnString(Sdk.Utf8("CSWC"), Sdk.Utf8Len("CSWC")); return;
case 0x313ce567: Sdk.PutU64(w, 18); Sdk.ReturnWord(w); return;
case 0x18160ddd: Sdk.Zero(slot, 32); Host.StorageLoad(slot, w); Sdk.ReturnWord(w); return;
case 0x70a08231: Sdk.RequireArgs(1); Sdk.ArgAddress(0, a); BalanceSlot(a + 12, slot); Host.StorageLoad(slot, w); Sdk.ReturnWord(w); return;
case 0xdd62ed3e: Sdk.RequireArgs(2); Sdk.ArgAddress(0, a); Sdk.ArgAddress(1, b); AllowanceSlot(a + 12, b + 12, slot); Host.StorageLoad(slot, w); Sdk.ReturnWord(w); return;
case 0xa9059cbb:
Sdk.RequireArgs(2); Sdk.ArgAddress(0, a); Sdk.ArgWord(1, b); Host.Caller(me);
if (!Move(me, a + 12, b)) { Sdk.RevertWith(Sdk.Utf8("ERC20: insufficient balance"), Sdk.Utf8Len("ERC20: insufficient balance")); return; }
Sdk.ReturnBool(true); return;
case 0x095ea7b3:
Sdk.RequireArgs(2); Sdk.ArgAddress(0, a); Sdk.ArgWord(1, b); Host.Caller(me);
if (Sdk.IsZero(a + 12, 20)) { Sdk.RevertWith(Sdk.Utf8("ERC20: approve to the zero address"), Sdk.Utf8Len("ERC20: approve to the zero address")); return; }
AllowanceSlot(me, a + 12, slot);
Host.StorageStore(slot, b);
Emit(true, me, a + 12, b);
Sdk.ReturnBool(true); return;
case 0x23b872dd:
Sdk.RequireArgs(3); Sdk.ArgAddress(0, a); Sdk.ArgAddress(1, b); Sdk.ArgWord(2, c); Host.Caller(me);
AllowanceSlot(a + 12, me, slot);
Host.StorageLoad(slot, w);
if (!Sdk.SubU256(w, c, w)) { Sdk.RevertWith(Sdk.Utf8("ERC20: insufficient allowance"), Sdk.Utf8Len("ERC20: insufficient allowance")); return; }
Host.StorageStore(slot, w);
if (!Move(a + 12, b + 12, c)) { Sdk.RevertWith(Sdk.Utf8("ERC20: insufficient balance"), Sdk.Utf8Len("ERC20: insufficient balance")); return; }
Sdk.ReturnBool(true); return;
default:
Host.Revert(null, 0); return;
}
}
}
Compile and deploy with Contract Studio¶
- Open Contract Studio in the Windows Wallet. Choose Custom contract, select the language, and paste its complete source above. This walkthrough uses the source editor, separate from the precompiled no-code token wizard in Create a token.
- Install or select the pinned compiler in Studio. See the EVM language guides or
the C++ setup and C# setup.
Studio supplies the bundled OpenZeppelin import for Solidity and the
pqwerai.h/PqwerAI.Contracts.csSDK for C++ / C#. A C# DLL by itself is not a chain deployment artifact. - Compile. Confirm the ABI includes the functions listed below. For Solidity select the
SolidityCoincontract. Keep the source, compiler version, ABI and compiled artifact together. - Check the connected chain: public test network 20778, expected genesis identity and, for C++ / C#, WASM VM yes. Use a locally controlled account funded with test tPQAI for fees.
- For Solidity, Vyper, Yul, Fe, C++ and C#, enter
1000000000000000000000000asinitialSupply. Leave constructor arguments empty for Huff. Set attached native value to 0. - Simulate deployment, estimate gas and review the account, network, constructor supply and fee. Sign locally, submit, wait for a successful receipt and save the deployed address. Apply your finality policy.
- Read
totalSupply()andbalanceOf(deployer): both must return the raw supply. Readname(),symbol()anddecimals()as a second check. Verify EVM source/artifact identity or WASM module identity using the matching artifact.
The constructor emits Transfer(zero address, deployer, supply). A WASM creation receipt also includes the
node's WasmDeployed(bytes32,uint32) event; this is a module receipt, not a second mint.
Try the same operations in every language¶
The deployed address and ABI identify the token regardless of its source language. In Studio's contract console,
read-only calls can be queried directly; send a signed transaction for each state change. eth_call only simulates
an operation and does not persist a transfer or approval. Keep attached tPQAI at zero.
| Function | Purpose |
|---|---|
name(), symbol(), decimals() |
Token metadata; decimals is 18 |
totalSupply() |
Fixed raw supply |
balanceOf(address) |
Account balance in base units |
transfer(to, value) |
Move the caller's tokens; returns true |
approve(spender, value) |
Set a spender's allowance; returns true |
allowance(owner, spender) |
Read the remaining allowance |
transferFrom(from, to, value) |
Move tokens using the caller's allowance; returns true |
For a small manual exercise, use three accounts you control: Alice deploys, Bob receives, Carol spends.
| Signed by | Transaction / query | Expected result |
|---|---|---|
| Alice | transfer(Bob, 300000000000000000000) |
Bob receives 300 tokens |
| Bob | approve(Carol, 100000000000000000000) |
Carol may spend 100 of Bob's tokens |
| Carol | transferFrom(Bob, Carol, 60000000000000000000) |
Carol receives 60; Bob has 240 |
| Any | allowance(Bob, Carol) |
40000000000000000000 (40 tokens) |
| Carol | Try spending 41 more tokens | Reverts: remaining allowance is only 40 |
| Bob | approve(Carol, 0) |
Revokes the remaining allowance |
Successful transfers emit Transfer; explicit approvals emit Approval. Prefer limited allowances. When
replacing an existing nonzero allowance, revoke it first and confirm the receipt before granting the new amount.
The examples can differ in optional behavior such as whether a maximum-uint256 allowance is consumed; this
walkthrough uses ordinary, finite allowances.
What was tested¶
Author verification, 23 November 2026: all seven sources compiled twice with the pinned Studio compilers,
producing identical creation bytecode for each language. Every example then passed 35 execution steps in
the node's EVM or WASM engine at Core revision f5d9ce2c6d67e2cb2ae95b93526b284e6fab9dd3, matching the
Core bundled with wallet 0.8.21. Mint event contents and transfer/approval topics and values were checked.
The suite covered metadata, supply, balances, transfers, self-transfers, zero-value transfers, approvals,
transferFrom, allowance consumption and revocation, insufficient balances/allowances, failed-call storage
rollback, zero recipients/spenders, malformed arguments, unknown selectors and attached native value.
Negative tests required a contract revert; invalid jumps and out-of-gas failures did not count as a pass.
These were disposable VM tests, without broadcasting a deployment to any public chain. They do not establish a
security audit, a live deployed address, a completed wallet UI flow or release-gate acceptance. The source bundle
contains the exact tested examples. The separate Foundry tutorial retains
its existing owner-mintable PQToken; it is a different example from these fixed-supply tokens.
Sources at 83ff5b69f0 (project repository, not public yet): developers/examples/basic-token/BasicToken.sol · developers/examples/basic-token/BasicToken.vy · developers/examples/basic-token/BasicToken.yul · developers/examples/basic-token/BasicToken.huff · developers/examples/basic-token/BasicToken.fe · developers/examples/basic-token/BasicToken.cpp · developers/examples/basic-token/BasicToken.cs
Found a problem? Report a documentation issue · Build testnet-20778+83ff5b69f0