TransactionBuilder#

tradeexecutor.ethereum.tx.TransactionBuilder Python class in Trading Strategy framework.

class TransactionBuilder[source]#

Bases: ABC

Base class for different transaction builders.

We can build different transactions depending if we execute them directly from the hot wallet, or through a smart contract:

Life cycle:

  • TransactionBuilder is created once at the application startup

See also tradeeexecutor.ethereum.routing_state.EthereumRoutingState.create_signed_transaction().

__init__(web3)[source]#
Parameters:

web3 (Web3) –

Methods

__init__(web3)

broadcast(tx)

Broadcast the transaction.

broadcast_and_wait_transactions_to_complete(...)

Watch multiple transactions executed at parallel.

decode_signed_bytes(tx)

Get raw transaction data out from the signed tx bytes.

fetch_gas_price_suggestion()

Calculate the suggested gas price based on a policy.

get_erc_20_balance_address()

Get the address that holds ERC-20 supply

get_gas_wallet_address()

Get the address that holds native token for gas fees

get_gas_wallet_balance()

Get the balance of the native currency (ETH, BNB, MATIC) of the wallet.

get_token_delivery_address()

Get the target address for ERC-20 token delivery.

init()

Initialise the transaction builder.

serialise_to_broadcast_format(tx)

Prepare a transaction as a format ready to broadcast.

sign_transaction(contract, args_bound_func)

Createa a signed tranaction and set up tx broadcast parameters.

__init__(web3)[source]#
Parameters:

web3 (Web3) –

fetch_gas_price_suggestion()[source]#

Calculate the suggested gas price based on a policy.

Return type:

GasPriceSuggestion

broadcast(tx)[source]#

Broadcast the transaction.

Push the transaction to the peer-to-peer network / MEV relay to be includedin the

Sets the tx.broadcasted_at timestamp.

Returns:

Transaction hash, or tx_hash

Parameters:

tx (BlockchainTransaction) –

Return type:

HexBytes

static serialise_to_broadcast_format(tx)[source]#

Prepare a transaction as a format ready to broadcast.

Parameters:

tx (BlockchainTransaction) –

Return type:

SignedTransaction

static decode_signed_bytes(tx)[source]#

Get raw transaction data out from the signed tx bytes.

Parameters:

tx (BlockchainTransaction) –

Return type:

dict

static broadcast_and_wait_transactions_to_complete(web3, txs, confirmation_block_count=0, max_timeout=datetime.timedelta(seconds=300), poll_delay=datetime.timedelta(seconds=1), revert_reasons=False)[source]#

Watch multiple transactions executed at parallel.

Modifies the given transaction objects in-place and updates block inclusion and succeed status.

Note

This method is designed to be used only in unit testing as a shortcut.

Parameters:
abstract init()[source]#

Initialise the transaction builder.

Called on application startup.

abstract sign_transaction(contract, args_bound_func, gas_limit=None, gas_price_suggestion=None, asset_deltas=None)[source]#

Createa a signed tranaction and set up tx broadcast parameters.

Parameters:
  • args_bound_func (ContractFunction) – A Solidity function with its arguments bound to the function instance.

  • gas_limit (Optional[int]) –

    Max gas limit per this transaction.

    The transaction will fail if the gas limit is exceeded.

    If set to None then it is up to the signed to figure it out based on the function hints.

  • gas_price_suggestion (Optional[GasPriceSuggestion]) –

    What gas price will be used.

    Support old-style and London style transactions.

  • asset_deltas (Optional[List[AssetDelta]]) – Expected assets inbound and outbound.

  • contract (Contract) –

Returns:

Prepared BlockchainTransaction instance.

This transaction object can be stored in the persistent state.

Return type:

BlockchainTransaction

abstract get_token_delivery_address()[source]#

Get the target address for ERC-20 token delivery.

Where do Uniswap should send the tokens after a swap.

Return type:

str

abstract get_erc_20_balance_address()[source]#

Get the address that holds ERC-20 supply

Return type:

str

abstract get_gas_wallet_address()[source]#

Get the address that holds native token for gas fees

Return type:

str

abstract get_gas_wallet_balance()[source]#

Get the balance of the native currency (ETH, BNB, MATIC) of the wallet.

Useful to check if you have enough cryptocurrency for the gas fees.

Return type:

Decimal