From 8a94eeaf3916a0ce0a6c667b5926101ef8f31250 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Tue, 5 Oct 2021 12:43:43 -0400 Subject: [PATCH 1/9] Add .envrc to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 40ea1a2..83df4b6 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ cache # k8s .helm + +# env +.envrc From d2c397f2123790bc9bc2e2bd7b76cda2a125393d Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 14:53:38 -0400 Subject: [PATCH 2/9] Change classifications => classifiers --- mev_inspect/classifiers/specs/aave.py | 12 +++++++++--- mev_inspect/classifiers/specs/balancer.py | 16 ++++++++++++---- mev_inspect/classifiers/specs/curve.py | 5 ++++- mev_inspect/classifiers/specs/erc20.py | 17 +++++++++++++---- mev_inspect/classifiers/specs/uniswap.py | 17 ++++++++++++----- mev_inspect/classifiers/specs/weth.py | 16 ++++++++++++---- mev_inspect/classifiers/specs/zero_ex.py | 4 +++- mev_inspect/classifiers/trace.py | 7 +++++-- mev_inspect/schemas/classified_traces.py | 9 --------- mev_inspect/schemas/classifiers.py | 16 ++++++++++++++++ 10 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 mev_inspect/schemas/classifiers.py diff --git a/mev_inspect/classifiers/specs/aave.py b/mev_inspect/classifiers/specs/aave.py index c12e785..f8613b1 100644 --- a/mev_inspect/classifiers/specs/aave.py +++ b/mev_inspect/classifiers/specs/aave.py @@ -1,14 +1,20 @@ from mev_inspect.schemas.classified_traces import ( Classification, - ClassifierSpec, Protocol, ) +from mev_inspect.schemas.classifiers import ( + ClassifierSpec, + Classifier, +) + AAVE_SPEC = ClassifierSpec( abi_name="AaveLendingPool", protocol=Protocol.aave, - classifications={ - "liquidationCall(address,address,address,uint256,bool)": Classification.liquidate, + classifiers={ + "liquidationCall(address,address,address,uint256,bool)": Classifier( + classification=Classification.liquidate, + ) }, ) diff --git a/mev_inspect/classifiers/specs/balancer.py b/mev_inspect/classifiers/specs/balancer.py index 835d1bf..d9c01f9 100644 --- a/mev_inspect/classifiers/specs/balancer.py +++ b/mev_inspect/classifiers/specs/balancer.py @@ -1,16 +1,24 @@ from mev_inspect.schemas.classified_traces import ( Classification, - ClassifierSpec, Protocol, ) +from mev_inspect.schemas.classifiers import ( + ClassifierSpec, + Classifier, +) + BALANCER_V1_SPECS = [ ClassifierSpec( abi_name="BPool", protocol=Protocol.balancer_v1, - classifications={ - "swapExactAmountIn(address,uint256,address,uint256,uint256)": Classification.swap, - "swapExactAmountOut(address,uint256,address,uint256,uint256)": Classification.swap, + classifiers={ + "swapExactAmountIn(address,uint256,address,uint256,uint256)": Classifier( + classification=Classification.swap, + ), + "swapExactAmountOut(address,uint256,address,uint256,uint256)": Classifier( + classification=Classification.swap, + ), }, ), ] diff --git a/mev_inspect/classifiers/specs/curve.py b/mev_inspect/classifiers/specs/curve.py index 79610f9..a631b8c 100644 --- a/mev_inspect/classifiers/specs/curve.py +++ b/mev_inspect/classifiers/specs/curve.py @@ -1,8 +1,11 @@ from mev_inspect.schemas.classified_traces import ( - ClassifierSpec, Protocol, ) +from mev_inspect.schemas.classifiers import ( + ClassifierSpec, +) + """ Deployment addresses found here https://curve.readthedocs.io/ref-addresses.html diff --git a/mev_inspect/classifiers/specs/erc20.py b/mev_inspect/classifiers/specs/erc20.py index 02cd3ae..8e1ef4f 100644 --- a/mev_inspect/classifiers/specs/erc20.py +++ b/mev_inspect/classifiers/specs/erc20.py @@ -1,15 +1,24 @@ from mev_inspect.schemas.classified_traces import ( Classification, +) +from mev_inspect.schemas.classifiers import ( ClassifierSpec, + Classifier, ) ERC20_SPEC = ClassifierSpec( abi_name="ERC20", - classifications={ - "transferFrom(address,address,uint256)": Classification.transfer, - "transfer(address,uint256)": Classification.transfer, - "burn(address)": Classification.burn, + classifiers={ + "transferFrom(address,address,uint256)": Classifier( + classification=Classification.transfer, + ), + "transfer(address,uint256)": Classifier( + classification=Classification.transfer, + ), + "burn(address)": Classifier( + classification=Classification.burn, + ), }, ) diff --git a/mev_inspect/classifiers/specs/uniswap.py b/mev_inspect/classifiers/specs/uniswap.py index e6c9f5a..d02e40d 100644 --- a/mev_inspect/classifiers/specs/uniswap.py +++ b/mev_inspect/classifiers/specs/uniswap.py @@ -1,8 +1,11 @@ from mev_inspect.schemas.classified_traces import ( Classification, - ClassifierSpec, Protocol, ) +from mev_inspect.schemas.classifiers import ( + ClassifierSpec, + Classifier, +) UNISWAP_V3_CONTRACT_SPECS = [ @@ -66,8 +69,10 @@ UNISWAP_V3_CONTRACT_SPECS = [ UNISWAP_V3_GENERAL_SPECS = [ ClassifierSpec( abi_name="UniswapV3Pool", - classifications={ - "swap(address,bool,int256,uint160,bytes)": Classification.swap, + classifiers={ + "swap(address,bool,int256,uint160,bytes)": Classifier( + classification=Classification.swap, + ) }, ), ClassifierSpec( @@ -97,8 +102,10 @@ UNISWAPPY_V2_CONTRACT_SPECS = [ UNISWAPPY_V2_PAIR_SPEC = ClassifierSpec( abi_name="UniswapV2Pair", - classifications={ - "swap(uint256,uint256,address,bytes)": Classification.swap, + classifiers={ + "swap(uint256,uint256,address,bytes)": Classifier( + classification=Classification.swap, + ), }, ) diff --git a/mev_inspect/classifiers/specs/weth.py b/mev_inspect/classifiers/specs/weth.py index df85790..f2f68b8 100644 --- a/mev_inspect/classifiers/specs/weth.py +++ b/mev_inspect/classifiers/specs/weth.py @@ -1,16 +1,24 @@ from mev_inspect.schemas.classified_traces import ( Classification, - ClassifierSpec, Protocol, ) +from mev_inspect.schemas.classifiers import ( + ClassifierSpec, + Classifier, +) + WETH_SPEC = ClassifierSpec( abi_name="WETH9", protocol=Protocol.weth, valid_contract_addresses=["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"], - classifications={ - "transferFrom(address,address,uint256)": Classification.transfer, - "transfer(address,uint256)": Classification.transfer, + classifiers={ + "transferFrom(address,address,uint256)": Classifier( + classification=Classification.transfer, + ), + "transfer(address,uint256)": Classifier( + classification=Classification.transfer, + ), }, ) diff --git a/mev_inspect/classifiers/specs/zero_ex.py b/mev_inspect/classifiers/specs/zero_ex.py index 4f47a03..f558b1d 100644 --- a/mev_inspect/classifiers/specs/zero_ex.py +++ b/mev_inspect/classifiers/specs/zero_ex.py @@ -1,7 +1,9 @@ from mev_inspect.schemas.classified_traces import ( - ClassifierSpec, Protocol, ) +from mev_inspect.schemas.classifiers import ( + ClassifierSpec, +) ZEROX_CONTRACT_SPECS = [ diff --git a/mev_inspect/classifiers/trace.py b/mev_inspect/classifiers/trace.py index 9968eb6..841ca29 100644 --- a/mev_inspect/classifiers/trace.py +++ b/mev_inspect/classifiers/trace.py @@ -67,8 +67,11 @@ class TraceClassifier: if call_data is not None: signature = call_data.function_signature - classification = spec.classifications.get( - signature, Classification.unknown + classifier = spec.classifiers.get(signature) + classification = ( + Classification.unknown + if classifier is None + else classifier.classification ) return DecodedCallTrace( diff --git a/mev_inspect/schemas/classified_traces.py b/mev_inspect/schemas/classified_traces.py index d50cb8b..e226edf 100644 --- a/mev_inspect/schemas/classified_traces.py +++ b/mev_inspect/schemas/classified_traces.py @@ -1,8 +1,6 @@ from enum import Enum from typing import Any, Dict, List, Optional -from pydantic import BaseModel - from .blocks import Trace @@ -64,10 +62,3 @@ class DecodedCallTrace(CallTrace): gas_used: Optional[int] function_name: Optional[str] function_signature: Optional[str] - - -class ClassifierSpec(BaseModel): - abi_name: str - protocol: Optional[Protocol] = None - valid_contract_addresses: Optional[List[str]] = None - classifications: Dict[str, Classification] = {} diff --git a/mev_inspect/schemas/classifiers.py b/mev_inspect/schemas/classifiers.py new file mode 100644 index 0000000..006ab58 --- /dev/null +++ b/mev_inspect/schemas/classifiers.py @@ -0,0 +1,16 @@ +from typing import Dict, List, Optional + +from pydantic import BaseModel + +from .classified_traces import Classification, Protocol + + +class Classifier(BaseModel): + classification: Classification + + +class ClassifierSpec(BaseModel): + abi_name: str + protocol: Optional[Protocol] = None + valid_contract_addresses: Optional[List[str]] = None + classifiers: Dict[str, Classifier] = {} From 621a2798c84e75156f8dd624aa883883adfd81a3 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 14:55:00 -0400 Subject: [PATCH 3/9] No burn --- mev_inspect/classifiers/specs/erc20.py | 3 --- mev_inspect/schemas/classified_traces.py | 1 - 2 files changed, 4 deletions(-) diff --git a/mev_inspect/classifiers/specs/erc20.py b/mev_inspect/classifiers/specs/erc20.py index 8e1ef4f..f6c89dc 100644 --- a/mev_inspect/classifiers/specs/erc20.py +++ b/mev_inspect/classifiers/specs/erc20.py @@ -16,9 +16,6 @@ ERC20_SPEC = ClassifierSpec( "transfer(address,uint256)": Classifier( classification=Classification.transfer, ), - "burn(address)": Classifier( - classification=Classification.burn, - ), }, ) diff --git a/mev_inspect/schemas/classified_traces.py b/mev_inspect/schemas/classified_traces.py index e226edf..81e3ec2 100644 --- a/mev_inspect/schemas/classified_traces.py +++ b/mev_inspect/schemas/classified_traces.py @@ -7,7 +7,6 @@ from .blocks import Trace class Classification(Enum): unknown = "unknown" swap = "swap" - burn = "burn" transfer = "transfer" liquidate = "liquidate" From d57a2d021d90a172e05f5fab6c2750d2e92999c9 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 15:12:44 -0400 Subject: [PATCH 4/9] Add specific classifiers for each type --- mev_inspect/classifiers/specs/aave.py | 7 ++----- mev_inspect/classifiers/specs/balancer.py | 11 +++-------- mev_inspect/classifiers/specs/erc20.py | 13 +++---------- mev_inspect/classifiers/specs/uniswap.py | 11 +++-------- mev_inspect/classifiers/specs/weth.py | 11 +++-------- mev_inspect/schemas/classifiers.py | 13 +++++++++++++ 6 files changed, 27 insertions(+), 39 deletions(-) diff --git a/mev_inspect/classifiers/specs/aave.py b/mev_inspect/classifiers/specs/aave.py index f8613b1..7a42ed5 100644 --- a/mev_inspect/classifiers/specs/aave.py +++ b/mev_inspect/classifiers/specs/aave.py @@ -1,10 +1,9 @@ from mev_inspect.schemas.classified_traces import ( - Classification, Protocol, ) from mev_inspect.schemas.classifiers import ( ClassifierSpec, - Classifier, + LiquidationClassifier, ) @@ -12,9 +11,7 @@ AAVE_SPEC = ClassifierSpec( abi_name="AaveLendingPool", protocol=Protocol.aave, classifiers={ - "liquidationCall(address,address,address,uint256,bool)": Classifier( - classification=Classification.liquidate, - ) + "liquidationCall(address,address,address,uint256,bool)": LiquidationClassifier(), }, ) diff --git a/mev_inspect/classifiers/specs/balancer.py b/mev_inspect/classifiers/specs/balancer.py index d9c01f9..a9db73c 100644 --- a/mev_inspect/classifiers/specs/balancer.py +++ b/mev_inspect/classifiers/specs/balancer.py @@ -1,10 +1,9 @@ from mev_inspect.schemas.classified_traces import ( - Classification, Protocol, ) from mev_inspect.schemas.classifiers import ( ClassifierSpec, - Classifier, + SwapClassifier, ) @@ -13,12 +12,8 @@ BALANCER_V1_SPECS = [ abi_name="BPool", protocol=Protocol.balancer_v1, classifiers={ - "swapExactAmountIn(address,uint256,address,uint256,uint256)": Classifier( - classification=Classification.swap, - ), - "swapExactAmountOut(address,uint256,address,uint256,uint256)": Classifier( - classification=Classification.swap, - ), + "swapExactAmountIn(address,uint256,address,uint256,uint256)": SwapClassifier(), + "swapExactAmountOut(address,uint256,address,uint256,uint256)": SwapClassifier(), }, ), ] diff --git a/mev_inspect/classifiers/specs/erc20.py b/mev_inspect/classifiers/specs/erc20.py index f6c89dc..27093d4 100644 --- a/mev_inspect/classifiers/specs/erc20.py +++ b/mev_inspect/classifiers/specs/erc20.py @@ -1,21 +1,14 @@ -from mev_inspect.schemas.classified_traces import ( - Classification, -) from mev_inspect.schemas.classifiers import ( ClassifierSpec, - Classifier, + TransferClassifier, ) ERC20_SPEC = ClassifierSpec( abi_name="ERC20", classifiers={ - "transferFrom(address,address,uint256)": Classifier( - classification=Classification.transfer, - ), - "transfer(address,uint256)": Classifier( - classification=Classification.transfer, - ), + "transferFrom(address,address,uint256)": TransferClassifier(), + "transfer(address,uint256)": TransferClassifier(), }, ) diff --git a/mev_inspect/classifiers/specs/uniswap.py b/mev_inspect/classifiers/specs/uniswap.py index d02e40d..19b9f16 100644 --- a/mev_inspect/classifiers/specs/uniswap.py +++ b/mev_inspect/classifiers/specs/uniswap.py @@ -1,10 +1,9 @@ from mev_inspect.schemas.classified_traces import ( - Classification, Protocol, ) from mev_inspect.schemas.classifiers import ( ClassifierSpec, - Classifier, + SwapClassifier, ) @@ -70,9 +69,7 @@ UNISWAP_V3_GENERAL_SPECS = [ ClassifierSpec( abi_name="UniswapV3Pool", classifiers={ - "swap(address,bool,int256,uint160,bytes)": Classifier( - classification=Classification.swap, - ) + "swap(address,bool,int256,uint160,bytes)": SwapClassifier(), }, ), ClassifierSpec( @@ -103,9 +100,7 @@ UNISWAPPY_V2_CONTRACT_SPECS = [ UNISWAPPY_V2_PAIR_SPEC = ClassifierSpec( abi_name="UniswapV2Pair", classifiers={ - "swap(uint256,uint256,address,bytes)": Classifier( - classification=Classification.swap, - ), + "swap(uint256,uint256,address,bytes)": SwapClassifier(), }, ) diff --git a/mev_inspect/classifiers/specs/weth.py b/mev_inspect/classifiers/specs/weth.py index f2f68b8..65589d8 100644 --- a/mev_inspect/classifiers/specs/weth.py +++ b/mev_inspect/classifiers/specs/weth.py @@ -1,10 +1,9 @@ from mev_inspect.schemas.classified_traces import ( - Classification, Protocol, ) from mev_inspect.schemas.classifiers import ( ClassifierSpec, - Classifier, + TransferClassifier, ) @@ -13,12 +12,8 @@ WETH_SPEC = ClassifierSpec( protocol=Protocol.weth, valid_contract_addresses=["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"], classifiers={ - "transferFrom(address,address,uint256)": Classifier( - classification=Classification.transfer, - ), - "transfer(address,uint256)": Classifier( - classification=Classification.transfer, - ), + "transferFrom(address,address,uint256)": TransferClassifier(), + "transfer(address,uint256)": TransferClassifier(), }, ) diff --git a/mev_inspect/schemas/classifiers.py b/mev_inspect/schemas/classifiers.py index 006ab58..16741f1 100644 --- a/mev_inspect/schemas/classifiers.py +++ b/mev_inspect/schemas/classifiers.py @@ -1,4 +1,5 @@ from typing import Dict, List, Optional +from typing_extensions import Literal from pydantic import BaseModel @@ -9,6 +10,18 @@ class Classifier(BaseModel): classification: Classification +class TransferClassifier(Classifier): + classification: Literal[Classification.transfer] = Classification.transfer + + +class SwapClassifier(Classifier): + classification: Literal[Classification.swap] = Classification.swap + + +class LiquidationClassifier(Classifier): + classification: Literal[Classification.liquidate] = Classification.liquidate + + class ClassifierSpec(BaseModel): abi_name: str protocol: Optional[Protocol] = None From 86ee26dd1ae5ea4b715bd40c4e5979261a5178b2 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 15:14:24 -0400 Subject: [PATCH 5/9] Make Classifier a union --- mev_inspect/schemas/classifiers.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/mev_inspect/schemas/classifiers.py b/mev_inspect/schemas/classifiers.py index 16741f1..3823603 100644 --- a/mev_inspect/schemas/classifiers.py +++ b/mev_inspect/schemas/classifiers.py @@ -1,4 +1,4 @@ -from typing import Dict, List, Optional +from typing import Dict, List, Optional, Union from typing_extensions import Literal from pydantic import BaseModel @@ -6,22 +6,21 @@ from pydantic import BaseModel from .classified_traces import Classification, Protocol -class Classifier(BaseModel): - classification: Classification - - -class TransferClassifier(Classifier): +class TransferClassifier(BaseModel): classification: Literal[Classification.transfer] = Classification.transfer -class SwapClassifier(Classifier): +class SwapClassifier(BaseModel): classification: Literal[Classification.swap] = Classification.swap -class LiquidationClassifier(Classifier): +class LiquidationClassifier(BaseModel): classification: Literal[Classification.liquidate] = Classification.liquidate +Classifier = Union[TransferClassifier, SwapClassifier, LiquidationClassifier] + + class ClassifierSpec(BaseModel): abi_name: str protocol: Optional[Protocol] = None From 02c9c1cddcee73e9ed4cac4a4cd78985a2cc49b9 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 15:28:50 -0400 Subject: [PATCH 6/9] Add transfer parsing to transfer classifiers --- mev_inspect/classifiers/specs/erc20.py | 21 +++++++++++++++++++-- mev_inspect/classifiers/specs/weth.py | 20 ++++++++++++++++++-- mev_inspect/schemas/classifiers.py | 6 ++++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/mev_inspect/classifiers/specs/erc20.py b/mev_inspect/classifiers/specs/erc20.py index 27093d4..ae9b8e9 100644 --- a/mev_inspect/classifiers/specs/erc20.py +++ b/mev_inspect/classifiers/specs/erc20.py @@ -1,14 +1,31 @@ +from mev_inspect.schemas.classified_traces import DecodedCallTrace from mev_inspect.schemas.classifiers import ( ClassifierSpec, TransferClassifier, ) +from mev_inspect.schemas.transfers import ERC20Transfer + + +def get_erc20_transfer(trace: DecodedCallTrace) -> ERC20Transfer: + return ERC20Transfer( + block_number=trace.block_number, + transaction_hash=trace.transaction_hash, + trace_address=trace.trace_address, + amount=trace.inputs["amount"], + to_address=trace.inputs["recipient"], + from_address=trace.inputs.get("sender", trace.from_address), + token_address=trace.to_address, + ) + + +ERC20_TRANSFER_CLASSIFIER = TransferClassifier(get_transfer=get_erc20_transfer) ERC20_SPEC = ClassifierSpec( abi_name="ERC20", classifiers={ - "transferFrom(address,address,uint256)": TransferClassifier(), - "transfer(address,uint256)": TransferClassifier(), + "transferFrom(address,address,uint256)": ERC20_TRANSFER_CLASSIFIER, + "transfer(address,uint256)": ERC20_TRANSFER_CLASSIFIER, }, ) diff --git a/mev_inspect/classifiers/specs/weth.py b/mev_inspect/classifiers/specs/weth.py index 65589d8..76bd893 100644 --- a/mev_inspect/classifiers/specs/weth.py +++ b/mev_inspect/classifiers/specs/weth.py @@ -3,17 +3,33 @@ from mev_inspect.schemas.classified_traces import ( ) from mev_inspect.schemas.classifiers import ( ClassifierSpec, + DecodedCallTrace, TransferClassifier, ) +from mev_inspect.schemas.transfers import ERC20Transfer +def get_weth_transfer(trace: DecodedCallTrace) -> ERC20Transfer: + return ERC20Transfer( + block_number=trace.block_number, + transaction_hash=trace.transaction_hash, + trace_address=trace.trace_address, + amount=trace.inputs["wad"], + to_address=trace.inputs["dst"], + from_address=trace.from_address, + token_address=trace.to_address, + ) + + +WETH_TRANSFER_CLASSIFIER = TransferClassifier(get_transfer=get_weth_transfer) + WETH_SPEC = ClassifierSpec( abi_name="WETH9", protocol=Protocol.weth, valid_contract_addresses=["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"], classifiers={ - "transferFrom(address,address,uint256)": TransferClassifier(), - "transfer(address,uint256)": TransferClassifier(), + "transferFrom(address,address,uint256)": WETH_TRANSFER_CLASSIFIER, + "transfer(address,uint256)": WETH_TRANSFER_CLASSIFIER, }, ) diff --git a/mev_inspect/schemas/classifiers.py b/mev_inspect/schemas/classifiers.py index 3823603..e3e7a34 100644 --- a/mev_inspect/schemas/classifiers.py +++ b/mev_inspect/schemas/classifiers.py @@ -1,13 +1,15 @@ -from typing import Dict, List, Optional, Union +from typing import Callable, Dict, List, Optional, Union from typing_extensions import Literal from pydantic import BaseModel -from .classified_traces import Classification, Protocol +from .classified_traces import Classification, DecodedCallTrace, Protocol +from .transfers import Transfer class TransferClassifier(BaseModel): classification: Literal[Classification.transfer] = Classification.transfer + get_transfer: Callable[[DecodedCallTrace], Transfer] class SwapClassifier(BaseModel): From f1379cc0a0c0b8ccfa080133edcbee6548d0b00e Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 15:56:28 -0400 Subject: [PATCH 7/9] Switch to class instead of instance --- mev_inspect/classifiers/specs/aave.py | 2 +- mev_inspect/classifiers/specs/balancer.py | 4 +-- mev_inspect/classifiers/specs/erc20.py | 29 +++++++++-------- mev_inspect/classifiers/specs/uniswap.py | 4 +-- mev_inspect/classifiers/specs/weth.py | 28 ++++++++--------- mev_inspect/classifiers/trace.py | 2 +- mev_inspect/inspect_block.py | 2 ++ mev_inspect/schemas/classified_traces.py | 4 +-- mev_inspect/schemas/classifiers.py | 38 ++++++++++++++++------- mev_inspect/transfers.py | 27 +++++++++++++--- 10 files changed, 87 insertions(+), 53 deletions(-) diff --git a/mev_inspect/classifiers/specs/aave.py b/mev_inspect/classifiers/specs/aave.py index 7a42ed5..4def777 100644 --- a/mev_inspect/classifiers/specs/aave.py +++ b/mev_inspect/classifiers/specs/aave.py @@ -11,7 +11,7 @@ AAVE_SPEC = ClassifierSpec( abi_name="AaveLendingPool", protocol=Protocol.aave, classifiers={ - "liquidationCall(address,address,address,uint256,bool)": LiquidationClassifier(), + "liquidationCall(address,address,address,uint256,bool)": LiquidationClassifier, }, ) diff --git a/mev_inspect/classifiers/specs/balancer.py b/mev_inspect/classifiers/specs/balancer.py index a9db73c..9b05dfd 100644 --- a/mev_inspect/classifiers/specs/balancer.py +++ b/mev_inspect/classifiers/specs/balancer.py @@ -12,8 +12,8 @@ BALANCER_V1_SPECS = [ abi_name="BPool", protocol=Protocol.balancer_v1, classifiers={ - "swapExactAmountIn(address,uint256,address,uint256,uint256)": SwapClassifier(), - "swapExactAmountOut(address,uint256,address,uint256,uint256)": SwapClassifier(), + "swapExactAmountIn(address,uint256,address,uint256,uint256)": SwapClassifier, + "swapExactAmountOut(address,uint256,address,uint256,uint256)": SwapClassifier, }, ), ] diff --git a/mev_inspect/classifiers/specs/erc20.py b/mev_inspect/classifiers/specs/erc20.py index ae9b8e9..aee3179 100644 --- a/mev_inspect/classifiers/specs/erc20.py +++ b/mev_inspect/classifiers/specs/erc20.py @@ -6,26 +6,25 @@ from mev_inspect.schemas.classifiers import ( from mev_inspect.schemas.transfers import ERC20Transfer -def get_erc20_transfer(trace: DecodedCallTrace) -> ERC20Transfer: - return ERC20Transfer( - block_number=trace.block_number, - transaction_hash=trace.transaction_hash, - trace_address=trace.trace_address, - amount=trace.inputs["amount"], - to_address=trace.inputs["recipient"], - from_address=trace.inputs.get("sender", trace.from_address), - token_address=trace.to_address, - ) - - -ERC20_TRANSFER_CLASSIFIER = TransferClassifier(get_transfer=get_erc20_transfer) +class ERC20TransferClassifier(TransferClassifier): + @staticmethod + def get_transfer(trace: DecodedCallTrace) -> ERC20Transfer: + return ERC20Transfer( + block_number=trace.block_number, + transaction_hash=trace.transaction_hash, + trace_address=trace.trace_address, + amount=trace.inputs["amount"], + to_address=trace.inputs["recipient"], + from_address=trace.inputs.get("sender", trace.from_address), + token_address=trace.to_address, + ) ERC20_SPEC = ClassifierSpec( abi_name="ERC20", classifiers={ - "transferFrom(address,address,uint256)": ERC20_TRANSFER_CLASSIFIER, - "transfer(address,uint256)": ERC20_TRANSFER_CLASSIFIER, + "transferFrom(address,address,uint256)": ERC20TransferClassifier, + "transfer(address,uint256)": ERC20TransferClassifier, }, ) diff --git a/mev_inspect/classifiers/specs/uniswap.py b/mev_inspect/classifiers/specs/uniswap.py index 19b9f16..ad6d146 100644 --- a/mev_inspect/classifiers/specs/uniswap.py +++ b/mev_inspect/classifiers/specs/uniswap.py @@ -69,7 +69,7 @@ UNISWAP_V3_GENERAL_SPECS = [ ClassifierSpec( abi_name="UniswapV3Pool", classifiers={ - "swap(address,bool,int256,uint160,bytes)": SwapClassifier(), + "swap(address,bool,int256,uint160,bytes)": SwapClassifier, }, ), ClassifierSpec( @@ -100,7 +100,7 @@ UNISWAPPY_V2_CONTRACT_SPECS = [ UNISWAPPY_V2_PAIR_SPEC = ClassifierSpec( abi_name="UniswapV2Pair", classifiers={ - "swap(uint256,uint256,address,bytes)": SwapClassifier(), + "swap(uint256,uint256,address,bytes)": SwapClassifier, }, ) diff --git a/mev_inspect/classifiers/specs/weth.py b/mev_inspect/classifiers/specs/weth.py index 76bd893..e84f2f3 100644 --- a/mev_inspect/classifiers/specs/weth.py +++ b/mev_inspect/classifiers/specs/weth.py @@ -9,27 +9,27 @@ from mev_inspect.schemas.classifiers import ( from mev_inspect.schemas.transfers import ERC20Transfer -def get_weth_transfer(trace: DecodedCallTrace) -> ERC20Transfer: - return ERC20Transfer( - block_number=trace.block_number, - transaction_hash=trace.transaction_hash, - trace_address=trace.trace_address, - amount=trace.inputs["wad"], - to_address=trace.inputs["dst"], - from_address=trace.from_address, - token_address=trace.to_address, - ) +class WethTransferClassifier(TransferClassifier): + @staticmethod + def get_transfer(trace: DecodedCallTrace) -> ERC20Transfer: + return ERC20Transfer( + block_number=trace.block_number, + transaction_hash=trace.transaction_hash, + trace_address=trace.trace_address, + amount=trace.inputs["wad"], + to_address=trace.inputs["dst"], + from_address=trace.from_address, + token_address=trace.to_address, + ) -WETH_TRANSFER_CLASSIFIER = TransferClassifier(get_transfer=get_weth_transfer) - WETH_SPEC = ClassifierSpec( abi_name="WETH9", protocol=Protocol.weth, valid_contract_addresses=["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"], classifiers={ - "transferFrom(address,address,uint256)": WETH_TRANSFER_CLASSIFIER, - "transfer(address,uint256)": WETH_TRANSFER_CLASSIFIER, + "transferFrom(address,address,uint256)": WethTransferClassifier, + "transfer(address,uint256)": WethTransferClassifier, }, ) diff --git a/mev_inspect/classifiers/trace.py b/mev_inspect/classifiers/trace.py index 841ca29..ba7f140 100644 --- a/mev_inspect/classifiers/trace.py +++ b/mev_inspect/classifiers/trace.py @@ -71,7 +71,7 @@ class TraceClassifier: classification = ( Classification.unknown if classifier is None - else classifier.classification + else classifier.get_classification() ) return DecodedCallTrace( diff --git a/mev_inspect/inspect_block.py b/mev_inspect/inspect_block.py index 597e2db..e9af60b 100644 --- a/mev_inspect/inspect_block.py +++ b/mev_inspect/inspect_block.py @@ -64,6 +64,8 @@ def inspect_block( write_classified_traces(db_session, classified_traces) transfers = get_transfers(classified_traces) + logger.info(f"Found {len(transfers)} transfers") + if should_write_transfers: delete_transfers_for_block(db_session, block_number) write_transfers(db_session, transfers) diff --git a/mev_inspect/schemas/classified_traces.py b/mev_inspect/schemas/classified_traces.py index 81e3ec2..87336b6 100644 --- a/mev_inspect/schemas/classified_traces.py +++ b/mev_inspect/schemas/classified_traces.py @@ -59,5 +59,5 @@ class DecodedCallTrace(CallTrace): protocol: Optional[Protocol] gas: Optional[int] gas_used: Optional[int] - function_name: Optional[str] - function_signature: Optional[str] + function_name: str + function_signature: str diff --git a/mev_inspect/schemas/classifiers.py b/mev_inspect/schemas/classifiers.py index e3e7a34..ab0b887 100644 --- a/mev_inspect/schemas/classifiers.py +++ b/mev_inspect/schemas/classifiers.py @@ -1,30 +1,44 @@ -from typing import Callable, Dict, List, Optional, Union -from typing_extensions import Literal +from abc import ABC, abstractmethod +from typing import Dict, List, Optional, Type from pydantic import BaseModel from .classified_traces import Classification, DecodedCallTrace, Protocol -from .transfers import Transfer +from .transfers import ERC20Transfer -class TransferClassifier(BaseModel): - classification: Literal[Classification.transfer] = Classification.transfer - get_transfer: Callable[[DecodedCallTrace], Transfer] +class Classifier(ABC): + @staticmethod + @abstractmethod + def get_classification() -> Classification: + pass -class SwapClassifier(BaseModel): - classification: Literal[Classification.swap] = Classification.swap +class TransferClassifier(Classifier): + @staticmethod + def get_classification() -> Classification: + return Classification.transfer + + @staticmethod + @abstractmethod + def get_transfer(trace: DecodedCallTrace) -> ERC20Transfer: + pass -class LiquidationClassifier(BaseModel): - classification: Literal[Classification.liquidate] = Classification.liquidate +class SwapClassifier(Classifier): + @staticmethod + def get_classification() -> Classification: + return Classification.swap -Classifier = Union[TransferClassifier, SwapClassifier, LiquidationClassifier] +class LiquidationClassifier(Classifier): + @staticmethod + def get_classification() -> Classification: + return Classification.liquidate class ClassifierSpec(BaseModel): abi_name: str protocol: Optional[Protocol] = None valid_contract_addresses: Optional[List[str]] = None - classifiers: Dict[str, Classifier] = {} + classifiers: Dict[str, Type[Classifier]] = {} diff --git a/mev_inspect/transfers.py b/mev_inspect/transfers.py index fbe3a48..90959cd 100644 --- a/mev_inspect/transfers.py +++ b/mev_inspect/transfers.py @@ -1,6 +1,13 @@ -from typing import Dict, List, Optional, Sequence +from typing import Dict, List, Optional, Sequence, Tuple -from mev_inspect.schemas.classified_traces import Classification, ClassifiedTrace +from mev_inspect.classifiers.specs import ALL_CLASSIFIER_SPECS +from mev_inspect.schemas.classifiers import ClassifierSpec, TransferClassifier +from mev_inspect.schemas.classified_traces import ( + Classification, + ClassifiedTrace, + DecodedCallTrace, + Protocol, +) from mev_inspect.schemas.transfers import ERC20Transfer, EthTransfer, TransferGeneric from mev_inspect.traces import is_child_trace_address, get_child_traces @@ -18,9 +25,21 @@ def get_eth_transfers(traces: List[ClassifiedTrace]) -> List[EthTransfer]: def get_transfers(traces: List[ClassifiedTrace]) -> List[ERC20Transfer]: transfers = [] + specs_by_abi_name_and_protocol: Dict[ + Tuple[str, Optional[Protocol]], ClassifierSpec + ] = {(spec.abi_name, spec.protocol): spec for spec in ALL_CLASSIFIER_SPECS} + for trace in traces: - if trace.classification == Classification.transfer: - transfers.append(ERC20Transfer.from_trace(trace)) + if not isinstance(trace, DecodedCallTrace): + continue + + abi_name_and_protocol = (trace.abi_name, trace.protocol) + spec = specs_by_abi_name_and_protocol.get(abi_name_and_protocol) + + if spec is not None: + classifier = spec.classifiers.get(trace.function_signature) + if classifier is not None and issubclass(classifier, TransferClassifier): + transfers.append(classifier.get_transfer(trace)) return transfers From 058cbeed9404789216c3b478c5f88ac4273d5dd0 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 16:00:17 -0400 Subject: [PATCH 8/9] Fix tests for decoded call trace --- tests/helpers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/helpers.py b/tests/helpers.py index 540a327..21f3264 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -56,6 +56,8 @@ def make_swap_trace( classification=Classification.swap, from_address=from_address, to_address=pool_address, + function_name="swap", + function_signature="swap()", inputs={recipient_input_key: recipient_address}, abi_name=abi_name, block_hash=str(block_number), From e3eb858ed94daf154340508a8ed61bf18ce6c664 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Wed, 6 Oct 2021 16:43:04 -0400 Subject: [PATCH 9/9] Fail at runtime if not implemented --- mev_inspect/schemas/classifiers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mev_inspect/schemas/classifiers.py b/mev_inspect/schemas/classifiers.py index ab0b887..5cee8d0 100644 --- a/mev_inspect/schemas/classifiers.py +++ b/mev_inspect/schemas/classifiers.py @@ -11,7 +11,7 @@ class Classifier(ABC): @staticmethod @abstractmethod def get_classification() -> Classification: - pass + raise NotImplementedError() class TransferClassifier(Classifier): @@ -22,7 +22,7 @@ class TransferClassifier(Classifier): @staticmethod @abstractmethod def get_transfer(trace: DecodedCallTrace) -> ERC20Transfer: - pass + raise NotImplementedError() class SwapClassifier(Classifier):