From e1c2dbd3bb21da2e7d2fabfd231ee34c7095bab8 Mon Sep 17 00:00:00 2001 From: leovct Date: Tue, 28 Nov 2023 09:05:24 +0100 Subject: [PATCH] chore: specify funding amount in eth --- cmd/fund/cmd.go | 14 +++++----- cmd/fund/fund.go | 50 +++++++++++++++-------------------- util/{parse.go => convert.go} | 8 ++++++ 3 files changed, 37 insertions(+), 35 deletions(-) rename util/{parse.go => convert.go} (76%) diff --git a/cmd/fund/cmd.go b/cmd/fund/cmd.go index 40a4f59e..9f005a3b 100644 --- a/cmd/fund/cmd.go +++ b/cmd/fund/cmd.go @@ -2,6 +2,7 @@ package fund import ( "errors" + "math" _ "embed" @@ -17,11 +18,10 @@ type cmdFundParams struct { RpcUrl *string PrivateKey *string - WalletCount *uint64 - WalletFundingHexAmount *string - OutputFile *string - - FunderAddress *string + WalletCount *uint64 + FundingAmountInEth *float64 + FunderAddress *string + OutputFile *string } var ( @@ -52,7 +52,7 @@ func init() { // Wallet parameters. p.WalletCount = flagSet.Uint64P("wallets", "w", 10, "The number of wallets to fund") - p.WalletFundingHexAmount = flagSet.StringP("amount", "a", "0xb1a2bc2ec50000", "The amount of wei to send to each wallet") // 0xb1a2bc2ec50000 represensts 0.05 ether. + p.FundingAmountInEth = flagSet.Float64P("eth-amount", "a", 0.05, "The amount of ether to send to each wallet") p.OutputFile = flagSet.StringP("file", "f", "wallets.json", "The output JSON file path for storing the addresses and private keys of funded wallets") // Funder contract parameters. @@ -79,7 +79,7 @@ func checkFlags() error { if params.WalletCount != nil && *params.WalletCount == 0 { return errors.New("the number of wallets to fund is set to zero") } - if params.WalletFundingHexAmount != nil && *params.WalletFundingHexAmount == "" { + if params.FundingAmountInEth != nil && math.Abs(*params.FundingAmountInEth) <= 1e-9 { return errors.New("the amount of eth to send to each wallet is set to zero") } diff --git a/cmd/fund/fund.go b/cmd/fund/fund.go index 6cf7dd3d..a7f0a737 100644 --- a/cmd/fund/fund.go +++ b/cmd/fund/fund.go @@ -23,8 +23,8 @@ import ( ethrpc "github.com/ethereum/go-ethereum/rpc" ) -// The amount in Wei to send to the Funder contract. -const funderContractFundingAmount = "1000000000000000000" // 10 ether. +// The initial balance of Ether to send to the Funder contract. +const funderContractBalanceInEth = 1_000.0 var ( // The current chain ID for transaction replay protection. @@ -62,22 +62,12 @@ func runFunding(ctx context.Context) error { } // Deploy or instantiate the Funder contract. - var contractAddress common.Address var contract *funder.Funder - contractAddress, contract, err = deployOrInstantiateFunderContract(ctx, c, tops, &bind.CallOpts{}) + contract, err = deployOrInstantiateFunderContract(ctx, c, tops, &bind.CallOpts{}) if err != nil { return err } - // Fund the Funder contract. - amount, ok := new(big.Int).SetString(funderContractFundingAmount, 10) - if !ok { - return errors.New("unable to format the amount to send to the Funder contract") - } - if err = fundContract(ctx, c, contractAddress, amount); err != nil { - return err - } - // Generate a set of wallets. var addresses []common.Address addresses, err = generateWallets(int(*params.WalletCount)) @@ -132,24 +122,28 @@ func initializeParams(ctx context.Context, c *ethclient.Client) error { // deployOrInstantiateFunderContract deploys or instantiates a Funder contract. // If the pre-deployed address is specified, the contract will not be deployed. -func deployOrInstantiateFunderContract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (common.Address, *funder.Funder, error) { - // Format the funding amount. - fundingAmount, err := util.HexToBigInt(*params.WalletFundingHexAmount) - if err != nil { - log.Error().Err(err).Msg("Unable to parse funding amount") - return common.Address{}, nil, err - } - +func deployOrInstantiateFunderContract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (*funder.Funder, error) { // Deploy the contract if no pre-deployed address flag is provided. var contractAddress common.Address + var err error if *params.FunderAddress == "" { // Deploy the Funder contract. - contractAddress, _, _, err = funder.DeployFunder(tops, c, fundingAmount) + // Note: `fundingAmountInWei` reprensents the amount the Funder contract will send to each newly generated wallets. + fundingAmountInWei := util.EthToWei(*params.FundingAmountInEth) + contractAddress, _, _, err = funder.DeployFunder(tops, c, fundingAmountInWei) if err != nil { log.Error().Err(err).Msg("Unable to deploy Funder contract") - return common.Address{}, nil, err + return nil, err } log.Debug().Interface("address", contractAddress).Msg("Funder contract deployed") + + // Fund the Funder contract. + // Note: `funderContractBalanceInWei` reprensents the initial balance of the Funder contract. + // The contract needs initial funds to be able to fund wallets. + funderContractBalanceInWei := util.EthToWei(funderContractBalanceInEth) + if err = fundContract(ctx, c, contractAddress, funderContractBalanceInWei); err != nil { + return nil, err + } } else { // Use the pre-deployed address. contractAddress = common.HexToAddress(*params.FunderAddress) @@ -159,9 +153,9 @@ func deployOrInstantiateFunderContract(ctx context.Context, c *ethclient.Client, contract, err := funder.NewFunder(contractAddress, c) if err != nil { log.Error().Err(err).Msg("Unable to instantiate Funder contract") - return common.Address{}, nil, err + return nil, err } - return contractAddress, contract, nil + return contract, nil } func fundContract(ctx context.Context, c *ethclient.Client, contractAddress common.Address, amount *big.Int) error { @@ -202,12 +196,12 @@ func fundContract(ctx context.Context, c *ethclient.Client, contractAddress comm } // Get contract balance. - var balance *big.Int - balance, err = c.BalanceAt(ctx, contractAddress, nil) + var weiBalance *big.Int + weiBalance, err = c.BalanceAt(ctx, contractAddress, nil) if err != nil { return err } - log.Debug().Interface("balance", balance).Msg("Funder contract funded") + log.Debug().Interface("weiBalance", weiBalance).Msg("Funder contract funded") return nil } diff --git a/util/parse.go b/util/convert.go similarity index 76% rename from util/parse.go rename to util/convert.go index 8be89baf..1d6534b3 100644 --- a/util/parse.go +++ b/util/convert.go @@ -29,3 +29,11 @@ func HexToBigInt(hexString string) (*big.Int, error) { bigInt.SetBytes(rawGas) return bigInt, nil } + +// EthToWei converts a given amount of Ether to Wei. +func EthToWei(ethAmount float64) *big.Int { + weiBigFloat := new(big.Float).SetFloat64(ethAmount) + weiBigFloat.Mul(weiBigFloat, new(big.Float).SetInt64(1e18)) + weiBigInt, _ := weiBigFloat.Int(nil) + return weiBigInt +}