Skip to content

Commit 225268a

Browse files
committed
refactor flags
1 parent df3eb1d commit 225268a

File tree

8 files changed

+88
-84
lines changed

8 files changed

+88
-84
lines changed

cmd/flags/gas.go

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,76 @@
11
package flags
22

3+
import (
4+
"fmt"
5+
"math/big"
6+
"regexp"
7+
"strings"
8+
9+
"github.com/bcdevtools/devd/v3/cmd/utils"
10+
"github.com/spf13/cobra"
11+
)
12+
313
const (
4-
FlagGas = "gas"
14+
FlagGasLimit = "gas"
515
FlagGasPrices = "gas-prices"
616
)
17+
18+
func ReadFlagGasLimit(cmd *cobra.Command, flag string, _default uint64) (uint64, error) {
19+
gasLimit, _ := cmd.Flags().GetString(flag)
20+
if gasLimit == "" {
21+
gasLimit = fmt.Sprintf("%d", _default)
22+
}
23+
24+
bi, err := utils.ReadShortInt(gasLimit)
25+
if err != nil {
26+
return 0, err
27+
}
28+
29+
if !bi.IsUint64() {
30+
return 0, fmt.Errorf("invalid gas limit %s", gasLimit)
31+
}
32+
33+
num := bi.Uint64()
34+
if num < 21_000 {
35+
return 0, fmt.Errorf("minimum gas limit is 21k, too low: %s", gasLimit)
36+
}
37+
if num > 35_000_000 {
38+
return 0, fmt.Errorf("gas limit is too high: %s", gasLimit)
39+
}
40+
41+
return num, nil
42+
}
43+
44+
func ReadFlagGasPrices(cmd *cobra.Command, flag string, _default uint64) (*big.Int, error) {
45+
gasPrices, _ := cmd.Flags().GetString(flag)
46+
if gasPrices == "" {
47+
gasPrices = fmt.Sprintf("%d", _default)
48+
}
49+
50+
if regexp.MustCompile(`^\d+g$`).MatchString(gasPrices) {
51+
gasPrices = strings.TrimSuffix(gasPrices, "g")
52+
bi, ok := new(big.Int).SetString(gasPrices, 10)
53+
if !ok {
54+
panic("failed to parse gas prices")
55+
}
56+
bi = new(big.Int).Mul(bi, big.NewInt(1e9))
57+
return bi, nil
58+
}
59+
60+
if regexp.MustCompile(`^\d+gwei$`).MatchString(gasPrices) {
61+
gasPrices = strings.TrimSuffix(gasPrices, "gwei")
62+
bi, ok := new(big.Int).SetString(gasPrices, 10)
63+
if !ok {
64+
panic("failed to parse gas prices")
65+
}
66+
bi = new(big.Int).Mul(bi, big.NewInt(1e9))
67+
return bi, nil
68+
}
69+
70+
bi, err := utils.ReadShortInt(gasPrices)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
return bi, nil
76+
}

cmd/query/balance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Bech32 account address is accepted.`, flagErc20),
175175

176176
cmd.Flags().String(flags.FlagEvmRpc, "", flags.FlagEvmRpcDesc)
177177
cmd.Flags().String(flags.FlagCosmosRest, "", flags.FlagCosmosRestDesc)
178-
cmd.Flags().Int64(flags.FlagHeight, 0, "query balance at specific height")
178+
cmd.Flags().String(flags.FlagHeight, "", "query balance at specific height")
179179
cmd.Flags().Bool(flagErc20, false, "query balance of ERC-20 contracts of `x/erc20` module and virtual frontier bank contracts")
180180

181181
return cmd

cmd/query/erc20.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Support bech32 address format`,
9696
}
9797

9898
cmd.Flags().String(flags.FlagEvmRpc, "", flags.FlagEvmRpcDesc)
99-
cmd.Flags().Int64(flags.FlagHeight, 0, "query balance at specific height")
99+
cmd.Flags().String(flags.FlagHeight, "", "query balance at specific height")
100100

101101
return cmd
102102
}

