adjusted profit calculation

This commit is contained in:
Guilherme Peyrelongue Heise 2021-08-10 12:11:11 -04:00
parent 508847b8df
commit 771df11093
3 changed files with 93 additions and 126 deletions

View File

@ -10,13 +10,14 @@ from mev_inspect import block
# External Libraries # External Libraries
import json import json
import pandas as pd
from typing import Optional from typing import Optional
from web3 import Web3 from web3 import Web3
from typing import List, Optional from typing import List, Optional
# TODO: adjust profit to new model # TODO: adjust profit to new model
# unit test # unit test
# coinbase check / collateral source # coinbase check / collateral source
# #
# #
# Block inspect # Block inspect
@ -25,83 +26,110 @@ from typing import List, Optional
# #
# #
all_traces = [] liquidations = []
result = [] result = []
# Protocol contract address must be in included, below is AaveLendingPoolCoreV1
addrs = ['0x3dfd23A6c5E8BbcFc9581d2E864a68feb6a076d3'] addrs = ['0x3dfd23A6c5E8BbcFc9581d2E864a68feb6a076d3']
transfers = [] from_doubles = []
transfers_to = []
transfers_from = []
# Inspect list of classified traces and identify liquidation # Inspect list of classified traces and identify liquidation
def liquidations(traces: List[ClassifiedTrace]): def find_liquidations(traces: List[ClassifiedTrace]):
event = [] tx = []
# For each trace
for k in range(len(traces)):
trace = traces[k]
# Check for liquidation and register trace and unique liquidator # For each trace
if trace.classification == Classification.liquidate: for count, trace in enumerate(traces):
all_traces.append(trace) # Check for liquidation and register trace and unique liquidator
liquidator = trace.from_address if (
if liquidator not in addrs: addrs.append(liquidator) trace.classification == Classification.liquidate and
print(f"\nLiquidation found: \n\t{liquidator}") trace.transaction_hash not in tx
print(f"\nHash: \n\t{trace.transaction_hash}\n") ):
# Found liquidation, now parse inputs for data liquidations.append(trace)
for i in trace.inputs: addrs.append(trace.from_address)
tx.append(trace.transaction_hash)
print(f"\nLiquidation found: \n\t{trace.from_address}")
print(f"\nHash: \n\t{trace.transaction_hash}\n")
print(f"\nGas Used: {trace.gas_used}\n")
if(i == '_purchaseAmount'): # Found liquidation, now parse inputs for data
liquidation_amount = trace.inputs[i] for i in trace.inputs:
elif (i == '_collateral'):
collateral_type = trace.inputs[i]
elif (i == '_reserve'):
reserve = trace.inputs[i]
elif(i == '_user'):
liquidated_usr = trace.inputs[i]
# Register liquidation if(i == '_purchaseAmount'):
result.append( liquidation_amount = trace.inputs[i]
Liquidation(collateral_type=collateral_type, elif (i == '_collateral'):
collateral_amount=liquidation_amount, collateral_type = trace.inputs[i]
collateral_source="", print(f"Collateral type: {collateral_type}")
reserve=reserve, #This will be the address of the sent token
strategy=StrategyType.liquidation, elif (i == '_reserve'):
traces=all_traces, reserve = trace.inputs[i]
protocols=[trace.protocol],) print(f"Reserve: {reserve}")
) #This will be the address of the received token
elif(i == '_user'):
liquidated_usr = trace.inputs[i]
# Register liquidation
result.append(
Liquidation(collateral_type=collateral_type,
collateral_amount=liquidation_amount,
collateral_source="",
reserve=reserve,
strategy=StrategyType.liquidation,
traces=liquidations,
protocols=[trace.protocol],)
)
# Check for transfer from a registered liquidator # Check for transfer from a registered liquidator
elif ( elif (
trace.classification == Classification.transfer and trace.classification == Classification.transfer and
'sender' in trace.inputs and 'sender' in trace.inputs and
trace.inputs['sender'] in addrs trace.inputs['sender'] in addrs and
): trace.transaction_hash not in from_doubles
):
liquidator = next(addrs[i] for i in range(len(addrs)) if trace.inputs['sender'] == addrs[i]) liquidator = next(addrs[i] for i in range(len(addrs)) if trace.inputs['sender'] == addrs[i])
transfers.append(trace) transfers_from.append(["from", liquidator, trace.transaction_hash, trace.inputs['amount']])
from_doubles.append(trace.transaction_hash)
print(f""" print(f"""
\nTransfer from liquidator {liquidator}: \nTransfer from liquidator {liquidator}:
\n\tAmount in received token: {trace.inputs['amount']} to \n\tAmount sent: {trace.inputs['amount']} to
\n\t{trace.inputs['recipient']} \n\t{trace.inputs['recipient']}
\n\tTransaction: {trace.transaction_hash}\n \n\tTransaction: {trace.transaction_hash}\n
""") """)
# Check for transfer to a registered liquidator # Check for transfer to a registered liquidator
elif ( elif (
trace.classification == Classification.transfer and trace.classification == Classification.transfer and
trace.inputs['recipient'] in addrs trace.inputs['recipient'] in addrs
): ):
liquidator = next(addrs[i] for i in range(len(addrs)) if trace.inputs['recipient'] == addrs[i]) liquidator = next(addrs[i] for i in range(len(addrs)) if trace.inputs['recipient'] == addrs[i])
transfers.append(trace) transfers_to.append(["to", liquidator, trace.transaction_hash, trace.inputs['amount']])
print(f""" print(f"""
\nTransfer to liquidator {liquidator}: \nTransfer to liquidator {liquidator}:
\n\tAmount in received token: {trace.inputs['amount']} from \n\tAmount in received token: {trace.inputs['amount']} from
\n\t{trace.from_address} \n\t{trace.from_address}
\n\tTransaction: {trace.transaction_hash}\n \n\tTransaction: {trace.transaction_hash}\n
""") """)
return result for count, trace in enumerate(liquidations):
tx = trace.transaction_hash
#convert token to ETH
#profit = transfers[count][2] - transfers[count+1][2]
profits = []
for count, trace in enumerate(transfers_to):
profits.append({"liquidator" : transfers_to[count][1],
"transaction" : transfers_to[count][2],
"profit" : transfers_to[count][3] - transfers_from[count][3]})
print(profits)
print(liquidations)
return result
rpc = 'http://162.55.96.141:8546/' rpc = 'http://162.55.96.141:8546/'
@ -110,4 +138,4 @@ base_provider = Web3.HTTPProvider(rpc)
block_data = block.create_from_block_number(block_number, base_provider) block_data = block.create_from_block_number(block_number, base_provider)
trace_clasifier = TraceClassifier(CLASSIFIER_SPECS) trace_clasifier = TraceClassifier(CLASSIFIER_SPECS)
classified_traces = trace_clasifier.classify(block_data.traces) classified_traces = trace_clasifier.classify(block_data.traces)
fin = liquidations(classified_traces) fin = find_liquidations(classified_traces)

