harb/containers/bootstrap.sh

162 lines
5.1 KiB
Bash
Raw Normal View History

2025-09-24 10:57:22 +02:00
#!/usr/bin/env bash
set -euo pipefail
# Install jq if not available
if ! command -v jq >/dev/null 2>&1; then
apk add --no-cache jq >/dev/null 2>&1 || apt-get update && apt-get install -y jq >/dev/null 2>&1 || true
fi
2025-09-24 10:57:22 +02:00
ROOT_DIR=/workspace
GIT_BRANCH="${GIT_BRANCH:-}"
# Checkout branch if specified
if [[ -n "$GIT_BRANCH" ]]; then
cd "$ROOT_DIR"
git config --global --add safe.directory "$ROOT_DIR" 2>/dev/null || true
CURRENT=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
if [[ "$CURRENT" != "$GIT_BRANCH" ]]; then
echo "[bootstrap] Switching to branch: $GIT_BRANCH"
# Try local branch first, then remote
if git rev-parse --verify "$GIT_BRANCH" >/dev/null 2>&1; then
git checkout "$GIT_BRANCH" 2>/dev/null || echo "[bootstrap] WARNING: Could not checkout $GIT_BRANCH"
else
git fetch origin "$GIT_BRANCH" 2>/dev/null || true
git checkout "$GIT_BRANCH" 2>/dev/null || echo "[bootstrap] WARNING: Could not checkout $GIT_BRANCH"
fi
fi
fi
STATE_DIR=$ROOT_DIR/tmp/containers
2025-09-24 10:57:22 +02:00
LOG_DIR=$STATE_DIR/logs
SETUP_LOG=$LOG_DIR/setup.log
MNEMONIC_FILE=$ROOT_DIR/onchain/.secret.local
mkdir -p "$LOG_DIR"
: >"$SETUP_LOG"
# ── Configure shared bootstrap variables ──
2025-09-24 10:57:22 +02:00
ANVIL_RPC=${ANVIL_RPC:-"http://anvil:8545"}
CONTRACT_ENV=$STATE_DIR/contracts.env
LOG_FILE=$SETUP_LOG
ONCHAIN_DIR=$ROOT_DIR/onchain
2025-09-24 10:57:22 +02:00
TXNBOT_FUND_VALUE=${TXNBOT_FUND_VALUE:-1ether}
# Source shared bootstrap functions
# shellcheck source=../scripts/bootstrap-common.sh
source "$ROOT_DIR/scripts/bootstrap-common.sh"
# ── Local-only helpers ─────────────────────────────────────────────────
2025-09-24 10:57:22 +02:00
maybe_set_deployer_from_mnemonic() {
if [[ -n "$DEPLOYER_PK" && "$DEPLOYER_PK" != "$DEFAULT_DEPLOYER_PK" ]]; then
2025-09-24 10:57:22 +02:00
return
fi
if [[ -f "$MNEMONIC_FILE" ]]; then
local mnemonic pk addr
mnemonic="$(tr -d '\n\r' <"$MNEMONIC_FILE")"
if [[ -n "$mnemonic" ]]; then
pk="$(cast wallet private-key --mnemonic "$mnemonic" --mnemonic-derivation-path "m/44'/60'/0'/0/0")"
addr="$(cast wallet address --private-key "$pk")"
DEPLOYER_PK=${pk}
DEPLOYER_ADDR=${addr}
2025-09-24 10:57:22 +02:00
fi
fi
}
derive_txnbot_wallet() {
if [[ -f "$MNEMONIC_FILE" ]]; then
local mnemonic
mnemonic="$(tr -d '\n\r' <"$MNEMONIC_FILE")"
if [[ -n "$mnemonic" ]]; then
TXNBOT_PRIVATE_KEY="$(cast wallet private-key --mnemonic "$mnemonic" --mnemonic-index 2)"
TXNBOT_ADDRESS="$(cast wallet address --private-key "$TXNBOT_PRIVATE_KEY")"
bootstrap_log "Derived txnBot wallet: $TXNBOT_ADDRESS (account index 2)"
return
fi
fi
# Fallback to hardcoded Anvil account 1
TXNBOT_PRIVATE_KEY=$DEFAULT_TXNBOT_PK
TXNBOT_ADDRESS=$DEFAULT_TXNBOT_ADDR
bootstrap_log "Using default txnBot wallet: $TXNBOT_ADDRESS"
2025-10-07 21:57:32 +00:00
}
2025-09-24 10:57:22 +02:00
write_ponder_env() {
cat >"$ROOT_DIR/services/ponder/.env.local" <<EOPONDER
PONDER_NETWORK=BASE_SEPOLIA_LOCAL_FORK
KRAIKEN_ADDRESS=$KRAIKEN
STAKE_ADDRESS=$STAKE
START_BLOCK=$DEPLOY_BLOCK
PONDER_RPC_URL_BASE_SEPOLIA_LOCAL_FORK=$ANVIL_RPC
DATABASE_URL=postgresql://ponder:ponder_local@postgres:5432/ponder_local
DATABASE_SCHEMA=ponder_local_${DEPLOY_BLOCK}
2025-09-24 10:57:22 +02:00
EOPONDER
}
write_txn_bot_env() {
local txnbot_env=$STATE_DIR/txnBot.env
2025-10-11 10:55:49 +00:00
local provider_url=${TXNBOT_PROVIDER_URL:-$ANVIL_RPC}
local graphql_endpoint=${TXNBOT_GRAPHQL_ENDPOINT:-http://ponder:42069/graphql}
cat >"$txnbot_env" <<EOTXNBOT
2025-09-24 10:57:22 +02:00
ENVIRONMENT=BASE_SEPOLIA_LOCAL_FORK
2025-10-11 10:55:49 +00:00
PROVIDER_URL=$provider_url
2025-09-24 10:57:22 +02:00
PRIVATE_KEY=$TXNBOT_PRIVATE_KEY
LM_CONTRACT_ADDRESS=$LIQUIDITY_MANAGER
STAKE_CONTRACT_ADDRESS=$STAKE
2025-10-11 10:55:49 +00:00
GRAPHQL_ENDPOINT=$graphql_endpoint
2025-09-24 10:57:22 +02:00
WALLET_ADDRESS=$TXNBOT_ADDRESS
PORT=43069
EOTXNBOT
}
prime_chain() {
bootstrap_log "Pre-mining 5 blocks (minimal warmup for fast Ponder sync)..."
if cast rpc --rpc-url "$ANVIL_RPC" anvil_mine "0x5" "0x1" >/dev/null 2>&1; then
bootstrap_log "Mined 5 blocks"
else
bootstrap_log "Batch mining failed, using individual evm_mine calls"
for i in {1..5}; do
cast rpc --rpc-url "$ANVIL_RPC" evm_mine >/dev/null 2>&1 || true
done
2025-09-24 10:57:22 +02:00
fi
bootstrap_log "Pre-mining complete"
2025-09-24 10:57:22 +02:00
}
# ── Main ───────────────────────────────────────────────────────────────
2025-09-24 10:57:22 +02:00
main() {
local start_time
start_time=$(date +%s%3N)
bootstrap_log "Waiting for Anvil"
2025-09-24 10:57:22 +02:00
wait_for_rpc
maybe_set_deployer_from_mnemonic
derive_txnbot_wallet
2025-09-24 10:57:22 +02:00
run_forge_script
extract_addresses
write_contracts_env
2025-09-24 10:57:22 +02:00
fund_liquidity_manager
call_recenter
seed_application_state
2025-10-07 21:57:32 +00:00
write_deployments_json
2025-09-24 10:57:22 +02:00
write_ponder_env
write_txn_bot_env
fund_txn_bot_wallet
prime_chain &
local prime_pid=$!
wait "$prime_pid"
local end_time
end_time=$(date +%s%3N)
local elapsed_ms=$((end_time - start_time))
local elapsed_sec
elapsed_sec=$(awk -v ms="$elapsed_ms" 'BEGIN { printf "%.3f", ms/1000 }')
bootstrap_log "Bootstrap complete in ${elapsed_sec}s"
bootstrap_log "Kraiken: $KRAIKEN"
bootstrap_log "Stake: $STAKE"
bootstrap_log "LiquidityManager: $LIQUIDITY_MANAGER"
bootstrap_log "txnBot: $TXNBOT_ADDRESS"
2025-09-24 10:57:22 +02:00
}
main "$@"