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,6 +10,7 @@ from mev_inspect import block
# External Libraries
import json
import pandas as pd
from typing import Optional
from web3 import Web3
from typing import List, Optional
@ -25,25 +26,33 @@ from typing import List, Optional
#
#
all_traces = []
liquidations = []
result = []
# Protocol contract address must be in included, below is AaveLendingPoolCoreV1
addrs = ['0x3dfd23A6c5E8BbcFc9581d2E864a68feb6a076d3']
transfers = []
from_doubles = []
transfers_to = []
transfers_from = []
# Inspect list of classified traces and identify liquidation
def liquidations(traces: List[ClassifiedTrace]):
event = []
def find_liquidations(traces: List[ClassifiedTrace]):
tx = []
# For each trace
for k in range(len(traces)):
trace = traces[k]
for count, trace in enumerate(traces):
# Check for liquidation and register trace and unique liquidator
if trace.classification == Classification.liquidate:
if (
trace.classification == Classification.liquidate and
trace.transaction_hash not in tx
):
all_traces.append(trace)
liquidator = trace.from_address
if liquidator not in addrs: addrs.append(liquidator)
print(f"\nLiquidation found: \n\t{liquidator}")
liquidations.append(trace)
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")
# Found liquidation, now parse inputs for data
for i in trace.inputs:
@ -52,11 +61,14 @@ def liquidations(traces: List[ClassifiedTrace]):
liquidation_amount = trace.inputs[i]
elif (i == '_collateral'):
collateral_type = trace.inputs[i]
print(f"Collateral type: {collateral_type}")
#This will be the address of the sent token
elif (i == '_reserve'):
reserve = trace.inputs[i]
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,
@ -64,7 +76,7 @@ def liquidations(traces: List[ClassifiedTrace]):
collateral_source="",
reserve=reserve,
strategy=StrategyType.liquidation,
traces=all_traces,
traces=liquidations,
protocols=[trace.protocol],)
)
@ -72,15 +84,17 @@ def liquidations(traces: List[ClassifiedTrace]):
elif (
trace.classification == Classification.transfer 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])
transfers.append(trace)
transfers_from.append(["from", liquidator, trace.transaction_hash, trace.inputs['amount']])
from_doubles.append(trace.transaction_hash)
print(f"""
\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\tTransaction: {trace.transaction_hash}\n
""")
@ -92,7 +106,7 @@ def liquidations(traces: List[ClassifiedTrace]):
):
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"""
\nTransfer to liquidator {liquidator}:
@ -101,6 +115,20 @@ def liquidations(traces: List[ClassifiedTrace]):
\n\tTransaction: {trace.transaction_hash}\n
""")
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
@ -110,4 +138,4 @@ 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)
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