-
Notifications
You must be signed in to change notification settings - Fork 33
/
setup.sh
executable file
·309 lines (263 loc) · 10.5 KB
/
setup.sh
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
#!/bin/bash
# Function to ask for user confirmation
ask_for_confirmation() {
local prompt=$1
local response=""
while true; do
read -p "$prompt [y/n]: " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
return 0
elif [[ $response =~ ^([nN][oO]|[nN])$ ]]; then
return 1
else
echo "Invalid response. Please enter y or n." >&2
fi
done
}
# Function to read and validate user input
read_input_and_validate() {
local prompt=$1
local default=$2
local validation_func=$3
local valid=false
local input=""
# While input is invalid, keep asking for input
while [[ $valid == false ]]; do
read -p "$prompt " input
# Trim leading and trailing spaces
input=$(echo "$input" | xargs)
# If input is empty, use default value
if [[ -z $input ]]; then
input=$default
fi
# Validate input
if $validation_func "$input"; then
valid=true
fi
done
echo "$input"
}
# Function to validate NETWORK
validate_network() {
if [[ $1 != "mainnet" && $1 != "testnet" ]]; then
echo "Invalid NETWORK. It should be either mainnet or testnet." >&2
return 1
fi
return 0
}
# Function to validate MNEMONIC
validate_mnemonic() {
if [[ $(echo $1 | wc -w) -ne 24 ]]; then
echo "Invalid MNEMONIC. It should be 24 words separated by spaces." >&2
return 1
fi
return 0
}
# Function to validate Pinata API KEY (20 characters long)
validate_pinata_api_key() {
if [[ ! $1 =~ ^[a-zA-Z0-9]+$ ]]; then
echo "Invalid PINATA_API_KEY. It should be 20 characters long." >&2
echo "Please visit https://app.pinata.cloud/developers/api-keys to get your API keys." >&2
return 1
fi
return 0
}
# Function to validate Pinata SECRET KEY (64 characters long)
validate_pinata_secret() {
if [[ ! $1 =~ ^[a-zA-Z0-9]+$ ]]; then
echo "Invalid PINATA_SECRET. It should be 64 characters long." >&2
echo "Please visit https://app.pinata.cloud/developers/api-keys to get your API keys." >&2
return 1
fi
return 0
}
# Function to validate Telegram Bot Token
validate_telegram_bot_token() {
if [[ ! $1 =~ ^[0-9]+:[a-zA-Z0-9_-]+$ ]]; then
echo "Invalid TELEGRAM_BOT_TOKEN. It should be in the format 1234567890:ABCdefGHIjklMNoPQRsTUVwxyZ." >&2
echo "Please visit https://core.telegram.org/bots#how-do-i-create-a-bot to get your API keys." >&2
return 1
fi
return 0
}
# Function to validate non-empty input
validate_non_empty() {
if [[ -z $1 ]]; then
echo "Input cannot be empty." >&2
return 1
fi
return 0
}
# Function to initialize .env file
initialize_env_file() {
local NETWORK="testnet"
local MNEMONIC=$(node_modules/.bin/ts-node ./scripts/generate-mnemonic.ts)
local CORS_ENABLED="false"
local CORS_ORIGIN="*"
local NGROK_ENABLED="false"
local NGROK_AUTHTOKEN=""
local NGROK_DOMAIN=""
if ask_for_confirmation "Do you want to enable CORS?"; then
CORS_ENABLED="true"
CORS_ORIGIN=$(read_input_and_validate "Please enter your CORS_ORIGIN (e.g. http://localhost:3000) or press Enter to use the default [*]:" "*" validate_non_empty)
else
CORS_ENABLED="false"
CORS_ORIGIN="*"
fi
if ask_for_confirmation "Do you want to enable ngrok?"; then
NGROK_ENABLED="true"
NGROK_AUTHTOKEN=$(read_input_and_validate "Please enter your NGROK_AUTHTOKEN (e.g. 0A1B2C3D4E5F6G7H8I9J0K1L2M3_4N5O6P7Q8R9S0T1U2V3W4):" "" validate_non_empty)
NGROK_DOMAIN=$(read_input_and_validate "Please enter your NGROK_DOMAIN (this requires registering in the https://dashboard.ngrok.com/cloud-edge/domains):" "" validate_non_empty)
else
NGROK_ENABLED="false"
NGROK_AUTHTOKEN=""
NGROK_DOMAIN=""
fi
NETWORK=$(read_input_and_validate "Please enter your NETWORK (mainnet or testnet) or press Enter to use the default [$NETWORK]:" "$NETWORK" validate_network)
MNEMONIC=$(read_input_and_validate "Please enter your MNEMONIC (24 words separated by spaces) or press Enter to use the generated [hidden]:" "$MNEMONIC" validate_mnemonic)
local PINATA_API_KEY=$(read_input_and_validate "Please enter your PINATA_API_KEY (20 characters long):" "" validate_pinata_api_key)
local PINATA_SECRET=$(read_input_and_validate "Please enter your PINATA_SECRET (64 characters long):" "" validate_pinata_secret)
local TELEGRAM_BOT_TOKEN=$(read_input_and_validate "Please enter your TELEGRAM_BOT_TOKEN (in the format 1234567890:ABCdefGHIjklMNoPQRsTUVwxyZ):" "" validate_telegram_bot_token)
# Creating the .env file
cat << EOF > .env
# Web Server Configuration
CORS_ENABLED=$CORS_ENABLED
CORS_ORIGIN=$CORS_ORIGIN
NGROK_ENABLED=$NGROK_ENABLED
NGROK_AUTHTOKEN=$NGROK_AUTHTOKEN
NGROK_DOMAIN=$NGROK_DOMAIN
# Network Configuration
NETWORK=$NETWORK
# Your MNEMONIC
MNEMONIC="$MNEMONIC"
# Pinata API Keys
PINATA_API_KEY=$PINATA_API_KEY
PINATA_SECRET=$PINATA_SECRET
# Telegram Bot Token
TELEGRAM_BOT_TOKEN=$TELEGRAM_BOT_TOKEN
# Jetton
JETTON_ADDRESS=
# SBT Collection
FIRST_TIME_SBT_COLLECTION_ADDRESS=
FIVE_TIMES_SBT_COLLECTION_ADDRESS=
EOF
echo ".env file successfully created."
}
# Function to deploy wallet contract
deploy_wallet_contract() {
echo "Deploying wallet contract..."
if ! node_modules/.bin/ts-node ./scripts/deploy-wallet.ts; then
echo "Failed to deploy wallet contract." >&2
exit 1
fi
}
# Running npm install with frozen lockfile
echo "Running npm install..."
npm install
# Check if .env file already exists
if [ -f ".env" ]; then
if ask_for_confirmation ".env file already exists. Do you want to overwrite it?"; then
initialize_env_file
else
echo "Skipping .env file initialization." >&2
fi
else
initialize_env_file
fi
# Initialize database
if [ ! -f "db.sqlite" ]; then
echo "Initializing database..."
npm run typeorm:run-migrations
fi
# Deploying wallet contract (if no, write note to deploy it later and exit)
if [[ $(node_modules/.bin/ts-node ./scripts/get-wallet-state.ts | tail -n 1) != "active" ]]; then
if ask_for_confirmation "Do you want to deploy the wallet contract now?"; then
deploy_wallet_contract
else
echo "Please restart the ./setup.sh script to deploy the wallet contract later." >&2
exit 1
fi
fi
# Load all variables from .env file
set -o allexport
source .env
set +o allexport
# Check if jetton address is not set in .env file
if [[ -z $JETTON_ADDRESS ]]; then
if ask_for_confirmation "JETTON_ADDRESS is not set in .env file. Do you want to deploy it now?"; then
# Deploy jetton address from wallet contract
echo "Deploying jetton address from wallet contract..."
JETTON_ADDRESS=$(node_modules/.bin/ts-node ./scripts/deploy-jetton.ts | tail -n 1)
# Check if jetton address is empty
if [[ -z $JETTON_ADDRESS ]]; then
echo "Failed to get jetton address from wallet contract." >&2
exit 1
fi
# Update .env file
echo "Updating .env file..."
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s/^JETTON_ADDRESS=.*/JETTON_ADDRESS=\"$JETTON_ADDRESS\"/" .env
else
sed -i "s/^JETTON_ADDRESS=.*/JETTON_ADDRESS=\"$JETTON_ADDRESS\"/" .env
fi
else
echo "Please set JETTON_ADDRESS in .env file and run this script again." >&2
exit 1
fi
fi
# Print out the JETTON_ADDRESS
echo "JETTON_ADDRESS is set to $JETTON_ADDRESS"
# Check if FIRST_TIME_SBT_COLLECTION_ADDRESS is not set in .env file
if [[ -z $FIRST_TIME_SBT_COLLECTION_ADDRESS ]]; then
if ask_for_confirmation "FIRST_TIME_SBT_COLLECTION_ADDRESS is not set in .env file. Do you want to deploy it now?"; then
# Deploy first time sbt collection address from wallet contract
echo "Getting first time sbt collection address from wallet contract..."
FIRST_TIME_SBT_COLLECTION_ADDRESS=$(node_modules/.bin/ts-node ./scripts/deploy-sbt-collection.ts first-time | tail -n 1)
# Check if first time sbt collection address is empty
if [[ -z $FIRST_TIME_SBT_COLLECTION_ADDRESS ]]; then
echo "Failed to get first time sbt collection address from wallet contract." >&2
exit 1
fi
# Update .env file
echo "Updating .env file..."
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s/^FIRST_TIME_SBT_COLLECTION_ADDRESS=.*/FIRST_TIME_SBT_COLLECTION_ADDRESS=\"$FIRST_TIME_SBT_COLLECTION_ADDRESS\"/" .env
else
sed -i "s/^FIRST_TIME_SBT_COLLECTION_ADDRESS=.*/FIRST_TIME_SBT_COLLECTION_ADDRESS=\"$FIRST_TIME_SBT_COLLECTION_ADDRESS\"/" .env
fi
else
echo "Please set FIRST_TIME_SBT_COLLECTION_ADDRESS in .env file and run this script again." >&2
exit 1
fi
fi
# Print out the FIRST_TIME_SBT_COLLECTION_ADDRESS
echo "FIRST_TIME_SBT_COLLECTION_ADDRESS is set to $FIRST_TIME_SBT_COLLECTION_ADDRESS"
# Check if FIVE_TIMES_SBT_COLLECTION_ADDRESS is not set in .env file
if [[ -z $FIVE_TIMES_SBT_COLLECTION_ADDRESS ]]; then
if ask_for_confirmation "FIVE_TIMES_SBT_COLLECTION_ADDRESS is not set in .env file. Do you want to deploy it now?"; then
# Deploy five times sbt collection address from wallet contract
echo "Getting five times sbt collection address from wallet contract..."
FIVE_TIMES_SBT_COLLECTION_ADDRESS=$(node_modules/.bin/ts-node ./scripts/deploy-sbt-collection.ts five-times | tail -n 1)
# Check if five times sbt collection address is empty
if [[ -z $FIVE_TIMES_SBT_COLLECTION_ADDRESS ]]; then
echo "Failed to get five times sbt collection address from wallet contract." >&2
exit 1
fi
# Update .env file
echo "Updating .env file..."
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s/^FIVE_TIMES_SBT_COLLECTION_ADDRESS=.*/FIVE_TIMES_SBT_COLLECTION_ADDRESS=\"$FIVE_TIMES_SBT_COLLECTION_ADDRESS\"/" .env
else
sed -i "s/^FIVE_TIMES_SBT_COLLECTION_ADDRESS=.*/FIVE_TIMES_SBT_COLLECTION_ADDRESS=\"$FIVE_TIMES_SBT_COLLECTION_ADDRESS\"/" .env
fi
else
echo "Please set FIVE_TIMES_SBT_COLLECTION_ADDRESS in .env file and run this script again." >&2
exit 1
fi
fi
# Print out the FIVE_TIMES_SBT_COLLECTION_ADDRESS
echo "FIVE_TIMES_SBT_COLLECTION_ADDRESS is set to $FIVE_TIMES_SBT_COLLECTION_ADDRESS"
# Finished
echo "Setup finished successfully. Please run the following command to start the app:"
echo ""
echo "docker-compose -f ./docker-compose.dev.yaml up"