Skip to content

Commit

Permalink
revert marshal changes (fixed by #22161)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Oct 8, 2024
1 parent 1e6c755 commit e6e4583
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 33 deletions.
1 change: 0 additions & 1 deletion client/v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

* [#21853](https://github.com/cosmos/cosmos-sdk/pull/21853) Fix `*big.Int` unmarshalling in txs.
* [#21853](https://github.com/cosmos/cosmos-sdk/pull/21853) Fix `*big.Int` marshalling in queries.

## [v2.0.0-beta.5] - 2024-09-18

Expand Down
33 changes: 1 addition & 32 deletions client/v2/autocli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"math/big"
"strings"
"time"

Expand Down Expand Up @@ -172,7 +171,7 @@ func (b *Builder) BuildQueryMethodCommand(ctx context.Context, descriptor protor
}

func encoder(encoder aminojson.Encoder) aminojson.Encoder {
customEncoder := encoder.DefineTypeEncoding("google.protobuf.Duration", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error {
return encoder.DefineTypeEncoding("google.protobuf.Duration", func(_ *aminojson.Encoder, msg protoreflect.Message, w io.Writer) error {
var (
secondsName protoreflect.Name = "seconds"
nanosName protoreflect.Name = "nanos"
Expand Down Expand Up @@ -232,34 +231,4 @@ func encoder(encoder aminojson.Encoder) aminojson.Encoder {
_, err = fmt.Fprintf(w, `"%s"`, sdk.NewDecCoinFromDec(denom, amountDec)) // TODO(@julienrbrt): Eventually remove this SDK dependency
return err
})

customEncoder.DefineScalarEncoding("cosmos.Dec", func(_ *aminojson.Encoder, value protoreflect.Value, w io.Writer) error {
decStr := value.String()
if strings.Contains(decStr, "[") { // check if it's a bytes field (e.g mint inflation)
decStr = string(value.Bytes())
}

// If the decimal doesn't contain a point, we assume it's a value formatted using the legacy
// `math.Dec`. So we try to parse it as an integer and then convert it to a
// decimal.
if !strings.Contains(decStr, ".") {
parsedInt, ok := new(big.Int).SetString(decStr, 10)
if !ok {
return fmt.Errorf("invalid decimal: %s", decStr)
}

// We assume the decimal has 18 digits of precision.
decStr = math.LegacyNewDecFromBigIntWithPrec(parsedInt, math.LegacyPrecision).String()
}

formatted, err := math.FormatDec(decStr)
if err != nil {
return fmt.Errorf("cannot format decimal %s: %w", decStr, err)
}

_, err = fmt.Fprintf(w, `"%s"`, formatted)
return err
})

return customEncoder
}

0 comments on commit e6e4583

Please sign in to comment.