#!/usr/bin/env bash
# Mint all 12 c* tokens on Chain 138 to the deployer.
# Usage: ./scripts/mint-all-c-star-138.sh [amount_human]
#   amount_human = 1000000 (default = 1M each). 6 decimals.
#   For cXAUC/cXAUT, amount_human is troy ounces (1 token = 1 troy oz Au), not USD.
# Requires: PRIVATE_KEY, RPC_URL_138 in .env.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"
[ -f .env ] && set -a && source .env && set +a

RPC="${RPC_URL_138:-${RPC_URL:-http://192.168.11.211:8545}}"
AMOUNT_HUMAN="${1:-1000000}"
BASE_UNITS=$((AMOUNT_HUMAN * 1000000))

[ -n "${PRIVATE_KEY:-}" ] || { echo "PRIVATE_KEY not set"; exit 1; }
DEPLOYER=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null) || exit 1

echo "=== Mint all c* on Chain 138 ==="
echo "  Deployer: $DEPLOYER  Amount: $AMOUNT_HUMAN tokens each ($BASE_UNITS base)"
echo ""

for pair in "cUSDT:0x93E66202A11B1772E55407B32B44e5Cd8eda7f22" "cUSDC:0xf22258f57794CC8E06237084b353Ab30fFfa640b" "cEURC:0x8085961F9cF02b4d800A3c6d386D31da4B34266a" "cEURT:0xdf4b71c61E5912712C1Bdd451416B9aC26949d72" "cGBPC:0x003960f16D9d34F2e98d62723B6721Fb92074aD2" "cGBPT:0x350f54e4D23795f86A9c03988c7135357CCaD97c" "cAUDC:0xD51482e567c03899eecE3CAe8a058161FD56069D" "cJPYC:0xEe269e1226a334182aace90056EE4ee5Cc8A6770" "cCHFC:0x873990849DDa5117d7C644f0aF24370797C03885" "cCADC:0x54dBd40cF05e15906A2C21f600937e96787f5679" "cXAUC:0x290E52a8819A4fbD0714E517225429aA2B70EC6b" "cXAUT:0x94e408E26c6FD8F4ee00b54dF19082FDA07dC96E"; do
  sym="${pair%%:*}"
  addr="${pair#*:}"
  echo -n "Minting $sym... "
  if cast send "$addr" "mint(address,uint256)" "$DEPLOYER" "$BASE_UNITS" --rpc-url "$RPC" --private-key "$PRIVATE_KEY" --legacy --gas-limit 100000 2>/dev/null; then echo "OK"; else echo "FAIL"; fi
done
echo "Done."
