Break into two functions

This commit is contained in:
Luke Van Seters 2021-12-02 14:46:00 -05:00
parent e05da92cd2
commit d4e8046947

View File

@ -1,4 +1,4 @@
from typing import List from typing import List, Optional
from mev_inspect.schemas.sandwiches import Sandwich from mev_inspect.schemas.sandwiches import Sandwich
from mev_inspect.schemas.swaps import Swap from mev_inspect.schemas.swaps import Swap
@ -14,10 +14,22 @@ def get_sandwiches(swaps: List[Swap]) -> List[Sandwich]:
sandwiches: List[Sandwich] = [] sandwiches: List[Sandwich] = []
for index, front_swap in enumerate(ordered_swaps): for index, swap in enumerate(ordered_swaps):
sandwicher_address = front_swap.from_address rest_swaps = ordered_swaps[index + 1 :]
rest_swaps = ordered_swaps[index:] sandwich = _get_sandwich_starting_with_swap(swap, rest_swaps)
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 = [] sandwiched_swaps = []
for other_swap in rest_swaps: for other_swap in rest_swaps:
@ -36,16 +48,12 @@ def get_sandwiches(swaps: List[Swap]) -> List[Sandwich]:
and other_swap.token_in_address == front_swap.token_out_address and other_swap.token_in_address == front_swap.token_out_address
and other_swap.from_address == sandwicher_address and other_swap.from_address == sandwicher_address
): ):
sandwiches.append( return Sandwich(
Sandwich(
block_number=front_swap.block_number, block_number=front_swap.block_number,
sandwicher_address=sandwicher_address, sandwicher_address=sandwicher_address,
frontrun_transaction_hash=front_swap.transaction_hash, frontrun_transaction_hash=front_swap.transaction_hash,
backrun_transaction_hash=other_swap.transaction_hash, backrun_transaction_hash=other_swap.transaction_hash,
sandwiched_swaps=sandwiched_swaps, sandwiched_swaps=sandwiched_swaps,
) )
)
break return None
return sandwiches