From 244be7e990b297fc35ee60eec6e5f83226dfb4ec Mon Sep 17 00:00:00 2001 From: Guilherme Peyrelongue Heise Date: Fri, 6 Aug 2021 02:07:28 -0400 Subject: [PATCH] liquidation data type and inspector clean up --- liquidation_inspector.py | 96 +++++++++++++++++++-------------- mev_inspect/schemas/strategy.py | 2 + 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/liquidation_inspector.py b/liquidation_inspector.py index 73c2548..64f0610 100644 --- a/liquidation_inspector.py +++ b/liquidation_inspector.py @@ -17,72 +17,88 @@ from mev_inspect import block # poetry run inspect -b 12498502 -r 'http://162.55.96.141:8546/' all_traces = [] result = [] +addrs = ['0x3dfd23A6c5E8BbcFc9581d2E864a68feb6a076d3'] +transfers = [] # Inspect list of classified traces and identify liquidation def liquidations(traces: List[ClassifiedTrace]): event = [] # For each trace - for k in range(1, len(traces)): + for k in range(len(traces)): trace = traces[k] - try: - next = traces[k+1] - except IndexError: - break - # Liquidation condition + + # Check for liquidation and register trace and unique liquidator if trace.classification == Classification.liquidate: - # Collateral data from the liquidation. - # The inputs will differ by DEX, this is AAVE + all_traces.append(trace) liquidator = trace.from_address - prev = traces[k-1] - #print(f"Previous: {prev.classification} from {prev.from_address} to {prev.to_address}") + if liquidator not in addrs: addrs.append(liquidator) print(f"Liquidation found: {liquidator}") print(f"Hash: {trace.transaction_hash}") + + # Found liquidation, now parse inputs for data for i in trace.inputs: + if(i == '_purchaseAmount'): liquidation_amount = trace.inputs[i] - print(f"\tAmount: {liquidation_amount}") elif (i == '_collateral'): collateral_type = trace.inputs[i] - print(f"\tCollateral Address: {collateral_type}") elif (i == '_reserve'): reserve = trace.inputs[i] - print(f"\tReserve Address: {reserve}") elif(i == '_user'): liquidated_usr = trace.inputs[i] - print(f"\tLiquidated Account Address: {liquidated_usr}") - # Define the address of the liquidator - # Find a transfer before liquidation with a to_address corresponding to the liquidator - for tx in traces[0:int(k)]: - if ((tx.classification==Classification.transfer) and ('sender' in tx.inputs) and (tx.inputs['sender'] == liquidator)): - amount_sent = tx.inputs['amount'] - all_traces.append(tx) - print(f"Transfer from liquidator {liquidator}: \nAmount in received token: {tx.inputs['amount']} to \n{tx.inputs['recipient']} \nTransaction: {tx.transaction_hash}") - elif ((tx.classification==Classification.transfer) and (tx.inputs['recipient']==liquidator)): - amount_received = tx.inputs['amount'] - all_traces.append(tx) - print(f"Transfer to liquidator {liquidator}: \nAmount in received token: {tx.inputs['amount']} from \n{tx.from_address} \nTransaction: {tx.transaction_hash}") + # Register liquidation + result.append( + Liquidation(collateral_type=collateral_type, + collateral_amount=liquidation_amount, + collateral_source="", + reserve=reserve, + strategy=StrategyType.liquidation, + traces=all_traces, + protocols=[trace.protocol],) + ) + + # Check for transfer from liquidator + elif ( + trace.classification == Classification.transfer and + 'sender' in trace.inputs and + trace.inputs['sender'] in addrs and + trace.inputs['amount'] not in transfers + ): + + liquidator = next(addrs[i] for i in range(len(addrs)) if trace.inputs['sender'] == addrs[i]) + transfers.append(trace) + + print(f""" + \nTransfer from liquidator {liquidator}: + \nAmount in received token: {trace.inputs['amount']} to + \n{trace.inputs['recipient']} + \nTransaction: {trace.transaction_hash} + """) + + # Check for transfer to liquidator + elif ( + trace.classification == Classification.transfer and + trace.inputs['recipient'] in addrs + ): + + liquidator = next(addrs[i] for i in range(len(addrs)) if trace.inputs['recipient'] == addrs[i]) + transfers.append(trace) + + print(f""" + \nTransfer to liquidator {liquidator}: + \nAmount in received token: {trace.inputs['amount']} from + \n{trace.from_address} + \nTransaction: {trace.transaction_hash} + """) - try: - profit = amount_received - amount_sent - except UnboundLocalError: - print("No match ;[") - profit = 0 - # Tag liquidation - result.append(Liquidation(strategy=StrategyType.liquidation, - traces=all_traces, - protocols=[trace.protocol], - collateral_type=collateral_type, - collateral_amount=liquidation_amount, - profit=profit, - reserve=reserve, - collateral_source="",)) return result + rpc = 'http://162.55.96.141:8546/' block_number = 12498502 base_provider = Web3.HTTPProvider(rpc) block_data = block.create_from_block_number(block_number, base_provider) trace_clasifier = TraceClassifier(CLASSIFIER_SPECS) classified_traces = trace_clasifier.classify(block_data.traces) -print(liquidations(classified_traces)[2]) +fin = liquidations(classified_traces) diff --git a/mev_inspect/schemas/strategy.py b/mev_inspect/schemas/strategy.py index 87befd5..3a69227 100644 --- a/mev_inspect/schemas/strategy.py +++ b/mev_inspect/schemas/strategy.py @@ -17,4 +17,6 @@ class Liquidation(Strategy): collateral_amount: int collateral_source: str reserve: str + +class LiquidationData(Liquidation): profit: float