From a464a0ccccd82b5006c8d55776f7e5e0c9d25b56 Mon Sep 17 00:00:00 2001 From: Taarush Vemulapalli Date: Sat, 16 Oct 2021 13:11:27 -0400 Subject: [PATCH] has_liquidations helper --- mev_inspect/compound_liquidations.py | 8 +++++--- mev_inspect/liquidations.py | 14 +++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mev_inspect/compound_liquidations.py b/mev_inspect/compound_liquidations.py index 201db2e..e84db9b 100644 --- a/mev_inspect/compound_liquidations.py +++ b/mev_inspect/compound_liquidations.py @@ -20,11 +20,13 @@ CREAM_CR_ETHER = "0xD06527D5e56A3495252A528C4987003b712860eE" # helper, only queried once in the beginning (inspect_block) def fetch_all_underlying_markets(w3: Web3, protocol: Protocol) -> Dict[str, str]: if protocol == Protocol.compound_v2: - C_ETHER = V2_C_ETHER + c_ether = V2_C_ETHER address = V2_COMPTROLLER_ADDRESS elif protocol == Protocol.cream: - C_ETHER = CREAM_CR_ETHER + c_ether = CREAM_CR_ETHER address = CREAM_COMPTROLLER_ADDRESS + else: + raise ValueError(f"No Comptroller found for {protocol}") token_mapping = {} comptroller_abi = get_raw_abi("Comptroller", Protocol.compound_v2) comptroller_instance = w3.eth.contract(address=address, abi=comptroller_abi) @@ -32,7 +34,7 @@ def fetch_all_underlying_markets(w3: Web3, protocol: Protocol) -> Dict[str, str] token_abi = get_raw_abi("CToken", Protocol.compound_v2) for token in markets: # make an exception for cETH (as it has no .underlying()) - if token != C_ETHER: + if token != c_ether: token_instance = w3.eth.contract(address=token, abi=token_abi) underlying_token = token_instance.functions.underlying().call() token_mapping[ diff --git a/mev_inspect/liquidations.py b/mev_inspect/liquidations.py index 91e20b9..f0bad7f 100644 --- a/mev_inspect/liquidations.py +++ b/mev_inspect/liquidations.py @@ -14,17 +14,21 @@ from mev_inspect.schemas.classified_traces import ( from mev_inspect.schemas.liquidations import Liquidation +def has_liquidations(classified_traces: List[ClassifiedTrace]) -> bool: + liquidations_exist = False + for classified_trace in classified_traces: + if classified_trace.classification == Classification.liquidate: + liquidations_exist = True + return liquidations_exist + + def get_liquidations( classified_traces: List[ClassifiedTrace], w3: Web3 ) -> List[Liquidation]: # to avoid contract calls to fetch comp/cream markets # unless there is a liquidation - has_liquidations = False - for classified_trace in classified_traces: - if classified_trace.classification == Classification.liquidate: - has_liquidations = True - if has_liquidations: + if has_liquidations(classified_traces): aave_liquidations = get_aave_liquidations(classified_traces) comp_markets = fetch_all_underlying_markets(w3, Protocol.compound_v2) cream_markets = fetch_all_underlying_markets(w3, Protocol.cream)