forked from Rickyy1017/Stellar-Defi-Vault
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.sh
More file actions
executable file
·312 lines (259 loc) · 6.77 KB
/
Copy pathpool.sh
File metadata and controls
executable file
·312 lines (259 loc) · 6.77 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
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
310
311
312
#!/usr/bin/env sh
set -eu
DECIMALS=10000000
DRY_RUN=0
load_env() {
if [ -f .env ]; then
set -a
# shellcheck disable=SC1091
. ./.env
set +a
fi
}
show_help() {
cat <<'EOF'
Usage:
scripts/pool.sh [--dry-run] [command] [args...]
Commands:
stake [amount] [address]
unstake [shares] [address]
claim [address]
position [address]
pending [address]
pool-info
Environment:
CONTRACT_ID Soroban contract ID
IDENTITY Local stellar identity or address used as --source
NETWORK Named Stellar network, defaults to testnet
EOF
}
prompt_value() {
label=$1
default_value=${2-}
if [ -n "$default_value" ]; then
printf "%s [%s]: " "$label" "$default_value" >&2
else
printf "%s: " "$label" >&2
fi
IFS= read -r REPLY_VALUE
if [ -z "$REPLY_VALUE" ]; then
REPLY_VALUE=$default_value
fi
}
ensure_config() {
load_env
if [ -z "${CONTRACT_ID:-}" ]; then
prompt_value "Contract ID" ""
CONTRACT_ID=$REPLY_VALUE
fi
if [ -z "${IDENTITY:-}" ]; then
prompt_value "Identity" ""
IDENTITY=$REPLY_VALUE
fi
NETWORK=${NETWORK:-testnet}
}
require_stellar() {
if [ "$DRY_RUN" -eq 0 ] && ! command -v stellar >/dev/null 2>&1; then
printf "Error: stellar CLI is not installed or not on PATH.\n" >&2
exit 1
fi
}
quote_arg() {
printf "%s" "$1" | sed "s/'/'\\\\''/g"
}
print_command() {
printf "stellar"
for arg in "$@"; do
printf " '%s'" "$(quote_arg "$arg")"
done
printf "\n"
}
run_contract() {
fn=$1
shift
ensure_config
require_stellar
set -- contract invoke --id "$CONTRACT_ID" --source "$IDENTITY" --network "$NETWORK" --fn "$fn" "$@"
if [ "$DRY_RUN" -eq 1 ]; then
print_command "$@"
return 0
fi
output=$(stellar "$@" 2>&1) || {
printf "Error running '%s':\n%s\n" "$fn" "$output" >&2
return 1
}
printf "%s" "$output" | tr -d '\r'
}
is_integer() {
case "$1" in
-[0-9]*|[0-9]*) return 0 ;;
*) return 1 ;;
esac
}
format_amount() {
raw=$1
sign=""
if [ "${raw#-}" != "$raw" ]; then
sign="-"
raw=${raw#-}
fi
whole=$((raw / DECIMALS))
fraction=$((raw % DECIMALS))
printf "%s%s.%07d" "$sign" "$whole" "$fraction"
}
human_or_raw() {
value=$1
if is_integer "$value"; then
format_amount "$value"
else
printf "%s" "$value"
fi
}
run_stake() {
amount=${1-}
address=${2-${IDENTITY:-}}
if [ -z "$amount" ]; then
prompt_value "Amount (raw units, 7 decimals)" ""
amount=$REPLY_VALUE
fi
prompt_value "Staker address" "$address"
address=$REPLY_VALUE
output=$(run_contract stake --staker "$address" --amount "$amount") || return 1
[ "$DRY_RUN" -eq 1 ] && return 0
printf "Staked %s tokens for %s\n" "$(format_amount "$amount")" "$address"
printf "Minted shares: %s (%s raw)\n" "$(human_or_raw "$output")" "$output"
}
run_unstake() {
shares=${1-}
address=${2-${IDENTITY:-}}
if [ -z "$shares" ]; then
prompt_value "Shares to unstake (raw units)" ""
shares=$REPLY_VALUE
fi
prompt_value "Staker address" "$address"
address=$REPLY_VALUE
output=$(run_contract unstake --staker "$address" --shares "$shares") || return 1
[ "$DRY_RUN" -eq 1 ] && return 0
printf "Burned shares: %s (%s raw)\n" "$(human_or_raw "$shares")" "$shares"
printf "Returned tokens: %s (%s raw)\n" "$(human_or_raw "$output")" "$output"
}
run_claim() {
address=${1-${IDENTITY:-}}
prompt_value "Staker address" "$address"
address=$REPLY_VALUE
output=$(run_contract claim --staker "$address") || return 1
[ "$DRY_RUN" -eq 1 ] && return 0
printf "Claimed reward for %s: %s (%s raw)\n" "$address" "$(human_or_raw "$output")" "$output"
}
run_position() {
address=${1-${IDENTITY:-}}
prompt_value "Address" "$address"
address=$REPLY_VALUE
stake_weight=$(run_contract current_vote_weight --user "$address") || return 1
pending_reward=$(run_contract calc_pending_reward --user "$address") || return 1
boost_multiplier=$(run_contract get_boost_multiplier --user "$address") || return 1
[ "$DRY_RUN" -eq 1 ] && return 0
printf "Address: %s\n" "$address"
printf "Staked shares: %s (%s raw)\n" "$(human_or_raw "$stake_weight")" "$stake_weight"
printf "Pending reward: %s (%s raw)\n" "$(human_or_raw "$pending_reward")" "$pending_reward"
printf "Boost multiplier: %s bps\n" "$boost_multiplier"
}
run_pending() {
address=${1-${IDENTITY:-}}
prompt_value "Address" "$address"
address=$REPLY_VALUE
output=$(run_contract calc_pending_reward --user "$address") || return 1
[ "$DRY_RUN" -eq 1 ] && return 0
printf "Pending reward for %s: %s (%s raw)\n" "$address" "$(human_or_raw "$output")" "$output"
}
run_pool_info() {
total_staked=$(run_contract total_staked) || return 1
vault_state=$(run_contract vault_state) || return 1
min_stake=$(run_contract get_min_stake) || return 1
reward_rate=$(run_contract get_reward_rate_bps) || return 1
reward_pool=$(run_contract get_reward_pool_balance) || return 1
withdrawal_limit=$(run_contract get_withdrawal_limit) || return 1
lock_config=$(run_contract get_lock_config) || return 1
boost_schedule=$(run_contract get_boost_schedule) || return 1
[ "$DRY_RUN" -eq 1 ] && return 0
printf "Pool info\n"
printf "Total staked shares: %s (%s raw)\n" "$(human_or_raw "$total_staked")" "$total_staked"
printf "Vault state: %s\n" "$vault_state"
printf "Minimum stake: %s (%s raw)\n" "$(human_or_raw "$min_stake")" "$min_stake"
printf "Reward APR: %s bps\n" "$reward_rate"
printf "Reward pool: %s (%s raw)\n" "$(human_or_raw "$reward_pool")" "$reward_pool"
printf "Withdrawal limit: %s (%s raw)\n" "$(human_or_raw "$withdrawal_limit")" "$withdrawal_limit"
printf "Lock config: %s\n" "$lock_config"
printf "Boost schedule: %s\n" "$boost_schedule"
}
choose_action() {
cat <<'EOF' >&2
Choose an action:
1. stake
2. unstake
3. claim
4. check position
5. check pending reward
6. pool info
EOF
prompt_value "Selection" "1"
case "$REPLY_VALUE" in
1|stake) ACTION=stake ;;
2|unstake) ACTION=unstake ;;
3|claim) ACTION=claim ;;
4|position) ACTION=position ;;
5|pending) ACTION=pending ;;
6|pool-info|pool_info) ACTION=pool-info ;;
*)
printf "Unknown selection: %s\n" "$REPLY_VALUE" >&2
exit 1
;;
esac
}
load_env
while [ $# -gt 0 ]; do
case "$1" in
--dry-run)
DRY_RUN=1
shift
;;
-h|--help)
show_help
exit 0
;;
*)
break
;;
esac
done
ACTION=${1-}
if [ -n "$ACTION" ]; then
shift
else
choose_action
fi
case "$ACTION" in
stake)
run_stake "${1-}" "${2-}"
;;
unstake)
run_unstake "${1-}" "${2-}"
;;
claim)
run_claim "${1-}"
;;
position)
run_position "${1-}"
;;
pending)
run_pending "${1-}"
;;
pool-info|pool_info)
run_pool_info
;;
*)
printf "Unknown command: %s\n" "$ACTION" >&2
show_help >&2
exit 1
;;
esac