From d4e80469477200571c6c1302980e97403e334e6e Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Thu, 2 Dec 2021 14:46:00 -0500 Subject: [PATCH] Break into two functions --- mev_inspect/sandwiches.py | 74 ++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/mev_inspect/sandwiches.py b/mev_inspect/sandwiches.py index 346d39e..456fc9b 100644 --- a/mev_inspect/sandwiches.py +++ b/mev_inspect/sandwiches.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Optional from mev_inspect.schemas.sandwiches import Sandwich from mev_inspect.schemas.swaps import Swap @@ -14,38 +14,46 @@ def get_sandwiches(swaps: List[Swap]) -> List[Sandwich]: sandwiches: List[Sandwich] = [] - for index, front_swap in enumerate(ordered_swaps): - sandwicher_address = front_swap.from_address - rest_swaps = ordered_swaps[index:] + for index, swap in enumerate(ordered_swaps): + rest_swaps = ordered_swaps[index + 1 :] + sandwich = _get_sandwich_starting_with_swap(swap, rest_swaps) - sandwiched_swaps = [] - - for other_swap in rest_swaps: - if other_swap.transaction_hash == front_swap.transaction_hash: - continue - - if other_swap.contract_address == front_swap.contract_address: - if ( - other_swap.token_in_address == front_swap.token_in_address - and other_swap.token_out_address == front_swap.token_out_address - and other_swap.from_address != sandwicher_address - ): - sandwiched_swaps.append(other_swap) - elif ( - other_swap.token_out_address == front_swap.token_in_address - and other_swap.token_in_address == front_swap.token_out_address - and other_swap.from_address == sandwicher_address - ): - sandwiches.append( - Sandwich( - block_number=front_swap.block_number, - sandwicher_address=sandwicher_address, - frontrun_transaction_hash=front_swap.transaction_hash, - backrun_transaction_hash=other_swap.transaction_hash, - sandwiched_swaps=sandwiched_swaps, - ) - ) - - break + if sandwich is not None: + sandwiches.append(sandwich) return sandwiches + + +def _get_sandwich_starting_with_swap( + front_swap: Swap, + rest_swaps: List[Swap], +) -> Optional[Sandwich]: + + sandwicher_address = front_swap.from_address + sandwiched_swaps = [] + + for other_swap in rest_swaps: + if other_swap.transaction_hash == front_swap.transaction_hash: + continue + + if other_swap.contract_address == front_swap.contract_address: + if ( + other_swap.token_in_address == front_swap.token_in_address + and other_swap.token_out_address == front_swap.token_out_address + and other_swap.from_address != sandwicher_address + ): + sandwiched_swaps.append(other_swap) + elif ( + other_swap.token_out_address == front_swap.token_in_address + and other_swap.token_in_address == front_swap.token_out_address + and other_swap.from_address == sandwicher_address + ): + return Sandwich( + block_number=front_swap.block_number, + sandwicher_address=sandwicher_address, + frontrun_transaction_hash=front_swap.transaction_hash, + backrun_transaction_hash=other_swap.transaction_hash, + sandwiched_swaps=sandwiched_swaps, + ) + + return None