Skip to content

Python

Python modules run in a Python interpreter (RustPython 0.6.0, without the CPython standard library) compiled to wasm32-unknown-unknown. hivec build embeds your source into a pre-initialized copy of it.

counter.py
from hivekit import hive
@hive.define("increment")
def increment(input):
count = hive.db.get("count", 0) + input.get("by", 1)
hive.db.set("count", count)
hive.emit("incremented", {"count": count})
return {"count": count}
@hive.define("relay")
def relay(input):
return hive.call(input["address"], input["function"], input.get("input", ""))
@hive.define("note")
def note(ctx): # first parameter named ctx -> context object
ctx.storage.set("note", ctx.input["text"])
ctx.log.info("saved")
return {"hash": hive.hash(ctx.input["text"])}

Handler styles: fn(input), fn(ctx) (first parameter named ctx), fn(input, ctx) or fn().

  • Input: the call input parsed as JSON; the raw string if it is not JSON; {} if empty.
  • Output: a returned str is the output as-is; None → ""; anything else → compact JSON.
  • Errors: an exception or hive.fail(msg) fails the call; state changes and events are discarded.
  • hive.call returns the decoded callee output and raises HiveCallError (.code) on failure; hive.call_raw returns the raw string; hive.try_call returns None.
  • hive.storage holds raw strings; hive.db (and ctx.db) stores JSON values in the same state. db.keys(), db.all() and hive.files work only in local runs: nodes have no key enumeration and no files.
  • import json works. Only deterministic built-in modules can be imported (itertools, _functools, _collections, _operator, …). time, random, os, socket, threads and third-party packages are not available, by design. Pass timestamps and randomness in the input, or derive values with hive.hash().
  • String hashing uses a fixed seed. Event data must not contain floats.
module.wasm ~9.6 MiB (pre-initialized interpreter) + your source (≤ 1 MiB); the node limit is 12 MiB
Gas per call ~3.5–5 M for a small handler; a hive.call into another Python module adds ~5 M
Terminal window
hivec build counter.py # → dist/counter.hbc (+ counter.manifest.json)
hivec inspect dist/counter.hbc # uses `ndsr inspect` when available
hivec run counter.py increment '{"by": 2}' # builds, then `ndsr run` (gas limit 1e9 by default)
hivec run counter.py increment '{"by": 2}' --data-dir .state
hivec run --local counter.py increment '{"by": 2}' # in-process, no node
hivec functions counter.py

From Python: compile_file(path), compile_source(text, name), read_hbc(bytes), manifest_address(manifest, wasm), canonical_json(obj), keccak256(data), and hive.invoke_local(name, input) for unit tests.