Building and parsing operations

Operation group

class pytezos.operation.group.OperationGroup(contents=None, protocol=None, chain_id=None, branch=None, signature=None, shell=None, key=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=100, counter=None)[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 gas limit (default is 100)

  • counter – Override counter value (for manual handling)

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, bh2)

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, op2: dict)

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

Parameters
  • op1

    Inline endorsement {

    ”branch”: $block_hash, “operations”: {

    ”kind”: “endorsement”, “level”: integer ∈ [-2^31-2, 2^31+2]

    }, “signature”?: $Signature

    }

  • op2 – Inline endorsement

Returns

dict or OperationGroup

endorsement(level: int)

Endorse a block.

Parameters

level – Endorsed level

Returns

dict or OperationGroup

fill(counter=None)[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)

Return type

OperationGroup

forge(validate=True)[source]

Convert json representation of the operation group into bytes.

Parameters

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

Returns

Hex string

hash() → str[source]

Calculate the Base58 encoded operation group hash.

inject(_async=True, preapply=True, check_result=True, num_blocks_wait=2)[source]

Inject the signed operation group.

Parameters
  • _async – do not wait for operation inclusion (default is True)

  • preapply – do a preapply before injection

  • check_result – raise RpcError in case operation is refused

  • num_blocks_wait – number of blocks to wait for injection

Returns

operation group with metadata (raw RPC response)

json_payload() → dict[source]

Get json payload used for the preapply.

operation(content)[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

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()[source]

Parse the preapply result.

Return type

OperationResult

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='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)

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

sign()[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

using(shell: pytezos.rpc.shell.ShellQuery = None, key: pytezos.crypto.Key = None)

Change current rpc endpoint and account (private key).

Parameters
  • shell – one of ‘carthagenet’, ‘mainnet’, ‘delphinet’, ‘dalphanet’ or RPC node uri, or instance of ShellQuery

  • key – base58 encoded key, path to the faucet file, alias from tezos-client, or instance of Key

Returns

A copy of current object with changes applied

Operation result

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

Operation result representation + useful parsing helpers for operation group

static burned(operation_group) → 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) → 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) → list[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, **predicates)[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)[source]

Initialize with origination content.

Parameters

content

Return type

OperationResult

classmethod from_transaction(content: dict)[source]

Initialize with transaction content.

Parameters

content

Return type

OperationResult

static get_contents(operation_group: dict, **predicates)[source]
static get_result(content)[source]
static is_applied(operation_group) → 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)[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)[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) → list[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) → 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, consumed_gas: int, extra_size: int, reserve=10) → 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) → 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) → int[source]

Get default gas limit by operation kind.

Parameters

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

pytezos.operation.fees.default_storage_limit(content)[source]

Get default storage limit by operation kind.

Parameters

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

Local forge

pytezos.operation.forge.forge_activate_account(content: dict)[source]
pytezos.operation.forge.forge_delegation(content)[source]
pytezos.operation.forge.forge_operation(content) → bytes[source]

Forge operation content (locally).

Parameters

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

pytezos.operation.forge.forge_operation_group(operation_group)[source]

Forge operation group (locally).

Parameters

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

pytezos.operation.forge.forge_origination(content)[source]
pytezos.operation.forge.forge_reveal(content)[source]
pytezos.operation.forge.forge_transaction(content)[source]
pytezos.operation.forge.has_parameters(content)[source]