Skip to content

Commit

Permalink
feat: allow setting the base denom (#16257)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
emidev98 and alexanderbez authored May 23, 2023
1 parent 81ba019 commit 995d2bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (tx) [#15992](https://github.com/cosmos/cosmos-sdk/pull/15992) Add `WithExtensionOptions` in tx Factory to allow `SetExtensionOptions` with given extension options.
* (types/simulation) [#16074](https://github.com/cosmos/cosmos-sdk/pull/16074) Add generic SimulationStoreDecoder for modules using collections.
* (cli) [#16209](https://github.com/cosmos/cosmos-sdk/pull/16209) Make `StartCmd` more customizable.
* (types) [#16257](https://github.com/cosmos/cosmos-sdk/pull/16257) Allow setting the base denom in the denom registry.

### Improvements

Expand Down
11 changes: 11 additions & 0 deletions types/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ func GetDenomUnit(denom string) (Dec, bool) {
return unit, true
}

// SetBaseDenom allow overwritting the base denom
// if the denom has registered before, otherwise return error
func SetBaseDenom(denom string) error {
_, ok := denomUnits[denom]
if !ok {
return fmt.Errorf("denom %s not registered", denom)
}
baseDenom = denom
return nil
}

// GetBaseDenom returns the denom of smallest unit registered
func GetBaseDenom() (string, error) {
if baseDenom == "" {
Expand Down
16 changes: 16 additions & 0 deletions types/denom_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func (s *internalDenomTestSuite) TestRegisterDenom() {
s.Require().False(ok)
s.Require().Equal(math.LegacyZeroDec(), res)

err := SetBaseDenom(atom)
s.Require().NoError(err)

res, ok = GetDenomUnit(atom)
s.Require().True(ok)
s.Require().Equal(atomUnit, res)

// reset registration
baseDenom = ""
denomUnits = map[string]Dec{}
Expand Down Expand Up @@ -192,3 +199,12 @@ func (s *internalDenomTestSuite) TestDecOperationOrder() {
baseDenom = ""
denomUnits = map[string]Dec{}
}

func (s *internalDenomTestSuite) TestSetBaseDenomError() {
err := SetBaseDenom(atom)
s.Require().Error(err)

// reset registration
baseDenom = ""
denomUnits = map[string]Dec{}
}

0 comments on commit 995d2bc

Please sign in to comment.