RoutingModelBase#
tradeexecutor.ethereum.routing_model.RoutingModelBase class.
- class RoutingModelBase[source]#
Bases:
RoutingModel
A simple router that does not optimise the trade execution cost. Designed for uniswap-v2 forks.
Able to trade on multiple exchanges
Able to three-way trades through predefined intermediary hops, either on the exchange itself or some outside exchange
- __init__(allowed_intermediary_pairs, reserve_token_address, chain_id=None)[source]#
- Parameters
addresses –
Defines router smart contracts to be used with each DEX.
Each Uniswap v2 is uniquely identified by its factory contract. Addresses always lowercase. Factory Router map
For Uniswap V3, addresses is a dict of factory, router, position_manager, and quoter addresses
allowed_intermediary_pairs (Dict[str, str]) –
Quote token address -> pair smart contract address mapping.
Because we hold our reserves only in one currecy e.g. BUSD and we want to trade e.g. Cake/BNB pairs, we need to whitelist BNB as an allowed intermediary token. This makes it possible to do BUSD -> BNB -> Cake trade. This set is the list of pair smart contract addresses that are allowed to be used as a hop.
chain_id (Optional[ChainId]) – Store the chain id for which these routes were generated for.
reserve_token_address (str) – Token address of our reserve currency. Relevent for buy/sell routing. Lowercase.
Methods
__init__
(allowed_intermediary_pairs, ...[, ...])- param addresses
convert_address_dict_to_lower
(factory_router_map)Convert all key addresses to lowercase to avoid mix up with Ethereum address checksums
create_routing_state
(universe, ...)Create a new routing state for this cycle.
execute_trades_internal
(pair_universe, ...)Split for testability.
get_default_trading_fee
()Get the trading/LP fee applied to all trading pairs.
get_reserve_asset
(pair_universe)Translate our reserve token address tok an asset description.
intermediary_pair_assertion
(intermediary_pair)make_direct_trade
(routing_state, ...[, ...])Prepare a trade where target pair has out reserve asset as a quote token.
make_multihop_trade
(routing_state, ...[, ...])Prepare a trade where target pair has out reserve asset as a quote token.
perform_preflight_checks_and_logging
(...)"Checks the integrity of the routing.
pre_trade_assertions
(reserve_asset_amount, ...)Some basic assertions made at the beginning of the trade() method on child class.
reserve_asset_logging
(pair_universe)route_pair
(pair_universe, trading_pair)Return Uniswap routing information (path components) for a trading pair.
route_pair_assertions
(trading_pair, ...)route_trade
(pair_universe, trade)Figure out how to map an abstract trade to smart contracts.
setup_trades
(routing_state, trades[, ...])Strategy and live execution connection.
trade
(routing_state, target_pair, ...[, ...])- param routing_state
- __init__(allowed_intermediary_pairs, reserve_token_address, chain_id=None)[source]#
- Parameters
addresses –
Defines router smart contracts to be used with each DEX.
Each Uniswap v2 is uniquely identified by its factory contract. Addresses always lowercase. Factory Router map
For Uniswap V3, addresses is a dict of factory, router, position_manager, and quoter addresses
allowed_intermediary_pairs (Dict[str, str]) –
Quote token address -> pair smart contract address mapping.
Because we hold our reserves only in one currecy e.g. BUSD and we want to trade e.g. Cake/BNB pairs, we need to whitelist BNB as an allowed intermediary token. This makes it possible to do BUSD -> BNB -> Cake trade. This set is the list of pair smart contract addresses that are allowed to be used as a hop.
chain_id (Optional[ChainId]) – Store the chain id for which these routes were generated for.
reserve_token_address (str) – Token address of our reserve currency. Relevent for buy/sell routing. Lowercase.
- get_reserve_asset(pair_universe)[source]#
Translate our reserve token address tok an asset description.
- Parameters
pair_universe (PandasPairUniverse) –
- Return type
- make_direct_trade(routing_state, target_pair, reserve_asset, reserve_amount, max_slippage, check_balances=False)[source]#
Prepare a trade where target pair has out reserve asset as a quote token.
- Returns
List of approval transactions (if any needed)
- Parameters
routing_state (EthereumRoutingStateBase) –
target_pair (TradingPairIdentifier) –
reserve_asset (AssetIdentifier) –
reserve_amount (int) –
max_slippage (float) –
- Return type
- make_multihop_trade(routing_state, target_pair, intermediary_pair, reserve_asset, reserve_amount, max_slippage, check_balances=False)[source]#
Prepare a trade where target pair has out reserve asset as a quote token.
- Returns
List of approval transactions (if any needed)
- Parameters
routing_state (EthereumRoutingStateBase) –
target_pair (TradingPairIdentifier) –
intermediary_pair (TradingPairIdentifier) –
reserve_asset (AssetIdentifier) –
reserve_amount (int) –
max_slippage (float) –
- Return type
- trade(routing_state, target_pair, reserve_asset, reserve_asset_amount, max_slippage=0.01, check_balances=False, intermediary_pair=None)[source]#
- Parameters
routing_state (EthereumRoutingStateBase) –
target_pair (TradingPairIdentifier) –
reserve_asset (AssetIdentifier) –
reserve_asset_amount (int) –
max_slippage (float) – Max slippage per trade. 0.01 is 1%.
check_balances – Check on-chain balances that the account has enough tokens and raise exception if not.
intermediary_pair (Optional[TradingPairIdentifier]) – If the trade needs to be routed through a intermediary pool, e.g. BUSD -> BNB -> Cake.
- Returns
List of prepared transactions to make this trade. These transactions, like approve() may relate to the earlier transactions in the routing_state.
- Return type
- execute_trades_internal(pair_universe, routing_state, trades, check_balances=False)[source]#
Split for testability.
- Parameters
check_balances – Check that the wallet has enough reserves to perform the trades before executing them. Because we are selling before buying. sometimes we do no know this until the sell tx has been completed.
max_slippage – The max slipppage tolerated before the trade fails. 0.01 is 1%.
pair_universe (PandasPairUniverse) –
routing_state (EthereumRoutingStateBase) –
trades (List[TradeExecution]) –
- setup_trades(routing_state, trades, check_balances=False)[source]#
Strategy and live execution connection.
Turns abstract strategy trades to real blockchain transactions.
Modifies TradeExecution objects in place and associates a blockchain transaction for each
Signs tranactions from the hot wallet and broadcasts them to the network
- Parameters
check_balances – Check that the wallet has enough reserves to perform the trades before executing them. Because we are selling before buying. sometimes we do no know this until the sell tx has been completed.
max_slippage – Max slippaeg tolerated per trade. 0.01 is 1%.
routing_state (EthereumRoutingStateBase) –
trades (List[TradeExecution]) –
- create_routing_state(universe, execution_details, StateClass)[source]#
Create a new routing state for this cycle.
Connect routing to web3 and hot wallet
Read on-chain data on what gas fee we are going to use
Setup transaction builder based on this information
- Parameters
universe (StrategyExecutionUniverse) –
execution_details (dict) –
StateClass (Type[EthereumRoutingStateBase]) –
- Return type
- route_pair(pair_universe, trading_pair)[source]#
Return Uniswap routing information (path components) for a trading pair.
For three-way pairs, figure out the intermedia step.
- Returns
(router address, target pair, intermediate pair) tuple
- Parameters
pair_universe (PandasPairUniverse) –
trading_pair (TradingPairIdentifier) –
- Return type
Tuple[TradingPairIdentifier, Optional[TradingPairIdentifier]]
- route_trade(pair_universe, trade)[source]#
Figure out how to map an abstract trade to smart contracts.
Decide if we can do a direct trade in the pair pool. or if we need to hop through another pool to buy the token we want to buy.
- Returns
target pair, intermediary pair tuple
- Parameters
pair_universe (PandasPairUniverse) –
trade (TradeExecution) –
- Return type
Tuple[TradingPairIdentifier, Optional[TradingPairIdentifier]]
- static convert_address_dict_to_lower(factory_router_map)[source]#
Convert all key addresses to lowercase to avoid mix up with Ethereum address checksums
- Return type
- static pre_trade_assertions(reserve_asset_amount, max_slippage, target_pair, reserve_asset)[source]#
Some basic assertions made at the beginning of the trade() method on child class.
- returns: None.
An error will be raised during method call if assertions aren’t met.
- Parameters
reserve_asset_amount (int) –
max_slippage (float) –
target_pair (TradingPairIdentifier) –
reserve_asset (AssetIdentifier) –
- Return type
None