#!/usr/bin/env bash
# [LITCOIN docs note] Example contributed by EcoWealth (vealth.net), hosted verbatim below this line. All wallet addresses and server paths are EcoWealth's own production values - replace with YOURS before any use.
# Bankr staked-LIT rewards claim rail (operator standing GO 2026-07-06:
# "litcoin bankr stake yield claim was supposed to be done too, once every 2
# days or so"). The 565M tier-4 stake accrues ~8.8k LIT/day; claiming is FREE
# (coordinator-assisted, no cooldown/penalty — MONEY_FLOW_RUNBOOKS §3). Claim
# when >= 15k has accrued, then send ALL wallet LIT to the Zora signer so the
# 4h micro-router turns it into ECO. Staked principal is untouchable by this
# (it lives in the staking proxy, not the wallet). Miner 0xc91 stays separate.
# Crontab: 10 6 */2 * *  (backup taken before install)
set -uo pipefail
export PATH="/usr/bin:/usr/local/bin:$PATH"
cd /home/ubuntu/ecowealth || exit 1
LOG="logs/bankr-lit-claim.log"; mkdir -p logs
TS="$(date -u +%Y-%m-%dT%H-%M-%SZ)"
W="0x538aa4800ae2bd8e90556899514376ab96113a8e"
{
  echo "===== bankr-lit-claim $TS ====="
  CLAIMABLE=$(curl -s "https://api.litcoin.app/v1/claims/status?wallet=$W" | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>{try{console.log(Math.floor(Number(JSON.parse(d).claimable)/1e18))}catch{console.log(0)}})')
  echo "claimable: ${CLAIMABLE:-0} LIT"
  if [ "${CLAIMABLE:-0}" -ge 15000 ]; then
    echo "claiming via /v1/claims/bankr..."
    curl -s -X POST "https://api.litcoin.app/v1/claims/bankr" -H "Content-Type: application/json" -d "{\"wallet\":\"$W\"}" | head -c 300; echo
    sleep 60
    key=$(grep '^BANKR_WALLET_API_KEY=' .env | cut -d= -f2-)
    bankr login --api-key "$key" >/dev/null 2>&1 || echo "WARN: bankr login failed"
    bankr agent "Send all of my LITCOIN tokens on Base to 0x034F29D2CCFf01551304ad92d2599B850D076c89" || echo "WARN: send failed"
  else
    echo "below 15k threshold — skip (accrues ~8.8k/day; next check in 2 days)"
  fi
  echo "===== end $TS ====="
} >> "$LOG" 2>&1
