Gut processor. Make a simple classification object
This commit is contained in:
parent
1a9a7425b9
commit
dc583d4d07
@ -1,43 +1,28 @@
|
||||
from typing import List
|
||||
|
||||
from mev_inspect.inspectors import Inspector
|
||||
from mev_inspect.schemas.blocks import Block, NestedTrace, TraceType
|
||||
from mev_inspect.schemas.blocks import Block, TraceType
|
||||
from mev_inspect.schemas.classifications import (
|
||||
Classification,
|
||||
UnknownClassification,
|
||||
ClassificationType,
|
||||
)
|
||||
from mev_inspect.traces import as_nested_traces
|
||||
|
||||
|
||||
class Processor:
|
||||
def __init__(self, inspectors: List[Inspector]) -> None:
|
||||
self._inspectors = inspectors
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def get_transaction_evaluations(
|
||||
def process(
|
||||
self,
|
||||
block: Block,
|
||||
) -> List[Classification]:
|
||||
transaction_traces = (
|
||||
trace for trace in block.traces if trace.type != TraceType.reward
|
||||
)
|
||||
|
||||
return [
|
||||
self._run_inspectors(nested_trace)
|
||||
for nested_trace in as_nested_traces(transaction_traces)
|
||||
Classification(
|
||||
transaction_hash=trace.transaction_hash,
|
||||
block_number=trace.block_number,
|
||||
trace_type=trace.type,
|
||||
trace_address=trace.trace_address,
|
||||
classification_type=ClassificationType.unknown,
|
||||
)
|
||||
for trace in block.traces
|
||||
if trace.type != TraceType.reward
|
||||
]
|
||||
|
||||
def _run_inspectors(self, nested_trace: NestedTrace) -> Classification:
|
||||
for inspector in self._inspectors:
|
||||
classification = inspector.inspect(nested_trace)
|
||||
|
||||
if classification is not None:
|
||||
return classification
|
||||
|
||||
internal_classifications = [
|
||||
self._run_inspectors(subtrace) for subtrace in nested_trace.subtraces
|
||||
]
|
||||
|
||||
return UnknownClassification(
|
||||
trace=nested_trace.trace,
|
||||
internal_classifications=internal_classifications,
|
||||
)
|
||||
|
@ -1,14 +1,18 @@
|
||||
from enum import Enum
|
||||
from typing import List
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from .blocks import Trace
|
||||
from .blocks import TraceType
|
||||
|
||||
|
||||
class ClassificationType(Enum):
|
||||
unknown = "unknown"
|
||||
|
||||
|
||||
class Classification(BaseModel):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownClassification(Classification):
|
||||
trace: Trace
|
||||
internal_classifications: List[Classification]
|
||||
transaction_hash: str
|
||||
block_number: int
|
||||
trace_type: TraceType
|
||||
trace_address: List[int]
|
||||
classification_type: ClassificationType
|
||||
|
@ -3,7 +3,6 @@ import argparse
|
||||
from web3 import Web3
|
||||
|
||||
from mev_inspect import block
|
||||
from mev_inspect.inspectors.uniswap import UniswapInspector
|
||||
from mev_inspect.processor import Processor
|
||||
|
||||
parser = argparse.ArgumentParser(description="Inspect some blocks.")
|
||||
@ -17,25 +16,32 @@ parser.add_argument(
|
||||
parser.add_argument(
|
||||
"-rpc", metavar="r", help="rpc endpoint, this needs to have parity style traces"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
## Set up the base provider, but don't wrap it in web3 so we can make requests to it with make_request()
|
||||
base_provider = Web3.HTTPProvider(args.rpc)
|
||||
|
||||
## Get block data that we need
|
||||
block_data = block.create_from_block_number(args.block_number[0], base_provider)
|
||||
print(f"Total traces: {len(block_data.traces)}")
|
||||
def inspect_block(base_provider, block_number):
|
||||
block_data = block.create_from_block_number(block_number, base_provider)
|
||||
print(f"Total traces: {len(block_data.traces)}")
|
||||
|
||||
total_transactions = len(
|
||||
set(t.transaction_hash for t in block_data.traces if t.transaction_hash is not None)
|
||||
)
|
||||
print(f"Total transactions: {total_transactions}")
|
||||
total_transactions = len(
|
||||
set(
|
||||
t.transaction_hash
|
||||
for t in block_data.traces
|
||||
if t.transaction_hash is not None
|
||||
)
|
||||
)
|
||||
print(f"Total transactions: {total_transactions}")
|
||||
|
||||
## Build a Uniswap inspector
|
||||
uniswap_inspector = UniswapInspector(base_provider)
|
||||
processor = Processor()
|
||||
classifications = processor.process(block_data)
|
||||
|
||||
## Create a processor, pass in an ARRAY of inspects
|
||||
processor = Processor([uniswap_inspector, uniswap_inspector])
|
||||
print(f"Returned {len(classifications)} classifications")
|
||||
|
||||
classifications = processor.get_transaction_evaluations(block_data)
|
||||
print(f"Returned {len(classifications)} classifications")
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = parser.parse_args()
|
||||
|
||||
## Set up the base provider, but don't wrap it in web3 so we can make requests to it with make_request()
|
||||
|
||||
w3_base_provider = Web3.HTTPProvider(args.rpc)
|
||||
|
||||
inspect_block(w3_base_provider, args.block_number[0])
|
||||
|
Loading…
x
Reference in New Issue
Block a user