From 852934319e430ace0b9c3ba4e2d175bcd7575f7e Mon Sep 17 00:00:00 2001 From: Mantrachain Development Support Date: Mon, 16 Sep 2024 23:01:02 +0900 Subject: [PATCH] feat(types/collections): add `LegacyDec` collection value (backport #21693) (#21724) (#6) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: John Letey Co-authored-by: sontrinh16 --- CHANGELOG.md | 1 + types/collections.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 088c7bfa0c3e..c89409b685ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (cli) [#20779](https://github.com/cosmos/cosmos-sdk/pull/20779) Added `module-hash-by-height` command to query and retrieve module hashes at a specified blockchain height, enhancing debugging capabilities. * (cli) [#21372](https://github.com/cosmos/cosmos-sdk/pull/21372) Added a `bulk-add-genesis-account` genesis command to add many genesis accounts at once. +* (types/collections) [#21724](https://github.com/cosmos/cosmos-sdk/pull/21724) Added `LegacyDec` collection value. ### Improvements diff --git a/types/collections.go b/types/collections.go index 41b9e18606c9..4d2c84ca8299 100644 --- a/types/collections.go +++ b/types/collections.go @@ -32,6 +32,9 @@ var ( // IntValue represents a collections.ValueCodec to work with Int. IntValue collcodec.ValueCodec[math.Int] = intValueCodec{} + // LegacyDecValue represents a collections.ValueCodec to work with LegacyDec. + LegacyDecValue collcodec.ValueCodec[math.LegacyDec] = legacyDecValueCodec{} + // TimeKey represents a collections.KeyCodec to work with time.Time // Deprecated: exists only for state compatibility reasons, should not // be used for new storage keys using time. Please use the time KeyCodec @@ -39,6 +42,10 @@ var ( TimeKey collcodec.KeyCodec[time.Time] = timeKeyCodec{} ) +const ( + LegacyDec string = "math.LegacyDec" +) + type addressUnion interface { AccAddress | ValAddress | ConsAddress String() string @@ -166,6 +173,42 @@ func (i intValueCodec) ValueType() string { return "math.Int" } +type legacyDecValueCodec struct{} + +func (i legacyDecValueCodec) Encode(value math.LegacyDec) ([]byte, error) { + return value.Marshal() +} + +func (i legacyDecValueCodec) Decode(b []byte) (math.LegacyDec, error) { + v := new(math.LegacyDec) + err := v.Unmarshal(b) + if err != nil { + return math.LegacyDec{}, err + } + return *v, nil +} + +func (i legacyDecValueCodec) EncodeJSON(value math.LegacyDec) ([]byte, error) { + return value.MarshalJSON() +} + +func (i legacyDecValueCodec) DecodeJSON(b []byte) (math.LegacyDec, error) { + v := new(math.LegacyDec) + err := v.UnmarshalJSON(b) + if err != nil { + return math.LegacyDec{}, err + } + return *v, nil +} + +func (i legacyDecValueCodec) Stringify(value math.LegacyDec) string { + return value.String() +} + +func (i legacyDecValueCodec) ValueType() string { + return LegacyDec +} + type timeKeyCodec struct{} func (timeKeyCodec) Encode(buffer []byte, key time.Time) (int, error) {