Adjust arbitrage path creation to not depend on pool_address
This commit is contained in:
parent
ed63b6bb38
commit
4b6c1a82e6
@ -1,5 +1,6 @@
|
|||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
from typing import List, Optional
|
from typing import List
|
||||||
|
from functools import cmp_to_key
|
||||||
|
|
||||||
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
|
||||||
@ -23,70 +24,70 @@ def get_arbitrages(swaps: List[Swap]) -> List[Arbitrage]:
|
|||||||
|
|
||||||
|
|
||||||
def _get_arbitrages_from_swaps(swaps: List[Swap]) -> List[Arbitrage]:
|
def _get_arbitrages_from_swaps(swaps: List[Swap]) -> List[Arbitrage]:
|
||||||
pool_addresses = {swap.pool_address for swap in swaps}
|
|
||||||
|
|
||||||
all_arbitrages = []
|
all_arbitrages = []
|
||||||
|
|
||||||
for index, first_swap in enumerate(swaps):
|
ordered_swaps = _order_swaps_by_trace_order(swaps)
|
||||||
other_swaps = swaps[:index] + swaps[index + 1 :]
|
|
||||||
|
|
||||||
if first_swap.from_address not in pool_addresses:
|
grouped_swaps: List[List[Swap]] = []
|
||||||
arbitrage = _get_arbitrage_starting_with_swap(first_swap, other_swaps)
|
|
||||||
|
|
||||||
if arbitrage is not None:
|
# An arbitrage is defined as multiple swaps in a row that result in the initial token being returned.
|
||||||
all_arbitrages.append(arbitrage)
|
# Ex: [WETH -> UNI, UNI -> DAI, DAI -> WETH]
|
||||||
|
# Code below assumes there can be multiple arbitrages per swap set, but they'll be serial.
|
||||||
|
# Ex: [WETH -> UNI, UNI -> WETH, DAI -> SUSHI, SUSHI -> DAI]
|
||||||
|
# Non-working ex: [WETH -> UNI, DAI -> SUSHI, UNI -> WETH, SUSHI -> DAI]
|
||||||
|
for start_swap_index in range(len(ordered_swaps)):
|
||||||
|
current_swap = ordered_swaps[start_swap_index]
|
||||||
|
swap_path: List[Swap] = [current_swap]
|
||||||
|
|
||||||
|
for path_index in range(start_swap_index + 1, len(ordered_swaps)):
|
||||||
|
next_swap = ordered_swaps[path_index]
|
||||||
|
if current_swap.token_out_address == next_swap.token_in_address:
|
||||||
|
swap_path.append(next_swap)
|
||||||
|
current_swap = next_swap
|
||||||
|
if (
|
||||||
|
swap_path[0].token_in_address == next_swap.token_out_address
|
||||||
|
): # Cycle complete
|
||||||
|
grouped_swaps.append(swap_path)
|
||||||
|
swap_path = []
|
||||||
|
else:
|
||||||
|
swap_path = []
|
||||||
|
|
||||||
|
for swap_path in grouped_swaps:
|
||||||
|
start_amount = swap_path[0].token_in_amount
|
||||||
|
end_amount = swap_path[-1].token_out_amount
|
||||||
|
profit_amount = end_amount - start_amount
|
||||||
|
|
||||||
|
arb = Arbitrage(
|
||||||
|
swaps=swap_path,
|
||||||
|
block_number=swap_path[0].block_number,
|
||||||
|
transaction_hash=swap_path[0].transaction_hash,
|
||||||
|
account_address=swap_path[0].from_address,
|
||||||
|
profit_token_address=swap_path[0].token_in_address,
|
||||||
|
start_amount=start_amount,
|
||||||
|
end_amount=end_amount,
|
||||||
|
profit_amount=profit_amount,
|
||||||
|
)
|
||||||
|
all_arbitrages.append(arb)
|
||||||
|
|
||||||
return all_arbitrages
|
return all_arbitrages
|
||||||
|
|
||||||
|
|
||||||
def _get_arbitrage_starting_with_swap(
|
def _order_swaps_by_trace_order(unordered_swaps: List[Swap]) -> List[Swap]:
|
||||||
start_swap: Swap,
|
unordered_swaps.sort(key=cmp_to_key(_compare_trace_address))
|
||||||
other_swaps: List[Swap],
|
return unordered_swaps
|
||||||
) -> Optional[Arbitrage]:
|
|
||||||
swap_path = [start_swap]
|
|
||||||
current_swap: Swap = start_swap
|
|
||||||
|
|
||||||
while True:
|
|
||||||
next_swap = _get_swap_from_address(
|
|
||||||
current_swap.to_address,
|
|
||||||
current_swap.token_out_address,
|
|
||||||
other_swaps,
|
|
||||||
)
|
|
||||||
|
|
||||||
if next_swap is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
swap_path.append(next_swap)
|
|
||||||
current_swap = next_swap
|
|
||||||
|
|
||||||
if (
|
|
||||||
current_swap.to_address == start_swap.from_address
|
|
||||||
and current_swap.token_out_address == start_swap.token_in_address
|
|
||||||
):
|
|
||||||
|
|
||||||
start_amount = start_swap.token_in_amount
|
|
||||||
end_amount = current_swap.token_out_amount
|
|
||||||
profit_amount = end_amount - start_amount
|
|
||||||
|
|
||||||
return Arbitrage(
|
|
||||||
swaps=swap_path,
|
|
||||||
block_number=start_swap.block_number,
|
|
||||||
transaction_hash=start_swap.transaction_hash,
|
|
||||||
account_address=start_swap.from_address,
|
|
||||||
profit_token_address=start_swap.token_in_address,
|
|
||||||
start_amount=start_amount,
|
|
||||||
end_amount=end_amount,
|
|
||||||
profit_amount=profit_amount,
|
|
||||||
)
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _get_swap_from_address(
|
def _compare_trace_address(a: Swap, b: Swap):
|
||||||
address: str, token_address: str, swaps: List[Swap]
|
if a.trace_address == b.trace_address:
|
||||||
) -> Optional[Swap]:
|
return 0
|
||||||
for swap in swaps:
|
for i in range(0, max(len(a.trace_address), len(b.trace_address))):
|
||||||
if swap.pool_address == address and swap.token_in_address == token_address:
|
if len(a.trace_address) == i:
|
||||||
return swap
|
return -1
|
||||||
|
if len(b.trace_address) == i:
|
||||||
|
return 1
|
||||||
|
|
||||||
return None
|
if a.trace_address[i] > b.trace_address[i]:
|
||||||
|
return 1
|
||||||
|
if a.trace_address[i] < b.trace_address[i]:
|
||||||
|
return -1
|
||||||
|
return 0
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
from mev_inspect.arbitrages import get_arbitrages
|
import itertools
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from mev_inspect.arbitrages import get_arbitrages, _order_swaps_by_trace_order
|
||||||
from mev_inspect.schemas.swaps import Swap
|
from mev_inspect.schemas.swaps import Swap
|
||||||
from mev_inspect.swaps import (
|
from mev_inspect.swaps import (
|
||||||
UNISWAP_V2_PAIR_ABI_NAME,
|
UNISWAP_V2_PAIR_ABI_NAME,
|
||||||
@ -158,3 +161,50 @@ def test_three_pool_arbitrage(get_transaction_hashes, get_addresses):
|
|||||||
assert arbitrage.start_amount == first_token_in_amount
|
assert arbitrage.start_amount == first_token_in_amount
|
||||||
assert arbitrage.end_amount == first_token_out_amount
|
assert arbitrage.end_amount == first_token_out_amount
|
||||||
assert arbitrage.profit_amount == first_token_out_amount - first_token_in_amount
|
assert arbitrage.profit_amount == first_token_out_amount - first_token_in_amount
|
||||||
|
|
||||||
|
|
||||||
|
def test_order_swaps_by_trace_order():
|
||||||
|
trace_address_0 = []
|
||||||
|
trace_address_1 = [0]
|
||||||
|
trace_address_2 = [1]
|
||||||
|
trace_address_3 = [1, 1]
|
||||||
|
trace_address_4 = [2, 1, 3]
|
||||||
|
trace_address_5 = [2, 2]
|
||||||
|
|
||||||
|
permutated_trace_addresses: List[List[int]] = itertools.permutations(
|
||||||
|
[
|
||||||
|
trace_address_0,
|
||||||
|
trace_address_1,
|
||||||
|
trace_address_2,
|
||||||
|
trace_address_3,
|
||||||
|
trace_address_4,
|
||||||
|
trace_address_5,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
for trace_addresses in permutated_trace_addresses:
|
||||||
|
print("permute")
|
||||||
|
swaps: List[Swap] = []
|
||||||
|
for trace_address in trace_addresses:
|
||||||
|
swap = Swap(
|
||||||
|
abi_name=UNISWAP_V3_POOL_ABI_NAME,
|
||||||
|
transaction_hash="0xfake",
|
||||||
|
block_number=0,
|
||||||
|
trace_address=trace_address,
|
||||||
|
pool_address="0xfake",
|
||||||
|
from_address="0xfake",
|
||||||
|
to_address="0xfake",
|
||||||
|
token_in_address="0xfake",
|
||||||
|
token_in_amount=1,
|
||||||
|
token_out_address="0xfake",
|
||||||
|
token_out_amount=1,
|
||||||
|
)
|
||||||
|
swaps.append(swap)
|
||||||
|
ordered_swaps = _order_swaps_by_trace_order(swaps)
|
||||||
|
|
||||||
|
assert ordered_swaps[0].trace_address == trace_address_0
|
||||||
|
assert ordered_swaps[1].trace_address == trace_address_1
|
||||||
|
assert ordered_swaps[2].trace_address == trace_address_2
|
||||||
|
assert ordered_swaps[3].trace_address == trace_address_3
|
||||||
|
assert ordered_swaps[4].trace_address == trace_address_4
|
||||||
|
assert ordered_swaps[5].trace_address == trace_address_5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user