View File

@ -1,61 +0,0 @@
import json
from typing import Optional
from web3 import Web3
from typing import List, Optional
from mev_inspect import utils
from mev_inspect.config import load_config
from mev_inspect.schemas.blocks import NestedTrace, TraceType
from mev_inspect.schemas.classifications import Classification
from mev_inspect.schemas.strategy import Strategy, Liquidation
from .base import StrategyInspector
class LiquidationInspector(StrategyInspector):
def __init__(self):
self.result = []
# Inspect list of classified traces and identify liquidation
def inspect(self, traces):
event = []
# For each trace
for i in range(1, len(traces)):
trace = traces[i]
# Liquidation condition
if trace.classification == Classification.liquidate:
# Collateral data from the liquidation.
# The inputs will differ by DEX, this is AAVE
for i in trace.inputs:
if( i["name"] == '_purchaseAmount'):
value = trace.inputs[i]
elif (i["name"] == '_collateral'):
collateral_type = trace.inputs[i]
elif (i["name"] == '_reserve'):
collateral_source = trace.inputs[i]
# Define the address of the liquidator
liquidator = trace.from_address
# Find a transfer before liquidation with a to_address corresponding to the liquidator
for trace in traces[:i]:
if (trace.classification == Classification.transfer & trace.to_address == liquidator):
profit =
#Calculate profit
# Tag liquidation
event.append(Strategy(strategy=StategyType.liquidation,
traces=[trace],
protocols=[trace.protocol]))
return result