forked from flyworker/ether-test
-
Notifications
You must be signed in to change notification settings - Fork 8
/
csv_reader_test.go
118 lines (98 loc) · 2.97 KB
/
csv_reader_test.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
package main
import (
"bufio"
"context"
"crypto/ecdsa"
"math/big"
"os"
"strings"
"testing"
"time"
"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"
"github.com/joho/godotenv"
)
// ReadAddressesFromCSV reads Ethereum addresses from a CSV file.
func ReadAddressesFromCSV(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var addresses []string
scanner := bufio.NewScanner(file)
isHeader := true
for scanner.Scan() {
if isHeader {
// Skip the header
isHeader = false
continue
}
line := scanner.Text()
fields := strings.Split(line, ";")
if len(fields) > 0 {
addresses = append(addresses, fields[0])
}
}
return addresses, scanner.Err()
}
func TestSendEthToAddresses(t *testing.T) {
err := godotenv.Load()
if err != nil {
t.Fatalf("Error loading .env file: %v", err)
}
senderPrivateKey := os.Getenv("SENDER_PRIVATE_KEY")
rpcURL := os.Getenv("RPC_URL") // Make sure this is set in your .env file
if senderPrivateKey == "" {
t.Fatal("No private key found in .env file")
}
client, err := ethclient.Dial(rpcURL)
if err != nil {
t.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
defer client.Close()
privateKey, err := crypto.HexToECDSA(senderPrivateKey)
if err != nil {
t.Fatalf("Failed to parse private key: %v", err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
t.Fatal("Cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
t.Fatalf("Failed to get nonce: %v", err)
}
gasLimit := uint64(21000) // Standard limit for a transfer
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
t.Fatalf("Failed to suggest gas price: %v", err)
}
amount := big.NewInt(1e12) // 0.000001 ETH in Wei
addresses, err := ReadAddressesFromCSV("./../../../ethereum-address/000000000003.csv")
if err != nil {
t.Fatalf("Failed to read addresses from CSV: %v", err)
}
for _, recipientAddress := range addresses {
tx := types.NewTransaction(nonce, common.HexToAddress(recipientAddress), amount, gasLimit, gasPrice, nil)
chainID, err := client.NetworkID(context.Background())
if err != nil {
t.Fatalf("Failed to get network ID: %v", err)
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
t.Fatalf("Failed to sign transaction: %v", err)
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
t.Fatalf("Failed to send transaction to %s: %v", recipientAddress, err)
}
t.Logf("Transaction sent to %s: %s", recipientAddress, signedTx.Hash().Hex())
nonce++ // Increment nonce for next transaction
time.Sleep(2 * time.Second)
}
}