EthereumRoutingStateBase#

tradeexecutor.ethereum.routing_state.EthereumRoutingStateBase class.

class EthereumRoutingStateBase[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=2000000, 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.

  • swap_gas_limit – What is the max gas we are willing to pay for a swap.

  • web3 (Optional[Web3]) –

Methods

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

param pair_universe

check_has_enough_tokens(erc_20, amount)

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

ensure_token_approved(token_address, ...)

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

get_base_and_quote(target_pair, reserve_asset)

Get base and quote token from the pair and reserve asset.

get_base_quote_intermediary(target_pair, ...)

get_signed_tx(swap_func, gas_limit)

is_approved_on_chain(token_address, ...)

is_route_approved(router_address)

mark_router_approved(token_address, ...)

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=2000000, 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.

  • swap_gas_limit – What is the max gas we are willing to pay for a swap.

  • web3 (Optional[Web3]) –

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
ensure_token_approved(token_address, router_address)[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) –

Returns

Create 0 or 1 transactions if needs to be approved

Return type

List[BlockchainTransaction]

get_base_and_quote(target_pair, reserve_asset)[source]#

Get base and quote token from the pair and reserve asset.

See: https://tradingstrategy.ai/docs/programming/market-data/trading-pairs.html

Parameters
  • target_pair – Pair to be traded

  • reserver_asset – Asset to be kept as reserves

Returns

(base_token: Contract, quote_token: Contract)

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