Source code for genlayer.chain

__all__ = (
	'ON',
	'IAccount',
	'Account',
	'Event',
	'ExternalMessageParams',
	'InternalMessageParams',
	'id',
)

import dataclasses
import typing

import genlayer._internal.on_chain.gl_call as gl_call
import genlayer.calldata as calldata
from genlayer import IS_IN_VM
from genlayer.types import Address, u256

type ON = typing.Literal['decided', 'finalized']
"""When the transaction message should be applied: ``'decided'`` or ``'finalized'``"""


[docs] @typing.final @dataclasses.dataclass(frozen=True) class InternalMessageParams(calldata.DataclassMixin): """ Fee parameters for a balance-funded internal message (``use_balance``). GenVM meters the primary fee from these params against the emitting contract's balance. The child transaction's ``declaredBudget`` also includes its descendant grant. Field names and layout mirror the executor's calldata encoding exactly. :param leader_time_units_allocation: time units allocated to the leader per round :param validator_time_units_allocation: time units allocated to each validator per round :param execution_budget_per_round: gas budget granted to the child execution per round :param rotations: per-round rotation allocations; must be non-empty (``appeal_rounds`` is ``len(rotations) - 1``) :param max_price_gen_per_time_unit: per-time-unit GEN price cap; must be non-zero :param storage_fee_max_gas_price: max gas price for the storage-fee component; must be non-zero :param receipt_fee_max_gas_price: max gas price for the receipt-fee component; must be non-zero """ leader_time_units_allocation: u256 validator_time_units_allocation: u256 execution_budget_per_round: u256 rotations: list[u256] max_price_gen_per_time_unit: u256 storage_fee_max_gas_price: u256 receipt_fee_max_gas_price: u256
[docs] @typing.final @dataclasses.dataclass(frozen=True) class ExternalMessageParams(calldata.DataclassMixin): gas_limit: u256 max_gas_price: u256
[docs] @typing.runtime_checkable class IAccount(typing.Protocol): """ Protocol defining the interface for on-chain accounts. Be that a contract or an EoA. """ @property def address(self) -> Address: """ Return the address of this account. """ ... @property def balance(self) -> u256: """ Returns current balance of this account. """ ...
[docs] class Account(IAccount): """ Class for on-chain accounts. """ __slots__ = ('_address',)
[docs] def __init__(self, address: Address, /): """ Get account at given address. Can be used to emit transfer messages to any address, even if there is no contract deployed at it. :param address: target account address :returns: account instance for the given address """ self._address = address
@property def address(self) -> Address: """ Return the address of this account. """ return self._address @property def balance(self) -> u256: """ Return current balance of this account. """ from genlayer.contract import get_at return get_at(self.address).balance
[docs] def emit_transfer(self, value: u256) -> None: """ Send value through an EVM-level external message, applied only on finalization. The calldata is empty and the recipient's contract code is never executed. Any nonzero address is accepted, including an Intelligent Contract address. The value comes from the emitting contract's balance, while fees use the external allocation pool. External messages do not support ``use_balance``, descendant grants or an ``on`` parameter. :param value: Amount of native tokens to transfer :raises ValueError: If value is not positive or the recipient is the zero address """ from genlayer._internal.on_chain import eth eth.perform_send(self.address, b'', value)
import inspect # noqa: E402 from genlayer._internal import reflect # noqa: E402 from genlayer.types.keccak import Keccak256 # noqa: E402 from .vm import public_abi as ABI # noqa: E402
[docs] class Event: # editorconfig-checker-disable """ .. code-block:: python class TransferOccurredEvent(gl.chain.Event): def __init__(self, sender: Address, to: Address, /): ... class TransferOccurredEvent(gl.chain.Event): def __init__(self, sender: Address, to: Address, /, **blob): ... Positional arguments bind to the indexed fields in declaration order, while the signature and the topics list the indexed fields **sorted** by name. """ # editorconfig-checker-enable
[docs] def __init__(self): """ Base class cannot be instantiated directly, it is only used for inheritance """ raise NotImplementedError()
signature: str """ Event signature (built-in/main topic). Consists of name and indexed fields in parenthesis, **sorted** Example: ``TransferOccurredEvent(from,to)`` """ name: str """ Event name. If not overridden it will be set to ``__name__`` """ indexed: tuple[str, ...] """ tuple of indexed arguments name in **sorted** order """ _blob: dict[str, calldata.Encodable] __slots__ = ('_blob',)
[docs] @staticmethod def emit_raw( topics: list[bytes], blob: calldata.Encodable, /, ) -> None: """ Emit a raw event with the given topics and blob of data. :param topics: list of topic byte strings (first is typically the event signature hash) :param blob: calldata-encodable payload """ gl_call.gl_call_generic( { 'EmitEvent': { 'topics': topics, 'blob': blob, } }, lambda _x: None, ).get()
@staticmethod def _do_init(klass) -> None: old_init = klass.__init__ if old_init is Event.__init__: raise TypeError('event class must define __init__') klass.__slots__ = ('_blob',) sig = inspect.signature(old_init) indexed_args_lst: list[str] = [] event_name = getattr(klass, 'name', klass.__name__) for i, (name, param) in enumerate(sig.parameters.items()): with reflect.context_notes(f'parameter `{name}`'): if i == 0: if name != 'self': raise TypeError('first argument must be `self`') continue match param.kind: case inspect.Parameter.VAR_POSITIONAL: raise TypeError('`*args` is forbidden') case inspect.Parameter.KEYWORD_ONLY: raise TypeError('keyword-only arguments are forbidden') case inspect.Parameter.POSITIONAL_OR_KEYWORD: raise TypeError('specify `/` after indexed fields') case inspect.Parameter.VAR_KEYWORD: pass case inspect.Parameter.POSITIONAL_ONLY: indexed_args_lst.append(name) declared_args = tuple(indexed_args_lst) indexed_args = tuple(sorted(indexed_args_lst)) def __init__(self, *args, **kwargs): if len(args) != len(declared_args): raise TypeError( f'indexed fields mismatch, expected {declared_args}, but got {len(args)} positional arguments' ) for name, val in zip(declared_args, args): if name in kwargs: raise TypeError(f'indexed field `{name}` must not be present in blob') kwargs[name] = val self._blob = kwargs signature = event_name signature += '(' signature += ','.join(indexed_args) signature += ')' if len(indexed_args) > ABI.EVENT_MAX_TOPICS: import warnings warnings.warn( f'event has too many indexed fields, it may not be emitted correctly: `{signature}`' ) klass.name = event_name klass.signature = signature klass.indexed = indexed_args klass.__init__ = __init__ def __init_subclass__(cls) -> None: with reflect.context_notes('generating event class'): with reflect.context_type(cls): Event._do_init(cls)
[docs] def emit(self) -> None: """ Emit this event. """ topics = [Keccak256(self.signature.encode('utf-8')).digest()] for i in self.indexed: d = self._blob[i] as_cd = calldata.encode(d) if len(as_cd) > 32: as_cd = Keccak256(as_cd).digest() else: as_cd = as_cd + b'\x00' * (32 - len(as_cd)) topics.append(as_cd) Event.emit_raw(topics, self._blob)
if typing.TYPE_CHECKING or not IS_IN_VM: id: u256 = ... # type: ignore """ Chain ID. It is the same as in the message """ else: from genlayer.message import chain_id as id