EthereumRoutingState#

API documentation for tradeexecutor.ethereum.routing_state.EthereumRoutingState Python class in Trading Strategy framework.

class EthereumRoutingState[source]#

Bases: RoutingState

Manage transaction building for multiple Uniswap trades.

  • Lifespan is one rebalance - remembers already made approvals

  • Web3 connection and hot wallet

  • Approval tx creation

  • Swap tx creation

Manage the state of already given approvals here, so that we do not do duplicates.

The approvals are not persistent in the executor state, but are specific for each cycle.

__init__(pair_universe, *, tx_builder=None, swap_gas_limit=None, approve_gas_limit=None, web3=None)[source]#
Parameters:
  • pair_universe (PandasPairUniverse) – Pairs we trade

  • tx_builder (Optional[TransactionBuilder]) –

    For creating trade transactions.

    Can be set to None on DummyExecutionModel.

  • web3 (Optional[Web3]) –

    Use for routing smart contract reads.

    Given when tx_builder is not present.

  • swap_gas_limit (int | None) – What is the max gas we are willing to pay for a swap.

  • approve_gas_limit (int | None) –

Methods

__init__(pair_universe, *[, tx_builder, ...])

param pair_universe:

adjust_spend(asset, required_amount[, ...])

Check that our on-chain balances have enough tokens to cover the trade.

check_has_enough_tokens(erc_20, amount)

Check we have enough buy side tokens to do a trade.

create_signed_transaction(contract, ...[, notes])

ensure_token_approved(token_address, ...[, ...])

Make sure we have ERC-20 approve() for the trade

get_uniswap_for_pair()

Get a router for a trading pair.

is_approved_on_chain(token_address, ...)

is_route_approved(router_address)

mark_router_approved(token_address, ...)

trade_on_router_three_way(uniswap, ...[, ...])

Prepare the actual swap for three way trade.

trade_on_router_two_way(uniswap, ...[, ...])

Prepare the actual swap.

validate_exchange(target_pair, intermediary_pair)

Check routing happens on the same exchange

validate_pairs(target_pair, intermediary_pair)

Check we can chain two pairs

Attributes

__init__(pair_universe, *, tx_builder=None, swap_gas_limit=None, approve_gas_limit=None, web3=None)[source]#
Parameters:
  • pair_universe (PandasPairUniverse) – Pairs we trade

  • tx_builder (Optional[TransactionBuilder]) –

    For creating trade transactions.

    Can be set to None on DummyExecutionModel.

  • web3 (Optional[Web3]) –

    Use for routing smart contract reads.

    Given when tx_builder is not present.

  • swap_gas_limit (int | None) – What is the max gas we are willing to pay for a swap.

  • approve_gas_limit (int | None) –

abstract get_uniswap_for_pair()[source]#

Get a router for a trading pair.

abstract trade_on_router_two_way(uniswap, target_pair, reserve_asset, reserve_amount, max_slippage, check_balances, asset_deltas=None, notes='')[source]#

Prepare the actual swap. Same for Uniswap V2 and V3.

Parameters:
abstract trade_on_router_three_way(uniswap, target_pair, intermediary_pair, reserve_asset, reserve_amount, max_slippage, check_balances, asset_deltas=None, notes='')[source]#

Prepare the actual swap for three way trade.

Parameters:
check_has_enough_tokens(erc_20, amount)[source]#

Check we have enough buy side tokens to do a trade.

This might not be the case if we are preparing transactions ahead of time and sell might have not happened yet.

Parameters:
  • erc_20 (Contract) –

  • amount (int) –

ensure_token_approved(token_address, router_address, amount=115792089237316195423570985008687907853269984665640564039457584007913129639935)[source]#

Make sure we have ERC-20 approve() for the trade

  • Infinite approval on-chain

  • …or previous approval in this state,

Parameters:
  • token_address (str) –

  • router_address (str) –

  • amount (int) – How much to approve, default to approve infinite amount

Returns:

Create 0 or 1 transactions if needs to be approved

Return type:

List[BlockchainTransaction]

adjust_spend(asset, required_amount, epsilon=1e-05, check_balances=False)[source]#

Check that our on-chain balances have enough tokens to cover the trade.

This is designed to work with the case that there is one position and sell all trade is incoming. We do not want this trade to fail because some mishaps in the accounting, or at least have a very clear error when it does.

Raises:

OutOfBalance – We do not have enough tokens to cover the trade

Parameters:
  • epsilon (float) –

    How much floating point error tolerance is ok.

    Default to 1 BPS.

  • asset (AssetIdentifier) –

  • required_amount (int) –

  • check_balances (bool) –

Returns:

The actual token amount we can spent on the transaction

Return type:

int

static validate_pairs(target_pair, intermediary_pair)[source]#

Check we can chain two pairs

static validate_exchange(target_pair, intermediary_pair)[source]#

Check routing happens on the same exchange