From 34d0afdd9cc48a47a68abd5d3192c5385eb34f5d Mon Sep 17 00:00:00 2001 From: Taarush Vemulapalli Date: Sat, 16 Oct 2021 12:10:40 -0400 Subject: [PATCH] fixes for WETH/underlying_markets --- mev_inspect/compound_liquidations.py | 60 ++++++++++------------------ mev_inspect/liquidations.py | 13 +++--- tests/test_compound.py | 6 +-- 3 files changed, 33 insertions(+), 46 deletions(-) diff --git a/mev_inspect/compound_liquidations.py b/mev_inspect/compound_liquidations.py index 5acd9b6..201db2e 100644 --- a/mev_inspect/compound_liquidations.py +++ b/mev_inspect/compound_liquidations.py @@ -9,8 +9,8 @@ from mev_inspect.schemas.classified_traces import ( ) from mev_inspect.schemas.liquidations import Liquidation -from mev_inspect.classifiers.specs import WETH_ADDRESS from mev_inspect.abi import get_raw_abi +from mev_inspect.transfers import ETH_TOKEN_ADDRESS V2_COMPTROLLER_ADDRESS = "0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B" V2_C_ETHER = "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5" @@ -18,42 +18,27 @@ CREAM_COMPTROLLER_ADDRESS = "0x3d5BC3c8d13dcB8bF317092d84783c2697AE9258" CREAM_CR_ETHER = "0xD06527D5e56A3495252A528C4987003b712860eE" # helper, only queried once in the beginning (inspect_block) -def fetch_all_comp_markets(w3: Web3) -> Dict[str, str]: - c_token_mapping = {} - comp_v2_comptroller_abi = get_raw_abi("Comptroller", Protocol.compound_v2) - comptroller_instance = w3.eth.contract( - address=V2_COMPTROLLER_ADDRESS, abi=comp_v2_comptroller_abi - ) +def fetch_all_underlying_markets(w3: Web3, protocol: Protocol) -> Dict[str, str]: + if protocol == Protocol.compound_v2: + C_ETHER = V2_C_ETHER + address = V2_COMPTROLLER_ADDRESS + elif protocol == Protocol.cream: + C_ETHER = CREAM_CR_ETHER + address = CREAM_COMPTROLLER_ADDRESS + token_mapping = {} + comptroller_abi = get_raw_abi("Comptroller", Protocol.compound_v2) + comptroller_instance = w3.eth.contract(address=address, abi=comptroller_abi) markets = comptroller_instance.functions.getAllMarkets().call() - comp_v2_ctoken_abi = get_raw_abi("CToken", Protocol.compound_v2) - for c_token in markets: + token_abi = get_raw_abi("CToken", Protocol.compound_v2) + for token in markets: # make an exception for cETH (as it has no .underlying()) - if c_token != V2_C_ETHER: - ctoken_instance = w3.eth.contract(address=c_token, abi=comp_v2_ctoken_abi) - underlying_token = ctoken_instance.functions.underlying().call() - c_token_mapping[ - c_token.lower() + if token != C_ETHER: + token_instance = w3.eth.contract(address=token, abi=token_abi) + underlying_token = token_instance.functions.underlying().call() + token_mapping[ + token.lower() ] = underlying_token.lower() # make k:v lowercase for consistancy - return c_token_mapping - - -def fetch_all_cream_markets(w3: Web3) -> Dict[str, str]: - cr_token_mapping = {} - cream_comptroller_abi = get_raw_abi("Comptroller", Protocol.compound_v2) - comptroller_instance = w3.eth.contract( - address=CREAM_COMPTROLLER_ADDRESS, abi=cream_comptroller_abi - ) - markets = comptroller_instance.functions.getAllMarkets().call() - cream_crtoken_abi = get_raw_abi("CToken", Protocol.compound_v2) - for cr_token in markets: - # make an exception for cETH (as it has no .underlying()) - if cr_token != CREAM_CR_ETHER: - ctoken_instance = w3.eth.contract(address=cr_token, abi=cream_crtoken_abi) - underlying_token = ctoken_instance.functions.underlying().call() - cr_token_mapping[ - cr_token.lower() - ] = underlying_token.lower() # make k:v lowercase for consistancy - return cr_token_mapping + return token_mapping def get_compound_liquidations( @@ -80,24 +65,23 @@ def get_compound_liquidations( trace.transaction_hash, trace.trace_address, traces ) seize_trace = _get_seize_call(child_traces) + underlying_markets = {} if trace.protocol == Protocol.compound_v2: underlying_markets = collateral_by_c_token_address elif trace.protocol == Protocol.cream: underlying_markets = collateral_by_cr_token_address - else: - underlying_markets = {} if ( seize_trace is not None and seize_trace.inputs is not None - and underlying_markets is not {} + and len(underlying_markets) != 0 ): c_token_collateral = trace.inputs["cTokenCollateral"] if trace.abi_name == "CEther": liquidations.append( Liquidation( liquidated_user=trace.inputs["borrower"], - collateral_token_address=WETH_ADDRESS, # WETH since all cEther liquidations provide Ether + collateral_token_address=ETH_TOKEN_ADDRESS, # WETH since all cEther liquidations provide Ether debt_token_address=c_token_collateral, liquidator_user=seize_trace.inputs["liquidator"], debt_purchase_amount=trace.value, diff --git a/mev_inspect/liquidations.py b/mev_inspect/liquidations.py index 6559ce3..91e20b9 100644 --- a/mev_inspect/liquidations.py +++ b/mev_inspect/liquidations.py @@ -4,10 +4,13 @@ from web3 import Web3 from mev_inspect.aave_liquidations import get_aave_liquidations from mev_inspect.compound_liquidations import ( get_compound_liquidations, - fetch_all_comp_markets, - fetch_all_cream_markets, + fetch_all_underlying_markets, +) +from mev_inspect.schemas.classified_traces import ( + ClassifiedTrace, + Classification, + Protocol, ) -from mev_inspect.schemas.classified_traces import ClassifiedTrace, Classification from mev_inspect.schemas.liquidations import Liquidation @@ -23,8 +26,8 @@ def get_liquidations( if has_liquidations: aave_liquidations = get_aave_liquidations(classified_traces) - comp_markets = fetch_all_comp_markets(w3) - cream_markets = fetch_all_cream_markets(w3) + comp_markets = fetch_all_underlying_markets(w3, Protocol.compound_v2) + cream_markets = fetch_all_underlying_markets(w3, Protocol.cream) compound_liquidations = get_compound_liquidations( classified_traces, comp_markets, cream_markets ) diff --git a/tests/test_compound.py b/tests/test_compound.py index ee427cc..44ca054 100644 --- a/tests/test_compound.py +++ b/tests/test_compound.py @@ -18,7 +18,7 @@ def test_c_ether_liquidations(): Liquidation( liquidated_user="0xb5535a3681cf8d5431b8acfd779e2f79677ecce9", liquidator_user="0xe0090ec6895c087a393f0e45f1f85098a6c33bef", - collateral_token_address="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", + collateral_token_address="0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", debt_token_address="0x39aa39c021dfbae8fac545936693ac917d5e7563", debt_purchase_amount=268066492249420078, received_amount=4747650169097, @@ -43,7 +43,7 @@ def test_c_ether_liquidations(): Liquidation( liquidated_user="0x45df6f00166c3fb77dc16b9e47ff57bc6694e898", liquidator_user="0xe0090ec6895c087a393f0e45f1f85098a6c33bef", - collateral_token_address="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", + collateral_token_address="0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", debt_token_address="0x35a18000230da775cac24873d00ff85bccded550", debt_purchase_amount=414547860568297082, received_amount=321973320649, @@ -69,7 +69,7 @@ def test_c_ether_liquidations(): Liquidation( liquidated_user="0xacbcf5d2970eef25f02a27e9d9cd31027b058b9b", liquidator_user="0xe0090ec6895c087a393f0e45f1f85098a6c33bef", - collateral_token_address="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", + collateral_token_address="0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", debt_token_address="0x35a18000230da775cac24873d00ff85bccded550", debt_purchase_amount=1106497772527562662, received_amount=910895850496,