Compare commits
4 Commits
main
...
fix_sandwi
Author | SHA1 | Date | |
---|---|---|---|
|
458ad7f7f5 | ||
|
27199ac65b | ||
|
deb6853828 | ||
|
c8da3e34ae |
@ -42,32 +42,57 @@ def _get_sandwich_starting_with_swap(
|
|||||||
]:
|
]:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
for other_swap in rest_swaps:
|
for back_swap in rest_swaps:
|
||||||
if other_swap.transaction_hash == front_swap.transaction_hash:
|
if back_swap.transaction_hash == front_swap.transaction_hash:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if other_swap.contract_address == front_swap.contract_address:
|
if back_swap.contract_address == front_swap.contract_address:
|
||||||
if (
|
if (
|
||||||
other_swap.token_in_address == front_swap.token_in_address
|
back_swap.token_in_address == front_swap.token_in_address
|
||||||
and other_swap.token_out_address == front_swap.token_out_address
|
and back_swap.token_out_address == front_swap.token_out_address
|
||||||
and other_swap.from_address != sandwicher_address
|
and back_swap.from_address != sandwicher_address
|
||||||
):
|
):
|
||||||
sandwiched_swaps.append(other_swap)
|
sandwiched_swaps.append(back_swap)
|
||||||
elif (
|
elif (
|
||||||
other_swap.token_out_address == front_swap.token_in_address
|
back_swap.token_out_address == front_swap.token_in_address
|
||||||
and other_swap.token_in_address == front_swap.token_out_address
|
and back_swap.token_in_address == front_swap.token_out_address
|
||||||
and other_swap.from_address == sandwicher_address
|
and back_swap.from_address == sandwicher_address
|
||||||
):
|
):
|
||||||
if len(sandwiched_swaps) > 0:
|
if len(sandwiched_swaps) > 0:
|
||||||
|
profit_amount: float
|
||||||
|
if back_swap.token_in_amount == 0 and back_swap.error is None:
|
||||||
|
raise ValueError("Backrun cannot swap 0 tokens")
|
||||||
|
if back_swap.token_in_amount == front_swap.token_out_amount:
|
||||||
|
profit_amount = (
|
||||||
|
back_swap.token_out_amount - front_swap.token_in_amount
|
||||||
|
)
|
||||||
|
|
||||||
|
if back_swap.token_in_amount > front_swap.token_out_amount:
|
||||||
|
exchange_rate = (
|
||||||
|
front_swap.token_out_amount / back_swap.token_in_amount
|
||||||
|
)
|
||||||
|
profit_amount = (
|
||||||
|
exchange_rate * back_swap.token_out_amount
|
||||||
|
- front_swap.token_in_amount
|
||||||
|
)
|
||||||
|
|
||||||
|
if back_swap.token_in_amount < front_swap.token_out_amount:
|
||||||
|
exchange_rate = (
|
||||||
|
back_swap.token_in_amount / front_swap.token_out_amount
|
||||||
|
)
|
||||||
|
profit_amount = (
|
||||||
|
back_swap.token_out_amount
|
||||||
|
- exchange_rate * front_swap.token_in_amount
|
||||||
|
)
|
||||||
|
|
||||||
return Sandwich(
|
return Sandwich(
|
||||||
block_number=front_swap.block_number,
|
block_number=front_swap.block_number,
|
||||||
sandwicher_address=sandwicher_address,
|
sandwicher_address=sandwicher_address,
|
||||||
frontrun_swap=front_swap,
|
frontrun_swap=front_swap,
|
||||||
backrun_swap=other_swap,
|
backrun_swap=back_swap,
|
||||||
sandwiched_swaps=sandwiched_swaps,
|
sandwiched_swaps=sandwiched_swaps,
|
||||||
profit_token_address=front_swap.token_in_address,
|
profit_token_address=front_swap.token_in_address,
|
||||||
profit_amount=other_swap.token_out_amount
|
profit_amount=profit_amount,
|
||||||
- front_swap.token_in_amount,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
1
tests/blocks/13699765.json
Normal file
1
tests/blocks/13699765.json
Normal file
File diff suppressed because one or more lines are too long
1
tests/blocks/14659109.json
Normal file
1
tests/blocks/14659109.json
Normal file
File diff suppressed because one or more lines are too long
42
tests/test_heavy_sandwiches.py
Normal file
42
tests/test_heavy_sandwiches.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
from mev_inspect.classifiers.trace import TraceClassifier
|
||||||
|
from mev_inspect.sandwiches import get_sandwiches
|
||||||
|
from mev_inspect.schemas.sandwiches import Sandwich
|
||||||
|
from mev_inspect.schemas.swaps import Swap
|
||||||
|
from mev_inspect.swaps import get_swaps
|
||||||
|
from tests.utils import load_test_block
|
||||||
|
|
||||||
|
|
||||||
|
def test_back_heavy_sandwich_profits(trace_classifier: TraceClassifier):
|
||||||
|
block_number = 13699765
|
||||||
|
expected_sandwicher = "0x51399b32cd0186bb32230e24167489f3b2f47870"
|
||||||
|
expected_token_address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
|
||||||
|
expected_profit_amount = -435805264121298944
|
||||||
|
|
||||||
|
block = load_test_block(block_number)
|
||||||
|
classified_traces = trace_classifier.classify(block.traces)
|
||||||
|
swaps: List[Swap] = get_swaps(classified_traces)
|
||||||
|
result: List[Sandwich] = get_sandwiches(swaps)
|
||||||
|
|
||||||
|
for observed_sandwich in result:
|
||||||
|
if observed_sandwich.sandwicher_address == expected_sandwicher:
|
||||||
|
assert expected_token_address == observed_sandwich.profit_token_address
|
||||||
|
assert expected_profit_amount == observed_sandwich.profit_amount
|
||||||
|
|
||||||
|
|
||||||
|
def test_front_heavy_sandwich_profits(trace_classifier: TraceClassifier):
|
||||||
|
block_number = 14659109
|
||||||
|
expected_sandwicher = "0x01ff6318440f7d5553a82294d78262d5f5084eff"
|
||||||
|
expected_token_address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
|
||||||
|
expected_profit_amount = -180511102573164864
|
||||||
|
|
||||||
|
block = load_test_block(block_number)
|
||||||
|
classified_traces = trace_classifier.classify(block.traces)
|
||||||
|
swaps: List[Swap] = get_swaps(classified_traces)
|
||||||
|
result: List[Sandwich] = get_sandwiches(swaps)
|
||||||
|
|
||||||
|
for observed_sandwich in result:
|
||||||
|
if observed_sandwich.sandwicher_address == expected_sandwicher:
|
||||||
|
assert expected_token_address == observed_sandwich.profit_token_address
|
||||||
|
assert expected_profit_amount == observed_sandwich.profit_amount
|
Loading…
x
Reference in New Issue
Block a user