Added --outfile argument to "moonworm watch"

If specified, in addition to printing events and method calls to screen,
they are appended to the given file as JSON lines.
pull/60/head
Neeraj Kashyap 2022-02-28 15:00:54 -08:00
rodzic 930e601996
commit 09abceab4d
3 zmienionych plików z 53 dodań i 31 usunięć

Wyświetl plik

@ -138,6 +138,7 @@ def handle_watch(args: argparse.Namespace) -> None:
contract_abi=contract_abi,
num_confirmations=args.confirmations,
start_block=args.start,
outfile=args.outfile,
)
finally:
state_provider.clear_db_session()
@ -244,6 +245,13 @@ def generate_argument_parser() -> argparse.ArgumentParser:
help="Number of confirmations to wait for. Default=12",
)
watch_parser.add_argument(
"-o",
"--outfile",
default=None,
help="Optional JSONL (JsON lines) file into which to write events and method calls",
)
watch_parser.set_defaults(func=handle_watch)
watch_cu_parser = subcommands.add_parser(

Wyświetl plik

@ -1 +1 @@
MOONWORM_VERSION = "0.1.16"
MOONWORM_VERSION = "0.1.17"

Wyświetl plik

@ -1,8 +1,9 @@
from dataclasses import asdict
import json
import pprint as pp
import time
from typing import Any, Dict, List, Optional
import web3
from eth_typing.evm import ChecksumAddress
from tqdm import tqdm
from web3 import Web3
@ -51,6 +52,7 @@ def watch_contract(
num_confirmations: int = 10,
sleep_time: float = 1,
start_block: Optional[int] = None,
outfile: Optional[str] = None,
) -> None:
"""
Watches a contract for events and calls.
@ -72,34 +74,46 @@ def watch_contract(
progress_bar = tqdm(unit=" blocks")
progress_bar.set_description(f"Current block {current_block}")
while True:
time.sleep(sleep_time)
end_block = min(web3.eth.blockNumber - num_confirmations, current_block + 100)
if end_block < current_block:
sleep_time *= 2
continue
sleep_time /= 2
crawler.crawl(current_block, end_block)
if state.state:
print("Got transaction calls:")
for call in state.state:
pp.pprint(call, width=200, indent=4)
state.flush()
for event_abi in event_abis:
all_events = _fetch_events_chunk(
web3,
event_abi,
current_block,
end_block,
[contract_address],
ofp = None
if outfile is not None:
ofp = open(outfile, "a")
try:
while True:
time.sleep(sleep_time)
end_block = min(
web3.eth.blockNumber - num_confirmations, current_block + 100
)
for event in all_events:
print("Got event:")
pp.pprint(event, width=200, indent=4)
if end_block < current_block:
sleep_time *= 2
continue
progress_bar.set_description(f"Current block {end_block}, Already watching for")
progress_bar.update(end_block - current_block + 1)
current_block = end_block + 1
sleep_time /= 2
crawler.crawl(current_block, end_block)
if state.state:
print("Got transaction calls:")
for call in state.state:
pp.pprint(call, width=200, indent=4)
json.dump(asdict(call), ofp)
state.flush()
for event_abi in event_abis:
all_events = _fetch_events_chunk(
web3,
event_abi,
current_block,
end_block,
[contract_address],
)
for event in all_events:
print("Got event:")
pp.pprint(event, width=200, indent=4)
json.dump(event, ofp)
progress_bar.set_description(
f"Current block {end_block}, Already watching for"
)
progress_bar.update(end_block - current_block + 1)
current_block = end_block + 1
finally:
ofp.close()