Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (types) [#23780](https://github.com/cosmos/cosmos-sdk/pull/23780) Add a ValueCodec for the math.Uint type that can be used in collections maps.
* (types) [#23919](https://github.com/cosmos/cosmos-sdk/pull/23919) Add MustValAddressFromBech32 function.
* (all) [#23708](https://github.com/cosmos/cosmos-sdk/pull/23708) Add unordered transaction support.
* Adds a `--timeout-timestamp` flag that allows users to specify a block time at which the unordered transactions should expire from the mempool.
Expand Down
6 changes: 6 additions & 0 deletions types/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ import (
func TestIntValue(t *testing.T) {
colltest.TestValueCodec(t, IntValue, math.NewInt(10005994859))
}

func TestUintValue(t *testing.T) {
colltest.TestValueCodec(t, UintValue, math.NewUint(1337))
colltest.TestValueCodec(t, UintValue, math.ZeroUint())
colltest.TestValueCodec(t, UintValue, math.NewUintFromString("1000000000000000000"))
}
46 changes: 45 additions & 1 deletion types/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var (
// IntValue represents a collections.ValueCodec to work with Int.
IntValue collcodec.ValueCodec[math.Int] = intValueCodec{}

// UintValue represents a collections.ValueCodec to work with Uint.
UintValue collcodec.ValueCodec[math.Uint] = uintValueCodec{}

// LegacyDecValue represents a collections.ValueCodec to work with LegacyDec.
LegacyDecValue collcodec.ValueCodec[math.LegacyDec] = legacyDecValueCodec{}

Expand All @@ -46,6 +49,11 @@ const (
LegacyDec string = "math.LegacyDec"
)

const (
Int string = "math.Int"
Uint string = "math.Uint"
)

type addressUnion interface {
AccAddress | ValAddress | ConsAddress
String() string
Expand Down Expand Up @@ -170,7 +178,43 @@ func (i intValueCodec) Stringify(value math.Int) string {
}

func (i intValueCodec) ValueType() string {
return "math.Int"
return Int
}

type uintValueCodec struct{}

func (i uintValueCodec) Encode(value math.Uint) ([]byte, error) {
return value.Marshal()
}

func (i uintValueCodec) Decode(b []byte) (math.Uint, error) {
v := new(math.Uint)
err := v.Unmarshal(b)
if err != nil {
return math.Uint{}, err
}
return *v, nil
}

func (i uintValueCodec) EncodeJSON(value math.Uint) ([]byte, error) {
return value.MarshalJSON()
}

func (i uintValueCodec) DecodeJSON(b []byte) (math.Uint, error) {
v := new(math.Uint)
err := v.UnmarshalJSON(b)
if err != nil {
return math.Uint{}, err
}
return *v, nil
}

func (i uintValueCodec) Stringify(value math.Uint) string {
return value.String()
}

func (i uintValueCodec) ValueType() string {
return Uint
}

type legacyDecValueCodec struct{}
Expand Down
Loading