Skip to content

Commit 46bf94d

Browse files
mergify[bot]johnleteysontrinh16
authored andcommitted
feat(types/collections): add LegacyDec collection value (backport cosmos#21693) (cosmos#21724)
Co-authored-by: John Letey <john@noble.xyz> Co-authored-by: sontrinh16 <trinhleson2000@gmail.com>
1 parent 1ee761d commit 46bf94d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4242

4343
* (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.
4444
* (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.
45+
* (types/collections) [#21724](https://github.com/cosmos/cosmos-sdk/pull/21724) Added `LegacyDec` collection value.
4546

4647
### Improvements
4748

types/collections.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ var (
3232
// IntValue represents a collections.ValueCodec to work with Int.
3333
IntValue collcodec.ValueCodec[math.Int] = intValueCodec{}
3434

35+
// LegacyDecValue represents a collections.ValueCodec to work with LegacyDec.
36+
LegacyDecValue collcodec.ValueCodec[math.LegacyDec] = legacyDecValueCodec{}
37+
3538
// TimeKey represents a collections.KeyCodec to work with time.Time
3639
// Deprecated: exists only for state compatibility reasons, should not
3740
// be used for new storage keys using time. Please use the time KeyCodec
3841
// provided in the collections package.
3942
TimeKey collcodec.KeyCodec[time.Time] = timeKeyCodec{}
4043
)
4144

45+
const (
46+
LegacyDec string = "math.LegacyDec"
47+
)
48+
4249
type addressUnion interface {
4350
AccAddress | ValAddress | ConsAddress
4451
String() string
@@ -166,6 +173,42 @@ func (i intValueCodec) ValueType() string {
166173
return "math.Int"
167174
}
168175

176+
type legacyDecValueCodec struct{}
177+
178+
func (i legacyDecValueCodec) Encode(value math.LegacyDec) ([]byte, error) {
179+
return value.Marshal()
180+
}
181+
182+
func (i legacyDecValueCodec) Decode(b []byte) (math.LegacyDec, error) {
183+
v := new(math.LegacyDec)
184+
err := v.Unmarshal(b)
185+
if err != nil {
186+
return math.LegacyDec{}, err
187+
}
188+
return *v, nil
189+
}
190+
191+
func (i legacyDecValueCodec) EncodeJSON(value math.LegacyDec) ([]byte, error) {
192+
return value.MarshalJSON()
193+
}
194+
195+
func (i legacyDecValueCodec) DecodeJSON(b []byte) (math.LegacyDec, error) {
196+
v := new(math.LegacyDec)
197+
err := v.UnmarshalJSON(b)
198+
if err != nil {
199+
return math.LegacyDec{}, err
200+
}
201+
return *v, nil
202+
}
203+
204+
func (i legacyDecValueCodec) Stringify(value math.LegacyDec) string {
205+
return value.String()
206+
}
207+
208+
func (i legacyDecValueCodec) ValueType() string {
209+
return LegacyDec
210+
}
211+
169212
type timeKeyCodec struct{}
170213

171214
func (timeKeyCodec) Encode(buffer []byte, key time.Time) (int, error) {

0 commit comments

Comments
 (0)