File size: 1,094 Bytes
8f82127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from dotenv import load_dotenv
from web3 import Web3
import asyncio
import sys
from db import init_db
from indexer import backfill_recent_transfers, watch_live

def main():
    load_dotenv()
    rpc_url = os.getenv("POLYGON_RPC_URL")
    if not rpc_url:
        raise SystemExit("POLYGON_RPC_URL missing in .env")

    w3 = Web3(Web3.HTTPProvider(rpc_url, request_kwargs={"timeout": 30}))
    if not w3.is_connected():
        raise SystemExit("Failed to connect to Polygon RPC")

    latest = w3.eth.block_number
    print(f"Connected. Latest block: {latest}")

    # initialize database (only first run)
    init_db()

    # check if running in test mode
    live = True
    if len(sys.argv) > 1 and sys.argv[1] == "--test":
        live = False

    if live:
        # first backfill a short history so DB has some data
        backfill_recent_transfers(block_window=5000)
        # then start live listener
        asyncio.run(watch_live())
    else:
        # just backfill, no live loop
        backfill_recent_transfers(block_window=500)

if __name__ == "__main__":
    main()