feat(types/collections): add LegacyDec collection value (backport #21693) (#21724)

Co-authored-by: John Letey <john@noble.xyz>
Co-authored-by: sontrinh16 <trinhleson2000@gmail.com>
This commit is contained in:
mergify[bot] 2024-09-16 09:31:14 +02:00 committed by GitHub
parent f78ed4997f
commit b21441cf59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 0 deletions

View File

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

View File

@ -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) {