JavaScript & TypeScript (engine)
With the js target, your script runs inside a JavaScript engine (Boa 0.22) compiled to
wasm32-unknown-unknown. .js, .mjs and .cjs files use it by default; .ts files use it with
--target js (TypeScript is transpiled first). You get the full language (classes, closures, JSON,
async, spread, Set), at a cost of about 2–3 M gas per call and a ~3.4 MB module.
const { hive, storage, db } = require('hivekit') // or: import { hive } from 'hivekit'
hive.define('increment', (input) => { const count = (db.get('count') || 0) + (input.by ?? 1) db.set('count', count) hive.emit('incremented', { count }) return { count }})
hive.define('relay', (input) => hive.call(input.address, input.function, input.input))
hive.define('note', (ctx) => { // first parameter named ctx → context object ctx.storage.set('note', ctx.input.text) return { hash: hive.hash(ctx.input.text) }})import { hive, db } from 'hivekit'
interface Profile { name: string; tags: string[] }
hive.define('saveProfile', async (input: { id: string; profile: Profile }) => { const saved = { ...input.profile, tags: [...new Set(input.profile.tags)].sort() } db.set(`profile:${input.id}`, saved) hive.emit('profile_saved', { id: input.id, tags: saved.tags.length }) return saved})Conventions (same as Python)
Section titled “Conventions (same as Python)”- Input: the call input parsed as JSON; the raw string if it is not JSON;
{}if empty. - Output: a returned string is the output as-is;
undefined→""; anything else →JSON.stringify. - Errors: a thrown error or
hive.fail(msg)fails the call (state and events discarded) with the message. - Async: handlers may be
async; onlyhive.*work can be awaited (there is no I/O). hive.callreturns the callee output decoded like input and throwsHiveCallError(with.code) on failure;hive.callRawreturns the raw string;hive.tryCallreturnsnull.storageholds raw strings;dbstores JSON values in the same state (db.get(key, default)).require('hivekit')is the only module available; bundle other dependencies into the source file.
Determinism
Section titled “Determinism”The engine has no clock, randomness, filesystem or network. Math.random(), Date.now(), Date() and
argument-less new Date() throw; new Date(timestamp) works. Event data must not contain floats.
Build, run, test
Section titled “Build, run, test”hivec build counter.js # → dist/counter.hbc (~3.4 MB)hivec build profile.ts --target jshivec run counter.js increment '{"by": 2}' # builds if needed, runs with ndsr (gas limit 1e9 by default)hivec run dist/counter.hbc increment '{"by":2}' --data-dir .nodehivec run --local counter.js increment '{}' # in-process Node host: no gas, no receiptIn Node, hive.invoke(name, input) runs a handler against an in-memory store for unit tests. The library
also exports compile, compileFile, readHbc, manifestAddress, canonicalJson and keccak256.