From 4e9ff10988a419042ae1109cdeb4218fe35e4ff6 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Mon, 3 Jan 2022 15:59:56 -0500 Subject: [PATCH 1/2] Cut out early from arbitrages if we've already found a shorter path --- mev_inspect/arbitrages.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mev_inspect/arbitrages.py b/mev_inspect/arbitrages.py index 893c159..8b3a72f 100644 --- a/mev_inspect/arbitrages.py +++ b/mev_inspect/arbitrages.py @@ -92,11 +92,21 @@ def _get_shortest_route( start_swap: Swap, end_swaps: List[Swap], all_swaps: List[Swap], + shortest_route_length: Optional[int] = None, ) -> Optional[List[Swap]]: + if len(end_swaps) == 0: + return None + + if shortest_route_length is not None and shortest_route_length <= 2: + return None + for end_swap in end_swaps: if start_swap.token_out_address == end_swap.token_in_address: return [start_swap, end_swap] + if shortest_route_length is not None and shortest_route_length == 3: + return None + other_swaps = [ swap for swap in all_swaps if (swap is not start_swap and swap not in end_swaps) ] @@ -105,6 +115,9 @@ def _get_shortest_route( return None shortest_remaining_route = None + shortest_remaining_route_length = ( + None if shortest_route_length is None else shortest_route_length - 1 + ) for next_swap in other_swaps: if start_swap.token_out_address == next_swap.token_in_address and ( @@ -116,6 +129,7 @@ def _get_shortest_route( next_swap, end_swaps, other_swaps, + shortest_route_length=shortest_remaining_route_length, ) if shortest_from_next is not None and ( @@ -123,6 +137,7 @@ def _get_shortest_route( or len(shortest_from_next) < len(shortest_remaining_route) ): shortest_remaining_route = shortest_from_next + shortest_remaining_route_length = len(shortest_from_next) if shortest_remaining_route is None: return None From bb94eba02a6c138bdfb871d06aeb00373d1d82f5 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Mon, 3 Jan 2022 16:09:34 -0500 Subject: [PATCH 2/2] Change to max_route_length to make the logic clearer --- mev_inspect/arbitrages.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mev_inspect/arbitrages.py b/mev_inspect/arbitrages.py index 8b3a72f..fe3491b 100644 --- a/mev_inspect/arbitrages.py +++ b/mev_inspect/arbitrages.py @@ -92,19 +92,19 @@ def _get_shortest_route( start_swap: Swap, end_swaps: List[Swap], all_swaps: List[Swap], - shortest_route_length: Optional[int] = None, + max_route_length: Optional[int] = None, ) -> Optional[List[Swap]]: if len(end_swaps) == 0: return None - if shortest_route_length is not None and shortest_route_length <= 2: + if max_route_length is not None and max_route_length < 2: return None for end_swap in end_swaps: if start_swap.token_out_address == end_swap.token_in_address: return [start_swap, end_swap] - if shortest_route_length is not None and shortest_route_length == 3: + if max_route_length is not None and max_route_length == 2: return None other_swaps = [ @@ -115,8 +115,8 @@ def _get_shortest_route( return None shortest_remaining_route = None - shortest_remaining_route_length = ( - None if shortest_route_length is None else shortest_route_length - 1 + max_remaining_route_length = ( + None if max_route_length is None else max_route_length - 1 ) for next_swap in other_swaps: @@ -129,7 +129,7 @@ def _get_shortest_route( next_swap, end_swaps, other_swaps, - shortest_route_length=shortest_remaining_route_length, + max_route_length=max_remaining_route_length, ) if shortest_from_next is not None and ( @@ -137,7 +137,7 @@ def _get_shortest_route( or len(shortest_from_next) < len(shortest_remaining_route) ): shortest_remaining_route = shortest_from_next - shortest_remaining_route_length = len(shortest_from_next) + max_remaining_route_length = len(shortest_from_next) - 1 if shortest_remaining_route is None: return None