Package genlayer.std#
gl#
Blockchain specific functionality, that won’t work without GenVM and reexports form
genlayer.pyprovided for convenience
- gl.calldata
Proxy to module
genlayer.py.calldata
- gl.advanced
Proxy to module
gl..advanced
- gl.wasi
Proxy to module
gl.._wasi
- gl.eth
Proxy to module
genlayer.py.eth
- class gl.Contract[source]#
Bases:
objectClass that indicates main user contract
- abstract __handle_undefined_method__(method_name: str, args: list[Any], kwargs: dict[str, Any])[source]#
Method that is called for no-method calls, must be either
@gl.public.writeor@gl.public.write.payable
- __on_errored_message__()[source]#
Method that is called when emitted message with non-zero value failed. This method is not abstract to just receive value. It must be
@gl.public.write.payable
- class gl.ContractAt[source]#
Bases:
GenVMContractProxyProvides a way to call view methods and send transactions to GenVM contracts
- emit(**data: Unpack[TransactionDataKwArgs])[source]#
Namespace with write message
- Returns:
object supporting
.name(*args, **kwargs)that emits a message and returnsNone
- emit_transfer(**data: Unpack[TransactionDataKwArgs])[source]#
Method to emit a message that transfers native tokens
- class gl.Event[source]#
Bases:
objectclass TransferOccurredEvent(gl.Event): def __init__(self, from: Address, to: Address, /): ... class TransferOccurredEvent(gl.Event): def __init__(self, from: Address, to: Address, /, **blob): ...
- class gl.MessageRawType[source]#
Bases:
TypedDict- __optional_keys__ = frozenset({})#
- __required_keys__ = frozenset({'chain_id', 'contract_address', 'datetime', 'entry_data', 'entry_kind', 'entry_stage_data', 'is_init', 'origin_address', 'sender_address', 'stack', 'value'})#
- __total__ = True#
- entry_stage_data: Encodable#
- class gl.MessageType[source]#
Bases:
NamedTupleMessageType(contract_address, sender_address, origin_address, value, chain_id)
- __getnewargs__()#
Return self as a plain tuple. Used by copy and pickle.
- static __new__(_cls, contract_address: Address, sender_address: Address, origin_address: Address, value: u256, chain_id: u256)#
Create new instance of MessageType(contract_address, sender_address, origin_address, value, chain_id)
- __repr__()#
Return a nicely formatted representation string
- gl.contract_interface(_contr: GenVMContractDeclaration) Callable[[Address], GenVMContractProxy][source]#
This decorator produces an “interface” for other GenVM contracts. It has no semantical value, but can be used for auto completion and type checks
@gl.contract_interface class MyContract: class View: def view_meth(self, i: int) -> int: ... class Write: def write_meth(self, i: int) -> None: ...
- gl.deploy_contract(*, code: bytes, args: Sequence[Any] = [], kwargs: Mapping[str, Any] = {}, **data: Unpack[DeploymentTransactionDataKwArgs]) Address | None[source]#
Function for deploying new genvm contracts
- Parameters:
- Returns:
address of new contract iff non-zero
salt_noncewas provided- Return type:
Address | None
Note
Refer to consensus documentation for exact specification of
salt_noncerequirements and it’s effect on addressorder of transactions
- gl.eq_principle_prompt_comparative(fn: Callable[[], T], principle: str) T[source]#
Comparative equivalence principle that utilizes NLP for verifying that results are equivalent
- Parameters:
- Return type:
T
See
gl.advanced..run_nondet()for description of data transformationsNote
As leader results are encoded as calldata,
format()is used for string representation. However, operating on strings by yourself is more safe in generalNote
supports
.lazy()version, which will returnLazy
- gl.eq_principle_prompt_non_comparative(fn: Callable[[], str], *, task: str, criteria: str) str[source]#
Non-comparative equivalence principle that must cover most common use cases
Both leader and validator finish their execution via NLP, that is used to perform
taskoninput. Leader just executes this task, but the validator checks if task was performed with integrity. This principle is useful when task is subjectiveSee
run_nondet()for description of data transformationsNote
supports
.lazy()version, which will returnLazy- Return type:
- gl.eq_principle_strict_eq(fn: Callable[[], T]) T[source]#
Comparative equivalence principle that checks for strict equality
- Parameters:
fn (Callable[[], T]) – functions to perform an action
- Return type:
T
See
gl.advanced..run_nondet()for description of data transformationsNote
supports
.lazy()version, which will returnLazy
- gl.eth_contract(contr: ContractDeclaration) Callable[[Address], ContractProxy]#
- Return type:
- gl.exec_prompt(prompt: str, **config: Unpack[ExecPromptKwArgs]) str | dict[source]#
API to execute a prompt (perform NLP)
- Parameters:
prompt (
str) – prompt itself**config (
ExecPromptKwArgs) – configuration
- Return type:
str
Note
supports
.lazy()version, which will returnLazy
- gl.get_webpage(url: str, **config: Unpack[GetWebpageKwArgs]) str | Image[source]#
API to get a webpage after rendering it
- Parameters:
url (
str) – url of website**config (
GetWebpageKwArgs) – configuration
- Return type:
str
Note
supports
.lazy()version, which will returnLazy
- gl.message: MessageType = Ellipsis#
Represents fields from a transaction message that was sent
- gl.message_raw: MessageRawType = Ellipsis#
Raw message as parsed json
- gl.private(f)[source]#
Decorator that marks method as private. As all methods are private by default it does nothing.
- class gl.public[source]#
Bases:
object- write = <genlayer.std.annotations._write object>#
Decorator that marks a contract method as a public write. Has .payable
@gl.public.write def foo(self) -> None: ... @gl.public.write.payable def bar(self) -> None: ... @gl.public.write.min_gas(leader=100, validator=20).payable def bar(self) -> None: ...
- Return type:
T
gl.calldata#
This module is responsible for working with genvm calldata
Calldata natively supports following types:
Primitive types:
Composite types:
list(and any othercollections.abc.Sequence)dictwithstrkeys (and any othercollections.abc.Mappingwithstrkeys)
For full calldata specification see genvm repo
- class gl.calldata.CalldataEncodable[source]#
Bases:
objectAbstract class to support calldata encoding for custom types
Can be used to simplify code
- exception gl.calldata.DecodingError[source]#
Bases:
ValueError
- gl.calldata.decode(mem0: Buffer, *, memview2bytes: Callable[[memoryview], Any] = bytes) Decoded[source]#
Decodes calldata encoded bytes into python DSL
Out of composite types it will contain only
dictandlist- Return type:
Decoded
- gl.calldata.encode(x: EncodableWithDefault, *, default: Callable[[EncodableWithDefault], Encodable] | None = None) bytes[source]#
Encodes python object into calldata bytes
- Parameters:
default (Callable[[EncodableWithDefault], Encodable] | None) – function to be applied to each object recursively, it must return object encodable to calldata
- Return type:
gl.advanced#
This module provides some “advanced” features that can be used for optimizations
Warning
If you are using something “advanced” you must know what you do
- exception gl.advanced.ContractError[source]#
Bases:
ExceptionRepresents “Contract error” result of a contract that is passed to validator function of
gl..run_nondet()Validating leader output and sandbox invocation are only places where contract can “handle” contract error
- __eq__(other)#
Return self==value.
- __hash__ = None#
- __repr__()#
Return repr(self).
- class gl.advanced.ContractReturn[source]#
Bases:
objectRepresents a normal “Return” result of a contract that is passed to validator function of
gl..run_nondet()- __eq__(other)#
Return self==value.
- __hash__ = None#
- __repr__()#
Return repr(self).
- data: Decoded#
- gl.advanced.emit_raw_event(name: str, indexed_fields_names: Sequence[str], blob: Encodable) None[source]#
- gl.advanced.run_nondet(leader_fn: Callable[[], T], validator_fn: Callable[[ContractReturn | Rollback | ContractError], bool]) Lazy[source]#
Most generic user-friendly api to execute a non-deterministic block
- Parameters:
leader_fn (Callable[[], T]) – function that is executed in the leader
validator_fn (Callable[[ContractReturn | Rollback | ContractError], bool]) – function that is executed in the validator that also checks leader result
- Return type:
Uses cloudpickle to pass a “function” to sub VM
Note
If validator_fn produces an error and leader_fn produces an error, executor itself will set result of this block to “agree” and fail entire contract with leader’s error. This is done because not all errors can be caught in code itself (i.e.
exit). If this behavior is not desired, just fast returnFalsefor leader error result.Warning
All sub-vm returns go through
genlayer.py.calldataencoding
- gl.advanced.sandbox(fn: Callable[[], T], *, allow_write_ops: bool = False) Lazy[source]#
Runs function in the sandbox
- Return type:
- gl.advanced.validator_handle_rollbacks_and_errors_default(fn: Callable[[], Decoded], leaders_result: ContractReturn | Rollback | ContractError) tuple[Decoded, Decoded][source]#
Default function to handle rollbacks and contract errors
Errors and rollbacks are always checked for strict equality, which means that it’s user responsibility to dump least possible text in there
- Returns:
ContractReturndata fields as(validator, leader)`iff both results are not errors/rollbacks- Return type:
tuple[Decoded, Decoded]
gl.eth#
This module is responsible for interactions with ghost/external contracts
- class gl.eth.ContractDeclaration[source]#
-
Interface for declaring interfaces of external contracts
- __init__(*args, **kwargs)#
- class gl.eth.ContractProxy[source]#
Bases:
Generic- __init__(address: Address, view_impl: Callable[[ContractProxy], TView], balance_impl: Callable[[ContractProxy], u256], send_impl: Callable[[ContractProxy, TransactionDataKwArgs], TWrite], transfer_impl: Callable[[ContractProxy, TransactionDataKwArgs], None])[source]#
- address#
- class gl.eth.InplaceTuple[source]#
Bases:
objectThis class indicates that tuple should be encoded/decoded in-place. Which means that even if it is dynamically sized, it is ignored. It is useful for encoding/decoding arguments and returns
tuple[InplaceTuple, str, u256]