cmd/query/eth_call.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func GetQueryEvmRpcEthCallCommand() *cobra.Command {
5050
}(),
5151
To: &contractAddress,
5252
Gas: func() uint64 {
53-
gas, err := flags.ReadFlagShortIntOrHexOrZero(cmd, flags.FlagGas)
53+
gas, err := flags.ReadFlagShortIntOrHexOrZero(cmd, flags.FlagGasLimit)
5454
utils.ExitOnErr(err, "failed to parse gas")
5555
if gas > 0 {
5656
utils.PrintfStdErr("INF: using gas: %d\n", gas)
@@ -89,9 +89,9 @@ func GetQueryEvmRpcEthCallCommand() *cobra.Command {
8989

9090
cmd.Flags().String(flags.FlagEvmRpc, "", flags.FlagEvmRpcDesc)
9191
cmd.Flags().StringP(flagFrom, "f", "", "the address from which the transaction is sent")
92-
cmd.Flags().StringP(flags.FlagGas, "g", "", "the integer of gas provided for the transaction execution, support short int and hex")
93-
cmd.Flags().StringP(flags.FlagGasPrices, "p", "", "the integer of gasPrice used for each paid gas encoded as hexadecimal, support short int and hex")
94-
cmd.Flags().StringP(flagValue, "v", "", "the integer of value sent with this transaction encoded as hexadecimal, support short int and hex")
92+
cmd.Flags().StringP(flags.FlagGasLimit, "g", "", "gas provided for the transaction execution, support short int and hex")
93+
cmd.Flags().StringP(flags.FlagGasPrices, "p", "", "gas prices used for each paid gas, support short int and hex")
94+
cmd.Flags().StringP(flagValue, "v", "", "value sent with this transaction, support short int and hex")
9595
cmd.Flags().StringP(flags.FlagHeight, "h", "latest", "the context height of the block to exec, accept \"latest\"/short int/hex")
9696

9797
return cmd

cmd/tx/deploy-contract.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ Predefined bytecode: erc20`,
4242

4343
cmd.Flags().String(flags.FlagEvmRpc, "", flags.FlagEvmRpcDesc)
4444
cmd.Flags().String(flags.FlagSecretKey, "", flags.FlagSecretKeyDesc)
45-
cmd.Flags().String(flagGasLimit, "4m", flagGasLimitDesc)
46-
cmd.Flags().String(flagGasPrices, "20b", flagGasPricesDesc)
45+
cmd.Flags().String(flags.FlagGasLimit, "4m", flagGasLimitDesc)
46+
cmd.Flags().String(flags.FlagGasPrices, "20b", flagGasPricesDesc)
4747
cmd.Flags().Bool(flagRawTx, false, flagRawTxDesc)
4848

4949
return cmd
@@ -54,10 +54,10 @@ func deployEvmContract(bytecode string, cmd *cobra.Command) {
5454

5555
ecdsaPrivateKey, _, from := flags.MustSecretEvmAccount(cmd)
5656

57-
gasPrices, err := readGasPrices(cmd)
57+
gasPrices, err := flags.ReadFlagGasPrices(cmd, flags.FlagGasPrices, 20_000_000_000)
5858
utils.ExitOnErr(err, "failed to parse gas price")
5959

60-
gasLimit, err := readGasLimit(cmd)
60+
gasLimit, err := flags.ReadFlagGasLimit(cmd, flags.FlagGasLimit, 500_000)
6161
utils.ExitOnErr(err, "failed to parse gas limit")
6262

6363
nonce, err := ethClient8545.NonceAt(context.Background(), *from, nil)

cmd/tx/root.go

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ package tx
33
import (
44
"bytes"
55
"encoding/hex"
6-
"fmt"
7-
"math/big"
8-
"regexp"
9-
"strings"
106

117
ethtypes "github.com/ethereum/go-ethereum/core/types"
128

@@ -15,9 +11,7 @@ import (
1511
)
1612

1713
const (
18-
flagGasLimit = "gas"
19-
flagGasPrices = "gas-prices"
20-
flagRawTx = "raw-tx"
14+
flagRawTx = "raw-tx"
2115
)
2216

2317
const (
@@ -41,66 +35,6 @@ func Commands() *cobra.Command {
4135
return cmd
4236
}
4337

44-
func readGasPrices(cmd *cobra.Command) (*big.Int, error) {
45-
gasPrices, _ := cmd.Flags().GetString(flagGasPrices)
46-
if gasPrices == "" {
47-
gasPrices = "20b"
48-
}
49-
50-
if regexp.MustCompile(`^\d+g$`).MatchString(gasPrices) {
51-
gasPrices = strings.TrimSuffix(gasPrices, "g")
52-
bi, ok := new(big.Int).SetString(gasPrices, 10)
53-
if !ok {
54-
panic("failed to parse gas prices")
55-
}
56-
bi = new(big.Int).Mul(bi, big.NewInt(1e9))
57-
return bi, nil
58-
}
59-
60-
if regexp.MustCompile(`^\d+gwei$`).MatchString(gasPrices) {
61-
gasPrices = strings.TrimSuffix(gasPrices, "gwei")
62-
bi, ok := new(big.Int).SetString(gasPrices, 10)
63-
if !ok {
64-
panic("failed to parse gas prices")
65-
}
66-
bi = new(big.Int).Mul(bi, big.NewInt(1e9))
67-
return bi, nil
68-
}
69-
70-
bi, err := utils.ReadShortInt(gasPrices)
71-
if err != nil {
72-
return nil, err
73-
}
74-
75-
return bi, nil
76-
}
77-
78-
func readGasLimit(cmd *cobra.Command) (uint64, error) {
79-
gasLimit, _ := cmd.Flags().GetString(flagGasLimit)
80-
if gasLimit == "" {
81-
gasLimit = "500k"
82-
}
83-
84-
bi, err := utils.ReadShortInt(gasLimit)
85-
if err != nil {
86-
return 0, err
87-
}
88-
89-
if !bi.IsUint64() {
90-
return 0, fmt.Errorf("invalid gas limit %s", gasLimit)
91-
}
92-
93-
num := bi.Uint64()
94-
if num < 21_000 {
95-
return 0, fmt.Errorf("minimum gas limit is 21k, too low: %s", gasLimit)
96-
}
97-
if num > 35_000_000 {
98-
return 0, fmt.Errorf("gas limit is too high: %s", gasLimit)
99-
}
100-
101-
return num, nil
102-
}
103-
10438
func printRawEvmTx(signedTx *ethtypes.Transaction) {
10539
var buf bytes.Buffer
10640
err := signedTx.EncodeRLP(&buf)

cmd/tx/send.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ Support short int`,
2727
Run: func(cmd *cobra.Command, args []string) {
2828
ethClient8545, _ := flags.MustGetEthClient(cmd)
2929

30-
gasPrices, err := readGasPrices(cmd)
30+
gasPrices, err := flags.ReadFlagGasPrices(cmd, flags.FlagGasPrices, 20_000_000_000)
3131
utils.ExitOnErr(err, "failed to parse gas price")
3232

33-
gasLimit, err := readGasLimit(cmd)
33+
gasLimit, err := flags.ReadFlagGasLimit(cmd, flags.FlagGasLimit, 500_000)
3434
utils.ExitOnErr(err, "failed to parse gas limit")
3535

3636
evmAddrs, err := utils.GetEvmAddressFromAnyFormatAddress(args[0])
@@ -97,7 +97,7 @@ Support short int`,
9797
}
9898
} else {
9999
if gasLimit > 21000 {
100-
utils.PrintfStdErr("WARN: setting gas limit by flag --%s will be ignored, ony use 21,000 gas\n", flagGasLimit)
100+
utils.PrintfStdErr("WARN: setting gas limit by flag --%s will be ignored, ony use 21,000 gas\n", flags.FlagGasPrices)
101101
}
102102
txData = ethtypes.LegacyTx{
103103
Nonce: nonce,
@@ -135,8 +135,8 @@ Support short int`,
135135
cmd.Flags().String(flags.FlagEvmRpc, "", flags.FlagEvmRpcDesc)
136136
cmd.Flags().String(flags.FlagSecretKey, "", flags.FlagSecretKeyDesc)
137137
cmd.Flags().String(flagErc20, "", "contract address if you want to send ERC-20 token instead of native coin")
138-
cmd.Flags().String(flagGasLimit, "500k", fmt.Sprintf("%s. Ignored during normal EVM transfer, fixed to 21k", flagGasLimitDesc))
139-
cmd.Flags().String(flagGasPrices, "20b", flagGasPricesDesc)
138+
cmd.Flags().String(flags.FlagGasLimit, "500k", fmt.Sprintf("%s. Ignored during normal EVM transfer, fixed to 21k", flagGasLimitDesc))
139+
cmd.Flags().String(flags.FlagGasPrices, "20b", flagGasPricesDesc)
140140
cmd.Flags().Bool(flagRawTx, false, flagRawTxDesc)
141141

142142
return cmd

cmd/utils/account_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func FromMnemonicToPrivateKey(mnemonic, password string) (*ecdsa.PrivateKey, err
2222
return nil, err
2323
}
2424

25-
// create a BTC-utils hd-derivation key chain
25+
// create a BTC-utils hd-derivation keychain
2626
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
2727
if err != nil {
2828
return nil, err

0 commit comments

Comments
 (0)