Skip to content

Commit

Permalink
chore: specify funding amount in eth
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Nov 28, 2023
1 parent a998f9c commit e1c2dbd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
14 changes: 7 additions & 7 deletions cmd/fund/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fund

import (
"errors"
"math"

_ "embed"

Expand All @@ -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 (
Expand Down Expand Up @@ -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.
Expand All @@ -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")
}

Expand Down
50 changes: 22 additions & 28 deletions cmd/fund/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions util/parse.go → util/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e1c2dbd

Please sign in to comment.