Building and parsing operations

Operation group

class pytezos.operation.group.OperationGroup(context: pytezos.context.impl.ExecutionContext, contents: Optional[List[Dict[str, Any]]] = None, protocol: Optional[str] = None, chain_id: Optional[str] = None, branch: Optional[str] = None, signature: Optional[str] = None, opg_hash: Optional[str] = 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(activation_code='', pkh='')

Activate recommended allocations for contributions to the TF fundraiser.

More info https://activate.tezos.com/

Parameters
  • activation_code – Secret code from pdf, leave empty for autocomplete

  • pkh – Public key hash, leave empty for autocomplete

Returns

dict or OperationGroup

autofill(gas_reserve: int = 100, burn_reserve: int = 100, counter: Optional[int] = None, ttl: Optional[int] = None, fee: Optional[int] = None, gas_limit: Optional[int] = None, storage_limit: Optional[int] = None, **kwargs)pytezos.operation.group.OperationGroup[source]

Fill the gaps and then simulate the operation in order to calculate fee, gas/storage limits.

Parameters
  • gas_reserve – Add a safe reserve for dynamically calculated gas limit (default is 100).

  • burn_reserve – Add a safe reserve for dynamically calculated storage limit (default is 100).

  • counter – Override counter value (for manual handling)

  • ttl – Number of blocks to wait in the mempool before removal (default is 5 for public network, 60 for sandbox)

  • fee – Explicitly set fee for operation. If not set fee will be calculated depending on results of operation dry-run.

  • gas_limit – Explicitly set gas limit for operation. If not set gas limit will be calculated depending on results of operation dry-run.

  • storage_limit – Explicitly set storage limit for operation. If not set storage limit will be calculated depending on results of operation dry-run.

Return type

OperationGroup

ballot(proposal, ballot, source='', period=0)

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

Parameters
  • proposal – Hash of the proposal

  • ballot – ‘Yay’, ‘Nay’ or ‘Pass’

  • source – Public key hash (of the signatory), leave None for autocomplete

  • period – Number of the current voting period, leave None for autocomplete

Returns

dict or OperationGroup

binary_payload()bytes[source]

Get binary payload used for injection/hash calculation.

delegation(delegate='', source='', counter=0, fee=0, gas_limit=0, storage_limit=0)

Delegate funds or register yourself as a delegate.

Parameters
  • delegate – tz address of delegate, leave None to register yourself as a delegate

  • source – Address from which funds will be delegated, leave None to use signatory address

  • counter – Current account counter, leave None for autocomplete

  • fee – Leave None for autocomplete

  • gas_limit – Leave None for autocomplete

  • storage_limit – Leave None for autocomplete

Returns

dict or OperationGroup

double_baking_evidence(bh1: str, bh2: str)

Provide evidence of double baking (two different blocks at the same height).

Parameters
  • bh1 – First block hash

  • bh2 – Second block hash

Returns

dict or OperationGroup

double_endorsement_evidence(op1: Dict[str, Any], op2: Dict[str, Any])

Provide evidence of double endorsement (endorsing two different blocks at the same block height).

Inline endorsement format:

{
    "branch": $block_hash,
    "operations": {
        "kind": "endorsement",
        "level": integer ∈ [-2^31-2, 2^31+2]
    },
    "signature"?: $Signature
}
Parameters
  • op1 – Inline endorsement

  • op2 – Inline endorsement

Returns

dict or OperationGroup

endorsement(level: int)Dict[str, Any]

Endorse a block.

Parameters

level – Endorsed level

Returns

dict or OperationGroup

endorsement_with_slot(endorsement: dict, slot: int)

Wrap endorsement

Parameters
  • endorsement – Inlined endorsement

  • slot – Minimum of the slots endorsed

Returns

dict or OperationGroup

failing_noop(arbitrary: str)

Operation to sign arbitrary data without the risk it will be used onchain.

Parameters

arbitrary – Message to sign

Returns

dict or OperationGroup

fill(counter: Optional[int] = None, ttl: Optional[int] = None, gas_limit: Optional[int] = None, storage_limit: Optional[int] = None, minimal_nanotez_per_gas_unit: Optional[int] = None, **kwargs)pytezos.operation.group.OperationGroup[source]

Try to fill all fields left unfilled, use approximate fees (not optimal, use autofill to simulate operation and get precise values).

Parameters
  • counter – Override counter value (for manual handling)

  • ttl – Number of blocks to wait in the mempool before removal (default is 5 for public network, 60 for sandbox)

  • gas_limit – Override gas_limit value (for manual handling)

  • storage_limit – Override storage_limit value (for manual handling)

  • minimal_nanotez_per_gas_unit – Override minimal_nanotez_per_gas_unit constant (for manual handling)

Return type

OperationGroup

forge(validate=False)str[source]

Convert json representation of the operation group into bytes.

Parameters

validate – Forge remotely also and compare results, default is False

Returns

Hex string

hash()str[source]

Calculate the Base58 encoded operation group hash.

inject(check_result: bool = True, num_blocks_wait: int = 5, time_between_blocks: Optional[int] = None, min_confirmations: int = 0, prevalidate: bool = True, **kwargs)[source]

Inject the signed operation group.

Parameters
  • check_result – raise RpcError in case operation is applied but has runtime errors

  • num_blocks_wait – number of blocks to wait for injection

  • time_between_blocks – override the corresponding parameter from constants

  • min_confirmations – number of block injections to wait for before returning

  • prevalidate – ask node to pre-validate the operation before the injection (True by default)

Returns

operation group with metadata (raw RPC response)

json_payload()Dict[str, Any][source]

Get JSON payload used for the injection.

message(block: Union[str, int] = 'genesis')bytes[source]

Get payload for the failing noop operation

Parameters

block – Specify operation branch (default is genesis)

Returns

Message bytes

operation(content: Dict[str, Any])pytezos.operation.group.OperationGroup[source]

Create new operation group with extra content added.

Parameters

content – Kind-specific operation body

Return type

OperationGroup

origination(script, balance=0, delegate=None, source='', counter=0, fee=0, gas_limit=0, storage_limit=0)

Deploy smart contract (scriptless KT accounts are not used for delegation since Babylon).

Parameters
  • script – {“code”: $Micheline, “storage”: $Micheline}

  • 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

  • delegate – Set contract delegate, default None

  • source – Address from which funds will be sent, leave None to use signatory address

  • counter – Current account counter, leave None for autocomplete

  • fee – Leave None for autocomplete

  • gas_limit – Leave None for autocomplete

  • storage_limit – Leave None for autocomplete

Returns

dict or OperationGroup

preapply()[source]

Preapply signed operation group.

Returns

RPC response from preapply

Deprecated since version 3.1.0: This will be removed in 4.0.0. use run_operation() instead

proposals(proposals, source='', period=0)

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

Parameters
  • proposals – List of proposal hashes or single proposal hash

  • source – Public key hash (of the signatory), leave None for autocomplete

  • period – Number of the current voting period, leave 0 for autocomplete

Returns

dict or OperationGroup

result()List[pytezos.operation.result.OperationResult][source]

Parse the preapply result.

Return type

List[OperationResult]

Deprecated since version 3.1.0: This will be removed in 4.0.0. use run_operation() instead

reveal(public_key='', source='', counter=0, fee=0, gas_limit=0, storage_limit=0)

Reveal the public key associated with a tz address.

Parameters
  • public_key – Public key to reveal, Base58 encoded

  • source – Public key hash of the key revealed, leave None to use signatory address

  • counter – Current account counter, leave None for autocomplete (More info https://tezos.stackexchange.com/questions/632/how-counter-grows)

  • fee – Leave None for autocomplete

  • gas_limit – Leave None for autocomplete

  • storage_limit – Leave None for autocomplete

Returns

dict or OperationGroup

run(block_id: str = 'head')[source]

Simulate operation without signature checks.

Parameters

block_id – Specify a level at which this operation should be applied (default is head)

Returns

RPC response from run_operation

run_operation(block_id: str = 'head')[source]

Simulate operation without signature checks.

Parameters

block_id – Specify a level at which this operation should be applied (default is head)

Returns

RPC response from run_operation

seed_nonce_revelation(level: int, nonce: str)

Reveal the nonce committed operation in the previous cycle.

More info https://tezos.stackexchange.com/questions/567/what-are-nonce-revelations

Parameters
  • level – When nonce hash was committed

  • nonce – Hex string

Returns

dict or OperationGroup

send(gas_reserve: int = 100, burn_reserve: int = 100, min_confirmations: int = 0, ttl: Optional[int] = None)pytezos.operation.group.OperationGroup[source]
Parameters
  • gas_reserve – Add a safe reserve for dynamically calculated gas limit (default is 100).

  • burn_reserve – Add a safe reserve for dynamically calculated storage limit (default is 100).

  • min_confirmations – number of block injections to wait for before returning (default is 0, i.e. async mode)

  • ttl – Number of blocks to wait in the mempool before removal (default is 5 for public network, 60 for sandbox)

Returns

OperationGroup with hash filled

send_async(ttl: int, counter: int, gas_limit: int, storage_limit: int, minimal_nanotez_per_gas_unit: Optional[int] = None)pytezos.operation.group.OperationGroup[source]

Send operation without simulation or pre-validation

Parameters
  • ttl – Number of blocks to wait in the mempool before removal (default is 5 for public network, 60 for sandbox)

  • counter – Set counter value

  • gas_limit – Set gas_limit value

  • storage_limit – Set storage_limit value

  • minimal_nanotez_per_gas_unit – Override minimal_nanotez_per_gas_unit constant

Return type

OperationGroup

sign()pytezos.operation.group.OperationGroup[source]

Sign the operation group with the key specified by using.

Return type

OperationGroup

transaction(destination, amount=0, parameters=None, source='', counter=0, fee=0, gas_limit=0, storage_limit=0)

Transfer tez to a given address (implicit or originated).

If the receiver is a smart contract, then optional parameters may be passed.

Parameters
  • source – Address from which funds will be sent, leave None to use signatory address

  • destination – Address

  • amount – Amount to send in microtez (int) or tez (Decimal) (optional)

  • counter – Current account counter, leave None for autocomplete

  • parameters – { “entrypoint”: $string, “value”: $Micheline expression } (optional)

  • fee – Leave None for autocomplete

  • gas_limit – Leave None for autocomplete

  • storage_limit – Leave None for autocomplete

Returns

dict or OperationGroup

with_slot()pytezos.operation.group.OperationGroup[source]

Wrap endorsement operation

Return type

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[pytezos.operation.result.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])pytezos.operation.result.OperationResult[source]

Initialize with origination content.

Parameters

content

Return type

OperationResult

classmethod from_transaction(content: Dict[str, Any])pytezos.operation.result.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: Optional[int] = 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: Optional[int] = None, minimal_nanotez_per_gas_unit: Optional[int] = 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: Optional[Dict[str, Any]] = 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: Optional[Dict[str, Any]] = 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_reveal(content: Dict[str, Any])bytes[source]
pytezos.operation.forge.forge_transaction(content: Dict[str, Any])bytes[source]
pytezos.operation.forge.has_parameters(content: Dict[str, Any])bool[source]