MCPcopy Create free account
hub / github.com/enviodev/hypersync-client-python / main

Function main

examples/watch.py:13–76  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11DAI_ADDRESS = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
12
13async def main():
14 bearer_token = os.getenv("ENVIO_API_TOKEN")
15 if not bearer_token:
16 raise ValueError("ENVIO_API_TOKEN environment variable is required. Please set it in your .env file.")
17
18 client = hypersync.HypersyncClient(ClientConfig(
19 url="https://eth.hypersync.xyz/",
20 bearer_token=bearer_token
21 ))
22
23 height = await client.get_height()
24
25 # The query to run
26 query = hypersync.Query(
27 # start from tip and get only new events
28 from_block=height,
29 # Select all logs from dai contract address
30 logs=[hypersync.LogSelection(
31 address=[DAI_ADDRESS],
32 topics=[["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]],
33 )],
34 # Select the fields we want, we get all fields we need for decoding the logs
35 field_selection=hypersync.FieldSelection(
36 log=[
37 LogField.DATA,
38 LogField.ADDRESS,
39 LogField.TOPIC0,
40 LogField.TOPIC1,
41 LogField.TOPIC2,
42 LogField.TOPIC3,
43 ]
44 )
45 )
46
47 decoder = hypersync.Decoder([
48 "Transfer(address indexed from, address indexed to, uint256 value)"
49 ])
50
51 total_dai_volume = 0
52 while True:
53 res = await client.get(query)
54
55 if len(res.data.logs) > 0:
56 # Decode the log on a background thread so we don't block the event loop.
57 # Can also use decoder.decode_logs_sync if it is more convenient.
58 decoded_logs = await decoder.decode_logs(res.data.logs)
59
60 for log in decoded_logs:
61 #skip invalid logs
62 if log is None:
63 continue
64
65 total_dai_volume += log.body[0].val
66
67 print(f"total DAI transfer volume is {total_dai_volume / 1e18} USD")
68
69 height = res.archive_height
70 while height < res.next_block:

Callers 1

watch.pyFile · 0.70

Calls 4

get_heightMethod · 0.95
getMethod · 0.95
decode_logsMethod · 0.95
ClientConfigClass · 0.90

Tested by

no test coverage detected