Skip to content

Commit

Permalink
Read primary ETH key based on chain id (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gopalswami authored Dec 16, 2024
1 parent ea4ffd8 commit d9c773e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
9 changes: 7 additions & 2 deletions framework/clclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,20 @@ func (c *ChainlinkClient) UpdateEthKeyMaxGasPriceGWei(keyId string, gWei int) (*
}

// ReadPrimaryETHKey reads updated information about the Chainlink's primary ETH key
func (c *ChainlinkClient) ReadPrimaryETHKey() (*ETHKeyData, error) {
func (c *ChainlinkClient) ReadPrimaryETHKey(chainId string) (*ETHKeyData, error) {
ethKeys, err := c.MustReadETHKeys()
if err != nil {
return nil, err
}
if len(ethKeys.Data) == 0 {
return nil, fmt.Errorf("Error retrieving primary eth key on node %s: No ETH keys present", c.URL())
}
return &ethKeys.Data[0], nil
for _, data := range ethKeys.Data {
if data.Attributes.ChainID == chainId {
return &data, nil
}
}
return nil, fmt.Errorf("error retrieving primary eth key on node %s: No ETH keys present for chain %s", c.URL(), chainId)
}

// ReadETHKeyAtIndex reads updated information about the Chainlink's ETH key at given index
Expand Down
21 changes: 13 additions & 8 deletions framework/components/simple_node_set/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
er "github.com/pkg/errors"
"github.com/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/framework/clclient"
"math/big"
Expand All @@ -18,7 +19,7 @@ import (
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: %v", err)
return er.Wrap(err, "failed to parse private key")
}
wei := new(big.Int)
amountWei := new(big.Float).Mul(amount, big.NewFloat(1e18))
Expand All @@ -33,29 +34,29 @@ func SendETH(client *ethclient.Client, privateKeyHex string, toAddress string, a

nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return fmt.Errorf("failed to fetch nonce: %v", err)
return er.Wrap(err, "failed to fetch nonce")
}

gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return fmt.Errorf("failed to fetch gas price: %v", err)
return er.Wrap(err, "failed to fetch gas price")
}
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: %v", err)
return er.Wrap(err, "failed to fetch chain ID")
}
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
return fmt.Errorf("failed to sign transaction: %v", err)
return er.Wrap(err, "failed to sign transaction")
}

err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
return fmt.Errorf("failed to send transaction: %v", err)
return er.Wrap(err, "failed to send transaction")
}
framework.L.Info().Msgf("Transaction sent: %s", signedTx.Hash().Hex())
_, err = bind.WaitMined(context.Background(), client, signedTx)
Expand All @@ -67,13 +68,17 @@ func FundNodes(c *ethclient.Client, nodes []*clclient.ChainlinkClient, pkey stri
if ethAmount == 0 {
return errors.New("funds_eth is 0, set some value in config, ex.: funds_eth = 30.0")
}
chainID, err := c.ChainID(context.Background())
if err != nil {
return er.Wrap(err, "failed to fetch chain ID")
}
for _, cl := range nodes {
ek, err := cl.ReadPrimaryETHKey()
ek, err := cl.ReadPrimaryETHKey(chainID.String())
if err != nil {
return err
}
if err := SendETH(c, pkey, ek.Attributes.Address, big.NewFloat(ethAmount)); err != nil {
return fmt.Errorf("failed to fund CL node %s: %w", ek.Attributes.Address, err)
return er.Wrapf(err, "failed to fund CL node %s", ek.Attributes.Address)
}
}
return nil
Expand Down

0 comments on commit d9c773e

Please sign in to comment.