-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun-auto-refill.sh
executable file
·128 lines (109 loc) · 4.74 KB
/
run-auto-refill.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
#!/bin/bash
REFILL_DEBUG=${REFILL_DEBUG:false}
if [[ -n "${REFILL_DEBUG}" ]] && "${REFILL_DEBUG}"; then
set -x
fi
# WEB3_RPC_URL=https://api.evm.qa01.dev.eclipsenetwork.xyz/
# REFILL_THRESHOLD="1000"
# REFILL_AMOUNT="100"
# REFILL_ADDRESS="0xbB41a3B478b08c356B1df3334d68C78720555453"
REFILL_TELEGRAM_TOKEN=${REFILL_TELEGRAM_TOKEN:-""}
REFILL_TELEGRAM_CHAT_ID=${REFILL_TELEGRAM_CHAT_ID:-""}
BALANCE_JSON=("{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBalance\",\"params\":[\"${REFILL_ADDRESS}\",\"latest\"],\"id\":1}")
REFILL_JSON=("{\"wallet\":\"${REFILL_ADDRESS}\",\"amount\":${REFILL_AMOUNT}}")
echo_log() {
echo "$(date "+%F %X.%3N") $*"
}
if [[ -z "${WEB3_RPC_URL}" ]]; then
echo_log "WEB3_RPC_URL not exist. exit."
exit 1
fi
if [[ -z "${REFILL_THRESHOLD}" ]]; then
echo_log "REFILL_THRESHOLD not exist. exit."
exit 1
fi
if [[ -z "${REFILL_AMOUNT}" ]]; then
echo_log "REFILL_AMOUNT not exist. exit."
exit 1
fi
if [[ -z "${REFILL_ADDRESS}" ]]; then
echo_log "REFILL_ADDRESS not exist. exit."
exit 1
fi
send_telegram_notification() {
local text
text="${1}"
tg_post_respond=$(curl -s -X POST https://api.telegram.org/bot${REFILL_TELEGRAM_TOKEN}/sendMessage \
-H "Content-Type: application/json" \
-d "{\"chat_id\": \"${REFILL_TELEGRAM_CHAT_ID}\", \"text\": \"${text}\", \"disable_notification\": false}")
echo_log "Telegram post respond: ${tg_post_respond}"
}
refill() {
local balance tg_message
while nc -z localhost "${FAUCET_RPC_PORT}" >/dev/null; do
echo_log "Stop faucet."
pkill -SIGINT faucet
sleep 1
done
while ! nc -z localhost "${FAUCET_RPC_PORT}" >/dev/null; do
echo_log "Start faucet."
NEON_ETH_PER_TIME_MAX_AMOUNT=${REFILL_AMOUNT} NEON_ETH_MAX_AMOUNT=${REFILL_AMOUNT} /run-faucet.sh &
sleep 5
done
# shellcheck disable=SC2128
before_balance=$(curl -s -H "Content-Type: application/json" -X POST --data "${BALANCE_JSON}" ${WEB3_RPC_URL} | jq -r ".result" | sed "s/0x//g")
before_balance=$(echo "ibase=16;obase=A;$(echo "${before_balance}" | tr '[:lower:]' '[:upper:]')" | bc)
echo_log "Before refill amount: ${before_balance}"
# Refill
# shellcheck disable=SC2128
refill_respond=$(curl -s -X POST http://localhost:${FAUCET_RPC_PORT}/request_neon -H 'Content-Type: application/json' -d "${REFILL_JSON}")
echo_log "Refill respond: ${refill_respond} wei"
# shellcheck disable=SC2128
after_balance=$(curl -s -H "Content-Type: application/json" -X POST --data "${BALANCE_JSON}" ${WEB3_RPC_URL} | jq -r ".result" | sed "s/0x//g")
after_balance=$(echo "ibase=16;obase=A;$(echo "${after_balance}" | tr '[:lower:]' '[:upper:]')" | bc)
echo_log "After refill amount: ${after_balance} wei"
if [[ "${REFILL_TELEGRAM_TOKEN}" != "" ]] && [[ "${REFILL_TELEGRAM_CHAT_ID}" != "" ]]; then
tg_message="Auto refill balance \n"
tg_message+="Address: ${REFILL_ADDRESS} \n"
tg_message+="Before balance: ${before_balance} wei \n"
tg_message+="After balance: ${after_balance} wei \n"
# tg_message+="After balance(eth) : ${after_balance:-18}\n"
send_telegram_notification "${tg_message}"
fi
while pidof faucet >/dev/null; do
echo_log "Stop faucet."
pkill -SIGINT faucet
sleep 1
done
}
main() {
local balance re
echo_log "Start auto-refill process."
while true; do
# shellcheck disable=SC2128
balance=$(curl -s -H "Content-Type: application/json" -X POST --data "${BALANCE_JSON}" ${WEB3_RPC_URL} | jq -r ".result" | sed "s/0x//g")
balance=$(echo "ibase=16;obase=A;$(echo "${balance}" | tr '[:lower:]' '[:upper:]')" | bc)
re='^[0-9]+$'
if ! [[ $balance =~ $re ]]; then
echo_log "Balance is not a number: \"${balance}\", retry after ${REFILL_TIME_PERIOD_SECS} second."
elif [[ ${#balance} -gt 18 ]]; then
if [[ ${balance:0:-18} -lt ${REFILL_THRESHOLD} ]]; then
echo_log "Balance ${balance:0:-18} is less than ${REFILL_THRESHOLD}, start re-fill amount ${REFILL_AMOUNT}."
refill
else
echo_log "Balance ${balance:0:-18} is greater equal than ${REFILL_THRESHOLD}, nothing to do."
fi
elif [[ ${#balance} -le 18 ]]; then
if [[ ${balance} -eq ${REFILL_THRESHOLD} ]]; then
echo_log "Balance ${balance} is equal refill threshold ${REFILL_THRESHOLD}, nothing to do."
else
echo_log "Balance ${balance} wei is less than 1+E18 wei, start re-fill amount."
refill
fi
else
echo_log "Balance: ${balance} ???, retry after ${REFILL_TIME_PERIOD_SECS} second."
fi
sleep "${REFILL_TIME_PERIOD_SECS}"
done
}
main "$@"