#!/usr/bin/env bash # Chain 138 path for deployer gas auto-route: try token-aggregation quote for cUSDT/cUSDC → WETH. # If no c*→WETH pool exists (current state), output "use genesis/validator only". # Uses config/deployer-gas-routes.json for 138 entry (tokenAggregationBaseUrl, token addresses). # # Usage: # ./scripts/deployment/chain138-tokens-to-gas.sh [--dry-run] [--amount-raw WEI] # TOKEN_AGGREGATION_URL=https://dbis-api.d-bis.org/api/v1 ./scripts/deployment/chain138-tokens-to-gas.sh # # Requires: curl, jq. Optional: RPC_URL_138, PRIVATE_KEY for execute path (not implemented in v1). set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" CONFIG="${PROJECT_ROOT}/config/deployer-gas-routes.json" DRY_RUN=false AMOUNT_RAW="${AMOUNT_RAW:-1000000000}" # Default: 1000 USDT (6 decimals) in raw units if [[ -f "$CONFIG" ]]; then BASE_URL="${TOKEN_AGGREGATION_URL:-$(jq -r '.chains[] | select(.chainId==138) | .tokenAggregationBaseUrl // empty' "$CONFIG")}" FALLBACK_URL="$(jq -r '.chains[] | select(.chainId==138) | .tokenAggregationFallbackUrl // empty' "$CONFIG")" CUSDT="$(jq -r '.chains[] | select(.chainId==138) | .cusdt // empty' "$CONFIG")" CUSDC="$(jq -r '.chains[] | select(.chainId==138) | .cusdc // empty' "$CONFIG")" WETH9="$(jq -r '.chains[] | select(.chainId==138) | .weth9 // empty' "$CONFIG")" fi BASE_URL="${BASE_URL:-https://dbis-api.d-bis.org/api/v1}" CUSDT="${CUSDT:-0x93E66202A11B1772E55407B32B44e5Cd8eda7f22}" CUSDC="${CUSDC:-0xf22258f57794CC8E06237084b353Ab30fFfa640b}" WETH9="${WETH9:-0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2}" for arg in "$@"; do case "$arg" in --dry-run) DRY_RUN=true ;; --amount-raw=*) AMOUNT_RAW="${arg#*=}" ;; esac done # Try quote cUSDT -> WETH quote_cusdt_weth() { local url="${1:-$BASE_URL}" curl -sS --connect-timeout 5 "${url}/quote?chainId=138&tokenIn=${CUSDT}&tokenOut=${WETH9}&amountIn=${AMOUNT_RAW}" 2>/dev/null || echo '{"amountOut":null,"error":"request failed"}' } # Try quote cUSDC -> WETH quote_cusdc_weth() { local url="${1:-$BASE_URL}" curl -sS --connect-timeout 5 "${url}/quote?chainId=138&tokenIn=${CUSDC}&tokenOut=${WETH9}&amountIn=${AMOUNT_RAW}" 2>/dev/null || echo '{"amountOut":null,"error":"request failed"}' } echo "Chain 138 path: tokens → gas (WETH)" echo " Token-aggregation: $BASE_URL" echo " Amount (raw): $AMOUNT_RAW" echo "" res_cusdt="$(quote_cusdt_weth)" res_cusdc="$(quote_cusdc_weth)" amount_out_cusdt="$(echo "$res_cusdt" | jq -r '.amountOut // empty' 2>/dev/null)" amount_out_cusdc="$(echo "$res_cusdc" | jq -r '.amountOut // empty' 2>/dev/null)" err_cusdt="$(echo "$res_cusdt" | jq -r '.error // empty' 2>/dev/null)" err_cusdc="$(echo "$res_cusdc" | jq -r '.error // empty' 2>/dev/null)" if [[ -n "$amount_out_cusdt" && "$amount_out_cusdt" != "null" && "$amount_out_cusdt" -gt 0 ]] 2>/dev/null; then echo " cUSDT → WETH: quote available (amountOut=$amount_out_cusdt). Pool exists; use DODO PMM swap then optional unwrap." echo " Action: build DODO swap tx (swapCUSDTFor* to WETH pool) and optional WETH9.withdraw; see DEX_AND_AGGREGATORS_CHAIN138_EXPLAINER.md." exit 0 fi if [[ -n "$amount_out_cusdc" && "$amount_out_cusdc" != "null" && "$amount_out_cusdc" -gt 0 ]] 2>/dev/null; then echo " cUSDC → WETH: quote available (amountOut=$amount_out_cusdc). Pool exists; use DODO PMM swap then optional unwrap." echo " Action: build DODO swap tx (swapCUSDCFor* to WETH pool) and optional WETH9.withdraw; see DEX_AND_AGGREGATORS_CHAIN138_EXPLAINER.md." exit 0 fi # Fallback URL if primary failed with network error if [[ -z "$amount_out_cusdt" && -z "$amount_out_cusdc" && -n "$FALLBACK_URL" ]]; then res_cusdt="$(quote_cusdt_weth "$FALLBACK_URL")" amount_out_cusdt="$(echo "$res_cusdt" | jq -r '.amountOut // empty' 2>/dev/null)" if [[ -n "$amount_out_cusdt" && "$amount_out_cusdt" != "null" && "$amount_out_cusdt" -gt 0 ]] 2>/dev/null; then echo " cUSDT → WETH: quote available (fallback URL). Pool exists." exit 0 fi fi echo " No cUSDT/cUSDC → WETH pool found on Chain 138 (token-aggregation returned no quote)." echo " Chain 138 gas: use genesis alloc or validator transfer. See FUNDING_AND_DEPLOYMENT_CHECKLIST.md." echo " Output: method=genesis_or_validator" exit 0