Files
proxmox/scripts/deployment/apply-mainnet-cwusdc-usdc-peg-tranche-from-wallet.sh
defiQUG dbd517b279 Sync workspace: config, docs, scripts, CI, operator rules, and submodule pointers.
- Update dbis_core, cross-chain-pmm-lps, explorer-monorepo, metamask-integration, pr-workspace/chains
- Omit embedded publish git dirs and empty placeholders from index

Made-with: Cursor
2026-04-12 06:12:20 -07:00

171 lines
5.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Apply one on-chain liquidity tranche toward Mainnet cWUSDC/USDC 1:1 vault peg
# using the deployer wallet (PRIVATE_KEY). Wraps add-mainnet-public-dodo-cw-liquidity.sh.
#
# Requires: ETHEREUM_MAINNET_RPC, PRIVATE_KEY, DODO_PMM_INTEGRATION_MAINNET
# Amounts are chosen like plan-mainnet-cwusdc-usdc-rebalance-liquidity.sh:
# - prefer largest asymmetric peg tranche when base_reserve > quote_reserve
# - else matched 1:1 deepen (min wallet cWUSDC, wallet USDC)
#
# Usage (from repo root):
# source scripts/lib/load-project-env.sh
# bash scripts/deployment/apply-mainnet-cwusdc-usdc-peg-tranche-from-wallet.sh --dry-run
# bash scripts/deployment/apply-mainnet-cwusdc-usdc-peg-tranche-from-wallet.sh --apply
#
# Exit:
# 0 — dry-run or live add submitted
# 3 — wallet cannot fund any tranche (print funding hint)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# shellcheck disable=SC1091
source "${PROJECT_ROOT}/scripts/lib/load-project-env.sh" 2>/dev/null || true
# shellcheck disable=SC1091
source "${PROJECT_ROOT}/smom-dbis-138/scripts/load-env.sh" >/dev/null 2>&1 || true
MODE="dry-run"
for a in "$@"; do
case "$a" in
--dry-run) MODE=dry-run ;;
--apply) MODE=apply ;;
--help|-h)
sed -n '1,25p' "$0"
exit 0
;;
*)
echo "[fail] unknown arg: $a (use --dry-run or --apply)" >&2
exit 2
;;
esac
done
require_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "[fail] missing required command: $1" >&2
exit 1
}
}
require_cmd cast
require_cmd python3
RPC_URL="${ETHEREUM_MAINNET_RPC:-}"
PRIVATE_KEY="${PRIVATE_KEY:-}"
INTEGRATION="${DODO_PMM_INTEGRATION_MAINNET:-}"
CWUSDC="${CWUSDC_MAINNET:-0x2de5F116bFcE3d0f922d9C8351e0c5Fc24b9284a}"
USDC="0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
POOL="${POOL_CWUSDC_USDC_MAINNET:-0x69776fc607e9edA8042e320e7e43f54d06c68f0E}"
if [[ -z "$RPC_URL" || -z "$PRIVATE_KEY" || -z "$INTEGRATION" ]]; then
echo "[fail] ETHEREUM_MAINNET_RPC, PRIVATE_KEY, and DODO_PMM_INTEGRATION_MAINNET are required" >&2
exit 1
fi
if [[ -n "$INTEGRATION" ]]; then
mapped_pool="$(cast call "$INTEGRATION" 'pools(address,address)(address)' "$CWUSDC" "$USDC" --rpc-url "$RPC_URL" 2>/dev/null | awk '{print $1}' || true)"
if [[ -n "$mapped_pool" && "$mapped_pool" != "0x0000000000000000000000000000000000000000" && "${mapped_pool,,}" != "${POOL,,}" ]]; then
POOL="$mapped_pool"
fi
fi
dep="$(cast wallet address --private-key "$PRIVATE_KEY")"
out="$(cast call "$POOL" 'getVaultReserve()(uint256,uint256)' --rpc-url "$RPC_URL")"
base_r="$(printf '%s\n' "$out" | sed -n '1p' | awk '{print $1}')"
quote_r="$(printf '%s\n' "$out" | sed -n '2p' | awk '{print $1}')"
wb="$(cast call "$CWUSDC" 'balanceOf(address)(uint256)' "$dep" --rpc-url "$RPC_URL" | awk '{print $1}')"
wq="$(cast call "$USDC" 'balanceOf(address)(uint256)' "$dep" --rpc-url "$RPC_URL" | awk '{print $1}')"
if (( base_r > quote_r )); then
G=$(( base_r - quote_r ))
else
G=$(( quote_r - base_r ))
fi
read -r opt_b opt_q <<<"$(
python3 - "$wb" "$wq" "$base_r" "$quote_r" <<'PY'
import sys
wb, wq, br, qr = map(int, sys.argv[1:5])
if br > qr:
g = br - qr
if g <= 0:
print("0 0")
else:
cap = min(wb, wq - g) if wq > g else 0
if cap < 1:
print("0 0")
else:
print(cap, cap + g)
else:
g = qr - br
if g <= 0:
print("0 0")
else:
cap = min(wq, wb - g) if wb > g else 0
if cap < 1:
print("0 0")
else:
print(cap + g, cap)
PY
)"
BASE_AMOUNT=""
QUOTE_AMOUNT=""
if (( opt_b >= 1 && opt_q >= 1 )); then
BASE_AMOUNT="$opt_b"
QUOTE_AMOUNT="$opt_q"
else
matched="$(python3 - "$wb" "$wq" <<'PY'
import sys
print(min(int(sys.argv[1]), int(sys.argv[2])))
PY
)"
if (( matched >= 1 )); then
BASE_AMOUNT="$matched"
QUOTE_AMOUNT="$matched"
fi
fi
to_human() {
python3 - "$1" <<'PY'
import sys
print(f"{int(sys.argv[1]) / 1_000_000:.6f}")
PY
}
echo "=== apply-mainnet-cwusdc-usdc-peg-tranche-from-wallet ($MODE) ==="
echo "deployer=$dep"
echo "pool=$POOL"
echo "reserve_base=$(to_human "$base_r") cWUSDC reserve_quote=$(to_human "$quote_r") USDC"
echo "wallet_cWUSDC=$(to_human "$wb") wallet_USDC=$(to_human "$wq")"
if [[ -z "$BASE_AMOUNT" || -z "$QUOTE_AMOUNT" ]]; then
echo
echo "[stop] No affordable tranche: need USDC on deployer for peg-close (asymmetric needs USDC_raw > gap when base-heavy)."
echo " gap_raw=$G human=$(to_human "$G") USDC-side deficit vs cWUSDC reserve"
echo " Send USDC (and keep cWUSDC) to: $dep"
echo " Then: bash scripts/deployment/apply-mainnet-cwusdc-usdc-peg-tranche-from-wallet.sh --dry-run"
echo " Flash path (no wallet USDC): see scripts/deployment/plan-mainnet-cwusdc-flash-quote-push-rebalance.sh"
exit 3
fi
echo "chosen_base_amount_raw=$BASE_AMOUNT chosen_quote_amount_raw=$QUOTE_AMOUNT"
echo "human_base=$(to_human "$BASE_AMOUNT") human_quote=$(to_human "$QUOTE_AMOUNT")"
ADD_ARGS=(
"${PROJECT_ROOT}/scripts/deployment/add-mainnet-public-dodo-cw-liquidity.sh"
"--pair=cwusdc-usdc"
"--base-amount=$BASE_AMOUNT"
"--quote-amount=$QUOTE_AMOUNT"
)
if [[ "$MODE" == "dry-run" ]]; then
ADD_ARGS+=("--dry-run")
else
ADD_ARGS+=("--execute")
fi
exec bash "${ADD_ARGS[@]}"