UniswapV3Routing#
API documentation for tradeexecutor.ethereum.uniswap_v3.uniswap_v3_routing.UniswapV3Routing Python class in Trading Strategy framework.
- class UniswapV3Routing[source]#
Bases:
EthereumRoutingModel
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__(address_map, allowed_intermediary_pairs, reserve_token_address, chain_id=None)[source]#
- Parameters:
address_map (Dict[str, HexAddress]) – Defines router smart contracts to be used with each DEX. Address map 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__
(address_map, ...[, chain_id])- param address_map:
convert_address_dict_to_lower
(address_dict)Convert all key addresses to lowercase to avoid mix up with Ethereum address checksums
create_routing_state
(universe, execution_details)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_leverage_trade
(routing_state, ...[, ...])Prepare a short 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.
mock_partial_deployment_for_analysis
(web3, ...)"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.
settle_trade
(web3, state, trade, receipts[, ...])Post-trade
setup_trades
(routing_state, trades[, ...])Strategy and live execution connection.
trade
(routing_state, target_pair, ...[, ...])- param routing_state:
- __init__(address_map, allowed_intermediary_pairs, reserve_token_address, chain_id=None)[source]#
- Parameters:
address_map (Dict[str, HexAddress]) – Defines router smart contracts to be used with each DEX. Address map 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.
- create_routing_state(universe, execution_details)[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) –
- Return type:
- perform_preflight_checks_and_logging(pair_universe)[source]#
“Checks the integrity of the routing.
Called from check-wallet to see our routing and balances are good
- Parameters:
pair_universe (PandasPairUniverse) –
- make_direct_trade(routing_state, target_pair, reserve_asset, reserve_amount, max_slippage, check_balances=False, asset_deltas=None, notes='')[source]#
Prepare a trade where target pair has out reserve asset as a quote token.
- Parameters:
max_slippage (float) –
Max slippage tolerance as percent.
E.g. 0.01 for 100 BPS slippage tolerance.
routing_state (EthereumRoutingState) –
target_pair (TradingPairIdentifier) –
reserve_asset (AssetIdentifier) –
reserve_amount (int) –
asset_deltas (Optional[List[AssetDelta]]) –
- Returns:
List of approval transactions (if any needed)
- Return type:
list[tradeexecutor.state.blockhain_transaction.BlockchainTransaction]
- make_multihop_trade(routing_state, target_pair, intermediary_pair, reserve_asset, reserve_amount, max_slippage, check_balances=False, asset_deltas=None, notes='')[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 (EthereumRoutingState) –
target_pair (TradingPairIdentifier) –
intermediary_pair (TradingPairIdentifier) –
reserve_asset (AssetIdentifier) –
reserve_amount (int) –
max_slippage (float) –
asset_deltas (Optional[List[AssetDelta]]) –
- Return type:
list[tradeexecutor.state.blockhain_transaction.BlockchainTransaction]
- settle_trade(web3, state, trade, receipts, stop_on_execution_failure=False)[source]#
Post-trade
Read on-chain data about the execution success and performance
Mark trade succeed or failed
- Parameters:
state (State) – Strategy state
web3 (Web3) –
Web3 connection.
TODO: Breaks abstraction. Figure better way to pass this around later. Maybe create an Ethereum-specific routing parent class?
trade (TradeExecution) – Trade executed in this execution batch
Blockchain receipts we received in this execution batch.
Hash -> receipt mapping.
stop_on_execution_failure –
Raise an error if the trade failed.
Used in unit testing.