Merge pull request #219 from flashbots/require-small-difference-arbs
Require token amounts in arbitrage swaps to be close to each other
This commit is contained in:
commit
e13f895593
@ -4,6 +4,8 @@ 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
|
||||||
|
|
||||||
|
MAX_TOKEN_AMOUNT_PERCENT_DIFFERENCE = 0.01
|
||||||
|
|
||||||
|
|
||||||
def get_arbitrages(swaps: List[Swap]) -> List[Arbitrage]:
|
def get_arbitrages(swaps: List[Swap]) -> List[Arbitrage]:
|
||||||
get_transaction_hash = lambda swap: swap.transaction_hash
|
get_transaction_hash = lambda swap: swap.transaction_hash
|
||||||
@ -101,7 +103,7 @@ def _get_shortest_route(
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
for end_swap in end_swaps:
|
for end_swap in end_swaps:
|
||||||
if start_swap.token_out_address == end_swap.token_in_address:
|
if _swap_outs_match_swap_ins(start_swap, end_swap):
|
||||||
return [start_swap, end_swap]
|
return [start_swap, end_swap]
|
||||||
|
|
||||||
if max_route_length is not None and max_route_length == 2:
|
if max_route_length is not None and max_route_length == 2:
|
||||||
@ -120,11 +122,7 @@ def _get_shortest_route(
|
|||||||
)
|
)
|
||||||
|
|
||||||
for next_swap in other_swaps:
|
for next_swap in other_swaps:
|
||||||
if start_swap.token_out_address == next_swap.token_in_address and (
|
if _swap_outs_match_swap_ins(start_swap, next_swap):
|
||||||
start_swap.contract_address == next_swap.from_address
|
|
||||||
or start_swap.to_address == next_swap.contract_address
|
|
||||||
or start_swap.to_address == next_swap.from_address
|
|
||||||
):
|
|
||||||
shortest_from_next = _get_shortest_route(
|
shortest_from_next = _get_shortest_route(
|
||||||
next_swap,
|
next_swap,
|
||||||
end_swaps,
|
end_swaps,
|
||||||
@ -174,3 +172,19 @@ def _get_all_start_end_swaps(swaps: List[Swap]) -> List[Tuple[Swap, List[Swap]]]
|
|||||||
valid_start_ends.append((potential_start_swap, ends_for_start))
|
valid_start_ends.append((potential_start_swap, ends_for_start))
|
||||||
|
|
||||||
return valid_start_ends
|
return valid_start_ends
|
||||||
|
|
||||||
|
|
||||||
|
def _swap_outs_match_swap_ins(swap_out, swap_in) -> bool:
|
||||||
|
if swap_out.token_out_address == swap_in.token_in_address and (
|
||||||
|
swap_out.contract_address == swap_in.from_address
|
||||||
|
or swap_out.to_address == swap_in.contract_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
|
||||||
|
)
|
||||||
|
|
||||||
|
if amount_percent_difference < MAX_TOKEN_AMOUNT_PERCENT_DIFFERENCE:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
File diff suppressed because one or more lines are too long
1
tests/blocks/12483198.json
Normal file
1
tests/blocks/12483198.json
Normal file
File diff suppressed because one or more lines are too long
@ -60,20 +60,20 @@ def test_arbitrage_real_block(trace_classifier: TraceClassifier):
|
|||||||
|
|
||||||
|
|
||||||
def test_reverting_arbitrage(trace_classifier: TraceClassifier):
|
def test_reverting_arbitrage(trace_classifier: TraceClassifier):
|
||||||
block = load_test_block(11473321)
|
block = load_test_block(12483198)
|
||||||
classified_traces = trace_classifier.classify(block.traces)
|
classified_traces = trace_classifier.classify(block.traces)
|
||||||
|
|
||||||
swaps = get_swaps(classified_traces)
|
swaps = get_swaps(classified_traces)
|
||||||
assert len(swaps) == 38
|
assert len(swaps) == 38
|
||||||
|
|
||||||
arbitrages = get_arbitrages(list(swaps))
|
arbitrages = get_arbitrages(list(swaps))
|
||||||
assert len(arbitrages) == 4
|
assert len(arbitrages) == 5
|
||||||
|
|
||||||
arbitrage_1 = [
|
reverting_arbitrage = [
|
||||||
arb
|
arb
|
||||||
for arb in arbitrages
|
for arb in arbitrages
|
||||||
if arb.transaction_hash
|
if arb.transaction_hash
|
||||||
== "0x565146ec57af69208b4a37e3a138ab85c6a6ff358fffb0077824a7378a67c4d6"
|
== "0x23a4dc7044666d3d4cc2d394a8017fc9d6b87018c20390d35266cea1af783e8a"
|
||||||
][0]
|
][0]
|
||||||
|
|
||||||
assert arbitrage_1.error == "Reverted"
|
assert reverting_arbitrage.error == "Reverted"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user