#!/usr/bin/env python3
# Low-float alert on the Aurum Desk — fire BEFORE it strands a recycler. Exit 0 when USDC < $120.
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/lowfloat_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")
log(f"LOW-FLOAT WATCH START — float ${bal() or 0:,.2f}")
deadline=time.time()+12*3600
while time.time()<deadline:
    time.sleep(120)
    u=bal()
    if u is not None and u < 120:
        log(f"LOW FLOAT — ${u:,.2f} left; pilot near draining, needs a top-up before it strands a recycler")
        sys.exit(0)
log("TIMEOUT — float healthy, no alert in 12h")
sys.exit(3)
