fix(client/v2): add encoder for cosmos.base.v1beta1.DecCoin (#19976)

This commit is contained in:
Julien Robert 2024-04-10 16:59:15 +02:00 committed by GitHub
parent 4c446f8b7d
commit 76bb0cd7e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

View File

@ -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.

View File

@ -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
@ -182,5 +186,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
})
}