Merge pull request #60 from zomglings/moonworm-watch-to-jsonl

Added "moonworm watch ... --outfile output.jsonl"
pull/62/head v0.1.17
Neeraj Kashyap 2022-03-14 14:24:16 -07:00 zatwierdzone przez GitHub
commit d13fcdac2f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 59 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()
@ -151,6 +152,7 @@ def handle_watch(args: argparse.Namespace) -> None:
contract_abi=contract_abi,
num_confirmations=args.confirmations,
start_block=args.start,
outfile=args.outfile,
)
@ -244,6 +246,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 @@
import json
import pprint as pp
import time
from dataclasses import asdict
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,51 @@ 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)
if ofp is not None:
print(json.dumps(asdict(call)), file=ofp)
ofp.flush()
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)
if ofp is not None:
print(json.dumps(event), file=ofp)
ofp.flush()
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:
if ofp is not None:
ofp.close()