CLVM: Chia Contract Language Virtual Machine

0.9.15 · active · verified Fri Apr 17

CLVM is the Python implementation of the Contract Language (Chialisp) Virtual Machine, a core component of the Chia blockchain. It provides the low-level logic for evaluating Chialisp programs, handling s-expression data structures, and executing CLVM opcodes. Currently at version 0.9.15, it receives regular updates, often in sync with the broader Chia ecosystem, to enhance security, performance, and type correctness.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compile a Chialisp program using `clvm_tools.binutils.assemble`, prepare arguments as `SExp` objects, and execute the program using `clvm.operators.run_program`. It also shows how to interpret the `SExp` result.

from clvm.SExp import SExp
from clvm.operators import run_program
from clvm_tools.binutils import assemble

# Define a simple Chialisp program: multiply an argument by 2
chialisp_code = '(mod (X) (* X 2))'
program = assemble(chialisp_code)

# Define arguments for the program as an SExp object (e.g., 5)
args = assemble('(5)') # Arguments must also be assembled or created as SExp

# Run the program
cost, result = run_program(program, args)

print(f"Chialisp program: {chialisp_code}")
print(f"Arguments: {args.as_python()}")
print(f"Execution Cost: {cost}")
print(f"Result: {result.as_int()}") # Convert the SExp result back to a Python int

# Example 2: Simple addition of two numbers provided as constants in the program
chialisp_code_add = '(+ 2 3)'
program_add = assemble(chialisp_code_add)

# For this program, no external arguments are needed
cost_add, result_add = run_program(program_add, assemble('()'))

print(f"\nChialisp program: {chialisp_code_add}")
print(f"Execution Cost: {cost_add}")
print(f"Result: {result_add.as_int()}")

view raw JSON →