-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcall_watch.py
More file actions
62 lines (51 loc) · 2 KB
/
call_watch.py
File metadata and controls
62 lines (51 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from dotenv import load_dotenv
import hypersync
import asyncio
import time
from hypersync import TransactionField
# Load environment variables from a .env file
load_dotenv()
DAI_ADDRESS = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
async def main():
bearer_token = os.getenv("ENVIO_API_TOKEN")
if not bearer_token:
raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")
client = hypersync.HypersyncClient(hypersync.ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
# The query to run
query = hypersync.Query(
# start from tip and get only new events
from_block=20519993,
# Select all logs from dai contract address
transactions=[hypersync.TransactionSelection(from_=[DAI_ADDRESS]), hypersync.TransactionSelection(to=[DAI_ADDRESS])],
# Select the fields we want, we get all fields we need for decoding the logs
field_selection=hypersync.FieldSelection(
transaction=[
TransactionField.HASH,
TransactionField.INPUT,
]
)
)
decoder = hypersync.CallDecoder([
"transfer(address dst, uint256 wad)"
])
while True:
res = await client.get(query)
if len(res.data.transactions) > 0:
# Decode the log on a background thread so we don't block the event loop.
# Can also use decoder.decode_logs_sync if it is more convenient.
decoded_calls = await decoder.decode_transactions_input(res.data.transactions)
for call in decoded_calls:
if call:
print(f"Call decoded: addr: {call[0].val}, wad: {call[1].val}")
height = res.archive_height
while height < res.next_block:
print(f"waiting for chain to advance. Height is {height}")
height = await client.get_height()
time.sleep(1)
# continue query from next_block
query.from_block = res.next_block
asyncio.run(main())