TypeScript (AssemblyScript)
.ts files build with AssemblyScript 0.28.20 by default (--runtime stub -O3). Modules are a few KB
and a small call costs around 12k gas. The pipeline is the same as ndsr compile: for the same source the
produced module.wasm is byte-identical.
import { hive, storage } from 'hivekit'
function increment(input: string): string { const cur = storage.get("count"); const next = (cur.length == 0 ? 0 : I64.parseInt(cur)) + 1; storage.set("count", next.toString()); hive.emit("incremented", "{\"n\":" + next.toString() + "}"); return next.toString();}
function fingerprint(input: string): string { return hive.hash(input); // keccak256, "0x…"}
hive.define("increment", increment);hive.define("fingerprint", fingerprint);- Handlers are
(input: string): string: a top-level function or a non-capturing arrow(input: string): string => …. hive.define("name", handler): the name must be a string literal matching[A-Za-z_][A-Za-z0-9_]{0,63}.- One source file. The only import allowed is
import { … } from 'hivekit'(for editors; it is stripped at build time). Put the import on its own line, without a trailing comment. - There is no built-in
JSON: build strings by hand (as above) or vendor a parser. - Rejected before compiling, with
file:line:async/await/Promise,any,unknown,undefined,try/catch,JSON, other imports,require,Date.now/Math.random/new Date(),console,eval,delete, generators,Symbol. Closures that capture locals are not supported. - Identifiers containing
__hive_or__allocare reserved.
Need any of those? Build the same file with --target js and it runs in the JavaScript engine instead
(JavaScript & TypeScript (engine)), at a much higher gas cost.
| Call | Meaning |
|---|---|
hive.define(name, fn) |
export fn as name |
hive.call(address, fn, input): string |
call another module; aborts this call if it fails |
hive.tryCall(address, fn, input): string | null |
same, null on failure (callee state reverted) |
hive.tryCallRaw(…): i64 |
raw packed result or error code |
hive.emit(name, dataJson) |
record an event (JSON string, integers only) |
hive.log(msg) |
node-local debug log |
hive.hash(data): string |
keccak256 hex of the UTF-8 bytes |
hive.fail(msg) |
fail the call; state changes and events are reverted |
storage.get(key): string |
this module’s value ("" if unset) |
storage.set(key, value) |
write ("" deletes) |
storage.del(key) |
delete |
A module that uses only hive.hash, hive.emit, hive.log and hive.fail imports no storage functions
and can be a committee worker. For example the fingerprint-only module above (without increment)
imports only env.abort and crypto.hash, and fingerprint("hello") returns
0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8 for 6,765 gas.
Build and run
Section titled “Build and run”hivec build counter.ts # → dist/counter.hbchivec inspect dist/counter.hbchivec run dist/counter.hbc increment --data-dir .nodehivec functions counter.tsYou can also compile AssemblyScript with the runtime itself: ndsr compile counter.ts -o counter.hbc
(needs Node.js; installs the pinned compiler on first use). ndsr compile does not accept the
import … from 'hivekit' line: remove it for that path. Its manifest names a different compiler
(ndsr-asc/[email protected]), so the address differs from the hivec build.