Merge pull request #72 from flashbots/write-transfers-2
Write transfers to transfers table on inspect
This commit is contained in:
commit
5756a7c405
28
mev_inspect/crud/transfers.py
Normal file
28
mev_inspect/crud/transfers.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import json
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from mev_inspect.models.transfers import TransferModel
|
||||||
|
from mev_inspect.schemas.transfers import ERC20Transfer
|
||||||
|
|
||||||
|
|
||||||
|
def delete_transfers_for_block(
|
||||||
|
db_session,
|
||||||
|
block_number: int,
|
||||||
|
) -> None:
|
||||||
|
(
|
||||||
|
db_session.query(TransferModel)
|
||||||
|
.filter(TransferModel.block_number == block_number)
|
||||||
|
.delete()
|
||||||
|
)
|
||||||
|
|
||||||
|
db_session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def write_transfers(
|
||||||
|
db_session,
|
||||||
|
transfers: List[ERC20Transfer],
|
||||||
|
) -> None:
|
||||||
|
models = [TransferModel(**json.loads(transfer.json())) for transfer in transfers]
|
||||||
|
|
||||||
|
db_session.bulk_save_objects(models)
|
||||||
|
db_session.commit()
|
@ -18,8 +18,10 @@ from mev_inspect.crud.miner_payments import (
|
|||||||
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.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
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -33,6 +35,7 @@ def inspect_block(
|
|||||||
should_cache: bool,
|
should_cache: bool,
|
||||||
should_write_classified_traces: bool = True,
|
should_write_classified_traces: bool = True,
|
||||||
should_write_swaps: bool = True,
|
should_write_swaps: bool = True,
|
||||||
|
should_write_transfers: bool = True,
|
||||||
should_write_arbitrages: bool = True,
|
should_write_arbitrages: bool = True,
|
||||||
should_write_miner_payments: bool = True,
|
should_write_miner_payments: bool = True,
|
||||||
):
|
):
|
||||||
@ -53,6 +56,11 @@ def inspect_block(
|
|||||||
delete_classified_traces_for_block(db_session, block_number)
|
delete_classified_traces_for_block(db_session, block_number)
|
||||||
write_classified_traces(db_session, classified_traces)
|
write_classified_traces(db_session, classified_traces)
|
||||||
|
|
||||||
|
transfers = get_transfers(classified_traces)
|
||||||
|
if should_write_transfers:
|
||||||
|
delete_transfers_for_block(db_session, block_number)
|
||||||
|
write_transfers(db_session, transfers)
|
||||||
|
|
||||||
swaps = get_swaps(classified_traces)
|
swaps = get_swaps(classified_traces)
|
||||||
logger.info(f"Found {len(swaps)} swaps")
|
logger.info(f"Found {len(swaps)} swaps")
|
||||||
|
|
||||||
|
17
mev_inspect/models/transfers.py
Normal file
17
mev_inspect/models/transfers.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from sqlalchemy import Column, Numeric, String, ARRAY, Integer
|
||||||
|
|
||||||
|
from .base import Base
|
||||||
|
|
||||||
|
|
||||||
|
class TransferModel(Base):
|
||||||
|
__tablename__ = "transfers"
|
||||||
|
|
||||||
|
block_number = Column(Numeric, nullable=False)
|
||||||
|
transaction_hash = Column(String, primary_key=True)
|
||||||
|
trace_address = Column(ARRAY(Integer), nullable=False)
|
||||||
|
protocol = Column(String, nullable=True)
|
||||||
|
from_address = Column(String, nullable=False)
|
||||||
|
to_address = Column(String, nullable=False)
|
||||||
|
token_address = Column(String, nullable=False)
|
||||||
|
amount = Column(Numeric, nullable=False)
|
||||||
|
error = Column(String, nullable=True)
|
@ -6,6 +6,7 @@ from .classified_traces import Classification, ClassifiedTrace, Protocol
|
|||||||
|
|
||||||
|
|
||||||
class Transfer(BaseModel):
|
class Transfer(BaseModel):
|
||||||
|
block_number: int
|
||||||
transaction_hash: str
|
transaction_hash: str
|
||||||
trace_address: List[int]
|
trace_address: List[int]
|
||||||
from_address: str
|
from_address: str
|
||||||
@ -21,6 +22,7 @@ class EthTransfer(Transfer):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def from_trace(cls, trace: ClassifiedTrace) -> "EthTransfer":
|
def from_trace(cls, trace: ClassifiedTrace) -> "EthTransfer":
|
||||||
return cls(
|
return cls(
|
||||||
|
block_number=trace.block_number,
|
||||||
transaction_hash=trace.transaction_hash,
|
transaction_hash=trace.transaction_hash,
|
||||||
trace_address=trace.trace_address,
|
trace_address=trace.trace_address,
|
||||||
amount=trace.value,
|
amount=trace.value,
|
||||||
@ -39,6 +41,7 @@ class ERC20Transfer(Transfer):
|
|||||||
|
|
||||||
if trace.protocol == Protocol.weth:
|
if trace.protocol == Protocol.weth:
|
||||||
return cls(
|
return cls(
|
||||||
|
block_number=trace.block_number,
|
||||||
transaction_hash=trace.transaction_hash,
|
transaction_hash=trace.transaction_hash,
|
||||||
trace_address=trace.trace_address,
|
trace_address=trace.trace_address,
|
||||||
amount=trace.inputs["wad"],
|
amount=trace.inputs["wad"],
|
||||||
@ -48,6 +51,7 @@ class ERC20Transfer(Transfer):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return cls(
|
return cls(
|
||||||
|
block_number=trace.block_number,
|
||||||
transaction_hash=trace.transaction_hash,
|
transaction_hash=trace.transaction_hash,
|
||||||
trace_address=trace.trace_address,
|
trace_address=trace.trace_address,
|
||||||
amount=trace.inputs["amount"],
|
amount=trace.inputs["amount"],
|
||||||
|
@ -14,6 +14,7 @@ def test_remove_child_transfers_of_transfers(get_transaction_hashes, get_address
|
|||||||
] = get_addresses(5)
|
] = get_addresses(5)
|
||||||
|
|
||||||
outer_transfer = ERC20Transfer(
|
outer_transfer = ERC20Transfer(
|
||||||
|
block_number=123,
|
||||||
transaction_hash=transaction_hash,
|
transaction_hash=transaction_hash,
|
||||||
trace_address=[0],
|
trace_address=[0],
|
||||||
from_address=alice_address,
|
from_address=alice_address,
|
||||||
@ -33,6 +34,7 @@ def test_remove_child_transfers_of_transfers(get_transaction_hashes, get_address
|
|||||||
)
|
)
|
||||||
|
|
||||||
other_transfer = ERC20Transfer(
|
other_transfer = ERC20Transfer(
|
||||||
|
block_number=123,
|
||||||
transaction_hash=transaction_hash,
|
transaction_hash=transaction_hash,
|
||||||
trace_address=[1],
|
trace_address=[1],
|
||||||
from_address=bob_address,
|
from_address=bob_address,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user