Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-sheep-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink-deployments-framework": patch
---

Updated evm nodes fund command
Comment thread
finleydecker marked this conversation as resolved.
Outdated
62 changes: 47 additions & 15 deletions engine/cld/legacy/cli/commands/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
chainsel "github.com/smartcontractkit/chain-selectors"
nodev1 "github.com/smartcontractkit/chainlink-protos/job-distributor/v1/node"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -232,15 +233,22 @@ var (
`)

evmNodesFundExample = cli.Examples(`
# Fund all nodes with at least 0.5 ETH (in wei) on chain 1 in staging
exemplar evm nodes fund --environment staging --selector 1 --amount 500000000000000000 --1559
# Fund all nodes with at least 0.5 ETH on chain 1 in staging (using --eth flag)
exemplar evm nodes fund --environment staging --selector 1 --eth 0.5 --1559

# Fund all nodes with 100 ETH
exemplar evm nodes fund --environment staging --selector 1 --eth 100

# Fund all nodes with specific wei amount (using --amount flag)
exemplar evm nodes fund --environment staging --selector 1 --amount 10000000000000000000
`)
)

func (c Commands) newEvmNodesFund(domain domain.Domain) *cobra.Command {
var (
amountStr string
use1559 bool
ethAmount string
weiAmount string
use1559 bool
)

cmd := cobra.Command{
Expand All @@ -251,6 +259,15 @@ func (c Commands) newEvmNodesFund(domain domain.Domain) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
envKey, _ := cmd.Flags().GetString("environment")
chainselector, _ := cmd.Flags().GetUint64("selector")

// Check that exactly one of --eth or --amount is provided
if ethAmount != "" && weiAmount != "" {
return errors.New("cannot use both --eth and --amount flags. Use --eth for ETH amounts (e.g., --eth 10) or --amount for wei amounts")
}

if ethAmount == "" && weiAmount == "" {
return errors.New("either --eth or --amount flag is required. Use --eth for ETH amounts (e.g., --eth 10) or --amount for wei amounts")
}

env, err := environment.Load(cmd.Context(), domain, envKey,
environment.WithLogger(c.lggr),
Expand All @@ -263,9 +280,23 @@ func (c Commands) newEvmNodesFund(domain domain.Domain) *cobra.Command {
return fmt.Errorf("chain not found for selector %d", chainselector)
}
chain := env.BlockChains.EVMChains()[cs.Selector]
targetAmount, success := big.NewInt(0).SetString(amountStr, 10)
if !success {
return errors.New("invalid amount")

var targetAmount *big.Int
if ethAmount != "" {
// Parse amount as ETH and convert to wei
targetAmountEth, success := big.NewFloat(0).SetString(ethAmount)
if !success {
return errors.New("invalid ETH amount")
}
targetAmountWei := new(big.Float).Mul(targetAmountEth, big.NewFloat(params.Ether))
targetAmount, _ = targetAmountWei.Int(nil)
Comment thread
finleydecker marked this conversation as resolved.
} else {
// Parse amount as wei
var success bool
targetAmount, success = big.NewInt(0).SetString(weiAmount, 10)
if !success {
return errors.New("invalid wei amount")
}
}
for _, node := range env.NodeIDs {
chainConfigs, err := env.Offchain.ListNodeChainConfigs(cmd.Context(),
Expand All @@ -289,15 +320,15 @@ func (c Commands) newEvmNodesFund(domain domain.Domain) *cobra.Command {
cmd.Println("Skipping bootstrap node", node)
continue
}
b, err := chain.Client.BalanceAt(cmd.Context(), common.HexToAddress(chainConfig.AccountAddress), nil)
if err != nil {
return fmt.Errorf("failed to get balance: %w", err)
}
cmd.Printf("Current balance %d for %s on node %s", b, chainConfig.AccountAddress, node)
// Let's fund the difference.
b, err := chain.Client.BalanceAt(cmd.Context(), common.HexToAddress(chainConfig.AccountAddress), nil)
if err != nil {
return fmt.Errorf("failed to get balance: %w", err)
}
cmd.Printf("Current balance %d for %s on node %s\n", b, chainConfig.AccountAddress, node)
Comment thread
finleydecker marked this conversation as resolved.
Outdated
Comment thread
finleydecker marked this conversation as resolved.
Outdated
// Let's fund the difference.
if b.Cmp(targetAmount) < 0 {
amount := big.NewInt(0).Sub(targetAmount, b)
cmd.Printf("Current balance insufficient, funding node %s's address %s with %d", node, chainConfig.AccountAddress, amount)
cmd.Printf("Current balance insufficient, funding node %s's address %s with %d\n", node, chainConfig.AccountAddress, amount)
Comment thread
finleydecker marked this conversation as resolved.
Comment thread
finleydecker marked this conversation as resolved.
err = sendGas(cmd.Context(), c.lggr, chain, amount.String(), chainConfig.AccountAddress, use1559)
if err != nil {
return fmt.Errorf("failed to send gas: %w", err)
Expand All @@ -310,7 +341,8 @@ func (c Commands) newEvmNodesFund(domain domain.Domain) *cobra.Command {
},
}

cmd.Flags().StringVarP(&amountStr, "amount", "a", "", "Target amount of gas to ensure for each node")
cmd.Flags().StringVar(&ethAmount, "eth", "", "Target amount in ETH to ensure for each node (e.g., 10 for 10 ETH)")
cmd.Flags().StringVarP(&weiAmount, "amount", "a", "", "Target amount in wei to ensure for each node (e.g., 10000000000000000000 for 10 ETH)")
cmd.Flags().BoolVar(&use1559, "1559", false, "Use EIP-1559 transaction")

return &cmd
Expand Down
10 changes: 7 additions & 3 deletions engine/cld/legacy/cli/commands/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,17 @@ func TestNewEvmNodesFund_Metadata(t *testing.T) {
require.Contains(t, cmd.Short, "Ensure all nodes have a certain amount of gas")
require.Contains(t, cmd.Long, "Ensure all OCR2 nodes have a target amount of gas in their account on an EVM chain.")
require.Contains(t, cmd.Example, "--amount")
require.Contains(t, cmd.Example, "--eth")

// Local flags
// Local flags - both --eth and --amount should exist
require.NotNil(t, cmd.Flags().Lookup("eth"), "eth flag should exist")
require.NotNil(t, cmd.Flags().Lookup("amount"), "amount flag should exist")
require.NotNil(t, cmd.Flags().Lookup("1559"), "1559 flag should exist")

// The local 'amount' flag should be required
_, err := cmd.Flags().GetString("amount")
// Both flags should be retrievable
_, err := cmd.Flags().GetString("eth")
require.NoError(t, err)
_, err = cmd.Flags().GetString("amount")
require.NoError(t, err)
Comment thread
finleydecker marked this conversation as resolved.
// Persistent flags live on the parent
parent := &cobra.Command{}
Expand Down
Loading