Skip to content

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.

counter.js
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) }
})
profile.ts (build with --target js)
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
})
  • 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; only hive.* work can be awaited (there is no I/O).
  • hive.call returns the callee output decoded like input and throws HiveCallError (with .code) on failure; hive.callRaw returns the raw string; hive.tryCall returns null.
  • storage holds raw strings; db stores JSON values in the same state (db.get(key, default)).
  • require('hivekit') is the only module available; bundle other dependencies into the source file.

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.

Terminal window
hivec build counter.js # → dist/counter.hbc (~3.4 MB)
hivec build profile.ts --target js
hivec 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 .node
hivec run --local counter.js increment '{}' # in-process Node host: no gas, no receipt

In 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.