From 525b7208711734aa9303e91fe0953c89c91dd42e Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 10 Apr 2024 22:02:13 +0200 Subject: [PATCH] fix(client/v2): add encoder for `cosmos.base.v1beta1.DecCoin` (backport #19976) (#20001) Co-authored-by: Julien Robert --- client/v2/CHANGELOG.md | 3 ++- client/v2/autocli/query.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/client/v2/CHANGELOG.md b/client/v2/CHANGELOG.md index 3048ccdb8ac1..b62662792bc1 100644 --- a/client/v2/CHANGELOG.md +++ b/client/v2/CHANGELOG.md @@ -44,11 +44,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (deps) [#19810](https://github.com/cosmos/cosmos-sdk/pull/19810) Bump `cosmossdk.io/store` to v1.1.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. diff --git a/client/v2/autocli/query.go b/client/v2/autocli/query.go index 92a04d732afe..eedd343bef32 100644 --- a/client/v2/autocli/query.go +++ b/client/v2/autocli/query.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "io" + "strings" "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 +16,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 @@ -183,5 +187,41 @@ 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() + decimalPlace := len(amount) - math.LegacyPrecision + if decimalPlace > 0 { + amount = amount[:decimalPlace] + "." + amount[decimalPlace:] + } else if decimalPlace == 0 { + amount = "0." + amount + } else { + amount = "0." + strings.Repeat("0", -decimalPlace) + amount + } + + 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 }) }