-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (191 loc) · 6.13 KB
/
main.go
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
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log/slog"
"math"
"math/big"
"net/http"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
const MIN_ERC20_ABI_JSON = `[{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]`
var (
ethCli *ethclient.Client
_erc20ABI abi.ABI
)
var (
node = flag.String("node", "https://rpc.merlinchain.io/", "")
token = flag.String("token", "0x967aEC3276b63c5E2262da9641DB9dbeBB07dC0d", "")
addr = flag.String("addr", "0x25aB3Efd52e6470681CE037cD546Dc60726948D3", "")
addrAlias = flag.String("addrName", "Meson", "")
threshold = flag.Float64("threshold", 2000, "")
interval = flag.String("interval", "3s", "")
dingToken = flag.String("dingToken", "", "")
muteDing = flag.Bool("mute", false, "")
)
func main() {
flag.Parse()
var err error
// check flag value
if !*muteDing && *dingToken == "" {
panic("dingToken shouldn't be empty when mute isn't true")
}
ethCli, err = ethclient.Dial(*node)
if err != nil {
panic(err)
}
_erc20ABI, err = abi.JSON(strings.NewReader(MIN_ERC20_ABI_JSON))
if err != nil {
panic(err)
}
ctx, stop := signal.NotifyContext(
context.Background(),
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGABRT,
syscall.SIGKILL,
)
defer stop()
tokenAddr := common.HexToAddress(*token)
accAddr := common.HexToAddress(*addr)
tokenSymbol, err := getERC20TokenSymbol(ctx, ethCli, tokenAddr)
if err != nil {
panic(err)
}
tokenDecimal, err := getERC20TokenDecimals(ctx, ethCli, tokenAddr)
if err != nil {
panic(err)
}
slog.With("decimals", tokenDecimal, "symbol", tokenSymbol, "tokenAddr", tokenAddr.Hex(), "accAddr", accAddr.Hex(), "accAlias", *addrAlias).Info("start monitor erc20 balance info")
intervalDur, err := time.ParseDuration(*interval)
if err != nil {
panic(err)
}
ticker := time.NewTicker(intervalDur)
for {
select {
case <-ctx.Done():
slog.Info("process exit")
return
case <-ticker.C:
bal, err := getERC20TokenBalance(context.Background(), ethCli, tokenAddr, accAddr)
if err != nil {
continue
}
balStr, balF := formatTokenAmtToHumanReadable(bal, int(tokenDecimal), 2)
slog.With("b", balStr+tokenSymbol).Info("bal info")
if balF >= *threshold && !*muteDing {
dingDingNotify(*dingToken, fmt.Sprintf("%s %s balance %v >= %v", *addrAlias, tokenSymbol, balStr, *threshold), true, nil)
}
}
}
}
func getERC20TokenDecimals(ctx context.Context, ethCli *ethclient.Client, token common.Address) (uint8, error) {
data, err := _erc20ABI.Pack("decimals")
if err != nil {
slog.With("err", err).Error("getERC20TokenDecimals pack error")
return 0, err
}
callMsg := ethereum.CallMsg{To: &token, Data: data}
result, err := ethCli.CallContract(ctx, callMsg, nil)
if err != nil {
slog.With("err", err).Error("getERC20TokenDecimals error")
return 0, err
}
valueAry, err := _erc20ABI.Unpack("decimals", result)
if err != nil {
slog.With("err", err).Error("getERC20TokenDecimals unpack error")
return 0, err
}
return valueAry[0].(uint8), nil
}
func getERC20TokenSymbol(ctx context.Context, ethCli *ethclient.Client, token common.Address) (string, error) {
data, err := _erc20ABI.Pack("symbol")
if err != nil {
slog.With("err", err).Error("getERC20TokenSymbol pack error")
return "", err
}
callMsg := ethereum.CallMsg{To: &token, Data: data}
result, err := ethCli.CallContract(ctx, callMsg, nil)
if err != nil {
slog.With("err", err).Error("getERC20TokenSymbol error")
return "", err
}
valueAry, err := _erc20ABI.Unpack("symbol", result)
if err != nil {
slog.With("err", err).Error("getERC20TokenSymbol unpack error")
return "", err
}
return valueAry[0].(string), nil
}
func getERC20TokenBalance(ctx context.Context, ethCli *ethclient.Client, token common.Address, acc common.Address) (*big.Int, error) {
data, err := _erc20ABI.Pack("balanceOf", acc)
if err != nil {
slog.With("err", err).Error("getERC20TokenBalance pack error")
return nil, err
}
callMsg := ethereum.CallMsg{To: &token, Data: data}
result, err := ethCli.CallContract(ctx, callMsg, nil)
if err != nil {
slog.With("err", err).Error("getERC20TokenBalance error")
return nil, err
}
valueAry, err := _erc20ABI.Unpack("balanceOf", result)
if err != nil {
slog.With("err", err).Error("getERC20TokenBalance unpack error")
return nil, err
}
return valueAry[0].(*big.Int), nil
}
func formatTokenAmtToHumanReadable(amt *big.Int, decimal int, prec int) (string, float64) {
amtBigFloat := new(big.Float).Quo(new(big.Float).SetInt(amt), big.NewFloat(math.Pow10(decimal)))
amtStr := amtBigFloat.Text('f', prec)
amtF, _ := strconv.ParseFloat(amtStr, 64)
return amtStr, amtF
}
func dingDingNotify(token, content string, isAtAll bool, atMobiles []string) {
url := "https://oapi.dingtalk.com/robot/send?access_token=" + token
if !isAtAll && len(atMobiles) > 0 {
atMobileStr := "@" + strings.Join(atMobiles, " @")
content = content + " \n " + atMobileStr
}
params := map[string]any{
"msgtype": "text",
"text": map[string]any{
"content": content,
},
"at": map[string]any{
"isAtAll": isAtAll,
"atMobiles": atMobiles,
},
}
paramsJson, err := json.Marshal(params)
if err != nil {
slog.With("err", err).Error("dingDingNotify marshal params error")
return
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(paramsJson))
if err != nil {
slog.With("err", err).Error("dingDingNotify send request error")
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
slog.With("err", err).Error("dingDingNotify read response error")
return
}
fmt.Println("ding notify rsp", string(body))
}