#!/usr/bin/env python3
# Watch the recycle DESK wallet for its USDC funding float. Exits (re-invoking Aura) when
# USDC lands (0), or TIMEOUT (3). Mainnet USDC balance of the Paul-controlled desk.
import time, pathlib, sys
from web3 import Web3
USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
DESK = pathlib.Path.home().joinpath(".config/amber/recycle_wallet.txt").read_text().strip()
LOGP = "/tmp/claude-1000/-home-paul-Aura/a2239ea0-e18d-4178-9864-2c3f48478801/scratchpad/desk_fund_watch.log"
w3 = Web3(Web3.HTTPProvider(pathlib.Path.home().joinpath('.eth_rpc').read_text().strip()))
erc = w3.eth.contract(address=Web3.to_checksum_address(USDC), abi=[
    {"name":"balanceOf","type":"function","stateMutability":"view",
     "inputs":[{"name":"a","type":"address"}],"outputs":[{"name":"","type":"uint256"}]}])

def usdc():
    for _ in range(5):
        try: return erc.functions.balanceOf(Web3.to_checksum_address(DESK)).call()/1e6
        except Exception: time.sleep(5)
    return None
def eth():
    for _ in range(5):
        try: return w3.eth.get_balance(Web3.to_checksum_address(DESK))/1e18
        except Exception: time.sleep(5)
    return None
def log(m):
    with open(LOGP,"a") as f: f.write(f"{time.strftime('%H:%M:%S')} {m}\n")

log(f"DESK FUND WATCH START — desk {DESK}, USDC ${usdc() or 0:,.2f}, ETH {eth() or 0:.4f}")
deadline = time.time() + 4*3600
while time.time() < deadline:
    time.sleep(90)
    u = usdc()
    if u and u > 0:
        log(f"DESK FUNDED — USDC ${u:,.2f} landed; ETH gas {eth():.4f}. Ready for end-to-end test.")
        sys.exit(0)
log("TIMEOUT — desk still unfunded after 4h")
sys.exit(3)
