From 9ce82a36de4f8e067d75830dc93fe84a19ede131 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Thu, 20 Jan 2022 18:57:14 -0500 Subject: [PATCH 1/5] Write protocol for uniswap v2 and v3 swaps. Require protocol for all future swaps. Write protocols to arbitrages --- mev_inspect/abis/{ => uniswap_v2}/UniswapV2Pair.json | 0 mev_inspect/abis/{ => uniswap_v3}/UniswapV3Pool.json | 0 mev_inspect/classifiers/specs/uniswap.py | 2 ++ mev_inspect/crud/arbitrages.py | 1 + mev_inspect/models/arbitrages.py | 3 ++- mev_inspect/schemas/swaps.py | 2 +- 6 files changed, 6 insertions(+), 2 deletions(-) rename mev_inspect/abis/{ => uniswap_v2}/UniswapV2Pair.json (100%) rename mev_inspect/abis/{ => uniswap_v3}/UniswapV3Pool.json (100%) diff --git a/mev_inspect/abis/UniswapV2Pair.json b/mev_inspect/abis/uniswap_v2/UniswapV2Pair.json similarity index 100% rename from mev_inspect/abis/UniswapV2Pair.json rename to mev_inspect/abis/uniswap_v2/UniswapV2Pair.json diff --git a/mev_inspect/abis/UniswapV3Pool.json b/mev_inspect/abis/uniswap_v3/UniswapV3Pool.json similarity index 100% rename from mev_inspect/abis/UniswapV3Pool.json rename to mev_inspect/abis/uniswap_v3/UniswapV3Pool.json diff --git a/mev_inspect/classifiers/specs/uniswap.py b/mev_inspect/classifiers/specs/uniswap.py index 57f7b38..2ead446 100644 --- a/mev_inspect/classifiers/specs/uniswap.py +++ b/mev_inspect/classifiers/specs/uniswap.py @@ -103,6 +103,7 @@ UNISWAP_V3_CONTRACT_SPECS = [ UNISWAP_V3_GENERAL_SPECS = [ ClassifierSpec( abi_name=UNISWAP_V3_POOL_ABI_NAME, + protocol=Protocol.uniswap_v3, classifiers={ "swap(address,bool,int256,uint160,bytes)": UniswapV3SwapClassifier, }, @@ -134,6 +135,7 @@ UNISWAPPY_V2_CONTRACT_SPECS = [ UNISWAPPY_V2_PAIR_SPEC = ClassifierSpec( abi_name=UNISWAP_V2_PAIR_ABI_NAME, + protocol=Protocol.uniswap_v2, classifiers={ "swap(uint256,uint256,address,bytes)": UniswapV2SwapClassifier, }, diff --git a/mev_inspect/crud/arbitrages.py b/mev_inspect/crud/arbitrages.py index 3fbe11d..e0eb180 100644 --- a/mev_inspect/crud/arbitrages.py +++ b/mev_inspect/crud/arbitrages.py @@ -41,6 +41,7 @@ def write_arbitrages( end_amount=arbitrage.end_amount, profit_amount=arbitrage.profit_amount, error=arbitrage.error, + protocols={swap.protocol.value for swap in arbitrage.swaps}, ) ) diff --git a/mev_inspect/models/arbitrages.py b/mev_inspect/models/arbitrages.py index bec5125..34c0434 100644 --- a/mev_inspect/models/arbitrages.py +++ b/mev_inspect/models/arbitrages.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, Numeric, String +from sqlalchemy import ARRAY, Column, Numeric, String from .base import Base @@ -15,3 +15,4 @@ class ArbitrageModel(Base): end_amount = Column(Numeric, nullable=False) profit_amount = Column(Numeric, nullable=False) error = Column(String, nullable=True) + protocols = Column(ARRAY(String)) diff --git a/mev_inspect/schemas/swaps.py b/mev_inspect/schemas/swaps.py index 7caec54..f15bc20 100644 --- a/mev_inspect/schemas/swaps.py +++ b/mev_inspect/schemas/swaps.py @@ -18,5 +18,5 @@ class Swap(BaseModel): token_in_amount: int token_out_address: str token_out_amount: int - protocol: Optional[Protocol] + protocol: Protocol error: Optional[str] From 2e1600b00281aaedbff1f0a708de0f51e33b317c Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Thu, 20 Jan 2022 19:01:27 -0500 Subject: [PATCH 2/5] Add protocol on test swaps --- tests/test_arbitrages.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_arbitrages.py b/tests/test_arbitrages.py index 1a321fa..086f2fc 100644 --- a/tests/test_arbitrages.py +++ b/tests/test_arbitrages.py @@ -6,6 +6,7 @@ from mev_inspect.classifiers.specs.uniswap import ( UNISWAP_V3_POOL_ABI_NAME, ) from mev_inspect.schemas.swaps import Swap +from mev_inspect.schemas.traces import Protocol def test_two_pool_arbitrage(get_transaction_hashes, get_addresses): @@ -35,6 +36,7 @@ def test_two_pool_arbitrage(get_transaction_hashes, get_addresses): block_number=block_number, trace_address=[0], contract_address=first_pool_address, + protocol=Protocol.uniswap_v2, from_address=account_address, to_address=second_pool_address, token_in_address=first_token_address, @@ -48,6 +50,7 @@ def test_two_pool_arbitrage(get_transaction_hashes, get_addresses): transaction_position=transaction_position, block_number=block_number, trace_address=[1], + protocol=Protocol.uniswap_v3, contract_address=second_pool_address, from_address=first_pool_address, to_address=account_address, @@ -62,6 +65,7 @@ def test_two_pool_arbitrage(get_transaction_hashes, get_addresses): abi_name=UNISWAP_V3_POOL_ABI_NAME, transaction_hash=transaction_hash, transaction_position=transaction_position, + protocol=Protocol.uniswap_v3, block_number=block_number, trace_address=[2, 0], contract_address=unrelated_pool_address, @@ -117,6 +121,7 @@ def test_three_pool_arbitrage(get_transaction_hashes, get_addresses): abi_name=UNISWAP_V2_PAIR_ABI_NAME, transaction_hash=transaction_hash, transaction_position=transaction_position, + protocol=Protocol.uniswap_v2, block_number=block_number, trace_address=[0], contract_address=first_pool_address, @@ -131,6 +136,7 @@ def test_three_pool_arbitrage(get_transaction_hashes, get_addresses): abi_name=UNISWAP_V3_POOL_ABI_NAME, transaction_hash=transaction_hash, transaction_position=transaction_position, + protocol=Protocol.uniswap_v3, block_number=block_number, trace_address=[1], contract_address=second_pool_address, @@ -145,6 +151,7 @@ def test_three_pool_arbitrage(get_transaction_hashes, get_addresses): abi_name=UNISWAP_V3_POOL_ABI_NAME, transaction_hash=transaction_hash, transaction_position=transaction_position, + protocol=Protocol.uniswap_v3, block_number=block_number, trace_address=[2], contract_address=third_pool_address, @@ -245,6 +252,7 @@ def create_generic_swap( abi_name=UNISWAP_V3_POOL_ABI_NAME, transaction_hash="0xfake", transaction_position=0, + protocol=Protocol.uniswap_v2, block_number=0, trace_address=trace_address, contract_address="0xfake", From a056919507a9468e7bd7d54b5d7fa1c964abe89b Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Thu, 20 Jan 2022 19:03:00 -0500 Subject: [PATCH 3/5] Fix test swaps --- tests/test_swaps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_swaps.py b/tests/test_swaps.py index d5d8a1b..b1fd658 100644 --- a/tests/test_swaps.py +++ b/tests/test_swaps.py @@ -72,7 +72,7 @@ def test_swaps( from_address=alice_address, contract_address=first_pool_address, abi_name=UNISWAP_V2_PAIR_ABI_NAME, - protocol=None, + protocol=Protocol.uniswap_v2, function_signature="swap(uint256,uint256,address,bytes)", recipient_address=bob_address, recipient_input_key="to", @@ -93,7 +93,7 @@ def test_swaps( from_address=bob_address, contract_address=second_pool_address, abi_name=UNISWAP_V3_POOL_ABI_NAME, - protocol=None, + protocol=Protocol.uniswap_v2, function_signature="swap(address,bool,int256,uint160,bytes)", recipient_address=carl_address, recipient_input_key="recipient", From 3afb854d13ac7f5a79ea5e78bb10d9e16f3ba057 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Thu, 20 Jan 2022 19:03:29 -0500 Subject: [PATCH 4/5] Require protocol to build a swap --- tests/helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index f31c866..0f22290 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,4 +1,4 @@ -from typing import List, Optional +from typing import List from mev_inspect.schemas.traces import ( Classification, @@ -48,7 +48,7 @@ def make_swap_trace( contract_address: str, abi_name: str, function_signature: str, - protocol: Optional[Protocol], + protocol: Protocol, recipient_address: str, recipient_input_key: str, ): From f37de76824cfbca1686032439f37424095a3fc5d Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Thu, 20 Jan 2022 19:09:35 -0500 Subject: [PATCH 5/5] Fix swap and sandwich tests --- tests/sandwiches/12775690.json | 14 +++++++------- tests/test_swaps.py | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/sandwiches/12775690.json b/tests/sandwiches/12775690.json index 337b4b5..04ab514 100644 --- a/tests/sandwiches/12775690.json +++ b/tests/sandwiches/12775690.json @@ -18,7 +18,7 @@ "token_in_amount": 12108789017249529876, "token_out_address": "0x9813037ee2218799597d83d4a5b6f3b6778218d9", "token_out_amount": 1114969767487478978357, - "protocol": null, + "protocol": "uniswap_v2", "error": null }, "backrun_swap": { @@ -37,7 +37,7 @@ "token_in_amount": 1114969767487478978357, "token_out_address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "token_out_amount": 12158780499164852150, - "protocol": null, + "protocol": "uniswap_v2", "error": null }, "sandwiched_swaps": [ @@ -56,7 +56,7 @@ "token_in_amount": 652974555369106606, "token_out_address": "0x9813037ee2218799597d83d4a5b6f3b6778218d9", "token_out_amount": 60000000000000000000, - "protocol": null, + "protocol": "uniswap_v2", "error": null }, { @@ -74,7 +74,7 @@ "token_in_amount": 300000000000000000, "token_out_address": "0x9813037ee2218799597d83d4a5b6f3b6778218d9", "token_out_amount": 27561865602394087181, - "protocol": null, + "protocol": "uniswap_v2", "error": null }, { @@ -92,7 +92,7 @@ "token_in_amount": 125000000000000000, "token_out_address": "0x9813037ee2218799597d83d4a5b6f3b6778218d9", "token_out_amount": 11483313070817976324, - "protocol": null, + "protocol": "uniswap_v2", "error": null }, { @@ -110,11 +110,11 @@ "token_in_amount": 30000000000000000000, "token_out_address": "0x9813037ee2218799597d83d4a5b6f3b6778218d9", "token_out_amount": 2742522049933966038599, - "protocol": null, + "protocol": "uniswap_v2", "error": null } ], "profit_token_address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "profit_amount": 49991481915322274 } -] \ No newline at end of file +] diff --git a/tests/test_swaps.py b/tests/test_swaps.py index b1fd658..03f1682 100644 --- a/tests/test_swaps.py +++ b/tests/test_swaps.py @@ -93,7 +93,7 @@ def test_swaps( from_address=bob_address, contract_address=second_pool_address, abi_name=UNISWAP_V3_POOL_ABI_NAME, - protocol=Protocol.uniswap_v2, + protocol=Protocol.uniswap_v3, function_signature="swap(address,bool,int256,uint160,bytes)", recipient_address=carl_address, recipient_input_key="recipient", @@ -198,7 +198,7 @@ def test_swaps( assert uni_v2_swap.transaction_hash == first_transaction_hash 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.protocol == Protocol.uniswap_v2 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 @@ -211,7 +211,7 @@ def test_swaps( assert uni_v3_swap.transaction_hash == second_transaction_hash 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.protocol == Protocol.uniswap_v3 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