Constrain sandwiches to require the backrun swap to be similar in amount and from the same address as the frontrun

This commit is contained in:
Luke Van Seters 2022-01-10 13:53:10 -05:00
parent a29b12bf0a
commit e388271b55
3 changed files with 30 additions and 15 deletions

View File

@ -3,6 +3,7 @@ from typing import List, Optional, Tuple
from mev_inspect.schemas.arbitrages import Arbitrage from mev_inspect.schemas.arbitrages import Arbitrage
from mev_inspect.schemas.swaps import Swap from mev_inspect.schemas.swaps import Swap
from mev_inspect.utils import equal_within_percent
MAX_TOKEN_AMOUNT_PERCENT_DIFFERENCE = 0.01 MAX_TOKEN_AMOUNT_PERCENT_DIFFERENCE = 0.01
@ -175,16 +176,16 @@ def _get_all_start_end_swaps(swaps: List[Swap]) -> List[Tuple[Swap, List[Swap]]]
def _swap_outs_match_swap_ins(swap_out, swap_in) -> bool: def _swap_outs_match_swap_ins(swap_out, swap_in) -> bool:
if swap_out.token_out_address == swap_in.token_in_address and ( return (
swap_out.token_out_address == swap_in.token_in_address
and (
swap_out.contract_address == swap_in.from_address swap_out.contract_address == swap_in.from_address
or swap_out.to_address == swap_in.contract_address or swap_out.to_address == swap_in.contract_address
or swap_out.to_address == swap_in.from_address or swap_out.to_address == swap_in.from_address
):
amount_percent_difference = abs(
(float(swap_out.token_out_amount) / swap_in.token_in_amount) - 1.0
) )
and equal_within_percent(
if amount_percent_difference < MAX_TOKEN_AMOUNT_PERCENT_DIFFERENCE: swap_out.token_out_amount,
return True swap_in.token_in_amount,
MAX_TOKEN_AMOUNT_PERCENT_DIFFERENCE,
return False )
)

View File

@ -2,6 +2,9 @@ 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
from mev_inspect.utils import equal_within_percent
SANDWICH_IN_OUT_MAX_PERCENT_DIFFERENCE = 0.01
def get_sandwiches(swaps: List[Swap]) -> List[Sandwich]: def get_sandwiches(swaps: List[Swap]) -> List[Sandwich]:
@ -45,10 +48,12 @@ def _get_sandwich_starting_with_swap(
elif ( elif (
other_swap.token_out_address == front_swap.token_in_address other_swap.token_out_address == front_swap.token_in_address
and other_swap.token_in_address == front_swap.token_out_address and other_swap.token_in_address == front_swap.token_out_address
and ( and equal_within_percent(
other_swap.to_address == sandwicher_address other_swap.token_in_amount,
or other_swap.from_address == sandwicher_address front_swap.token_out_amount,
SANDWICH_IN_OUT_MAX_PERCENT_DIFFERENCE,
) )
and other_swap.from_address == sandwicher_address
): ):
if len(sandwiched_swaps) > 0: if len(sandwiched_swaps) > 0:
return Sandwich( return Sandwich(

View File

@ -3,3 +3,12 @@ from hexbytes._utils import hexstr_to_bytes
def hex_to_int(value: str) -> int: def hex_to_int(value: str) -> int:
return int.from_bytes(hexstr_to_bytes(value), byteorder="big") return int.from_bytes(hexstr_to_bytes(value), byteorder="big")
def equal_within_percent(
first_value: int, second_value: int, threshold_percent: float
) -> bool:
difference = abs(
(first_value - second_value) / (0.5 * (first_value + second_value))
)
return difference < threshold_percent