Skip to content

Commit 6bd32f7

Browse files
Jerryvic911Jerryvic911
authored andcommitted
feat(scripts): add mock token setup for testnet deployment
- New setup-mock-token.sh: issues a mock Stellar asset, deploys its SAC, funds test-user identities, and wires it into SubscriptionRenewal via set_token_contract - deploy.sh: opt-in via SETUP_MOCK_TOKEN=true, off by default (never runs against mainnet automatically) - DEPLOYMENT.md: document the new script and its env vars Closes #1199
1 parent 6a58701 commit 6bd32f7

3 files changed

Lines changed: 163 additions & 0 deletions

File tree

contracts/DEPLOYMENT.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,34 @@ Update this section after each testnet deployment.
105105

106106
Network: `testnet`
107107
Last deployed: _(fill in after first deployment)_
108+
109+
To also issue a mock testnet asset and fund test accounts with it (see below), set `SETUP_MOCK_TOKEN=true`:
110+
111+
\`\`\`bash
112+
SETUP_MOCK_TOKEN=true bash scripts/deploy.sh testnet
113+
\`\`\`
114+
115+
---
116+
117+
## Mock Token Setup (Testnet)
118+
119+
`scripts/setup-mock-token.sh` issues a mock Stellar asset for exercising the
120+
escrow-locking flow (`renew` / `claim_escrow`) end-to-end on testnet, without
121+
needing a real payment rail:
122+
123+
\`\`\`bash
124+
cd contracts
125+
bash scripts/setup-mock-token.sh testnet <renewal_contract_id>
126+
\`\`\`
127+
128+
This will:
129+
1. Generate (or reuse) a funded issuer identity for the mock asset
130+
2. Deploy the asset's Stellar Asset Contract (SAC) — this is the token address used by `SubscriptionRenewal`
131+
3. Generate a couple of funded test-user identities, establish trustlines, and mint each of them a mock balance
132+
4. If a `SubscriptionRenewal` contract id is passed in, invoke `set_token_contract` to wire the mock token in as the escrow asset
133+
5. Save everything (issuer, token contract id, test user addresses) to `scripts/mock-token-testnet.env`
134+
135+
Override the asset code, mint amount, or number of test users with `TOKEN_CODE`, `MINT_AMOUNT`, or `NUM_TEST_USERS` env vars.
136+
137+
---
138+

contracts/scripts/deploy.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ echo " ContractUpgradeGovernance: $UPGRADE_ID"
6666
echo ""
6767
echo "==> Running initialization..."
6868
bash "$(dirname "$0")/init.sh" "$NETWORK" "$SECRET_KEY" "$RENEWAL_ID" "$LOGGING_ID" "$UPGRADE_ID"
69+
# Optional: issue a mock testnet asset, fund test accounts, and wire it into
70+
# SubscriptionRenewal's escrow token slot. Off by default so this never runs
71+
# against mainnet by accident.
72+
if [ "${SETUP_MOCK_TOKEN:-false}" = "true" ]; then
73+
echo ""
74+
echo "==> Setting up mock token for testing..."
75+
bash "$(dirname "$0")/setup-mock-token.sh" "$NETWORK" "$RENEWAL_ID"
76+
fi
6977

7078
echo ""
7179
echo "==> Add to backend/.env:"
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# setup-mock-token.sh
5+
#
6+
# Issues a mock Stellar asset, deploys its Stellar Asset Contract (SAC), funds
7+
# a handful of test accounts with it, and (optionally) wires the resulting
8+
# token address into an already-deployed SubscriptionRenewal contract via
9+
# `set_token_contract`, so the escrow-locking flow has an asset to move.
10+
#
11+
# Usage:
12+
# bash scripts/setup-mock-token.sh <network> [renewal_contract_id]
13+
#
14+
# Env vars:
15+
# TOKEN_CODE Asset code for the mock token (default: MOCK)
16+
# NUM_TEST_USERS How many funded test-user identities to create (default: 2)
17+
#
18+
# Requires: Stellar CLI v21+ (`stellar`), network access to Friendbot for the
19+
# chosen network (testnet/futurenet).
20+
21+
NETWORK=${1:-testnet}
22+
RENEWAL_ID=${2:-${SOROBAN_RENEWAL_ADDRESS:-''}}
23+
TOKEN_CODE=${TOKEN_CODE:-MOCK}
24+
NUM_TEST_USERS=${NUM_TEST_USERS:-2}
25+
26+
SCRIPT_DIR="$(dirname "$0")"
27+
28+
echo "==> Setting up mock token '$TOKEN_CODE' on $NETWORK"
29+
30+
# 1. Generate (or reuse) a funded issuer identity for the mock asset.
31+
ISSUER_KEY="mock-token-issuer"
32+
if ! stellar keys address "$ISSUER_KEY" >/dev/null 2>&1; then
33+
echo " Generating issuer identity: $ISSUER_KEY"
34+
stellar keys generate "$ISSUER_KEY" --network "$NETWORK" --fund
35+
else
36+
echo " Reusing existing issuer identity: $ISSUER_KEY"
37+
fi
38+
ISSUER_ADDRESS=$(stellar keys address "$ISSUER_KEY")
39+
echo " Issuer: $ISSUER_ADDRESS"
40+
41+
# 2. Deploy the Stellar Asset Contract (SAC) that wraps TOKEN_CODE:ISSUER.
42+
# This is the contract address you pass to `set_token_contract` on
43+
# SubscriptionRenewal.
44+
echo ""
45+
echo "==> Deploying Stellar Asset Contract for $TOKEN_CODE:$ISSUER_ADDRESS..."
46+
TOKEN_ID=$(stellar contract asset deploy \
47+
--source "$ISSUER_KEY" \
48+
--network "$NETWORK" \
49+
--asset "$TOKEN_CODE:$ISSUER_ADDRESS")
50+
echo " Token contract: $TOKEN_ID"
51+
52+
# 3. Generate funded test-user identities, establish trustlines to the mock
53+
# asset, and mint each of them a starting balance.
54+
MINT_AMOUNT=${MINT_AMOUNT:-100000000000} # 10,000.0000000 (7 decimals)
55+
TEST_USER_ADDRESSES=()
56+
57+
echo ""
58+
echo "==> Creating $NUM_TEST_USERS test user(s) and minting $TOKEN_CODE..."
59+
for i in $(seq 1 "$NUM_TEST_USERS"); do
60+
USER_KEY="mock-token-user-$i"
61+
62+
if ! stellar keys address "$USER_KEY" >/dev/null 2>&1; then
63+
echo " Generating test user: $USER_KEY"
64+
stellar keys generate "$USER_KEY" --network "$NETWORK" --fund
65+
else
66+
echo " Reusing existing test user: $USER_KEY"
67+
fi
68+
USER_ADDRESS=$(stellar keys address "$USER_KEY")
69+
70+
echo " Establishing trustline for $USER_KEY ($USER_ADDRESS)..."
71+
stellar tx new change-trust \
72+
--source "$USER_KEY" \
73+
--network "$NETWORK" \
74+
--line "$TOKEN_CODE:$ISSUER_ADDRESS" \
75+
--limit 1000000000000000000 >/dev/null
76+
77+
echo " Minting $MINT_AMOUNT (stroops) of $TOKEN_CODE to $USER_ADDRESS..."
78+
stellar contract invoke \
79+
--id "$TOKEN_ID" \
80+
--source "$ISSUER_KEY" \
81+
--network "$NETWORK" \
82+
-- mint \
83+
--to "$USER_ADDRESS" \
84+
--amount "$MINT_AMOUNT" >/dev/null
85+
86+
TEST_USER_ADDRESSES+=("$USER_ADDRESS")
87+
echo " $USER_KEY funded: $USER_ADDRESS"
88+
done
89+
90+
# 4. If a SubscriptionRenewal contract id was supplied, wire the mock token in
91+
# as the escrow asset so `renew` / `claim_escrow` can move real balances.
92+
if [ -n "$RENEWAL_ID" ]; then
93+
echo ""
94+
echo "==> Linking mock token to SubscriptionRenewal ($RENEWAL_ID)..."
95+
stellar contract invoke \
96+
--id "$RENEWAL_ID" \
97+
--source "$ISSUER_KEY" \
98+
--network "$NETWORK" \
99+
-- set_token_contract \
100+
--address "$TOKEN_ID"
101+
echo " Token contract linked. Note: this must be run with the contract's"
102+
echo " admin key — re-run with --source <admin_key> if $ISSUER_KEY is not admin."
103+
else
104+
echo ""
105+
echo "==> No renewal contract id supplied — skipping set_token_contract."
106+
echo " Run again with: bash scripts/setup-mock-token.sh $NETWORK <renewal_contract_id>"
107+
fi
108+
109+
# 5. Persist everything for reuse (backend .env, other scripts, manual testing).
110+
OUTPUT_FILE="$SCRIPT_DIR/mock-token-${NETWORK}.env"
111+
{
112+
echo "# Mock token setup on $NETWORK$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
113+
echo "MOCK_TOKEN_CODE=$TOKEN_CODE"
114+
echo "MOCK_TOKEN_ISSUER=$ISSUER_ADDRESS"
115+
echo "SOROBAN_MOCK_TOKEN_ADDRESS=$TOKEN_ID"
116+
for idx in "${!TEST_USER_ADDRESSES[@]}"; do
117+
echo "MOCK_TOKEN_TEST_USER_$((idx + 1))=${TEST_USER_ADDRESSES[$idx]}"
118+
done
119+
} > "$OUTPUT_FILE"
120+
121+
echo ""
122+
echo "==> Mock token setup complete."
123+
echo "SOROBAN_MOCK_TOKEN_ADDRESS=$TOKEN_ID"
124+
echo "Details saved to $OUTPUT_FILE"

0 commit comments

Comments
 (0)