Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow setting the base denom #16257

Merged
merged 6 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -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
emidev98 marked this conversation as resolved.
Show resolved Hide resolved

### 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{}
}