-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcli.sh
More file actions
executable file
·252 lines (227 loc) · 8.72 KB
/
Copy pathcli.sh
File metadata and controls
executable file
·252 lines (227 loc) · 8.72 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
#!/usr/bin/env bash
# =============================================================================
# StellarSettle – Interactive Testnet Setup & Multi-Contract Deploy CLI (#453)
# =============================================================================
#
# Wraps scripts/deploy.sh, scripts/package-wasm.sh, and scripts/smoke-test.sh
# behind a single entrypoint. With no arguments and a TTY, shows an interactive
# menu; otherwise dispatches to the requested subcommand.
#
# Usage:
# bash scripts/cli.sh # interactive menu (TTY only)
# bash scripts/cli.sh setup # interactive testnet .env wizard
# bash scripts/cli.sh build # build release WASM (wasm32v1-none)
# bash scripts/cli.sh package [dir] # package WASM + checksums
# bash scripts/cli.sh deploy [--dry-run]
# bash scripts/cli.sh smoke # run testnet smoke test
# bash scripts/cli.sh status # show current .env / contract IDs
# =============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
cd "${REPO_ROOT}"
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${CYAN}[cli]${NC} $*"; }
success() { echo -e "${GREEN}[cli]${NC} $*"; }
warn() { echo -e "${YELLOW}[cli]${NC} $*"; }
die() { echo -e "${RED}[cli]${NC} $*" >&2; exit 1; }
ENV_FILE="${REPO_ROOT}/.env"
ENV_EXAMPLE="${REPO_ROOT}/.env.example"
load_env() {
if [[ -f "${ENV_FILE}" ]]; then
set -o allexport
# shellcheck disable=SC1090
source "${ENV_FILE}"
set +o allexport
fi
}
prompt() {
# prompt <var> <label> [default]
local var="$1" label="$2" default="${3:-}" value=""
if [[ -n "${default}" ]]; then
read -r -p "$(echo -e "${CYAN}?${NC} ${label} [${default}]: ")" value
value="${value:-${default}}"
else
read -r -p "$(echo -e "${CYAN}?${NC} ${label}: ")" value
fi
printf -v "${var}" '%s' "${value}"
}
prompt_secret() {
local var="$1" label="$2" value=""
read -r -s -p "$(echo -e "${CYAN}?${NC} ${label}: ")" value
echo ""
printf -v "${var}" '%s' "${value}"
}
# ---------------------------------------------------------------------------
# setup — interactive testnet .env wizard
# ---------------------------------------------------------------------------
cmd_setup() {
info "StellarSettle testnet setup wizard"
echo ""
if [[ -f "${ENV_FILE}" ]]; then
warn ".env already exists — current values shown as defaults."
load_env
fi
local network secret admin fee tname tsym tdec tinv
prompt network "Stellar network (testnet|futurenet|mainnet)" "${STELLAR_NETWORK:-testnet}"
prompt_secret secret "Deployer secret key (S...)"
prompt admin "Admin public key (G...)" "${ADMIN_PUBLIC_KEY:-}"
prompt fee "Platform fee (bps, 300 = 3%)" "${PLATFORM_FEE_BPS:-300}"
prompt tname "Invoice token name" "${INVOICE_TOKEN_NAME:-StellarSettle Invoice Token}"
prompt tsym "Invoice token symbol" "${INVOICE_TOKEN_SYMBOL:-SSINV}"
prompt tdec "Invoice token decimals" "${INVOICE_TOKEN_DECIMALS:-7}"
prompt tinv "Invoice token invoice ID" "${INVOICE_TOKEN_INVOICE_ID:-INV001}"
[[ -n "${secret}" ]] || die "secret key is required"
[[ -n "${admin}" ]] || die "admin public key is required"
[[ "${network}" == "testnet" || "${network}" == "futurenet" || "${network}" == "mainnet" ]] \
|| die "network must be testnet, futurenet, or mainnet"
# Derive admin from secret if left blank? Requires stellar CLI; skip if empty.
if [[ -z "${admin}" ]]; then
die "admin public key is required"
fi
# Friendbot funding (testnet / futurenet only)
if [[ "${network}" == "testnet" || "${network}" == "futurenet" ]]; then
local fund=""
prompt fund "Fund ${admin} via Friendbot? (y/N)" "N"
if [[ "${fund}" == "y" || "${fund}" == "Y" ]]; then
local fb_url="https://friendbot.stellar.org?addr=${admin}"
if command -v curl >/dev/null 2>&1; then
info "Requesting Friendbot funding for ${admin} …"
if curl -fsS --max-time 30 "${fb_url}" >/dev/null; then
success "Friendbot funding requested."
else
warn "Friendbot request failed (account may already be funded)."
fi
else
warn "curl not found — fund manually: ${fb_url}"
fi
fi
fi
local wasm_target
wasm_target=$(sed -n 's/^[[:space:]]*targets[[:space:]]*=[[:space:]]*\["\([^"]*\)"\].*/\1/p' rust-toolchain.toml | head -n1)
wasm_target="${wasm_target:-wasm32v1-none}"
{
echo "# Generated by scripts/cli.sh setup — $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "STELLAR_NETWORK=${network}"
echo "STELLAR_SECRET_KEY=${secret}"
echo "ADMIN_PUBLIC_KEY=${admin}"
echo "PLATFORM_FEE_BPS=${fee}"
echo "INVOICE_TOKEN_NAME=\"${tname}\""
echo "INVOICE_TOKEN_SYMBOL=${tsym}"
echo "INVOICE_TOKEN_DECIMALS=${tdec}"
echo "INVOICE_TOKEN_INVOICE_ID=${tinv}"
echo "WASM_INVOICE_ESCROW=target/${wasm_target}/release/invoice_escrow.wasm"
echo "WASM_INVOICE_TOKEN=target/${wasm_target}/release/invoice_token.wasm"
echo "WASM_PAYMENT_DISTRIBUTOR=target/${wasm_target}/release/payment_distributor.wasm"
echo "INVOICE_ESCROW_CONTRACT_ID="
echo "INVOICE_TOKEN_CONTRACT_ID="
echo "PAYMENT_DISTRIBUTOR_CONTRACT_ID="
} > "${ENV_FILE}"
chmod 600 "${ENV_FILE}" 2>/dev/null || true
success ".env written to ${ENV_FILE} (mode 600)."
warn "Never commit .env to version control."
}
# ---------------------------------------------------------------------------
# build / package / deploy / smoke / status
# ---------------------------------------------------------------------------
cmd_build() {
local target
target=$(sed -n 's/^[[:space:]]*targets[[:space:]]*=[[:space:]]*\["\([^"]*\)"\].*/\1/p' rust-toolchain.toml | head -n1)
target="${target:-wasm32v1-none}"
info "Building release WASM for ${target} …"
cargo build --release --target "${target}" \
-p invoice-escrow -p invoice-token -p payment-distributor
success "Build complete: target/${target}/release/*.wasm"
}
cmd_package() {
local out="${1:-dist}"
bash "${SCRIPT_DIR}/package-wasm.sh" "${out}"
}
cmd_deploy() {
bash "${SCRIPT_DIR}/deploy.sh" "$@"
}
cmd_smoke() {
load_env
if [[ ! -f "${ENV_FILE}" ]]; then
die ".env not found — run 'cli.sh setup' first"
fi
bash "${SCRIPT_DIR}/smoke-test.sh"
}
cmd_status() {
echo -e "${BOLD}Environment${NC}"
if [[ -f "${ENV_FILE}" ]]; then
load_env
echo " .env: present"
echo " network: ${STELLAR_NETWORK:-<unset>}"
echo " admin: ${ADMIN_PUBLIC_KEY:-<unset>}"
echo " platform fee bps: ${PLATFORM_FEE_BPS:-<unset>}"
echo " escrow contract: ${INVOICE_ESCROW_CONTRACT_ID:-<not deployed>}"
echo " token contract: ${INVOICE_TOKEN_CONTRACT_ID:-<not deployed>}"
echo " distributor: ${PAYMENT_DISTRIBUTOR_CONTRACT_ID:-<not deployed>}"
else
echo " .env: missing (run 'cli.sh setup')"
fi
echo ""
echo -e "${BOLD}Toolchain${NC}"
sed -n 's/^[[:space:]]*channel[[:space:]]*=[[:space:]]*"\([^"]*\)".*/ channel: \1/p' rust-toolchain.toml
sed -n 's/^[[:space:]]*targets[[:space:]]*=[[:space:]]*\[\(.*\)\].*/ targets: \1/p' rust-toolchain.toml
}
# ---------------------------------------------------------------------------
# Interactive menu
# ---------------------------------------------------------------------------
menu() {
while true; do
echo ""
echo -e "${BOLD}StellarSettle CLI${NC}"
echo " 1) Setup testnet environment (.env wizard)"
echo " 2) Build WASM"
echo " 3) Package WASM + checksums"
echo " 4) Deploy contracts"
echo " 5) Run smoke test"
echo " 6) Status"
echo " q) Quit"
read -r -p "$(echo -e "${CYAN}?${NC} Choose: ")"
case "${REPLY}" in
1) cmd_setup ;;
2) cmd_build ;;
3) cmd_package ;;
4) cmd_deploy ;;
5) cmd_smoke ;;
6) cmd_status ;;
q|Q) success "Bye."; exit 0 ;;
*) warn "Unknown option." ;;
esac
done
}
# ---------------------------------------------------------------------------
# Dispatch
# ---------------------------------------------------------------------------
cmd="${1:-}"
if [[ -z "${cmd}" ]]; then
if [[ -t 0 && -t 1 ]]; then
menu
else
die "No subcommand given and stdin is not a TTY. Usage: cli.sh {setup|build|package|deploy|smoke|status}"
fi
exit 0
fi
shift || true
case "${cmd}" in
setup) cmd_setup ;;
build) cmd_build ;;
package) cmd_package "$@" ;;
deploy) cmd_deploy "$@" ;;
smoke) cmd_smoke ;;
status) cmd_status ;;
-h|--help|help)
sed -n '2,25p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
;;
*)
die "unknown subcommand '${cmd}' (expected setup|build|package|deploy|smoke|status)"
;;
esac