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.
Install and create a project
Section titled “Install and create a project”curl -fsSL https://necter.network/install.sh | sh -s -- pythonnecter-init python counter_py && cd counter_pypython3 -m venv .venv && . .venv/bin/activate # Python 3.10+ (e.g. python3.12 if python3 is older)pip install -r requirements.txthivec build counter.py # -> dist/counter.hbcndsr run dist/counter.hbc increment --input '{"by":2}' --gas 50000000The installer puts ndsr and necter-init in ~/.necter/bin. The SDK itself is the Python distribution
necter-hivekit (import name hivekit), installed per project into a virtual environment; it provides the
hivec command there. To add it to an existing environment:
pip install https://github.com/Necter-Network/hivekit/releases/download/v1.0.0/necter_hivekit-1.0.0-py3-none-any.whlndsr run defaults to 1,000,000 gas, which is too little for the Python interpreter (about 10–12 M per
call): pass --gas 50000000. Install the tools
A module
Section titled “A module”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 | ~10–12 M for a small handler; a hive.call into another Python module adds ~10–12 M. Use 50,000,000 as the gas limit for Python calls |
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.