-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwatch.py
More file actions
78 lines (63 loc) · 2.26 KB
/
watch.py
File metadata and controls
78 lines (63 loc) · 2.26 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
from dotenv import load_dotenv
import hypersync
import asyncio
import time
from hypersync import LogField, ClientConfig
# 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(ClientConfig(
url="https://eth.hypersync.xyz/",
bearer_token=bearer_token
))
height = await client.get_height()
# The query to run
query = hypersync.Query(
# start from tip and get only new events
from_block=height,
# Select all logs from dai contract address
logs=[hypersync.LogSelection(
address=[DAI_ADDRESS],
topics=[["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]],
)],
# Select the fields we want, we get all fields we need for decoding the logs
field_selection=hypersync.FieldSelection(
log=[
LogField.DATA,
LogField.ADDRESS,
LogField.TOPIC0,
LogField.TOPIC1,
LogField.TOPIC2,
LogField.TOPIC3,
]
)
)
decoder = hypersync.Decoder([
"Transfer(address indexed from, address indexed to, uint256 value)"
])
total_dai_volume = 0
while True:
res = await client.get(query)
if len(res.data.logs) > 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_logs = await decoder.decode_logs(res.data.logs)
for log in decoded_logs:
#skip invalid logs
if log is None:
continue
total_dai_volume += log.body[0].val
print(f"total DAI transfer volume is {total_dai_volume / 1e18} USD")
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())