-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguild-setup.sh
More file actions
247 lines (193 loc) · 9.55 KB
/
guild-setup.sh
File metadata and controls
247 lines (193 loc) · 9.55 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
#!/bin/bash
function load_config() {
if [ -f "$CONFIG_FILE" ]; then
STRUCTS_ACCOUNT=$(jq -r '.account_name' "$CONFIG_FILE")
echo -e "${GREEN}Loaded configuration for account: ${STRUCTS_ACCOUNT}${NC}"
PLAYER_ADDRESS=$(jq -r '.account_address' "$CONFIG_FILE")
PLAYER_ID=$(jq -r '.account_player_id' "$CONFIG_FILE")
NEW_PLAYER_ID=$(structsd ${PARAMS_QUERY} query structs address ${PLAYER_ADDRESS} | jq -r .playerId)
if [ "$NEW_PLAYER_ID" != "$PLAYER_ID" ]; then
echo -e "${RED} Player ID has changed from ${PLAYER_ID} to ${NEW_PLAYER_ID}.${NC}"
echo "{\"account_name\": \"$STRUCTS_ACCOUNT\",\"account_address\": \"$PLAYER_ADDRESS\",\"account_player_id\": \"$NEW_PLAYER_ID\"}" > "$CONFIG_FILE"
PLAYER_ID=$NEW_PLAYER_ID
fi
else
setup_config
fi
}
function setup_config() {
print_header
echo -e "${YELLOW}No configuration found. Let's set up your account.${NC}"
echo ""
# Get list of available accounts
ACCOUNTS_JSON=$(structsd keys list --output json)
ACCOUNT_COUNT=$(echo "$ACCOUNTS_JSON" | jq length)
if [ "$ACCOUNT_COUNT" -eq 0 ]; then
echo -e "${RED}No accounts found. Please create an account first.${NC}"
exit 1
fi
echo -e "${CYAN}Available accounts:${NC}"
echo ""
for ((i=0; i<$ACCOUNT_COUNT; i++)); do
ACCOUNT_NAME=$(echo "$ACCOUNTS_JSON" | jq -r ".[$i].name")
ACCOUNT_ADDRESS=$(echo "$ACCOUNTS_JSON" | jq -r ".[$i].address")
echo -e "${GREEN}$((i+1)).${NC} ${ACCOUNT_NAME} (${ACCOUNT_ADDRESS})"
done
echo ""
read -p "Select an account (1-$ACCOUNT_COUNT): " ACCOUNT_OPTION
if ! [[ "$ACCOUNT_OPTION" =~ ^[0-9]+$ ]] || [ "$ACCOUNT_OPTION" -lt 1 ] || [ "$ACCOUNT_OPTION" -gt "$ACCOUNT_COUNT" ]; then
echo -e "${RED}Invalid option. Please try again.${NC}"
press_enter_to_continue
setup_config
return
fi
STRUCTS_ACCOUNT=$(echo "$ACCOUNTS_JSON" | jq -r ".[$(($ACCOUNT_OPTION-1))].name")
echo -e "${GREEN}Selected account: ${STRUCTS_ACCOUNT_ADDRESS}${NC}"
PLAYER_ADDRESS=$(structsd ${PARAMS_KEYS} keys show ${STRUCTS_ACCOUNT} | jq -r .address)
PLAYER_ID=$(structsd ${PARAMS_QUERY} query structs address ${PLAYER_ADDRESS} | jq -r .playerId)
# Save configuration
mkdir -p "$CONFIG_DIR"
echo "{\"account_name\": \"$STRUCTS_ACCOUNT\",\"account_address\": \"$PLAYER_ADDRESS\",\"account_player_id\": \"$PLAYER_ID\"}" > "$CONFIG_FILE"
echo -e "${GREEN}Configuration saved.${NC}"
press_enter_to_continue
}
function load_guild_config() {
echo -e "${GREEN}Guild Config Load${NC}"
echo -e "${GREEN}Guild Config file: ${GUILD_CONFIG_FILE}${NC}"
if [ -f "${GUILD_CONFIG_FILE}" ]; then
echo -e "${GREEN}Config file found.${NC}"
GUILD_ID=$(jq -r '.guild.id' "${GUILD_CONFIG_FILE}")
echo -e "${GREEN}Loaded guild configuration for guild ID: ${GUILD_ID}${NC}"
CURRENT_NETWORK=$(structsd ${PARAMS_QUERY} status | jq -r .node_info.network)
if [ "$CURRENT_NETWORK" != "$LAST_NETWORK" ]; then
echo -e "${RED}New network detected!${NC}"
# TODO HERE
# recreate the guild record on chain
# get new guild id
# TODO load guild meta details from config
# TODO change guild.id to new ID
# Save details to file
# upload new details
# update guild record
fi
else
check_existing_guild
fi
}
function check_existing_guild() {
print_header
echo -e "${YELLOW}No guild configuration found. Checking for existing guild...${NC}"
echo ""
# Get player address and ID
PLAYER_ADDRESS=$(structsd ${PARAMS_KEYS} keys show ${STRUCTS_ACCOUNT} | jq -r .address)
PLAYER_ID=$(structsd ${PARAMS_QUERY} query structs address ${PLAYER_ADDRESS} | jq -r .playerId)
GUILD_ID=$(structsd ${PARAMS_QUERY} query structs player ${PLAYER_ID} | jq -r .Player.guildId)
if [[ -z "${GUILD_ID}" ]] || [ "$GUILD_ID" == "null" ]; then
echo -e "${YELLOW}No guilds found. Starting guild setup process...${NC}"
setup_guild
return
fi
# Check if the guild actually exists
# It really should by this point
GUILD_JSON=$(structsd ${PARAMS_QUERY} query structs guild ${GUILD_ID} | jq -r '.Guild')
if [ -z "$GUILD_JSON" ]; then
echo -e "${YELLOW}No guilds found. Starting guild setup process...${NC}"
setup_guild
return
fi
echo -e "${GREEN}Using guild ID: ${GUILD_ID}${NC}"
setup_guild_metadata
}
function setup_guild() {
print_header
echo -e "${CYAN}=== GUILD SETUP ===${NC}"
echo ""
# Get player address and ID
PLAYER_ADDRESS=$(structsd ${PARAMS_KEYS} keys show ${STRUCTS_ACCOUNT} | jq -r .address)
PLAYER_ID=$(structsd ${PARAMS_QUERY} query structs address ${PLAYER_ADDRESS} | jq -r .playerId)
echo -e "${YELLOW}Account: ${STRUCTS_ACCOUNT} Address: ${PLAYER_ADDRESS} Player ID: ${PLAYER_ID} ${NC}"
echo -e "${YELLOW}Checking current Player (${PLAYER_ID}) available capacity...${NC}"
PLAYER_CAPACITY=$(structsd ${PARAMS_QUERY} query structs player ${PLAYER_ID} | jq -r '.gridAttributes.capacity')
echo -e "${GREEN}Player capacity: ${PLAYER_CAPACITY}${NC}"
echo -e "${YELLOW}Checking for existing allocation...${NC}"
ALLOCATION_ID=$(structsd query structs allocation-all-by-source ${PLAYER_ID} --output json | jq -r '.Allocation[0].id')
if [[ -z "${ALLOCATION_ID}" ]] || [ "$ALLOCATION_ID" == "null" ]; then
echo -e "${YELLOW}Creating a new allocation...${NC}"
structsd ${PARAMS_TX} tx structs allocation-create ${PLAYER_ID} ${PLAYER_CAPACITY} --allocation-type automated --from ${STRUCTS_ACCOUNT}
sleep $SLEEP_TIME
ALLOCATION_ID=$(structsd query structs allocation-all-by-source ${PLAYER_ID} --output json | jq -r '.Allocation[0].id')
else
echo -e "${GREEN}Allocation already found: ${ALLOCATION_ID}${NC}"
fi
echo -e "${YELLOW}Checking for substation...${NC}"
SUBSTATION_ID=$(structsd ${PARAMS_QUERY} query structs allocation ${ALLOCATION_ID} | jq -r ".Allocation.destinationId")
if [[ -z "${SUBSTATION_ID}" ]] || [ "$SUBSTATION_ID" == "null" ]; then
echo -e "${YELLOW}Creating a new substation...${NC}"
structsd ${PARAMS_TX} tx structs substation-create ${PLAYER_ID} ${ALLOCATION_ID} --from ${STRUCTS_ACCOUNT}
sleep $SLEEP_TIME
SUBSTATION_ID=$(structsd ${PARAMS_QUERY} query structs allocation ${ALLOCATION_ID} | jq -r ".Allocation.destinationId")
else
echo -e "${GREEN}Substation already found: ${SUBSTATION_ID}${NC}"
fi
echo -e "${YELLOW}Checking if guild already exists...${NC}"
GUILD_ID=$(structsd ${PARAMS_QUERY} query structs player ${PLAYER_ID} | jq -r '.Player.guildId')
if [[ -z "${GUILD_ID}" ]] || [ "$GUILD_ID" == "null" ]; then
echo -e "${YELLOW}Creating a new guild...${NC}"
structsd ${PARAMS_TX} tx structs guild-create "temp.endpoint.com" ${SUBSTATION_ID} --from ${STRUCTS_ACCOUNT}
sleep $SLEEP_TIME
GUILD_ID=$(structsd ${PARAMS_QUERY} query structs player ${PLAYER_ID} | jq -r '.Player.guildId')
else
echo -e "${GREEN}Guild already found: ${GUILD_ID}${NC}"
fi
if [[ -z "${GUILD_ID}" ]] || [ "$GUILD_ID" == "null" ]; then
echo -e "${RED}Problem during guild creation!${NC}"
press_enter_to_continue
exit 1
fi
setup_guild_metadata
}
function setup_guild_metadata() {
print_header
echo -e "${CYAN}=== GUILD METADATA SETUP ===${NC}"
echo ""
echo -e "${YELLOW}Please provide details about your guild:${NC}"
echo ""
read -p "Guild Name: " GUILD_NAME
read -p "Guild Description: " GUILD_DESCRIPTION
read -p "Guild Tag (1-3 characters): " GUILD_TAG
read -p "Guild Logo URL: " GUILD_LOGO
read -p "Guild Website URL: " GUILD_WEBSITE
read -p "Guild Token Name (e.g., gld): " GUILD_TOKEN_NAME
read -p "Guild Token Smallest Unit Name (e.g., ugld): " GUILD_TOKEN_SMALLEST_VALUE_NAME
read -p "Discord Contact: " GUILD_SOCIAL_DISCORD_CONTACT
read -p "Base Energy Promise (milliwatt): " GUILD_BASE_ENERGY
# Create guild JSON
GUILD_JSON=$( jq -n \
--arg id "$GUILD_ID" \
--arg name "$GUILD_NAME" \
--arg description "$GUILD_DESCRIPTION" \
--arg tag "$GUILD_TAG" \
--arg logo "$GUILD_LOGO" \
--arg website "$GUILD_WEBSITE" \
--arg discordContact "$GUILD_SOCIAL_DISCORD_CONTACT" \
--arg coin "$GUILD_TOKEN_NAME" \
--arg smallestCoin "$GUILD_TOKEN_SMALLEST_VALUE_NAME" \
--arg baseEnergy "$GUILD_BASE_ENERGY" \
'{ guild: { id: $id, name: $name, description: $description, tag: $tag, baseEnergy: $baseEnergy, logo: $logo, website: $website, socials: { discordContact: $discordContact }, denom: {"6": $coin, "0": $smallestCoin } } }' )
# Save guild metadata
echo "$GUILD_JSON" > "$GUILD_CONFIG_FILE"
echo -e "${GREEN}Guild metadata saved.${NC}"
# Upload to paste service
echo "$GUILD_JSON" > "$GUILD_CONFIG_FILE.tmp"
UPLOAD_URL=$(curl --upload-file "$GUILD_CONFIG_FILE.tmp" 'https://paste.c-net.org/')
if [ -n "$UPLOAD_URL" ]; then
echo -e "${YELLOW}Updating guild endpoint...${NC}"
structsd ${PARAMS_TX} tx structs guild-update-endpoint ${GUILD_ID} "${UPLOAD_URL}" --from ${STRUCTS_ACCOUNT}
sleep $SLEEP_TIME
echo -e "${GREEN}Guild endpoint updated successfully!${NC}"
else
echo -e "${RED}Failed to upload guild metadata.${NC}"
fi
rm -f "$GUILD_CONFIG_FILE.tmp"
press_enter_to_continue
}