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.

Terminal window
curl -fsSL https://necter.network/install.sh | sh -s -- javascript
necter-init javascript counter_js && cd counter_js
npm install
npx hivec build counter.js # -> dist/counter.hbc
ndsr run dist/counter.hbc increment --input '{"by":2}' --gas 10000000

The installer puts ndsr and necter-init in ~/.necter/bin. The SDK itself is the npm package @necter/hivekit, a dev dependency of each project, so its CLI runs as npx hivec. To add it to an existing project:

Terminal window
npm install --save-dev https://github.com/Necter-Network/hivekit/releases/download/v1.0.0/necter-hivekit-1.0.0.tgz

ndsr run defaults to 1,000,000 gas, which is too little for the engine: pass --gas 10000000. Install the tools

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 (the HiveKit prelude provides it inside the module); 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
npx hivec build counter.js # → dist/counter.hbc (~3.4 MB)
npx hivec build profile.ts --target js
npx hivec run counter.js increment '{"by": 2}' # builds if needed, runs with ndsr (gas limit 1e9 by default)
npx hivec run dist/counter.hbc increment '{"by":2}' --data-dir .node
npx 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.