forked from Netwalls/BOXMEOUT_STELLA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·371 lines (324 loc) · 12.9 KB
/
deploy.sh
File metadata and controls
executable file
·371 lines (324 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash
# =============================================================================
# BoxMeOut Stella - Soroban Contract Deployment Script
# =============================================================================
# Builds, optimizes, deploys, and initializes all 5 contracts.
# Outputs deployed contract addresses to .env.contracts
#
# Usage:
# ./deploy.sh testnet # Deploy to testnet (default)
# ./deploy.sh mainnet # Deploy to mainnet (with confirmation)
# ./deploy.sh testnet --skip-build # Skip build step (use existing WASMs)
# ./deploy.sh testnet --only-init # Skip deploy, only initialize contracts
#
# Prerequisites:
# - stellar CLI installed (https://soroban.stellar.org/docs/getting-started/setup)
# - Rust + wasm32-unknown-unknown target installed
# - An identity configured: stellar keys generate <name> --network testnet
# =============================================================================
set -euo pipefail
# ---- Colors & Formatting ----
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
BOLD='\033[1m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_step() { echo -e "\n${BOLD}${CYAN}==> $1${NC}"; }
# ---- Script Directory ----
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CONTRACT_DIR="$SCRIPT_DIR/contracts/contracts/boxmeout"
WASM_DIR="$CONTRACT_DIR/target/wasm32-unknown-unknown/release"
ENV_FILE="$SCRIPT_DIR/.env.contracts"
# ---- Contracts ----
CONTRACTS=("oracle" "factory" "treasury" "amm" "market")
# ---- Parse Arguments ----
NETWORK="${1:-testnet}"
SKIP_BUILD=false
ONLY_INIT=false
for arg in "$@"; do
case $arg in
--skip-build) SKIP_BUILD=true ;;
--only-init) ONLY_INIT=true; SKIP_BUILD=true ;;
testnet|mainnet) NETWORK="$arg" ;;
esac
done
# ---- Network Configuration ----
case "$NETWORK" in
testnet)
RPC_URL="https://soroban-testnet.stellar.org"
NETWORK_PASSPHRASE="Test SDF Network ; September 2015"
HORIZON_URL="https://horizon-testnet.stellar.org"
;;
mainnet)
RPC_URL="https://soroban-mainnet.stellar.org"
NETWORK_PASSPHRASE="Public Global Stellar Network ; September 2015"
HORIZON_URL="https://horizon.stellar.org"
;;
*)
log_error "Unknown network: $NETWORK (use 'testnet' or 'mainnet')"
exit 1
;;
esac
# ---- Deployment Parameters (override via environment) ----
# Identity / source account for signing transactions
SOURCE_IDENTITY="${SOURCE_IDENTITY:-deployer}"
# Oracle configuration
ORACLE_REQUIRED_CONSENSUS="${ORACLE_REQUIRED_CONSENSUS:-2}"
# AMM configuration (max liquidity cap in stroops, default 1,000,000 USDC = 10^13 stroops)
AMM_MAX_LIQUIDITY_CAP="${AMM_MAX_LIQUIDITY_CAP:-10000000000000}"
# USDC token address (MUST be set for mainnet)
USDC_TOKEN_ADDRESS="${USDC_TOKEN_ADDRESS:-}"
# ---- Pre-flight Checks ----
log_step "Pre-flight Checks"
# Check stellar CLI
if ! command -v stellar &> /dev/null; then
log_error "stellar CLI not found. Install from https://soroban.stellar.org/docs/getting-started/setup"
exit 1
fi
log_success "stellar CLI found: $(stellar --version 2>/dev/null || echo 'unknown version')"
# Check source identity exists
if ! stellar keys address "$SOURCE_IDENTITY" &> /dev/null 2>&1; then
log_error "Identity '$SOURCE_IDENTITY' not found."
echo ""
echo " Create one with:"
echo " stellar keys generate $SOURCE_IDENTITY --network $NETWORK"
echo ""
echo " Or set SOURCE_IDENTITY to an existing identity:"
echo " SOURCE_IDENTITY=mykey ./deploy.sh $NETWORK"
exit 1
fi
ADMIN_ADDRESS=$(stellar keys address "$SOURCE_IDENTITY")
log_success "Source identity: $SOURCE_IDENTITY ($ADMIN_ADDRESS)"
log_info "Network: $NETWORK ($RPC_URL)"
# ---- Mainnet Safety Gate ----
if [ "$NETWORK" = "mainnet" ]; then
echo ""
log_warn "========================================="
log_warn " MAINNET DEPLOYMENT"
log_warn "========================================="
log_warn "Admin address: $ADMIN_ADDRESS"
log_warn "This will deploy contracts to MAINNET."
log_warn "Real funds will be used for fees."
echo ""
read -rp "Type 'DEPLOY TO MAINNET' to confirm: " confirmation
if [ "$confirmation" != "DEPLOY TO MAINNET" ]; then
log_error "Deployment cancelled."
exit 1
fi
echo ""
if [ -z "$USDC_TOKEN_ADDRESS" ]; then
log_error "USDC_TOKEN_ADDRESS must be set for mainnet deployment."
echo " Export it before running: export USDC_TOKEN_ADDRESS=C..."
exit 1
fi
fi
# ---- USDC Token Setup ----
log_step "USDC Token Configuration"
if [ -n "$USDC_TOKEN_ADDRESS" ]; then
log_success "Using provided USDC address: $USDC_TOKEN_ADDRESS"
else
if [ "$NETWORK" = "testnet" ]; then
log_info "No USDC_TOKEN_ADDRESS set. Deploying a test token on testnet..."
USDC_TOKEN_ADDRESS=$(stellar contract asset deploy \
--asset "USDC:$ADMIN_ADDRESS" \
--source "$SOURCE_IDENTITY" \
--network "$NETWORK" 2>&1 | tail -1)
log_success "Test USDC token deployed: $USDC_TOKEN_ADDRESS"
else
log_error "USDC_TOKEN_ADDRESS is required for mainnet."
exit 1
fi
fi
# ---- Step 1: Build Contracts ----
if [ "$SKIP_BUILD" = false ]; then
log_step "Step 1: Building Contracts"
bash "$SCRIPT_DIR/build_contracts.sh"
echo ""
# Optimize WASMs
log_info "Optimizing WASM files..."
for contract in "${CONTRACTS[@]}"; do
wasm_file="$WASM_DIR/${contract}.wasm"
if [ -f "$wasm_file" ]; then
stellar contract optimize --wasm "$wasm_file" 2>/dev/null || true
size=$(wc -c < "$wasm_file" | tr -d ' ')
log_success "Optimized ${contract}.wasm (${size} bytes)"
else
log_error "Missing ${contract}.wasm - build may have failed"
exit 1
fi
done
else
log_step "Step 1: Build (SKIPPED)"
# Verify WASMs exist
for contract in "${CONTRACTS[@]}"; do
if [ ! -f "$WASM_DIR/${contract}.wasm" ]; then
log_error "Missing ${contract}.wasm. Run without --skip-build first."
exit 1
fi
done
log_success "All WASM files present"
fi
# ---- Step 2: Deploy Contracts ----
declare -A CONTRACT_IDS
if [ "$ONLY_INIT" = false ]; then
log_step "Step 2: Deploying Contracts to $NETWORK"
for contract in "${CONTRACTS[@]}"; do
log_info "Deploying ${contract}..."
wasm_file="$WASM_DIR/${contract}.wasm"
contract_id=$(stellar contract deploy \
--wasm "$wasm_file" \
--source "$SOURCE_IDENTITY" \
--network "$NETWORK" \
2>&1 | tail -1)
if [ -z "$contract_id" ] || [[ "$contract_id" == *"error"* ]]; then
log_error "Failed to deploy ${contract}: $contract_id"
exit 1
fi
CONTRACT_IDS[$contract]="$contract_id"
log_success "${contract} deployed: $contract_id"
done
else
log_step "Step 2: Deploy (SKIPPED - loading from $ENV_FILE)"
if [ ! -f "$ENV_FILE" ]; then
log_error "No $ENV_FILE found. Deploy contracts first (run without --only-init)."
exit 1
fi
# shellcheck source=/dev/null
source "$ENV_FILE"
CONTRACT_IDS[oracle]="${ORACLE_CONTRACT_ADDRESS:?Missing ORACLE_CONTRACT_ADDRESS in $ENV_FILE}"
CONTRACT_IDS[factory]="${FACTORY_CONTRACT_ADDRESS:?Missing FACTORY_CONTRACT_ADDRESS in $ENV_FILE}"
CONTRACT_IDS[treasury]="${TREASURY_CONTRACT_ADDRESS:?Missing TREASURY_CONTRACT_ADDRESS in $ENV_FILE}"
CONTRACT_IDS[amm]="${AMM_CONTRACT_ADDRESS:?Missing AMM_CONTRACT_ADDRESS in $ENV_FILE}"
CONTRACT_IDS[market]="${MARKET_CONTRACT_ADDRESS:?Missing MARKET_CONTRACT_ADDRESS in $ENV_FILE}"
log_success "Loaded contract addresses from $ENV_FILE"
fi
# ---- Step 3: Write Contract Addresses ----
log_step "Step 3: Saving Contract Addresses"
cat > "$ENV_FILE" <<EOF
# BoxMeOut Stella - Deployed Contract Addresses
# Network: $NETWORK
# Deployed: $(date -u '+%Y-%m-%d %H:%M:%S UTC')
# Admin: $ADMIN_ADDRESS
# Stellar Network
STELLAR_NETWORK=$NETWORK
STELLAR_HORIZON_URL=$HORIZON_URL
STELLAR_SOROBAN_RPC_URL=$RPC_URL
# Contract Addresses
ORACLE_CONTRACT_ADDRESS=${CONTRACT_IDS[oracle]}
FACTORY_CONTRACT_ADDRESS=${CONTRACT_IDS[factory]}
TREASURY_CONTRACT_ADDRESS=${CONTRACT_IDS[treasury]}
AMM_CONTRACT_ADDRESS=${CONTRACT_IDS[amm]}
MARKET_CONTRACT_ADDRESS=${CONTRACT_IDS[market]}
USDC_TOKEN_ADDRESS=$USDC_TOKEN_ADDRESS
# Admin
ADMIN_ADDRESS=$ADMIN_ADDRESS
EOF
log_success "Addresses saved to $ENV_FILE"
# ---- Step 4: Initialize Contracts ----
log_step "Step 4: Initializing Contracts"
# 4a. Initialize Oracle (no cross-contract deps)
log_info "Initializing Oracle (consensus=$ORACLE_REQUIRED_CONSENSUS)..."
stellar contract invoke \
--id "${CONTRACT_IDS[oracle]}" \
--source "$SOURCE_IDENTITY" \
--network "$NETWORK" \
-- \
initialize \
--admin "$ADMIN_ADDRESS" \
--required_consensus "$ORACLE_REQUIRED_CONSENSUS"
log_success "Oracle initialized"
# 4b. Initialize Factory (needs USDC + Treasury address)
log_info "Initializing Factory..."
stellar contract invoke \
--id "${CONTRACT_IDS[factory]}" \
--source "$SOURCE_IDENTITY" \
--network "$NETWORK" \
-- \
initialize \
--admin "$ADMIN_ADDRESS" \
--usdc "$USDC_TOKEN_ADDRESS" \
--treasury "${CONTRACT_IDS[treasury]}"
log_success "Factory initialized"
# 4c. Initialize Treasury (needs USDC + Factory address)
log_info "Initializing Treasury..."
stellar contract invoke \
--id "${CONTRACT_IDS[treasury]}" \
--source "$SOURCE_IDENTITY" \
--network "$NETWORK" \
-- \
initialize \
--admin "$ADMIN_ADDRESS" \
--usdc_contract "$USDC_TOKEN_ADDRESS" \
--factory "${CONTRACT_IDS[factory]}"
log_success "Treasury initialized"
# 4d. Initialize AMM (needs Factory + USDC)
log_info "Initializing AMM (max_liquidity_cap=$AMM_MAX_LIQUIDITY_CAP)..."
stellar contract invoke \
--id "${CONTRACT_IDS[amm]}" \
--source "$SOURCE_IDENTITY" \
--network "$NETWORK" \
-- \
initialize \
--admin "$ADMIN_ADDRESS" \
--factory "${CONTRACT_IDS[factory]}" \
--usdc_token "$USDC_TOKEN_ADDRESS" \
--max_liquidity_cap "$AMM_MAX_LIQUIDITY_CAP"
log_success "AMM initialized"
# 4e. Market contract - deployed but initialized per-market via Factory
log_info "Market contract deployed (initialized per-market via Factory.create_market)"
log_success "Market WASM ready for Factory to instantiate"
# ---- Step 5: Update Backend .env ----
log_step "Step 5: Backend Environment"
BACKEND_ENV="$SCRIPT_DIR/backend/.env"
if [ -f "$BACKEND_ENV" ]; then
log_info "Updating $BACKEND_ENV with contract addresses..."
update_env_var() {
local key="$1" val="$2" file="$3"
if grep -q "^${key}=" "$file" 2>/dev/null; then
sed -i.bak "s|^${key}=.*|${key}=${val}|" "$file"
else
echo "${key}=${val}" >> "$file"
fi
}
update_env_var "STELLAR_NETWORK" "$NETWORK" "$BACKEND_ENV"
update_env_var "STELLAR_HORIZON_URL" "$HORIZON_URL" "$BACKEND_ENV"
update_env_var "STELLAR_SOROBAN_RPC_URL" "$RPC_URL" "$BACKEND_ENV"
update_env_var "FACTORY_CONTRACT_ADDRESS" "${CONTRACT_IDS[factory]}" "$BACKEND_ENV"
update_env_var "MARKET_CONTRACT_ADDRESS" "${CONTRACT_IDS[market]}" "$BACKEND_ENV"
update_env_var "TREASURY_CONTRACT_ADDRESS" "${CONTRACT_IDS[treasury]}" "$BACKEND_ENV"
update_env_var "ORACLE_CONTRACT_ADDRESS" "${CONTRACT_IDS[oracle]}" "$BACKEND_ENV"
update_env_var "AMM_CONTRACT_ADDRESS" "${CONTRACT_IDS[amm]}" "$BACKEND_ENV"
update_env_var "USDC_TOKEN_ADDRESS" "$USDC_TOKEN_ADDRESS" "$BACKEND_ENV"
# Clean up sed backup files
rm -f "${BACKEND_ENV}.bak"
log_success "Backend .env updated"
else
log_warn "No backend/.env found. Copy backend/.env.example and re-run with --only-init,"
log_warn "or manually copy values from $ENV_FILE"
fi
# ---- Summary ----
log_step "Deployment Complete!"
echo ""
echo -e "${BOLD}Network:${NC} $NETWORK"
echo -e "${BOLD}Admin:${NC} $ADMIN_ADDRESS"
echo -e "${BOLD}Env file:${NC} $ENV_FILE"
echo ""
echo -e "${BOLD}Contract Addresses:${NC}"
echo -e " Oracle: ${GREEN}${CONTRACT_IDS[oracle]}${NC}"
echo -e " Factory: ${GREEN}${CONTRACT_IDS[factory]}${NC}"
echo -e " Treasury: ${GREEN}${CONTRACT_IDS[treasury]}${NC}"
echo -e " AMM: ${GREEN}${CONTRACT_IDS[amm]}${NC}"
echo -e " Market: ${GREEN}${CONTRACT_IDS[market]}${NC}"
echo -e " USDC: ${GREEN}${USDC_TOKEN_ADDRESS}${NC}"
echo ""
echo -e "${BOLD}Next Steps:${NC}"
echo " 1. Verify contracts: stellar contract invoke --id <CONTRACT_ID> --network $NETWORK -- <function>"
echo " 2. Create a market: stellar contract invoke --id ${CONTRACT_IDS[factory]} --network $NETWORK -- create_market ..."
echo " 3. Start backend: cd backend && npm start"
echo ""