Add total profit by block table in db (#6)

* feat: add new alembic migration

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

* feat: add method to store total profits in bulk

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

Signed-off-by: Luca Georges Francois <luca@quartz.technology>
This commit is contained in:
Luca G.F 2022-12-27 08:33:22 -08:00 committed by GitHub
parent 919f34db7e
commit f24a802de5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,31 @@
"""create total profit by block table
Revision ID: a5a44a7c854d
Revises: 5c5375de15fd
Create Date: 2022-12-12 02:46:57.125040
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = "a5a44a7c854d"
down_revision = "5c5375de15fd"
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
"total_profit_by_block",
sa.Column("block_number", sa.Numeric, primary_key=True),
sa.Column("transaction_hash", sa.String(66), nullable=False),
sa.Column("token_debt", sa.String(66), nullable=True),
sa.Column("amount_debt", sa.Numeric, nullable=False),
sa.Column("token_received", sa.String(66), nullable=False),
sa.Column("amount_received", sa.Numeric, nullable=False),
)
def downgrade():
op.drop_table("total_profit_by_block")

View File

@ -0,0 +1,22 @@
from typing import List
from mev_inspect.db import write_as_csv
from mev_inspect.schemas.total_profits import TotalProfits
def write_total_profits_for_blocks(
inspect_db_session,
total_profits_for_blocks: List[TotalProfits],
) -> None:
items_generator = (
(
total_profits_for_unique_block.block_number,
total_profits_for_unique_block.transaction_hash,
total_profits_for_unique_block.token_debt,
total_profits_for_unique_block.amount_debt,
total_profits_for_unique_block.token_received,
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

@ -0,0 +1,10 @@
from pydantic import BaseModel
class TotalProfits(BaseModel):
block_number: int
transaction_hash: str
token_debt: str
amount_debt: int
token_received: str
amount_received: int