Merge remote-tracking branch 'upstream/main' into asyncpg

This commit is contained in:
carlomazzaferro 2021-11-25 14:22:18 +01:00
commit 40e4e2e111
No known key found for this signature in database
GPG Key ID: 0CED3103EF7B2187
15 changed files with 203 additions and 91 deletions

View File

@ -0,0 +1,27 @@
"""Rename pool_address to contract_address
Revision ID: 0cef835f7b36
Revises: 5427d62a2cc0
Create Date: 2021-11-19 15:36:15.152622
"""
from alembic import op
# revision identifiers, used by Alembic.
revision = "0cef835f7b36"
down_revision = "5427d62a2cc0"
branch_labels = None
depends_on = None
def upgrade():
op.alter_column(
"swaps", "pool_address", nullable=False, new_column_name="contract_address"
)
def downgrade():
op.alter_column(
"swaps", "contract_address", nullable=False, new_column_name="pool_address"
)

2
mev
View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
set -e

View File

@ -86,7 +86,7 @@ def _get_all_start_end_swaps(swaps: List[Swap]) -> List[Tuple[Swap, Swap]]:
- not swap[start].from_address in all_pool_addresses
- not swap[end].to_address in all_pool_addresses
"""
pool_addrs = [swap.pool_address for swap in swaps]
pool_addrs = [swap.contract_address for swap in swaps]
valid_start_ends: List[Tuple[Swap, Swap]] = []
for potential_start_swap in swaps:
for potential_end_swap in swaps:
@ -116,8 +116,8 @@ def _get_all_routes(
routes: List[List[Swap]] = []
for potential_next_swap in other_swaps:
if start_swap.token_out_address == potential_next_swap.token_in_address and (
start_swap.pool_address == potential_next_swap.from_address
or start_swap.to_address == potential_next_swap.pool_address
start_swap.contract_address == potential_next_swap.from_address
or start_swap.to_address == potential_next_swap.contract_address
or start_swap.to_address == potential_next_swap.from_address
):
remaining_swaps = [

View File

@ -0,0 +1,86 @@
from typing import Optional, List, Sequence
from mev_inspect.schemas.swaps import Swap
from mev_inspect.schemas.transfers import Transfer, ETH_TOKEN_ADDRESS
from mev_inspect.schemas.traces import DecodedCallTrace, ClassifiedTrace
def create_swap_from_transfers(
trace: DecodedCallTrace,
recipient_address: str,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
pool_address = trace.to_address
transfers_to_pool = []
if trace.value is not None and trace.value > 0:
transfers_to_pool = [_build_eth_transfer(trace)]
if len(transfers_to_pool) == 0:
transfers_to_pool = _filter_transfers(prior_transfers, to_address=pool_address)
if len(transfers_to_pool) == 0:
transfers_to_pool = _filter_transfers(child_transfers, to_address=pool_address)
if len(transfers_to_pool) == 0:
return None
transfers_from_pool_to_recipient = _filter_transfers(
child_transfers, to_address=recipient_address, from_address=pool_address
)
if len(transfers_from_pool_to_recipient) != 1:
return None
transfer_in = transfers_to_pool[-1]
transfer_out = transfers_from_pool_to_recipient[0]
return Swap(
abi_name=trace.abi_name,
transaction_hash=trace.transaction_hash,
block_number=trace.block_number,
trace_address=trace.trace_address,
contract_address=pool_address,
protocol=trace.protocol,
from_address=transfer_in.from_address,
to_address=transfer_out.to_address,
token_in_address=transfer_in.token_address,
token_in_amount=transfer_in.amount,
token_out_address=transfer_out.token_address,
token_out_amount=transfer_out.amount,
error=trace.error,
)
def _build_eth_transfer(trace: ClassifiedTrace) -> Transfer:
return Transfer(
block_number=trace.block_number,
transaction_hash=trace.transaction_hash,
trace_address=trace.trace_address,
amount=trace.value,
to_address=trace.to_address,
from_address=trace.from_address,
token_address=ETH_TOKEN_ADDRESS,
)
def _filter_transfers(
transfers: Sequence[Transfer],
to_address: Optional[str] = None,
from_address: Optional[str] = None,
) -> List[Transfer]:
filtered_transfers = []
for transfer in transfers:
if to_address is not None and transfer.to_address != to_address:
continue
if from_address is not None and transfer.from_address != from_address:
continue
filtered_transfers.append(transfer)
return filtered_transfers

View File

@ -1,3 +1,6 @@
from typing import Optional, List
from mev_inspect.schemas.transfers import Transfer
from mev_inspect.schemas.swaps import Swap
from mev_inspect.schemas.traces import (
DecodedCallTrace,
Protocol,
@ -6,15 +9,25 @@ from mev_inspect.schemas.classifiers import (
ClassifierSpec,
SwapClassifier,
)
from mev_inspect.classifiers.helpers import create_swap_from_transfers
BALANCER_V1_POOL_ABI_NAME = "BPool"
class BalancerSwapClassifier(SwapClassifier):
@staticmethod
def get_swap_recipient(trace: DecodedCallTrace) -> str:
return trace.from_address
def parse_swap(
trace: DecodedCallTrace,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
recipient_address = trace.from_address
swap = create_swap_from_transfers(
trace, recipient_address, prior_transfers, child_transfers
)
return swap
BALANCER_V1_SPECS = [

View File

@ -1,18 +1,32 @@
from typing import Optional, List
from mev_inspect.schemas.transfers import Transfer
from mev_inspect.schemas.swaps import Swap
from mev_inspect.schemas.traces import (
Protocol,
DecodedCallTrace,
)
from mev_inspect.schemas.classifiers import (
ClassifierSpec,
DecodedCallTrace,
SwapClassifier,
)
from mev_inspect.classifiers.helpers import create_swap_from_transfers
class CurveSwapClassifier(SwapClassifier):
@staticmethod
def get_swap_recipient(trace: DecodedCallTrace) -> str:
return trace.from_address
def parse_swap(
trace: DecodedCallTrace,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
recipient_address = trace.from_address
swap = create_swap_from_transfers(
trace, recipient_address, prior_transfers, child_transfers
)
return swap
CURVE_BASE_POOLS = [

View File

@ -1,3 +1,6 @@
from typing import Optional, List
from mev_inspect.schemas.transfers import Transfer
from mev_inspect.schemas.swaps import Swap
from mev_inspect.schemas.traces import (
DecodedCallTrace,
Protocol,
@ -6,6 +9,7 @@ from mev_inspect.schemas.classifiers import (
ClassifierSpec,
SwapClassifier,
)
from mev_inspect.classifiers.helpers import create_swap_from_transfers
UNISWAP_V2_PAIR_ABI_NAME = "UniswapV2Pair"
@ -14,20 +18,34 @@ UNISWAP_V3_POOL_ABI_NAME = "UniswapV3Pool"
class UniswapV3SwapClassifier(SwapClassifier):
@staticmethod
def get_swap_recipient(trace: DecodedCallTrace) -> str:
if trace.inputs is not None and "recipient" in trace.inputs:
return trace.inputs["recipient"]
else:
return trace.from_address
def parse_swap(
trace: DecodedCallTrace,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
recipient_address = trace.inputs.get("recipient", trace.from_address)
swap = create_swap_from_transfers(
trace, recipient_address, prior_transfers, child_transfers
)
return swap
class UniswapV2SwapClassifier(SwapClassifier):
@staticmethod
def get_swap_recipient(trace: DecodedCallTrace) -> str:
if trace.inputs is not None and "to" in trace.inputs:
return trace.inputs["to"]
else:
return trace.from_address
def parse_swap(
trace: DecodedCallTrace,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
recipient_address = trace.inputs.get("to", trace.from_address)
swap = create_swap_from_transfers(
trace, recipient_address, prior_transfers, child_transfers
)
return swap
UNISWAP_V3_CONTRACT_SPECS = [
@ -127,7 +145,7 @@ UNISWAPPY_V2_PAIR_SPEC = ClassifierSpec(
},
)
UNISWAP_CLASSIFIER_SPECS = [
UNISWAP_CLASSIFIER_SPECS: List = [
*UNISWAP_V3_CONTRACT_SPECS,
*UNISWAPPY_V2_CONTRACT_SPECS,
*UNISWAP_V3_GENERAL_SPECS,

View File

@ -5,7 +5,6 @@ from mev_inspect.schemas.classifiers import (
ClassifierSpec,
)
ZEROX_CONTRACT_SPECS = [
ClassifierSpec(
abi_name="exchangeProxy",

View File

@ -11,7 +11,7 @@ class SwapModel(Base):
block_number = Column(Numeric, nullable=False)
trace_address = Column(ARRAY(Integer), nullable=False)
protocol = Column(String, nullable=True)
pool_address = Column(String, nullable=False)
contract_address = Column(String, nullable=False)
from_address = Column(String, nullable=False)
to_address = Column(String, nullable=False)
token_in_address = Column(String, nullable=False)

View File

@ -5,6 +5,7 @@ from pydantic import BaseModel
from .traces import Classification, DecodedCallTrace, Protocol
from .transfers import Transfer
from .swaps import Swap
class Classifier(ABC):
@ -32,7 +33,11 @@ class SwapClassifier(Classifier):
@staticmethod
@abstractmethod
def get_swap_recipient(trace: DecodedCallTrace) -> str:
def parse_swap(
trace: DecodedCallTrace,
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
raise NotImplementedError()

View File

@ -10,7 +10,7 @@ class Swap(BaseModel):
transaction_hash: str
block_number: int
trace_address: List[int]
pool_address: str
contract_address: str
from_address: str
to_address: str
token_in_address: str

View File

@ -11,10 +11,8 @@ from mev_inspect.schemas.swaps import Swap
from mev_inspect.schemas.transfers import Transfer
from mev_inspect.traces import get_traces_by_transaction_hash
from mev_inspect.transfers import (
build_eth_transfer,
get_child_transfers,
get_transfer,
filter_transfers,
remove_child_transfers_of_transfers,
)
@ -67,56 +65,8 @@ def _parse_swap(
prior_transfers: List[Transfer],
child_transfers: List[Transfer],
) -> Optional[Swap]:
pool_address = trace.to_address
recipient_address = _get_recipient_address(trace)
if recipient_address is None:
return None
transfers_to_pool = []
if trace.value is not None and trace.value > 0:
transfers_to_pool = [build_eth_transfer(trace)]
if len(transfers_to_pool) == 0:
transfers_to_pool = filter_transfers(prior_transfers, to_address=pool_address)
if len(transfers_to_pool) == 0:
transfers_to_pool = filter_transfers(child_transfers, to_address=pool_address)
if len(transfers_to_pool) == 0:
return None
transfers_from_pool_to_recipient = filter_transfers(
child_transfers, to_address=recipient_address, from_address=pool_address
)
if len(transfers_from_pool_to_recipient) != 1:
return None
transfer_in = transfers_to_pool[-1]
transfer_out = transfers_from_pool_to_recipient[0]
return Swap(
abi_name=trace.abi_name,
transaction_hash=trace.transaction_hash,
block_number=trace.block_number,
trace_address=trace.trace_address,
pool_address=pool_address,
protocol=trace.protocol,
from_address=transfer_in.from_address,
to_address=transfer_out.to_address,
token_in_address=transfer_in.token_address,
token_in_amount=transfer_in.amount,
token_out_address=transfer_out.token_address,
token_out_amount=transfer_out.amount,
error=trace.error,
)
def _get_recipient_address(trace: DecodedCallTrace) -> Optional[str]:
classifier = get_classifier(trace)
if classifier is not None and issubclass(classifier, SwapClassifier):
return classifier.get_swap_recipient(trace)
return classifier.parse_swap(trace, prior_transfers, child_transfers)
return None

View File

@ -44,7 +44,7 @@ def make_swap_trace(
transaction_hash: str,
trace_address: List[int],
from_address: str,
pool_address: str,
contract_address: str,
abi_name: str,
function_signature: str,
protocol: Optional[Protocol],
@ -60,7 +60,7 @@ def make_swap_trace(
subtraces=0,
classification=Classification.swap,
from_address=from_address,
to_address=pool_address,
to_address=contract_address,
function_name="swap",
function_signature=function_signature,
inputs={recipient_input_key: recipient_address},

View File

@ -32,7 +32,7 @@ def test_two_pool_arbitrage(get_transaction_hashes, get_addresses):
transaction_hash=transaction_hash,
block_number=block_number,
trace_address=[0],
pool_address=first_pool_address,
contract_address=first_pool_address,
from_address=account_address,
to_address=second_pool_address,
token_in_address=first_token_address,
@ -45,7 +45,7 @@ def test_two_pool_arbitrage(get_transaction_hashes, get_addresses):
transaction_hash=transaction_hash,
block_number=block_number,
trace_address=[1],
pool_address=second_pool_address,
contract_address=second_pool_address,
from_address=first_pool_address,
to_address=account_address,
token_in_address=second_token_address,
@ -60,7 +60,7 @@ def test_two_pool_arbitrage(get_transaction_hashes, get_addresses):
transaction_hash=transaction_hash,
block_number=block_number,
trace_address=[2, 0],
pool_address=unrelated_pool_address,
contract_address=unrelated_pool_address,
from_address=account_address,
to_address=account_address,
token_in_address=second_token_address,
@ -113,7 +113,7 @@ def test_three_pool_arbitrage(get_transaction_hashes, get_addresses):
transaction_hash=transaction_hash,
block_number=block_number,
trace_address=[0],
pool_address=first_pool_address,
contract_address=first_pool_address,
from_address=account_address,
to_address=second_pool_address,
token_in_address=first_token_address,
@ -126,7 +126,7 @@ def test_three_pool_arbitrage(get_transaction_hashes, get_addresses):
transaction_hash=transaction_hash,
block_number=block_number,
trace_address=[1],
pool_address=second_pool_address,
contract_address=second_pool_address,
from_address=first_pool_address,
to_address=third_pool_address,
token_in_address=second_token_address,
@ -139,7 +139,7 @@ def test_three_pool_arbitrage(get_transaction_hashes, get_addresses):
transaction_hash=transaction_hash,
block_number=block_number,
trace_address=[2],
pool_address=third_pool_address,
contract_address=third_pool_address,
from_address=second_pool_address,
to_address=account_address,
token_in_address=third_token_address,
@ -220,7 +220,7 @@ def create_generic_swap(
transaction_hash="0xfake",
block_number=0,
trace_address=trace_address,
pool_address="0xfake",
contract_address="0xfake",
from_address="0xfake",
to_address="0xfake",
token_in_address=tok_a,

View File

@ -63,7 +63,7 @@ def test_swaps(
first_transaction_hash,
trace_address=[1],
from_address=alice_address,
pool_address=first_pool_address,
contract_address=first_pool_address,
abi_name=UNISWAP_V2_PAIR_ABI_NAME,
protocol=None,
function_signature="swap(uint256,uint256,address,bytes)",
@ -84,7 +84,7 @@ def test_swaps(
second_transaction_hash,
trace_address=[],
from_address=bob_address,
pool_address=second_pool_address,
contract_address=second_pool_address,
abi_name=UNISWAP_V3_POOL_ABI_NAME,
protocol=None,
function_signature="swap(address,bool,int256,uint160,bytes)",
@ -132,7 +132,7 @@ def test_swaps(
third_transaction_hash,
trace_address=[6],
from_address=bob_address,
pool_address=third_pool_address,
contract_address=third_pool_address,
abi_name=BALANCER_V1_POOL_ABI_NAME,
protocol=Protocol.balancer_v1,
function_signature="swapExactAmountIn(address,uint256,address,uint256,uint256)",
@ -160,7 +160,7 @@ def test_swaps(
assert uni_v2_swap.block_number == block_number
assert uni_v2_swap.trace_address == [1]
assert uni_v2_swap.protocol is None
assert uni_v2_swap.pool_address == first_pool_address
assert uni_v2_swap.contract_address == first_pool_address
assert uni_v2_swap.from_address == alice_address
assert uni_v2_swap.to_address == bob_address
assert uni_v2_swap.token_in_address == first_token_in_address
@ -173,7 +173,7 @@ def test_swaps(
assert uni_v3_swap.block_number == block_number
assert uni_v3_swap.trace_address == []
assert uni_v3_swap.protocol is None
assert uni_v3_swap.pool_address == second_pool_address
assert uni_v3_swap.contract_address == second_pool_address
assert uni_v3_swap.from_address == bob_address
assert uni_v3_swap.to_address == carl_address
assert uni_v3_swap.token_in_address == second_token_in_address
@ -186,7 +186,7 @@ def test_swaps(
assert bal_v1_swap.block_number == block_number
assert bal_v1_swap.trace_address == [6]
assert bal_v1_swap.protocol == Protocol.balancer_v1
assert bal_v1_swap.pool_address == third_pool_address
assert bal_v1_swap.contract_address == third_pool_address
assert bal_v1_swap.from_address == bob_address
assert bal_v1_swap.to_address == bob_address
assert bal_v1_swap.token_in_address == third_token_in_address