Skip to content

Commit

Permalink
feat(types/collections): add LegacyDec collection value (backport #…
Browse files Browse the repository at this point in the history
…21693) (#21724)

Co-authored-by: John Letey <john@noble.xyz>
Co-authored-by: sontrinh16 <trinhleson2000@gmail.com>
  • Loading branch information
3 people committed Sep 16, 2024
1 parent f78ed49 commit b21441c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 43 additions & 0 deletions types/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ 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
// provided in the collections package.
TimeKey collcodec.KeyCodec[time.Time] = timeKeyCodec{}
)

const (
LegacyDec string = "math.LegacyDec"
)

type addressUnion interface {
AccAddress | ValAddress | ConsAddress
String() string
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b21441c

Please sign in to comment.