From 1b18e16de31e52695ad641383032f30be83531f3 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 8 Apr 2024 13:03:11 +0200 Subject: [PATCH 1/5] fix(client/v2): add encoder for `cosmos.base.v1beta1.DecCoin` --- client/v2/autocli/query.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/client/v2/autocli/query.go b/client/v2/autocli/query.go index d93edb4dfce7..99a185eb333f 100644 --- a/client/v2/autocli/query.go +++ b/client/v2/autocli/query.go @@ -7,6 +7,7 @@ import ( "time" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" + "cosmossdk.io/math" "cosmossdk.io/x/tx/signing/aminojson" "github.com/cockroachdb/errors" "github.com/spf13/cobra" @@ -14,6 +15,8 @@ import ( "cosmossdk.io/client/v2/internal/flags" "cosmossdk.io/client/v2/internal/util" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // BuildQueryCommand builds the query commands for all the provided modules. If a custom command is provided for a @@ -182,5 +185,36 @@ func encoder(encoder aminojson.Encoder) aminojson.Encoder { _, err := fmt.Fprintf(w, `"%s"`, (time.Duration(seconds)*time.Second + (time.Duration(nanos) * time.Nanosecond)).String()) return err + }).DefineTypeEncoding("cosmos.base.v1beta1.DecCoin", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error { + var ( + denomName protoreflect.Name = "denom" + amountName protoreflect.Name = "amount" + ) + + fields := msg.Descriptor().Fields() + denomField := fields.ByName(denomName) + if denomField == nil { + return fmt.Errorf("expected denom field") + } + + denom := msg.Get(denomField).String() + + amountField := fields.ByName(amountName) + if amountField == nil { + return fmt.Errorf("expected amount field") + } + + amount := msg.Get(amountField).String() + if len(amount) >= math.LegacyPrecision { + amount = amount[:len(amount)-math.LegacyPrecision] + "." + amount[len(amount)-math.LegacyPrecision:] + } + + amountDec, err := math.LegacyNewDecFromStr(amount) + if err != nil { + return fmt.Errorf("invalid amount: %s: %w", amount, err) + } + + _, err = fmt.Fprintf(w, `"%s"`, sdk.NewDecCoinFromDec(denom, amountDec)) // TODO(@julienrbrt): Eventually remove this SDK dependency + return err }) } From 017c7f7c0991ecae105c79d9b1ed1c287bcc0852 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 8 Apr 2024 13:09:55 +0200 Subject: [PATCH 2/5] add changelog --- client/v2/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/v2/CHANGELOG.md b/client/v2/CHANGELOG.md index efabebf0491c..eabfebff70a5 100644 --- a/client/v2/CHANGELOG.md +++ b/client/v2/CHANGELOG.md @@ -48,11 +48,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#19618](https://github.com/cosmos/cosmos-sdk/pull/19618) Marshal enum as string in queries. * [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Use client context from root (or enhanced) command in autocli commands. - * Note, the given command must have a `client.Context` in its context. + * Note, the given command must have a `client.Context` in its context. * [#19216](https://github.com/cosmos/cosmos-sdk/pull/19216) Do not overwrite TxConfig, use directly the one provided in context. TxConfig should always be set in the `client.Context` in `root.go` of an app. ### Bug Fixes +* [#19976](https://github.com/cosmos/cosmos-sdk/pull/19976) Add encoder for `cosmos.base.v1beta1.DecCoin`. * [#19377](https://github.com/cosmos/cosmos-sdk/pull/19377) Partly fix comment parsing in autocli. * [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Simplify key flag parsing logic in flag handler. From da856da2d944acc158fb4a1e293f66dbed1d5193 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 8 Apr 2024 14:32:16 +0200 Subject: [PATCH 3/5] fix lint --- client/v2/autocli/common_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/v2/autocli/common_test.go b/client/v2/autocli/common_test.go index 2850abc226f3..9e8613be07ca 100644 --- a/client/v2/autocli/common_test.go +++ b/client/v2/autocli/common_test.go @@ -49,7 +49,7 @@ func initFixture(t *testing.T) *fixture { } }() - clientConn, err := grpc.Dial(listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials())) + clientConn, err := grpc.NewClient(listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials())) assert.NilError(t, err) encodingConfig := moduletestutil.MakeTestEncodingConfig(testutil.CodecOptions{}, bank.AppModule{}) From ba7fdc42e33f39723116d9ee7f5c90f5ce334385 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 10 Apr 2024 13:53:07 +0200 Subject: [PATCH 4/5] fixes --- client/v2/autocli/query.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/client/v2/autocli/query.go b/client/v2/autocli/query.go index 99a185eb333f..5393b25871f7 100644 --- a/client/v2/autocli/query.go +++ b/client/v2/autocli/query.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "strings" "time" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" @@ -205,8 +206,13 @@ func encoder(encoder aminojson.Encoder) aminojson.Encoder { } amount := msg.Get(amountField).String() - if len(amount) >= math.LegacyPrecision { + if len(amount) > math.LegacyPrecision { amount = amount[:len(amount)-math.LegacyPrecision] + "." + amount[len(amount)-math.LegacyPrecision:] + } else if len(amount) == math.LegacyPrecision { + amount = "0." + amount + } else { + decimalPlace := len(amount) - math.LegacyPrecision + amount = "0." + strings.Repeat("0", -decimalPlace) + amount } amountDec, err := math.LegacyNewDecFromStr(amount) From 1d9d0b360ac571d1c605ffdd217b3ef17bf14a41 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 10 Apr 2024 13:55:39 +0200 Subject: [PATCH 5/5] simplify --- client/v2/autocli/query.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/v2/autocli/query.go b/client/v2/autocli/query.go index 5393b25871f7..c2a3717a44bb 100644 --- a/client/v2/autocli/query.go +++ b/client/v2/autocli/query.go @@ -206,12 +206,12 @@ func encoder(encoder aminojson.Encoder) aminojson.Encoder { } amount := msg.Get(amountField).String() - if len(amount) > math.LegacyPrecision { - amount = amount[:len(amount)-math.LegacyPrecision] + "." + amount[len(amount)-math.LegacyPrecision:] - } else if len(amount) == math.LegacyPrecision { + decimalPlace := len(amount) - math.LegacyPrecision + if decimalPlace > 0 { + amount = amount[:decimalPlace] + "." + amount[decimalPlace:] + } else if decimalPlace == 0 { amount = "0." + amount } else { - decimalPlace := len(amount) - math.LegacyPrecision amount = "0." + strings.Repeat("0", -decimalPlace) + amount }