-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (187 loc) · 5.23 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
package main
import (
"bufio"
"context"
"crypto/ecdsa"
"flag"
"fmt"
"log/slog"
"math"
"math/big"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/lfz941/ERC20TokenRescue/erc20"
"github.com/lfz941/ERC20TokenRescue/input"
)
type RPCCli struct {
RPC string
Cli *ethclient.Client
}
var defaultRPCEndpoints = []string{
"https://rpc.ankr.com/eth",
"https://ethereum.rpc.subquery.network/public",
"https://ethereum-rpc.publicnode.com",
"https://cloudflare-eth.com/",
"https://1rpc.io/eth",
"https://eth-mainnet.public.blastapi.io",
"https://eth.drpc.org",
"https://gateway.tenderly.co/public/mainnet",
"https://eth-mainnet-public.unifra.io",
}
var (
masterRpcCli *ethclient.Client
)
var (
rpcEndpoints = flag.String("nodes", "", "multiple nodes can be splited with comma. there are default eth rpc endpoints if nodes is empty")
token = flag.String("token", "0xec53bf9167f50cdeb3ae105f56099aaab9061f83", "erc20 token addr")
privateKeyHex = flag.String("priv", "", "priv key") // TODO change to interactive input
targetAddrHex = flag.String("to", "", "the target address that erc20 token should transfer to")
startAt = flag.Int64("begin", 0, "schedule the begin timestamp, unit(s)")
)
func main() {
flag.Parse()
// check flag value
if *privateKeyHex == "" {
panic("the priv key shouldn't be empty")
}
if *targetAddrHex == "" {
panic("the target address shouldn't be empty")
}
privateKey, err := crypto.HexToECDSA(*privateKeyHex)
if err != nil {
panic(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
panic("invalid priv key")
}
accAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
targetAddr := common.HexToAddress(*targetAddrHex)
usedRPCEndpoints := defaultRPCEndpoints
if *rpcEndpoints != "" {
usedRPCEndpoints = strings.Split(*rpcEndpoints, ",")
}
// init rpcClients
rpcClients := make([]RPCCli, 0)
for i, endpoint := range usedRPCEndpoints {
client := initEthCli(i, endpoint)
if client != nil {
rpcClients = append(rpcClients, RPCCli{usedRPCEndpoints[i], client})
}
}
if len(rpcClients) == 0 {
panic("no valid rpc endpoint")
}
masterRpcCli = rpcClients[0].Cli
ctx, stop := signal.NotifyContext(
context.Background(),
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGABRT,
syscall.SIGKILL,
)
defer stop()
chainID, err := masterRpcCli.ChainID(ctx)
if err != nil {
panic(err)
}
tokenAddr := common.HexToAddress(*token)
// init erc20 token abi instance
erc20TokenIns, err := erc20.NewErc20(tokenAddr, masterRpcCli)
if err != nil {
panic(err)
}
// get token info
tokenSymbol, err := erc20TokenIns.Symbol(nil)
if err != nil {
panic(err)
}
tokenDecimal, err := erc20TokenIns.Decimals(nil)
if err != nil {
panic(err)
}
bal, err := erc20TokenIns.BalanceOf(nil, accAddr)
if err != nil {
panic(err)
}
balStr, _ := formatTokenAmtToHumanReadable(bal, int(tokenDecimal), 2)
slog.With("b", balStr+tokenSymbol).Info("bal info")
yes, err := input.GetConfirmation(fmt.Sprintf("Please confirm the rescue info: \n chainID: %d \n user: %s \n erc20 token: %s(%s) \n bal: %s \n", chainID.Int64(), accAddr.Hex(), tokenSymbol, tokenAddr.Hex(), balStr), bufio.NewReader(os.Stderr), os.Stderr)
if err != nil {
panic(err)
}
if !yes {
return
}
now := time.Now().Unix()
if *startAt > now {
waitDur := time.Duration(now-*startAt) * time.Second
fmt.Printf("rescue job was scheduled starting after %v\n", waitDur)
<-time.After(waitDur)
}
fmt.Println("start rescue job now")
for {
select {
case <-ctx.Done():
slog.Info("process exit")
return
default:
transactOpts, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
slog.With("err", err).Error("NewKeyedTransactorWithChainID error")
continue
}
var (
wg sync.WaitGroup
txHash string
)
wg.Add(len(rpcClients))
for i := range rpcClients {
go func(i int) {
defer wg.Done()
erc20Ins, err := erc20.NewErc20(tokenAddr, rpcClients[i].Cli)
if err != nil {
slog.With("err", err, "rpc", rpcClients[i].RPC).Error("NewErc20 error")
return
}
tx, err := erc20Ins.Transfer(transactOpts, targetAddr, bal)
if err != nil {
slog.With("err", err, "rpc", rpcClients[i].RPC).Error("transfer erc20 token error")
return
}
txHash = tx.Hash().String()
}(i)
}
wg.Wait()
if txHash != "" {
slog.With("tx", txHash).Info("transfer erc20 token success")
}
}
}
}
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 initEthCli(idx int, rpc string) *ethclient.Client {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
ethCli, err := ethclient.DialContext(ctx, rpc)
if err != nil {
slog.With("err", err, "idx", idx, "rpc", rpc).Error("dial ether rpc client error")
return nil
}
return ethCli
}