Skip to content

Commit

Permalink
Merge pull request #574 from okp4/feat/mint-inflation-bounds
Browse files Browse the repository at this point in the history
feat(mint): add optional inflation bounds params
  • Loading branch information
amimart authored Feb 27, 2024
2 parents 89bfdd1 + 3d26690 commit 60d0374
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 34 deletions.
2 changes: 2 additions & 0 deletions docs/proto/mint.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Params defines the parameters for the mint module.
| `mint_denom` | [string](#string) | | Denomination of the coin to be minted. |
| `inflation_coef` | [string](#string) | | Annual inflation coefficient, influencing the inflation rate based on the bonded ratio. Values range from 0 to 1, with higher values indicating higher inflation. |
| `blocks_per_year` | [uint64](#uint64) | | Estimated number of blocks per year. |
| `inflation_max` | [string](#string) | | Maximum annual inflation rate. |
| `inflation_min` | [string](#string) | | Minimum annual inflation rate. |

[//]: # (end messages)

Expand Down
15 changes: 15 additions & 0 deletions proto/mint/v1beta1/mint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ message Params {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];

// Estimated number of blocks per year.
uint64 blocks_per_year = 3;

// Maximum annual inflation rate.
string inflation_max = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = true
];

// Minimum annual inflation rate.
string inflation_min = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = true
];
}
9 changes: 7 additions & 2 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
return err
}

minter, err := types.NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, totalSupply)
minter, err := types.NewMinterWithInflationCoef(
params.InflationCoef,
bondedRatio,
params.InflationMin,
params.InflationMax,
totalSupply,
)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -66,6 +72,5 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)

return nil
}
4 changes: 3 additions & 1 deletion x/mint/client/cli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ func TestGetCmdQueryParams(t *testing.T) {
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
`[--height=1 --output=json]`,
`{"mint_denom":"","inflation_coef":"0","blocks_per_year":"0"}`,
`{"mint_denom":"","inflation_coef":"0","blocks_per_year":"0","inflation_max":null,"inflation_min":null}`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
`[--height=1 --output=text]`,
`blocks_per_year: "0"
inflation_coef: "0"
inflation_max: null
inflation_min: null
mint_denom: ""`,
},
}
Expand Down
65 changes: 64 additions & 1 deletion x/mint/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
)

func (s *IntegrationTestSuite) TestUpdateParams() {
validInfMin := math.LegacyNewDecWithPrec(3, 2)
validInfMax := math.LegacyNewDecWithPrec(20, 2)
invalidInfMin := math.LegacyNewDecWithPrec(-3, 2)
invalidInfMax := math.LegacyNewDecWithPrec(-20, 2)

testCases := []struct {
name string
request *types.MsgUpdateParams
Expand All @@ -34,12 +39,70 @@ func (s *IntegrationTestSuite) TestUpdateParams() {
expectErr: true,
},
{
name: "set full valid params",
name: "set invalid params for inflation max (negative value)",
request: &types.MsgUpdateParams{
Authority: s.mintKeeper.GetAuthority(),
Params: types.Params{
MintDenom: sdk.DefaultBondDenom,
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
InflationMax: &invalidInfMax,
InflationMin: nil,
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
},
},
expectErr: true,
},
{
name: "set invalid params for inflation min (negative value)",
request: &types.MsgUpdateParams{
Authority: s.mintKeeper.GetAuthority(),
Params: types.Params{
MintDenom: sdk.DefaultBondDenom,
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
InflationMax: nil,
InflationMin: &invalidInfMin,
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
},
},
expectErr: true,
},
{
name: "set invalid params for inflation min & max (min > max)",
request: &types.MsgUpdateParams{
Authority: s.mintKeeper.GetAuthority(),
Params: types.Params{
MintDenom: sdk.DefaultBondDenom,
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
InflationMax: &validInfMin,
InflationMin: &validInfMax,
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
},
},
expectErr: true,
},
{
name: "set full valid params with boundaries",
request: &types.MsgUpdateParams{
Authority: s.mintKeeper.GetAuthority(),
Params: types.Params{
MintDenom: sdk.DefaultBondDenom,
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
InflationMax: &validInfMax,
InflationMin: &validInfMin,
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
},
},
expectErr: false,
},
{
name: "set full valid params without boundaries",
request: &types.MsgUpdateParams{
Authority: s.mintKeeper.GetAuthority(),
Params: types.Params{
MintDenom: sdk.DefaultBondDenom,
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
InflationMax: nil,
InflationMin: nil,
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
},
},
Expand Down
2 changes: 1 addition & 1 deletion x/mint/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestRandomizedGenState(t *testing.T) {

inflationCoef := math.LegacyNewDecWithPrec(3, 2)
bondedRatio := math.LegacyNewDecWithPrec(2, 1)
minter, _ := types.NewMinterWithInflationCoef(inflationCoef, bondedRatio, simState.InitialStake)
minter, _ := types.NewMinterWithInflationCoef(inflationCoef, bondedRatio, nil, nil, simState.InitialStake)

require.Equal(t, uint64(6311520), mintGenesis.Params.BlocksPerYear)
require.Equal(t, "0.073000000000000000", mintGenesis.Params.InflationCoef.String())
Expand Down
156 changes: 133 additions & 23 deletions x/mint/types/mint.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ func NewMinterWithInitialInflation(inflation math.LegacyDec) Minter {
}

// NewMinterWithInflationCoef returns a new Minter with updated inflation and annual provisions values.
func NewMinterWithInflationCoef(inflationCoef math.LegacyDec, bondedRatio math.LegacyDec, totalSupply math.Int) (Minter, error) {
inflationRate, err := inflationRate(inflationCoef, bondedRatio)
func NewMinterWithInflationCoef(
inflationCoef, bondedRatio math.LegacyDec,
minBound, maxBound *math.LegacyDec,
totalSupply math.Int,
) (Minter, error) {
inflationRate, err := inflationRate(inflationCoef, bondedRatio, minBound, maxBound)
if err != nil {
return Minter{}, err
}
Expand Down Expand Up @@ -55,12 +59,20 @@ func (m Minter) Validate() error {

// inflationRate returns the inflation rate computed from the current bonded ratio
// and the inflation parameter.
func inflationRate(inflationCoef math.LegacyDec, bondedRatio math.LegacyDec) (math.LegacyDec, error) {
func inflationRate(inflationCoef, bondedRatio math.LegacyDec, minBound, maxBound *math.LegacyDec) (math.LegacyDec, error) {
if bondedRatio.IsZero() {
return math.LegacyZeroDec(), ErrBondedRatioIsZero
}

return inflationCoef.Quo(bondedRatio), nil
rate := inflationCoef.Quo(bondedRatio)
if minBound != nil {
rate = math.LegacyMaxDec(rate, *minBound)
}
if maxBound != nil {
rate = math.LegacyMinDec(rate, *maxBound)
}

return rate, nil
}

// BlockProvision returns the provisions for a block based on the annual
Expand Down
Loading

0 comments on commit 60d0374

Please sign in to comment.