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.
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().
Conventions (same as JavaScript)
Section titled “Conventions (same as JavaScript)”- Input: the call input parsed as JSON; the raw string if it is not JSON;
{}if empty. - Output: a returned
stris 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.callreturns the decoded callee output and raisesHiveCallError(.code) on failure;hive.call_rawreturns the raw string;hive.try_callreturnsNone.hive.storageholds raw strings;hive.db(andctx.db) stores JSON values in the same state.db.keys(),db.all()andhive.fileswork only in local runs: nodes have no key enumeration and no files.
Inside a module
Section titled “Inside a module”import jsonworks. 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 withhive.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 |
Build, run, test
Section titled “Build, run, test”hivec build counter.py # → dist/counter.hbc (+ counter.manifest.json)hivec inspect dist/counter.hbc # uses `ndsr inspect` when availablehivec run counter.py increment '{"by": 2}' # builds, then `ndsr run` (gas limit 1e9 by default)hivec run counter.py increment '{"by": 2}' --data-dir .statehivec run --local counter.py increment '{"by": 2}' # in-process, no nodehivec functions counter.pyFrom 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.