Add support for inspecting a range of blocks

This commit is contained in:
Luke Van Seters 2021-08-06 13:43:55 -04:00
parent c5dc3aa0f9
commit 6c727fb1f6
3 changed files with 82 additions and 17 deletions

View File

@ -41,6 +41,7 @@ build = 'scripts.poetry.docker:build'
attach = 'scripts.poetry.docker:attach'
exec = 'scripts.poetry.docker:exec'
inspect = 'scripts.poetry.inspect:inspect'
inspect-many = 'scripts.poetry.inspect:inspect_many'
[tool.black]
exclude = '''

View File

@ -20,13 +20,51 @@ from mev_inspect.arbitrages import get_arbitrages
from mev_inspect.swaps import get_swaps
@click.command()
@click.group()
def cli():
pass
@cli.command()
@click.argument("block_number", type=int)
@click.argument("rpc")
def inspect_block(block_number: int, rpc: str):
base_provider = Web3.HTTPProvider(rpc)
_inspect_block(base_provider, block_number)
@cli.command()
@click.argument("after_block", type=int)
@click.argument("before_block", type=int)
@click.argument("rpc")
def inspect_many_blocks(after_block: int, before_block: int, rpc: str):
base_provider = Web3.HTTPProvider(rpc)
for block_number in range(after_block + 1, before_block):
_inspect_block(
base_provider,
block_number,
should_print_stats=False,
should_write_classified_traces=False,
)
def _inspect_block(
base_provider,
block_number: int,
should_print_stats: bool = True,
should_write_classified_traces: bool = True,
should_write_swaps: bool = True,
should_write_arbitrages: bool = True,
):
block_message = f"Running for {block_number}"
dashes = "-" * len(block_message)
click.echo(dashes)
click.echo(block_message)
click.echo(dashes)
block_data = block.create_from_block_number(block_number, base_provider)
print(f"Total traces: {len(block_data.traces)}")
click.echo(f"Total traces: {len(block_data.traces)}")
total_transactions = len(
set(
@ -35,33 +73,35 @@ def inspect_block(block_number: int, rpc: str):
if t.transaction_hash is not None
)
)
print(f"Total transactions: {total_transactions}")
click.echo(f"Total transactions: {total_transactions}")
trace_clasifier = TraceClassifier(CLASSIFIER_SPECS)
classified_traces = trace_clasifier.classify(block_data.traces)
print(f"Returned {len(classified_traces)} classified traces")
click.echo(f"Returned {len(classified_traces)} classified traces")
db_session = get_session()
delete_classified_traces_for_block(db_session, block_number)
write_classified_traces(db_session, classified_traces)
if should_write_classified_traces:
delete_classified_traces_for_block(db_session, block_number)
write_classified_traces(db_session, classified_traces)
swaps = get_swaps(classified_traces)
print(f"Found {len(swaps)} swaps")
click.echo(f"Found {len(swaps)} swaps")
delete_swaps_for_block(db_session, block_number)
write_swaps(db_session, swaps)
if should_write_swaps:
delete_swaps_for_block(db_session, block_number)
write_swaps(db_session, swaps)
arbitrages = get_arbitrages(swaps)
print(f"Found {len(arbitrages)} arbitrages")
click.echo(f"Found {len(arbitrages)} arbitrages")
delete_arbitrages_for_block(db_session, block_number)
write_arbitrages(db_session, arbitrages)
if should_write_arbitrages:
delete_arbitrages_for_block(db_session, block_number)
write_arbitrages(db_session, arbitrages)
db_session.close()
stats = get_stats(classified_traces)
print(json.dumps(stats, indent=4))
if should_print_stats:
stats = get_stats(classified_traces)
click.echo(json.dumps(stats, indent=4))
def get_stats(classified_traces) -> dict:
@ -83,4 +123,4 @@ def get_stats(classified_traces) -> dict:
if __name__ == "__main__":
inspect_block()
cli()

View File

@ -19,7 +19,31 @@ def inspect(block_number: str, rpc: str):
"mev-inspect",
"python",
"./scripts/inspect_block.py",
"inspect-block",
block_number,
rpc,
]
)
@click.command()
@click.option("--after-block", type=str, help="look at blocks after this number")
@click.option("--before-block", type=str, help="look at blocks before this number")
@click.option(
"-r", "--rpc", help="rpc endpoint, this needs to have parity style traces"
)
def inspect_many(after_block: str, before_block: str, rpc: str):
check_call(
[
"docker",
"compose",
"exec",
"mev-inspect",
"python",
"./scripts/inspect_block.py",
"inspect-many-blocks",
after_block,
before_block,
rpc,
]
)