Add liquidations model/crud
This commit is contained in:
parent
cac1b13ac7
commit
1560ee9a99
31
mev_inspect/crud/liquidations.py
Normal file
31
mev_inspect/crud/liquidations.py
Normal 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()
|
@ -17,8 +17,13 @@ from mev_inspect.crud.miner_payments import (
|
|||||||
delete_miner_payments_for_block,
|
delete_miner_payments_for_block,
|
||||||
write_miner_payments,
|
write_miner_payments,
|
||||||
)
|
)
|
||||||
|
|
||||||
from mev_inspect.crud.swaps import delete_swaps_for_block, write_swaps
|
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.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.miner_payments import get_miner_payments
|
||||||
from mev_inspect.swaps import get_swaps
|
from mev_inspect.swaps import get_swaps
|
||||||
from mev_inspect.transfers import get_transfers
|
from mev_inspect.transfers import get_transfers
|
||||||
@ -38,6 +43,7 @@ def inspect_block(
|
|||||||
should_write_swaps: bool = True,
|
should_write_swaps: bool = True,
|
||||||
should_write_transfers: bool = True,
|
should_write_transfers: bool = True,
|
||||||
should_write_arbitrages: bool = True,
|
should_write_arbitrages: bool = True,
|
||||||
|
should_write_liquidations: bool = True,
|
||||||
should_write_miner_payments: bool = True,
|
should_write_miner_payments: bool = True,
|
||||||
):
|
):
|
||||||
block = create_from_block_number(base_provider, w3, block_number, should_cache)
|
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)
|
liquidations = get_aave_liquidations(classified_traces)
|
||||||
logger.info(f"Found {len(liquidations)} liquidations")
|
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(
|
miner_payments = get_miner_payments(
|
||||||
block.miner, block.base_fee_per_gas, classified_traces, block.receipts
|
block.miner, block.base_fee_per_gas, classified_traces, block.receipts
|
||||||
)
|
)
|
||||||
|
18
mev_inspect/models/liquidations.py
Normal file
18
mev_inspect/models/liquidations.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user