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()