Skip to main content

GenLayer Python SDK

Project description

GenLayerPY

License: MIT Discord Twitter

About

GenLayerPY SDK is a python library designed for developers building decentralized applications (Dapps) on the GenLayer protocol. This SDK provides a comprehensive set of tools to interact with the GenLayer network, including client creation, transaction handling, event subscriptions, and more, all while leveraging the power of web3.py as the underlying blockchain client.

Prerequisites

Before installing GenLayerPY SDK, ensure you have the following prerequisites installed:

  • Python (>=3.12)

🛠️ Installation and Usage

To install the GenLayerPY SDK, use the following command:

$ pip install genlayer-py

Here’s how to initialize the client and connect to the GenLayer Simulator:

Reading a Transaction

from genlayer_py import create_client
from genlayer_py.chains import localnet

client = create_client(
    chain=localnet,
)

transaction_hash = "0x..."

transaction = client.get_transaction(hash=transaction_hash)

Waiting for Transaction Receipt

from genlayer_py import create_client
from genlayer_py.chains import localnet
from genlayer_py.types import TransactionStatus

client = create_client(chain=localnet)

# Get simplified receipt (default - removes binary data, keeps execution results)
receipt = client.wait_for_transaction_receipt(
    transaction_hash="0x...",
    status=TransactionStatus.FINALIZED,
    full_transaction=False  # Default - simplified for readability
)

# Get complete receipt with all fields
full_receipt = client.wait_for_transaction_receipt(
    transaction_hash="0x...",
    status=TransactionStatus.FINALIZED,
    full_transaction=True  # Complete receipt with all internal data
)

Reading a contract

from genlayer_py import create_client
from genlayer_py.chains import localnet

client = create_client(
    chain=localnet,
)

result = client.read_contract(
    address=contract_address,
    function_name='get_complete_storage',
    args=[],
    state_status='accepted'
)

Writing a transaction

from genlayer_py.chains import localnet
from genlayer_py import create_client, create_account

client = create_client(
    chain=localnet,
)

account = create_account()

transaction_hash = client.write_contract(
    account=account,
    transaction=transaction,
    address=contract_address,
    function_name='account',
    args=['new_storage'],
    value=0, // value is optional, if you want to send some native token to the contract
)
receipt = client.wait_for_transaction_receipt(
    hash=transaction_hash,
    status=TransactionStatus.FINALIZED, // or ACCEPTED
    full_transaction=False  // False by default - returns simplified receipt for better readability
)

Checking execution results

A transaction can be finalized by consensus but still have a failed execution. Always check tx_execution_result before reading contract state:

from genlayer_py import create_client, create_account
from genlayer_py.chains import testnet_bradbury
from genlayer_py.types import TransactionStatus, ExecutionResult

client = create_client(chain=testnet_bradbury, account=create_account())

receipt = client.wait_for_transaction_receipt(
    transaction_hash=tx_hash,
    status=TransactionStatus.FINALIZED,
)

if receipt.get("tx_execution_result_name") == ExecutionResult.FINISHED_WITH_RETURN.value:
    # Execution succeeded — safe to read state
    result = client.read_contract(
        address=contract_address,
        function_name="get_storage",
        args=[],
    )
elif receipt.get("tx_execution_result_name") == ExecutionResult.FINISHED_WITH_ERROR.value:
    # Execution failed — contract state was not modified
    raise RuntimeError("Contract execution failed")
else:
    # NOT_VOTED — execution hasn't completed
    print("Execution result not yet available")

Fetching emitted messages and triggered transactions

Transactions can emit messages to other contracts. These messages create new child transactions when processed:

tx = client.get_transaction(transaction_hash=tx_hash)

# Messages emitted by the contract during execution
print(tx["messages"])
# [{"messageType": 1, "recipient": "0x...", "value": 0, "data": "0x...", "onAcceptance": True, "saltNonce": 0}, ...]

# Child transaction IDs created from those messages (separate call)
child_tx_ids = client.get_triggered_transaction_ids(transaction_hash=tx_hash)
print(child_tx_ids)
# ["0xabc...", "0xdef..."]

Debugging transaction execution

Use debug_trace_transaction to inspect the full execution trace of a transaction, including return data, errors, and GenVM logs:

trace = client.debug_trace_transaction(
    transaction_hash=tx_hash,
    round=0,  # optional, defaults to 0
)

print(trace["result_code"])   # 0=success, 1=user error, 2=VM error
print(trace["return_data"])   # hex-encoded contract return data
print(trace["stderr"])        # standard error output
print(trace["genvm_log"])     # detailed GenVM execution logs

🚀 Key Features

  • Client Creation: Easily create and configure a client to connect to GenLayer’s network.
  • Transaction Handling: Send and manage transactions on the GenLayer network.
  • Gas Estimation: Estimate gas fees for executing transactions on GenLayer.

* under development

📖 Documentation

For detailed information on how to use GenLayerPY SDK, please refer to our documentation.

Contributing

We welcome contributions to GenLayerPY SDK! Whether it's new features, improved infrastructure, or better documentation, your input is valuable. Please read our CONTRIBUTING guide for guidelines on how to submit your contributions.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

genlayer_py-0.15.1.tar.gz (34.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

genlayer_py-0.15.1-py3-none-any.whl (46.5 kB view details)

Uploaded Python 3

File details

Details for the file genlayer_py-0.15.1.tar.gz.

File metadata

  • Download URL: genlayer_py-0.15.1.tar.gz
  • Upload date:
  • Size: 34.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for genlayer_py-0.15.1.tar.gz
Algorithm Hash digest
SHA256 9bcd18f35324a6fd250e7f86b8746cd46df31f21d5a5657c9cd41c5d2fc38de9
MD5 1e5beb5fad30f3d75f701b38fdfaeec3
BLAKE2b-256 253e2e8d919a25060a48853a288ccf180f2c52e9baee93c66d35d17d741d2ad1

See more details on using hashes here.

File details

Details for the file genlayer_py-0.15.1-py3-none-any.whl.

File metadata

  • Download URL: genlayer_py-0.15.1-py3-none-any.whl
  • Upload date:
  • Size: 46.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for genlayer_py-0.15.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b861b59aae503d6fa83ff4ab731a7a3feee0a29dca98a4e6a4abd8b256ff2234
MD5 e2ef77b2c295d997751e56ab0116cca2
BLAKE2b-256 b5993c821409291fc9ec8e380c8731700c79396a5308b0e5358d0b9683e26a4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page