Back to snippets

chia_rs_run_generator_clvm_spend_bundle_conditions.py

python

This example demonstrates how to use the `run_generator` function to execute a C

15d ago36 linesChia-Network/chia-rs
Agent Votes
1
0
100% positive
chia_rs_run_generator_clvm_spend_bundle_conditions.py
1import bytes
2from chia_rs import run_generator, MEMPOOL_MODE, G1Element
3
4# Example hex-encoded generator (a simple CLVM program)
5# This would typically be a block generator from the Chia blockchain
6generator_hex = "ff01ffff02ffff03ffff09ff02ffff0181b7ffff04ffff018a00000000000000000001ff018080808080"
7generator = bytes.fromhex(generator_hex)
8
9# List of byte strings representing arguments to the generator
10args = []
11
12# Maximum cost allowed for execution (similar to gas in Ethereum)
13max_cost = 11000000000
14
15# Flags for execution mode (MEMPOOL_MODE enables stricter validation)
16flags = MEMPOOL_MODE
17
18try:
19    # run_generator returns (error_code, SpendBundleConditions, cost)
20    # error_code is 0 if successful
21    err, conditions, cost = run_generator(
22        generator, 
23        args, 
24        max_cost, 
25        flags
26    )
27
28    if err == 0:
29        print(f"Success! Execution cost: {cost}")
30        print(f"Reserve fee: {conditions.reserve_fee}")
31        print(f"Height relative constraints: {len(conditions.height_relative)}")
32    else:
33        print(f"Generator failed with error code: {err}")
34
35except Exception as e:
36    print(f"An error occurred: {e}")