feat: save profit to db (#17)

* feat: save profit to db

Signed-off-by: Arthurim <arthurbdauphine@gmail.com>

* fix: update total profit table and related crud

Signed-off-by: Luca Georges Francois <luca@quartz.technology>

Signed-off-by: Arthurim <arthurbdauphine@gmail.com>
Signed-off-by: Luca Georges Francois <luca@quartz.technology>
Co-authored-by: Luca Georges Francois <luca@quartz.technology>
This commit is contained in:
Eru Ilúvatar 2022-12-30 07:43:43 +00:00 committed by GitHub
parent 8b52740d7f
commit 880992362b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 34 deletions

View File

@ -18,7 +18,10 @@ depends_on = None
def upgrade(): def upgrade():
op.create_table( op.create_table(
"total_profit_by_block", "total_profit_by_block",
sa.Column("block_number", sa.Numeric, primary_key=True), sa.Column(
"id", sa.Integer, nullable=False, autoincrement=True, primary_key=True
),
sa.Column("block_number", sa.Numeric, nullable=False),
sa.Column("transaction_hash", sa.String(66), nullable=False), sa.Column("transaction_hash", sa.String(66), nullable=False),
sa.Column("token_debt", sa.String(66), nullable=True), sa.Column("token_debt", sa.String(66), nullable=True),
sa.Column("amount_debt", sa.Numeric, nullable=False), sa.Column("amount_debt", sa.Numeric, nullable=False),

View File

@ -1,6 +1,7 @@
import json
from typing import List from typing import List
from mev_inspect.db import write_as_csv from mev_inspect.models.total_profits import TotalProfitsModel
from mev_inspect.schemas.total_profits import TotalProfits from mev_inspect.schemas.total_profits import TotalProfits
@ -8,15 +9,10 @@ def write_total_profits_for_blocks(
inspect_db_session, inspect_db_session,
total_profits_for_blocks: List[TotalProfits], total_profits_for_blocks: List[TotalProfits],
) -> None: ) -> None:
items_generator = ( models = [
( TotalProfitsModel(**json.loads(swap.json()))
total_profits_for_unique_block.block_number, for swap in total_profits_for_blocks
total_profits_for_unique_block.transaction_hash, ]
total_profits_for_unique_block.token_debt,
total_profits_for_unique_block.amount_debt, inspect_db_session.bulk_save_objects(models)
total_profits_for_unique_block.token_received, inspect_db_session.commit()
total_profits_for_unique_block.amount_received,
)
for total_profits_for_unique_block in total_profits_for_blocks
)
write_as_csv(inspect_db_session, "total_profit_by_block", items_generator)

View File

@ -1,11 +1,13 @@
import logging import logging
from typing import Any, Dict from typing import Any, Dict, List
from sqlalchemy import orm from sqlalchemy import orm
from web3 import Web3 from web3 import Web3
from mev_inspect.arbitrages import get_arbitrages from mev_inspect.arbitrages import get_arbitrages
from mev_inspect.block import get_classified_traces_from_events from mev_inspect.block import get_classified_traces_from_events
from mev_inspect.crud.total_profits import write_total_profits_for_blocks
from mev_inspect.schemas.total_profits import TotalProfits
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -35,7 +37,7 @@ async def inspect_many_blocks(
arbitrages_payload = [] arbitrages_payload = []
liquidations_payload = [] liquidations_payload = []
profits = [] profits: List[TotalProfits] = []
async for swaps, liquidations in get_classified_traces_from_events( async for swaps, liquidations in get_classified_traces_from_events(
w3, after_block_number, before_block_number w3, after_block_number, before_block_number
): ):
@ -52,14 +54,16 @@ async def inspect_many_blocks(
arbitrages_payload.append(arb_payload) arbitrages_payload.append(arb_payload)
count += 1 count += 1
profits.append( profits.append(
[ TotalProfits(
arb.block_number, block_number=arb.block_number,
arb.transaction_hash, transaction_hash=arb.transaction_hash,
"", token_debt="",
0, amount_debt=0,
str(arb.profit_token_address).replace(TRAILING_ZEROS, ""), token_received=str(arb.profit_token_address).replace(
arb.profit_amount, TRAILING_ZEROS, ""
] ),
amount_received=arb.profit_amount,
)
) )
if len(liquidations) > 0: if len(liquidations) > 0:
@ -75,20 +79,25 @@ async def inspect_many_blocks(
liquidations_payload.append(liq_payload) liquidations_payload.append(liq_payload)
count += 1 count += 1
profits.append( profits.append(
[ TotalProfits(
liq.block_number, block_number=liq.block_number,
liq.transaction_hash, transaction_hash=liq.transaction_hash,
str(liq.debt_token_address).replace(TRAILING_ZEROS, ""), token_debt=str(liq.debt_token_address).replace(
liq.debt_purchase_amount, TRAILING_ZEROS, ""
str(liq.received_amount).replace(TRAILING_ZEROS, ""), ),
liq.received_amount, amount_debt=liq.debt_purchase_amount,
] token_received=str(liq.received_amount).replace(
TRAILING_ZEROS, ""
),
amount_received=liq.received_amount,
)
) )
if count > 0: if count > 0:
print("writing profits of {0} mev transactions".format(count)) print("writing profits of {0} mev transactions".format(count))
# @TODO: Write profits to DB write_total_profits_for_blocks(
print(inspect_db_session.info) inspect_db_session=inspect_db_session, total_profits_for_blocks=profits
)
arbitrages_payload = [] arbitrages_payload = []
liquidations_payload = [] liquidations_payload = []
count = 0 count = 0

View File

@ -0,0 +1,15 @@
from sqlalchemy import Column, Integer, Numeric, String
from .base import Base
class TotalProfitsModel(Base):
__tablename__ = "total_profit_by_block"
id = Column("id", Integer, nullable=False, autoincrement=True, primary_key=True)
block_number = Column("block_number", Numeric, nullable=False)
transaction_hash = Column("transaction_hash", String(66), nullable=False)
token_debt = Column("token_debt", String(66), nullable=True)
amount_debt = Column("amount_debt", Numeric, nullable=False)
token_received = Column("token_received", String(66), nullable=False)
amount_received = Column("amount_received", Numeric, nullable=False)