Skip to content

Commit 525b720

Browse files
fix(client/v2): add encoder for cosmos.base.v1beta1.DecCoin (backport #19976) (#20001)
Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 4b71767 commit 525b720

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

client/v2/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
4444
* (deps) [#19810](https://github.com/cosmos/cosmos-sdk/pull/19810) Bump `cosmossdk.io/store` to v1.1.0.
4545
* [#19618](https://github.com/cosmos/cosmos-sdk/pull/19618) Marshal enum as string in queries.
4646
* [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Use client context from root (or enhanced) command in autocli commands.
47-
* Note, the given command must have a `client.Context` in its context.
47+
* Note, the given command must have a `client.Context` in its context.
4848
* [#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.
4949

5050
### Bug Fixes
5151

52+
* [#19976](https://github.com/cosmos/cosmos-sdk/pull/19976) Add encoder for `cosmos.base.v1beta1.DecCoin`.
5253
* [#19377](https://github.com/cosmos/cosmos-sdk/pull/19377) Partly fix comment parsing in autocli.
5354
* [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Simplify key flag parsing logic in flag handler.
5455

client/v2/autocli/query.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"strings"
78
"time"
89

910
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
11+
"cosmossdk.io/math"
1012
"cosmossdk.io/x/tx/signing/aminojson"
1113
"github.com/cockroachdb/errors"
1214
"github.com/spf13/cobra"
1315
"google.golang.org/protobuf/reflect/protoreflect"
1416

1517
"cosmossdk.io/client/v2/internal/flags"
1618
"cosmossdk.io/client/v2/internal/util"
19+
20+
sdk "github.com/cosmos/cosmos-sdk/types"
1721
)
1822

1923
// 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 {
183187

184188
_, err := fmt.Fprintf(w, `"%s"`, (time.Duration(seconds)*time.Second + (time.Duration(nanos) * time.Nanosecond)).String())
185189
return err
190+
}).DefineTypeEncoding("cosmos.base.v1beta1.DecCoin", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error {
191+
var (
192+
denomName protoreflect.Name = "denom"
193+
amountName protoreflect.Name = "amount"
194+
)
195+
196+
fields := msg.Descriptor().Fields()
197+
denomField := fields.ByName(denomName)
198+
if denomField == nil {
199+
return fmt.Errorf("expected denom field")
200+
}
201+
202+
denom := msg.Get(denomField).String()
203+
204+
amountField := fields.ByName(amountName)
205+
if amountField == nil {
206+
return fmt.Errorf("expected amount field")
207+
}
208+
209+
amount := msg.Get(amountField).String()
210+
decimalPlace := len(amount) - math.LegacyPrecision
211+
if decimalPlace > 0 {
212+
amount = amount[:decimalPlace] + "." + amount[decimalPlace:]
213+
} else if decimalPlace == 0 {
214+
amount = "0." + amount
215+
} else {
216+
amount = "0." + strings.Repeat("0", -decimalPlace) + amount
217+
}
218+
219+
amountDec, err := math.LegacyNewDecFromStr(amount)
220+
if err != nil {
221+
return fmt.Errorf("invalid amount: %s: %w", amount, err)
222+
}
223+
224+
_, err = fmt.Fprintf(w, `"%s"`, sdk.NewDecCoinFromDec(denom, amountDec)) // TODO(@julienrbrt): Eventually remove this SDK dependency
225+
return err
186226
})
187227
}

0 commit comments

Comments
 (0)