Skip to content

Commit

Permalink
cli: ParseCoin doesn't do normalize (#7954)
Browse files Browse the repository at this point in the history
follow up on #7777
ParseCoin need to be treated similarly.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
yihuang and mergify[bot] authored Nov 25, 2020
1 parent c58a892 commit 512b533
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 30 deletions.
2 changes: 1 addition & 1 deletion types/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func BenchmarkParseCoin(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, coinStr := range coinStrs {
coin, err := types.ParseCoin(coinStr)
coin, err := types.ParseCoinNormalized(coinStr)
if err != nil {
b.Fatal(err)
}
Expand Down
27 changes: 6 additions & 21 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,9 @@ var (
// Denominations can be 3 ~ 128 characters long and support letters, followed by either
// a letter, a number or a separator ('/').
reDnmString = `[a-zA-Z][a-zA-Z0-9/]{2,127}`
reAmt = `[[:digit:]]+`
reDecAmt = `[[:digit:]]+(?:\.[[:digit:]]+)?|\.[[:digit:]]+`
reSpc = `[[:space:]]*`
reDnm *regexp.Regexp
reCoin *regexp.Regexp
reDecCoin *regexp.Regexp
)

Expand All @@ -625,7 +623,6 @@ func SetCoinDenomRegex(reFn func() string) {
coinDenomRegex = reFn

reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, coinDenomRegex()))
reCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reAmt, reSpc, coinDenomRegex()))
reDecCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reDecAmt, reSpc, coinDenomRegex()))
}

Expand All @@ -643,29 +640,17 @@ func mustValidateDenom(denom string) {
}
}

// ParseCoin parses a cli input for one coin type, returning errors if invalid or on an empty string
// ParseCoinNormalized parses and normalize a cli input for one coin type, returning errors if invalid or on an empty string
// as well.
// Expected format: "{amount}{denomination}"
func ParseCoin(coinStr string) (coin Coin, err error) {
coinStr = strings.TrimSpace(coinStr)

matches := reCoin.FindStringSubmatch(coinStr)
if matches == nil {
return Coin{}, fmt.Errorf("invalid coin expression: %s", coinStr)
}

denomStr, amountStr := matches[2], matches[1]

amount, ok := NewIntFromString(amountStr)
if !ok {
return Coin{}, fmt.Errorf("failed to parse coin amount: %s", amountStr)
}

if err := ValidateDenom(denomStr); err != nil {
func ParseCoinNormalized(coinStr string) (coin Coin, err error) {
decCoin, err := ParseDecCoin(coinStr)
if err != nil {
return Coin{}, err
}

return NewCoin(denomStr, amount), nil
coin, _ = NormalizeDecCoin(decCoin).TruncateDecimal()
return coin, nil
}

// ParseCoinsNormalized will parse out a list of coins separated by commas, and normalize them by converting to smallest
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/applications/transfer/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ to the counterparty channel. Any timeout set to 0 is disabled.`),
srcChannel := args[1]
receiver := args[2]

coin, err := sdk.ParseCoin(args[3])
coin, err := sdk.ParseCoinNormalized(args[3])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/staking/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)

unbond, err := sdk.ParseCoin("10stake")
unbond, err := sdk.ParseCoinNormalized("10stake")
s.Require().NoError(err)

val := s.network.Validators[0]
Expand Down
10 changes: 5 additions & 5 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ $ %s tx staking delegate %s1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59wm 1000stake --f
return err
}

amount, err := sdk.ParseCoin(args[1])
amount, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
Expand Down Expand Up @@ -231,7 +231,7 @@ $ %s tx staking redelegate %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj %s1l2rsakp3
return err
}

amount, err := sdk.ParseCoin(args[2])
amount, err := sdk.ParseCoinNormalized(args[2])
if err != nil {
return err
}
Expand Down Expand Up @@ -279,7 +279,7 @@ $ %s tx staking unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake --from
return err
}

amount, err := sdk.ParseCoin(args[1])
amount, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
Expand All @@ -300,7 +300,7 @@ $ %s tx staking unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake --from

func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) {
fAmount, _ := fs.GetString(FlagAmount)
amount, err := sdk.ParseCoin(fAmount)
amount, err := sdk.ParseCoinNormalized(fAmount)
if err != nil {
return txf, nil, err
}
Expand Down Expand Up @@ -514,7 +514,7 @@ func PrepareConfigForTxCreateValidator(flagSet *flag.FlagSet, moniker, nodeID, c
// BuildCreateValidatorMsg makes a new MsgCreateValidator.
func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorConfig, txBldr tx.Factory, generateOnly bool) (tx.Factory, sdk.Msg, error) {
amounstStr := config.Amount
amount, err := sdk.ParseCoin(amounstStr)
amount, err := sdk.ParseCoinNormalized(amounstStr)

if err != nil {
return txBldr, nil, err
Expand Down
2 changes: 1 addition & 1 deletion x/staking/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)

unbond, err := sdk.ParseCoin("10stake")
unbond, err := sdk.ParseCoinNormalized("10stake")
s.Require().NoError(err)

val := s.network.Validators[0]
Expand Down

0 comments on commit 512b533

Please sign in to comment.