-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
290 lines (249 loc) · 7.48 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
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
package main
import (
types2 "arbitrum-claim/types"
"context"
"crypto/ecdsa"
"errors"
"log"
"math/big"
"os"
"runtime"
"strconv"
"strings"
"github.com/joho/godotenv"
"arbitrum-claim/distributor"
"arbitrum-claim/proxy"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
if len(os.Args) < 2 {
log.Fatal(`
Choose mode
live = Subscribe and waiting block target & claim
claim = execute only claim function
transfer = execute only transfer function. transfer all $ARB to receive address
`)
}
/*
* live = Subscribe and waiting block target
* claim = execute only claim function
* transfer = execute only transfer function. transfer all $ARB to receive address
*/
mode := os.Args[1]
var err error
err = godotenv.Load()
if err != nil {
log.Fatalf("Error getting env, not comming through %v", err)
}
config := &types2.Config{
ContractAddressArbitrum: os.Getenv("CONTRACT_ADDRESS_ARBITRUM"),
ContractAddressArbitrumProxy: os.Getenv("CONTRACT_ADDRESS_ARBITRUM_PROXY"),
ContractAddressTokenDistributor: os.Getenv("CONTRACT_ADDRESS_TOKENDISTRIBUTOR"),
WalletPrivateKeys: strings.Split(strings.TrimSpace(os.Getenv("WALLET_PRIVATE_KEYS")), ","),
EthRpcHttp: os.Getenv("ETH_RPC_HTTP"),
EthRpcWss: os.Getenv("ETH_RPC_WSS"),
ArbRpcHttp: os.Getenv("ARB_RPC_HTTP"),
ArbRpcWss: os.Getenv("ARB_RPC_WSS"),
ReceiveAddress: os.Getenv("RECEIVE_ADDRESS"),
TargetBlockNo: os.Getenv("TARGET_BLOCK_NO"),
Model: mode,
}
// Connect to the Ethereum node
ethClient, err := ethclient.Dial(config.EthRpcWss)
if err != nil {
log.Fatal(err)
}
// Connect to the Arbitrum node
arbClient, err := ethclient.Dial(config.ArbRpcHttp)
if err != nil {
log.Fatal(err)
}
// Load the smart contract
arbitrumContractProxy, err := proxy.NewArbitrumProxy(common.HexToAddress(config.ContractAddressArbitrumProxy), arbClient)
if err != nil {
log.Fatal(err)
}
distributorContract, err := distributor.NewTokenDistributor(common.HexToAddress(config.ContractAddressTokenDistributor), arbClient)
if err != nil {
log.Fatal(err)
}
arbitrumClient := &types2.ArbitrumClient{
EthClient: ethClient,
ArbClient: arbClient,
ArbitrumContractProxy: arbitrumContractProxy,
DistributorContract: distributorContract,
}
// 开始
Start(arbitrumClient, config)
//err = test.Test(arbitrumClient, config)
//if err != nil {
// log.Println(err.Error())
//}
}
func Start(arbitrumClient *types2.ArbitrumClient, config *types2.Config) {
if config.Model == "live" {
log.Printf("Starting Mode Live...\n")
Subscribing(arbitrumClient, config)
} else if config.Model == "claim" {
log.Printf("Starting Mode Claim...\n")
for _, privateHashKey := range config.WalletPrivateKeys {
// go routine
go Claim(arbitrumClient, config, privateHashKey)
}
} else if config.Model == "transfer" {
log.Printf("Starting Mode Transfer...\n")
for _, privateHashKey := range config.WalletPrivateKeys {
// go routine
go Transfer(arbitrumClient, config, privateHashKey, common.HexToAddress(config.ReceiveAddress))
}
}
}
func Subscribing(arbitrumClient *types2.ArbitrumClient, config *types2.Config) {
headers := make(chan *types.Header)
sub, err := arbitrumClient.EthClient.SubscribeNewHead(context.Background(), headers)
if err != nil {
log.Fatal(err)
}
// Cast block number
targetBlockNo, err := strconv.ParseUint(config.TargetBlockNo, 10, 64)
if err != nil {
panic(err)
}
// Subscribing Mainnet to New Blocks
for {
select {
case err := <-sub.Err():
log.Fatal(err)
case header := <-headers:
block, err := arbitrumClient.EthClient.BlockByHash(context.Background(), header.Hash())
if err != nil {
log.Fatal(err)
}
// Waiting block number is 16890400
log.Println("New block:", block.Number().Uint64())
if block.Number().Uint64() == targetBlockNo {
log.Printf("Starting Claim block number: %d\n", targetBlockNo)
// Claim
for _, v := range config.WalletPrivateKeys {
// go routine
go Claim(arbitrumClient, config, v)
}
} else if block.Number().Uint64() == targetBlockNo+1 { // retry claim
log.Printf("Starting Claim block number: %d\n", targetBlockNo+1)
// Claim
for _, v := range config.WalletPrivateKeys {
// go routine
go Claim(arbitrumClient, config, v)
}
}
}
}
}
func Claim(arbitrumClient *types2.ArbitrumClient, config *types2.Config, privateHashKey string) error {
// Load private key
privateKey, err := crypto.HexToECDSA(privateHashKey)
if err != nil {
return err
}
/*
* Get Nonce
*/
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return errors.New("Error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
/*
* Get gas price
*/
// gasPrice, err := client.SuggestGasPrice(context.Background())
// if err != nil {
// return err
// }
/*
* Get gas price
*/
// Prepare the transactions
chainID, _ := arbitrumClient.EthClient.ChainID(context.Background())
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
return err
}
txOptions := bind.TransactOpts{
From: fromAddress,
Signer: auth.Signer,
GasLimit: 300000,
GasPrice: big.NewInt(20 * 1e9),
}
// Execute a state-changing function (write) in the smart contract
tx, err := arbitrumClient.DistributorContract.Claim(&txOptions)
if err != nil {
return err
}
log.Printf("[Claim] Wallet: %s, Transaction hash: %s\n", fromAddress, tx.Hash().Hex())
err = Transfer(arbitrumClient, config, privateHashKey, common.HexToAddress(config.ReceiveAddress))
if err != nil {
return err
}
return nil
}
func Transfer(arbitrumClient *types2.ArbitrumClient, config *types2.Config, hexkey string, receiveAddress common.Address) error {
// Load private key
privateKey, err := crypto.HexToECDSA(hexkey)
if err != nil {
return err
}
/*
* Get Nonce
*/
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return errors.New("Error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
/*
* Get gas price
*/
// gasPrice, err := client.SuggestGasPrice(context.Background())
// if err != nil {
// return err
// }
/*
* Get gas price
*/
// Prepare the transactions
chainID, _ := arbitrumClient.EthClient.ChainID(context.Background())
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
return err
}
txOptions := bind.TransactOpts{
From: fromAddress,
Signer: auth.Signer,
GasLimit: 300000,
GasPrice: big.NewInt(20 * 1e9),
}
// Check balance
balance, err := arbitrumClient.ArbitrumContractProxy.BalanceOf(nil, fromAddress)
if err != nil {
return err
}
log.Printf("[Transfer] From: %s, To: %s, Amount: %d\n", fromAddress, receiveAddress, balance)
if len(balance.Bits()) == 0 {
return errors.New("blanace is zero")
}
// Execute a state-changing function (write) in the smart contract
tx, err := arbitrumClient.ArbitrumContractProxy.Transfer(&txOptions, receiveAddress, balance)
if err != nil {
return err
}
log.Printf("[Transfer] Transaction hash: %s\n", tx.Hash().Hex())
return nil
}