-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathfund.go
80 lines (69 loc) · 2.5 KB
/
fund.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
package onchain
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"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/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/framework/clclient"
"github.com/smartcontractkit/chainlink-testing-framework/seth"
)
func SendETH(client *ethclient.Client, privateKeyHex string, toAddress string, amount *big.Float) error {
privateKey, err := crypto.HexToECDSA(privateKeyHex)
if err != nil {
return fmt.Errorf("failed to parse private key: %w", err)
}
wei := new(big.Int)
amountWei := new(big.Float).Mul(amount, big.NewFloat(1e18))
amountWei.Int(wei)
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return fmt.Errorf("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return fmt.Errorf("failed to fetch nonce: %w", err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return fmt.Errorf("failed to fetch gas price: %w", err)
}
gasLimit := uint64(21000) // Standard gas limit for ETH transfer
tx := types.NewTransaction(nonce, common.HexToAddress(toAddress), wei, gasLimit, gasPrice, nil)
chainID, err := client.NetworkID(context.Background())
if err != nil {
return fmt.Errorf("failed to fetch chain ID: %w", err)
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
return fmt.Errorf("failed to sign transaction: %w", err)
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return fmt.Errorf("failed to send transaction: %w", err)
}
framework.L.Info().Msgf("Transaction sent: %s", signedTx.Hash().Hex())
return nil
}
func FundNodes(sc *seth.Client, nodes []*clclient.ChainlinkClient, pkey string, ethAmount float64) error {
if ethAmount == 0 {
return errors.New("funds_eth is 0, set some value in config, ex.: funds_eth = 30.0")
}
for _, cl := range nodes {
ek, err := cl.ReadPrimaryETHKey(fmt.Sprint(sc.Cfg.Network.ChainID))
if err != nil {
return err
}
if err := SendETH(sc.Client, pkey, ek.Attributes.Address, big.NewFloat(ethAmount)); err != nil {
return fmt.Errorf("failed to fund CL node %s: %w", ek.Attributes.Address, err)
}
}
return nil
}