This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqb.sh
320 lines (304 loc) · 11.4 KB
/
qb.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
310
311
312
313
314
315
316
317
318
319
320
# shellcheck shell=sh disable=SC3043
# A fast and simple terminal plugin that can watch your BSC price in the terminal.
# https://github.com/Zhengqbbb/qb
# v1.0.0-beta1
# Copyright (c) 2021 Zhengqbbb
# License: MIT License
xrc param/v0
. $HOME/.qb/tool.sh
qb(){
param:dsl <<A
subcommands:
ls "list all your coins"
add "add coin address in your coins list"
star "put the selected coin on top"
del "delete coin in your coins list"
timer "change the refresh timer"
proxy "edit the address to use socket5 proxy"
account "load BSC address and check BSC balance"
A
param:run
xrc ui
xrc json
if [ "$PARAM_SUBCMD" = "help" ]; then
qb _param_help_doc
return 0
fi
local qb_source_path="$HOME/.qb"
local qb_data
local coin_list
local coin_list_length=0
local _acc_address
qb_data="$(cat "$qb_source_path/data.json")"
if [ -z "$qb_data" ] ; then
___qb_error_log_info "Empty source file: $qb_source_path/data.json"
return 1
fi
coin_list="$(json query qb_data.coins)"
coin_list_length="$(json length coin_list)"
_acc_address="$(___qb_json_unquote "$(json query qb_data.accountAddress)")"
if [ -z "$PARAM_SUBCMD" ]; then
___qb_control_run
return 0
fi
"___qb_control_${PARAM_SUBCMD}" "$@"
}
___qb_json_unquote() { local _qb_handed="$1"; _qb_handed="${_qb_handed#*\"}"; printf "%s" "${_qb_handed%\"*}";}
___qb_control_ls() {
local i=0
___qb_printf_line
if [ -z "$coin_list_length" ] || [ "$coin_list_length" -eq 0 ]; then
___qb_info_log_info 'Empty data list'
fi
while [ $i -lt "$coin_list_length" ]; do
printf "%-10s %-40s %s\n" \
"$(ui bold yellow "$((i+1)) ➜")" \
"$(___qb_printf_key_value 'Name' "$(___qb_json_unquote "$(json query coin_list.\[${i}\].name)")")" \
"$(ui bold yellow "➜") $(___qb_printf_key_value 'Address' "$(___qb_json_unquote "$(json query coin_list.\[${i}\].address)")")"
i=$((i+1))
done
___qb_printf_line
unset i
}
______qb_account_is_empty() { [ -z "$_acc_address" ] || [ "$_acc_address" = 'default' ] ; }
___qb_control_account() {
local _balance
local _time
______qb_control_use_proxy
if [ "$1" = 'edit' ];then
___qb_control_account ls
ui prompt "$(ui bold green 'Enter your BSC account address')" _acc_address
[ -z "$_acc_address" ] && _acc_address='default'
json put qb_data.accountAddress "${_acc_address}" > /dev/null
printf "%s\n" "$qb_data" > "$qb_source_path/data.json"
[ "$_acc_address" = 'default' ] && ___qb_success_log_info "Your BSC account has been $(ui bold cyan 'logouted')" && return
___qb_success_log_info "save your BSC account successfully !"
return 0
elif [ "$1" = 'ls' ]; then
______qb_account_is_empty && return 1
___qb_printf_line
printf "%s\n" "$(ui bold yellow "➜ ")$(___qb_printf_key_value "BSC-Address" "$_acc_address")"
___qb_printf_line
return 0
elif [ "$1" != 'val' ];then
______qb_account_is_empty && ___qb_info_log_info "Need to add your account address" && ___qb_control_account edit
qb account ls
___qb_info_log_info "Loadding BSC address balance..."
___qb_info_log_info "BSC Balance info from: $(ui bold underline yellow https://bscscan.com/address/"$_acc_address")"
fi
______qb_account_is_empty && return 0
_balance="$(______qb_account_get_account_balance "$_acc_address")"
_time="$(date +%H:%M:%S)"
printf "\n%s %s %s %s\n" \
"$(ui bold cyan "[$_time]")" \
"$(ui bold yellow "BSC Balance")" \
"$(ui bold cyan "➜")" \
"$(ui bold green "$_balance")"
unset _time
}
###
# @param {*} BSC address
# @return {*} coin usdt price balance
###
______qb_account_get_account_balance() {
local _balance
_balance=$(curl --connect-timeout 8 -m 15 https://bscscan.com/address/"$1" 2>/dev/null | awk '
BEGIN{ _index = 0 }
{
if (_index != 0) {
printf $0
exit
}
isSuccess = match($0, /^<a id="availableBalanceDropdown"/);
if (isSuccess != 0) {
_index = FNR + 1
}
}')
[ -n "$_balance" ] && printf "%s" "$_balance" && return
___qb_printf_net_warm "https://bscscan.com/address/$1"
}
___qb_control_add() {
local _coin_address
local _data
local _name
ui prompt "$(ui bold green 'Enter BSC coin address')" _coin_address
if [ -z "$_coin_address" ];then
___qb_error_log_info "Empty data"
return 1
fi
___qb_info_log_info "Loading coin data..."
______qb_control_use_proxy
_data="$(curl --connect-timeout 10 -m 18 https://api.pancakeswap.info/api/v2/tokens/"$_coin_address" 2>/dev/null)" 2>/dev/null
_name=$(json q _data.data.symbol 2>/dev/null)
if [ -z "$_data" ] || [ -z "$_name" ];then
___qb_printf_net_warm "https://api.pancakeswap.info/api/v2/tokens/${_coin_address}"
return 1
fi
local _item
_item=$(printf "%s" "{\"name\": ${_name},\"address\": \"${_coin_address}\"}")
json push qb_data.coins "$_item"
printf "%s\n" "$qb_data" > "$qb_source_path/data.json"
___qb_success_log_info "$(ui bold yellow "$_name"), add coin list successfully"
qb ls
unset _coin_address _data _name _item
}
______qb_control_list_by_index() {
___qb_control_ls
local _index
local _has_data
local _name
local _is_sure
local _item
if [ -z "$coin_list_length" ] || [ "$coin_list_length" -eq 0 ]; then
___qb_info_log_info 'Empty data list'
return 1
fi
ui prompt "$(ui bold green "Enter the index of the list you want to $1")" _index "=~" "[0-9]*"
_index=$((_index-1))
[ "$coin_list_length" -gt "$_index" ] && _has_data='true'
if [ -z $_has_data ];then
___qb_error_log_info "Error Index: $((_index+1))"
return 1
fi
_name="$(json q coin_list.\[${_index}\].name)"
if [ "$1" = 'delete' ]; then
ui prompt "Are you sure you want to $1 $(ui bold yellow "$_name") (y/n)" _is_sure
if [ -n "$_is_sure" ] && [ "$_is_sure" != 'y' ];then
return 1
fi
qb_data=$(json del qb_data.coins.\[${_index}\])
___qb_success_log_info "Successfully deleted $(ui bold yellow "$_name") $(ui bold green 'in your list')"
printf "%s\n" "$qb_data" > "$qb_source_path/data.json"
else
_item="$(json q coin_list.\[${_index}\])"
qb_data=$(json del qb_data.coins.\[${_index}\])
json prepend qb_data.coins "$_item"
___qb_success_log_info "Successfully star $(ui bold yellow "$_name") $(ui bold green 'in your list')"
printf "%s\n" "$qb_data" > "$qb_source_path/data.json"
fi
unset _index _has_data _name _is_sure _item
return 0
}
___qb_control_star() {
______qb_control_list_by_index 'star'
}
___qb_control_del() {
______qb_control_list_by_index 'delete'
}
___qb_control_timer() {
local _timer
_timer="$(___qb_json_unquote "$(json q qb_data.timer)")"
[ -n "$_timer" ] && ___qb_info_log_info "Your refresh timer is $_timer s"
ui prompt "$(ui bold green 'Enter the set refresh timer(s)')" _timer "=~" "[0-9]*"
json put qb_data.timer "${_timer}" > /dev/null
printf "%s\n" "$qb_data" > "$qb_source_path/data.json"
___qb_success_log_info "Your refresh timer(s) is setted: $(ui bold yellow "${_timer}")"
unset _timer
return 0
}
___qb_control_proxy() {
local _host
local _port
local _address
_host="$(___qb_json_unquote "$(json q qb_data.proxy.host)")"
if [ -n "$_host" ] && [ "$_host" != 'unset' ] && [ "$_host" != 'default' ];then
_host=$(___qb_json_unquote "$_host")
_port=$(___qb_json_unquote "$(json q qb_data.proxy.port)")
_address="socks5://$_host:$_port"
___qb_info_log_info "Your socket5 proxy address is $(ui underline bold yellow "$_address")"
fi
ui prompt "$(ui bold green 'Enter the host of the proxy.Such as ')$(ui bold underline yellow '127.0.0.1')" _host
ui prompt "$(ui bold green 'Enter the port of the proxy.Such as ')$(ui bold underline yellow '1086')" _port
if [ -z "$_host" ] && [ -z "$_port" ];then
json put qb_data.proxy.host 'unset' > /dev/null
json put qb_data.proxy.port 'unset' > /dev/null
printf "%s\n" "$qb_data" > "$qb_source_path/data.json"
export ALL_PROXY=
export all_proxy=
___qb_success_log_info "Your socket5 proxy proxy has been $(ui bold cyan 'cancelled')"
return 0
fi
_address="socks5://$_host:$_port"
json put qb_data.proxy.host "$_host" > /dev/null
json put qb_data.proxy.port "$_port" > /dev/null
printf "%s\n" "$qb_data" > "$qb_source_path/data.json"
___qb_success_log_info "Your socket5 proxy address is setted: $(ui bold underline yellow "${_address}")"
unset _has_host _host _port _address
return 0
}
______qb_control_use_proxy() {
local _host
local _port
_host=$(___qb_json_unquote "$(json q qb_data.proxy.host)")
_port=$(___qb_json_unquote "$(json q qb_data.proxy.port)")
if [ "${_host}" = 'unset' ] && [ "${_port}" = 'unset' ]; then
{ [ -n "$ALL_PROXY" ] || [ -n "$all_proxy" ] ; } && export ALL_PROXY= && export all_proxy=
return 0
elif [ -n "$_host" ] && [ "${_host}" != 'default' ];then
export ALL_PROXY=socks5://"${_host}":"${_port}"
export all_proxy=socks5://"${_host}":"${_port}"
fi
unset _host _port
}
___qb_control_run() {
local i
local _timer
___qb_draw_logo
_timer=$(___qb_json_unquote "$(json q qb_data.timer)")
[ -z "$_timer" ] && _timer=10
______qb_control_use_proxy
___qb_printf_line
if [ -z "$coin_list_length" ] || [ "$coin_list_length" -eq 0 ]; then
___qb_info_log_info 'Empty data list'
___qb_printf_line
return 1
fi
while true; do
i=0
while [ $i -lt "$coin_list_length" ]; do
local _address
local _name
local _data
local _usdt_price
local _time
_address=$(___qb_json_unquote "$(json q qb_data.coins.\[${i}\].address)")
_name=$(___qb_json_unquote "$(json q qb_data.coins.\[${i}\].name)")
_data="$(curl --connect-timeout 8 -m 15 https://api.pancakeswap.info/api/v2/tokens/"$_address" 2>/dev/null)" 2>/dev/null
[ -n "$_data" ] && _usdt_price=$(___qb_json_unquote "$(json q _data.data.price)")
_time="$(date +%H:%M:%S)"
if [ -z "$_data" ] || [ -z "$_usdt_price" ];then
___qb_printf_net_warm "https://api.pancakeswap.info/api/v2/tokens/${_address}"
unset _address _name _data _usdt_price _time
break
fi
[ -n "$_usdt_price" ] && printf "%s %-17s %s %s\n" \
"$(ui bold cyan "[$_time]")" \
"$(ui bold yellow "$_name")" \
"$(ui bold cyan "➜")" \
"$(ui bold yellow 'USDT price:') $(ui bold green "$_usdt_price")"
i=$((i+1))
unset _address _name _data _usdt_price _time
done
___qb_control_account val
___qb_printf_line
sleep "$_timer"
done
}
if [ -n "${BASH_VERSION}${ZSH_VERSION}" ] && [ "${-#*i}" != "$-" ]; then
xrc advise
advise qb - <<A
{
"ls": null,
"account": [
"ls",
"edit"
]
"add": null,
"star": null,
"del": null,
"timer": null,
"proxy": null,
"help": null
}
A
fi