Building and parsing operations

Operation group

class pytezos.operation.group.OperationGroup(context: ExecutionContext, contents: List[Dict[str, Any]] | None = None, protocol: str | None = None, chain_id: str | None = None, branch: str | None = None, signature: str | None = None, opg_hash: str | None = None, opg_result: Dict[str, Any] | None = None)[source]

Operation group representation: contents (single or multiple), signature, other fields, and also useful helpers for filling with precise fees, signing, forging, and injecting.

activate_account = <function ContentMixin.activate_account> Activate recommended allocations for contributions to the TF fundraiser.  More info https://activate.tezos.com/  :param activation_code: Secret code from pdf, leave empty for autocomplete :param pkh: Public key hash, leave empty for autocomplete :returns: dict or OperationGroup
autofill = <function OperationGroup.autofill> Fill the gaps and then simulate the operation in order to calculate fee, gas/storage limits.  :param gas_reserve: Add a safe reserve for dynamically calculated gas limit (default is 100). :param burn_reserve: Add a safe reserve for dynamically calculated storage limit (default is 100). :param counter: Override counter value (for manual handling) :param ttl: Number of blocks to wait in the mempool before removal (default is 5 for public network, 60 for sandbox) :param fee: Explicitly set fee for operation. If not set fee will be calculated depending on results of operation dry-run. :param gas_limit: Explicitly set gas limit for operation. If not set gas limit will be calculated depending on results of operation dry-run. In case of batch will be evenly split between operations. :param storage_limit: Explicitly set storage limit for operation. If not set storage limit will be calculated depending on results of operation dry-run. In case of batch will be evenly split between operations. :rtype: OperationGroup [source]
ballot = <function ContentMixin.ballot> Vote for a proposal in a given voting period.  Can only be submitted during Testing_vote or Promotion_vote periods, and only once per period. More info https://tezos.gitlab.io/master/whitedoc/voting.html  :param proposal: Hash of the proposal :param ballot: 'Yay', 'Nay' or 'Pass' :param source: Public key hash (of the signatory), leave None for autocomplete :param period: Number of the current voting period, leave None for autocomplete :returns: dict or OperationGroup
binary_payload = <function OperationGroup.binary_payload> Get binary payload used for injection/hash calculation.[source]
delegation = <function ContentMixin.delegation> Delegate funds or register yourself as a delegate.  :param delegate: tz address of delegate, leave None to register yourself as a delegate :param source: Address from which funds will be delegated, leave None to use signatory address :param counter: Current account counter, leave None for autocomplete :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup
double_baking_evidence = <function ContentMixin.double_baking_evidence> Provide evidence of double baking (two different blocks at the same height).  :param bh1: First block hash :param bh2: Second block hash :returns: dict or OperationGroup
double_endorsement_evidence = <function ContentMixin.double_endorsement_evidence> Provide evidence of double endorsement (endorsing two different blocks at the same block height).  Inline endorsement format:  .. code-block:: python  { "branch": $block_hash, "operations": { "kind": "endorsement", "level": integer [-2^31-2, 2^31+2] }, "signature"?: $Signature }  :param op1: Inline endorsement :param op2: Inline endorsement :returns: dict or OperationGroup
endorsement = <function ContentMixin.endorsement> Endorse a block.  :param level: Endorsed level :returns: dict or OperationGroup
endorsement_with_slot = <function ContentMixin.endorsement_with_slot> Wrap endorsement  :param endorsement: Inlined endorsement :param slot: Minimum of the slots endorsed :returns: dict or OperationGroup
failing_noop = <function ContentMixin.failing_noop> Operation to sign arbitrary data without the risk it will be used onchain.  :param arbitrary: Message to sign :returns: dict or OperationGroup
fill = <function OperationGroup.fill> Try to fill all fields left unfilled, use approximate fees (not optimal, use `autofill` to simulate operation and get precise values).  :param counter: Override counter value (for manual handling) :param ttl: Number of blocks to wait in the mempool before removal (default is 5 for public network, MAX for sandbox), -1 for MAX (if you have a private network and issues with block RPC queries) :param gas_limit: Override gas_limit value (for manual handling) :param storage_limit: Override storage_limit value (for manual handling) :param minimal_nanotez_per_gas_unit: Override minimal_nanotez_per_gas_unit constant (for manual handling) :rtype: OperationGroup [source]
forge = <function OperationGroup.forge> Convert json representation of the operation group into bytes.  :param validate: Forge remotely also and compare results, default is False :returns: Hex string [source]
hash = <function OperationGroup.hash> Calculate the Base58 encoded operation group hash.[source]
inject = <function OperationGroup.inject> Inject the signed operation group.  :param check_result: raise RpcError in case operation is applied but has runtime errors :param num_blocks_wait: number of blocks to wait for injection :param time_between_blocks: override the corresponding parameter from constants :param block_timeout: set block timeout (by default Pytezos will wait for a long time) :param min_confirmations: number of block injections to wait for before returning :param prevalidate: ask node to pre-validate the operation before the injection (True by default) :returns: operation group with metadata (raw RPC response) [source]
json_payload = <function OperationGroup.json_payload> Get JSON payload used for the injection.[source]
message = <function OperationGroup.message> Get payload for the failing noop operation  :param block: Specify operation branch (default is genesis) :returns: Message bytes [source]
operation = <function OperationGroup.operation> Create new operation group with extra content added.  :param content: Kind-specific operation body :rtype: OperationGroup [source]
origination = <function ContentMixin.origination> Deploy smart contract (scriptless KT accounts are not used for delegation since Babylon).  :param script: {"code": $Micheline, "storage": $Micheline} :param balance: Amount transferred on the balance, WARNING: there is no default way to withdraw funds. More info: https://tezos.stackexchange.com/questions/1315/can-i-withdraw-funds-from-an-empty-smart-contract :param delegate: Set contract delegate, default None :param source: Address from which funds will be sent, leave None to use signatory address :param counter: Current account counter, leave None for autocomplete :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup
preapply = <function OperationGroup.preapply> Preapply signed operation group.  :returns: RPC response from `preapply`   .. deprecated:: 3.1.0 This will be removed in 4.0.0. use `run_operation()` instead[source]
proposals = <function ContentMixin.proposals> Submit and/or upvote proposals to amend the protocol.  Can only be submitted during a proposal period. More info https://tezos.gitlab.io/master/whitedoc/voting.html  :param proposals: List of proposal hashes or single proposal hash :param source: Public key hash (of the signatory), leave None for autocomplete :param period: Number of the current voting period, leave 0 for autocomplete :returns: dict or OperationGroup
register_global_constant = <function ContentMixin.register_global_constant> Operation to register Micheline expressions in a global table of constants  :param value: Micheline expression :param source: Address from which funds will be delegated, leave None to use signatory address :param counter: Current account counter, leave None for autocomplete :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup
result = <function OperationGroup.result> Parse the preapply result.  :rtype: List[OperationResult]   .. deprecated:: 3.1.0 This will be removed in 4.0.0. use `run_operation()` instead[source]
reveal = <function ContentMixin.reveal> Reveal the public key associated with a tz address.  :param public_key: Public key to reveal, Base58 encoded :param source: Public key hash of the key revealed, leave None to use signatory address :param counter: Current account counter, leave None for autocomplete (More info https://tezos.stackexchange.com/questions/632/how-counter-grows) :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup
run = <function OperationGroup.run> Simulate operation without signature checks.  :param block_id: Specify a level at which this operation should be applied (default is head) :returns: RPC response from `run_operation` [source]
run_operation = <function OperationGroup.run_operation> Simulate operation without signature checks.  :param block_id: Specify a level at which this operation should be applied (default is head) :returns: RPC response from `run_operation` [source]
seed_nonce_revelation = <function ContentMixin.seed_nonce_revelation> Reveal the nonce committed operation in the previous cycle.  More info https://tezos.stackexchange.com/questions/567/what-are-nonce-revelations  :param level: When nonce hash was committed :param nonce: Hex string :returns: dict or OperationGroup
send = <function OperationGroup.send>   :param gas_reserve: Add a safe reserve for dynamically calculated gas limit (default is 100). :param burn_reserve: Add a safe reserve for dynamically calculated storage limit (default is 100). :param min_confirmations: number of block injections to wait for before returning (default is 0, i.e. async mode) :param ttl: Number of blocks to wait in the mempool before removal (default is 5 for public network, 60 for sandbox) :return: OperationGroup with hash filled [source]
send_async = <function OperationGroup.send_async>  Send operation without simulation or pre-validation  :param ttl: Number of blocks to wait in the mempool before removal (default is 5 for public network, 60 for sandbox) :param counter: Set counter value :param gas_limit: Set gas_limit value :param storage_limit: Set storage_limit value :param minimal_nanotez_per_gas_unit: Override minimal_nanotez_per_gas_unit constant :rtype: OperationGroup [source]
sign = <function OperationGroup.sign> Sign the operation group with the key specified by `using`.  :rtype: OperationGroup [source]
smart_rollup_add_messages = <function ContentMixin.smart_rollup_add_messages> Send external message to the global rollup inbox  :param message: Array of byte strings 2KB max each :param source: Address from which funds will be delegated, leave None to use signatory address :param counter: Current account counter, leave None for autocomplete :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup
smart_rollup_execute_outbox_message = <function ContentMixin.smart_rollup_execute_outbox_message> Execute outbox message on L1  :param rollup: Rollup address, base58 encoded (sr1) :param cemented_commitment: Commitment hash, base58 encoded (src1) :param output_proof: Byte string :param source: Address from which funds will be delegated, leave None to use signatory address :param counter: Current account counter, leave None for autocomplete :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup
transaction = <function ContentMixin.transaction> Transfer tez to a given address (implicit or originated).  If the receiver is a smart contract, then optional parameters may be passed.  :param source: Address from which funds will be sent, leave None to use signatory address :param destination: Address :param amount: Amount to send in microtez (int) or tez (Decimal) (optional) :param counter: Current account counter, leave None for autocomplete :param parameters: { "entrypoint": $string, "value": $Micheline expression } (optional) :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup
transfer_ticket = <function ContentMixin.transfer_ticket> Transfer ticket to another implicit account or to a smart rollup  :param ticket_contents: Micheline expression (value) :param ticket_ty: Micheline expression (type) :param ticket_ticketer: Ticketer address :param ticket_amount: Amount :param destination: Implicit or originated address (smart rollups not supported) :param entrypoint: Smart contract entrypoint (leave "default" for implicit accounts) :param source: Address from which funds will be delegated, leave None to use signatory address :param counter: Current account counter, leave None for autocomplete :param fee: Leave None for autocomplete :param gas_limit: Leave None for autocomplete :param storage_limit: Leave None for autocomplete :returns: dict or OperationGroup

Operation result

class pytezos.operation.result.OperationResult(**props)[source]

Operation result representation + useful parsing helpers for operation group

static burned(operation_group: Dict[str, Any]) int[source]

Get total burned (due to account allocations) for an operation group (recursively).

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

static consumed_gas(operation_group: Dict[str, Any]) int[source]

Get total consumed gas for an operation group (recursively).

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

static errors(operation_group: Dict[str, Any]) List[Dict[str, Any]][source]

Collect errors from all operation results in a group.

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

Returns:

list of errors [{“id”: “”, …}]

classmethod from_operation_group(operation_group: Dict[str, Any], **predicates) List[OperationResult][source]

Initialize with operation group contents.

Parameters:

operation_group – operation_group: {“branch”: “B…”, “contents”: […], …} :param predicates: filter contents using predicates field=value

Return type:

List[OperationResult]

classmethod from_origination(content: Dict[str, Any]) OperationResult[source]

Initialize with origination content.

Parameters:

content

Return type:

OperationResult

classmethod from_transaction(content: Dict[str, Any]) OperationResult[source]

Initialize with transaction content.

Parameters:

content

Return type:

OperationResult

static get_contents(operation_group: Dict[str, Any], **predicates) List[Dict[str, Any]][source]
static get_result(content: Dict[str, Any]) Dict[str, Any][source]
static is_applied(operation_group: Dict[str, Any]) bool[source]

Check if ALL operations in a group are applied.

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

static iter_contents(operation_group: Dict[str, Any]) Iterator[Dict[str, Any]][source]

Lazily iterate operation group contents including internal operations.

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

Returns:

generator

static iter_results(operation_group: Dict[str, Any]) Iterator[Dict[str, Any]][source]

Lazily iterate operation results including internal operation results.

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

Returns:

generator

static originated_contracts(operation_group: Dict[str, Any]) List[str][source]

Collect originated contract addresses from all operation results in a group.

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

Returns:

list of addresses [“tz12345…”, …]

static paid_storage_size_diff(operation_group: Dict[str, Any]) int[source]

Get total paid storage size diff for an operation group (recursively).

Parameters:

operation_group – {“branch”: “B…”, “contents”: […], …} OR a single content {“kind”: “transaction”, …}

Fees and limits

pytezos.operation.fees.calculate_fee(content: Dict[str, Any], consumed_gas: int, extra_size: int, reserve=10, minimal_nanotez_per_gas_unit: int | None = None) int[source]

Calculate minimal required operation fee.

Parameters:
  • content – operation content {…, “kind”: “transaction”, … }

  • consumed_gas – amount of gas consumed during the simulation (dry-run)

  • extra_size – size of the additional operation data (branch, etc)

  • reserve – safe reserve, just in case

pytezos.operation.fees.default_fee(content: Dict[str, Any], gas_limit: int | None = None, minimal_nanotez_per_gas_unit: int | None = None) int[source]

Take hard gas limit instead of precise amount (no simulation) and calculate fee.

Parameters:

content – operation content {…, “kind”: “transaction”, … }

pytezos.operation.fees.default_gas_limit(content: Dict[str, Any], constants: Dict[str, Any] | None = None) int[source]

Get default gas limit by operation kind.

Parameters:
  • content – operation content {…, “kind”: “transaction”, … }

  • constants – constants block from context

pytezos.operation.fees.default_storage_limit(content, constants: Dict[str, Any] | None = None) int[source]

Get default storage limit by operation kind.

Parameters:
  • content – operation content {…, “kind”: “transaction”, … }

  • constants – constants block from context

Local forge

pytezos.operation.forge.forge_activate_account(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_delegation(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_endorsement(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_endorsement_with_slot(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_entrypoint(entrypoint) bytes[source]

Encode Michelson contract entrypoint into the byte form.

Parameters:

entrypoint – string

pytezos.operation.forge.forge_failing_noop(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_inline_endorsement(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_operation(content: Dict[str, Any]) bytes[source]

Forge operation content (locally).

Parameters:

content – {.., “kind”: “transaction”, …}

pytezos.operation.forge.forge_operation_group(operation_group: Dict[str, Any]) bytes[source]

Forge operation group (locally).

Parameters:

operation_group – {“branch”: “B…”, “contents”: [], …}

pytezos.operation.forge.forge_origination(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_register_global_constant(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_reveal(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_smart_rollup_add_messages(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_smart_rollup_execute_outbox_message(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_tag(operation_tag: int) bytes[source]
pytezos.operation.forge.forge_transaction(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.forge_transfer_ticket(content: Dict[str, Any]) bytes[source]
pytezos.operation.forge.has_parameters(content: Dict[str, Any]) bool[source]