#!/usr/bin/env python3
# Watch the Aurum Desk USDC float for a top-up after the pilot drained it. Exit 0 when the
# float rises >= $10 over the current baseline (a real top-up landed), so Aura can auto-unpause.
import time, pathlib, sys
from web3 import Web3
USDC="0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
DESK="0x4FFDB5b7b14Ef24f19095B278D709CEd99257aF2"
LOGP="/tmp/claude-1000/-home-paul-Aura/a2239ea0-e18d-4178-9864-2c3f48478801/scratchpad/topup_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 bal():
    for _ in range(5):
        try: return erc.functions.balanceOf(Web3.to_checksum_address(DESK)).call()/1e6
        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")
base=bal() or 0.0
log(f"TOPUP WATCH START — float ${base:,.2f}")
deadline=time.time()+6*3600
while time.time()<deadline:
    time.sleep(60)
    cur=bal()
    if cur is not None and cur>=150:
        log(f"TOP-UP landed — float now ${cur:,.2f} (+${cur-base:,.2f}). Unpause the pilot.")
        sys.exit(0)
log("TIMEOUT — no top-up in 6h")
sys.exit(3)
