Add liquidations model/crud

This commit is contained in:
Gui Heise 2021-09-29 10:41:49 -04:00
parent cac1b13ac7
commit 1560ee9a99
3 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,31 @@
import json
from typing import List
from mev_inspect.models.liquidations import LiquidationModel
from mev_inspect.schemas.liquidations import Liquidation
def delete_liquidations_for_block(
db_session,
block_number: int,
) -> None:
(
db_session.query(LiquidationModel)
.filter(LiquidationModel.block_number == block_number)
.delete()
)
db_session.commit()
def write_liquidations(
db_session,
liquidations: List[Liquidation],
) -> None:
models = [
LiquidationModel(**json.loads(liquidation.json()))
for liquidation in liquidations
]
db_session.bulk_save_objects(models)
db_session.commit()

View File

@ -17,8 +17,13 @@ from mev_inspect.crud.miner_payments import (
delete_miner_payments_for_block,
write_miner_payments,
)
from mev_inspect.crud.swaps import delete_swaps_for_block, write_swaps
from mev_inspect.crud.transfers import delete_transfers_for_block, write_transfers
from mev_inspect.crud.liquidations import (
delete_liquidations_for_block,
write_liquidations,
)
from mev_inspect.miner_payments import get_miner_payments
from mev_inspect.swaps import get_swaps
from mev_inspect.transfers import get_transfers
@ -38,6 +43,7 @@ def inspect_block(
should_write_swaps: bool = True,
should_write_transfers: bool = True,
should_write_arbitrages: bool = True,
should_write_liquidations: bool = True,
should_write_miner_payments: bool = True,
):
block = create_from_block_number(base_provider, w3, block_number, should_cache)
@ -79,6 +85,10 @@ def inspect_block(
liquidations = get_aave_liquidations(classified_traces)
logger.info(f"Found {len(liquidations)} liquidations")
if should_write_liquidations:
delete_liquidations_for_block(db_session, block_number)
write_liquidations(db_session, liquidations)
miner_payments = get_miner_payments(
block.miner, block.base_fee_per_gas, classified_traces, block.receipts
)

View File

@ -0,0 +1,18 @@
from sqlalchemy import Column, Numeric, String, ARRAY, Integer
from .base import Base
class LiquidationModel(Base):
__tablename__ = "liquidations"
liquidated_user = Column(String, nullable=False)
liquidator_user = Column(String, nullable=False)
collateral_token_address = Column(String, nullable=False)
debt_token_address = Column(String, nullable=False)
debt_purchase_amount = Column(Numeric, nullable=False)
received_amount = Column(Numeric, nullable=False)
protocol = Column(String, nullable=True)
transaction_hash = Column(String, primary_key=True)
trace_address = Column(ARRAY(Integer), nullable=False)
block_number = Column(Numeric, nullable=False)