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