Skip to content

Commit 60d0374

Browse files
authored
Merge pull request #574 from okp4/feat/mint-inflation-bounds
feat(mint): add optional inflation bounds params
2 parents 89bfdd1 + 3d26690 commit 60d0374

File tree

10 files changed

+305
-34
lines changed

10 files changed

+305
-34
lines changed

docs/proto/mint.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Params defines the parameters for the mint module.
7474
| `mint_denom` | [string](#string) | | Denomination of the coin to be minted. |
7575
| `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. |
7676
| `blocks_per_year` | [uint64](#uint64) | | Estimated number of blocks per year. |
77+
| `inflation_max` | [string](#string) | | Maximum annual inflation rate. |
78+
| `inflation_min` | [string](#string) | | Minimum annual inflation rate. |
7779

7880
[//]: # (end messages)
7981

proto/mint/v1beta1/mint.proto

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ message Params {
3737
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
3838
(gogoproto.nullable) = false
3939
];
40+
4041
// Estimated number of blocks per year.
4142
uint64 blocks_per_year = 3;
43+
44+
// Maximum annual inflation rate.
45+
string inflation_max = 4 [
46+
(cosmos_proto.scalar) = "cosmos.Dec",
47+
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
48+
(gogoproto.nullable) = true
49+
];
50+
51+
// Minimum annual inflation rate.
52+
string inflation_min = 5 [
53+
(cosmos_proto.scalar) = "cosmos.Dec",
54+
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
55+
(gogoproto.nullable) = true
56+
];
4257
}

x/mint/abci.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
2828
return err
2929
}
3030

31-
minter, err := types.NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, totalSupply)
31+
minter, err := types.NewMinterWithInflationCoef(
32+
params.InflationCoef,
33+
bondedRatio,
34+
params.InflationMin,
35+
params.InflationMax,
36+
totalSupply,
37+
)
3238
if err != nil {
3339
panic(err)
3440
}
@@ -66,6 +72,5 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
6672
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
6773
),
6874
)
69-
7075
return nil
7176
}

x/mint/client/cli/query_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ func TestGetCmdQueryParams(t *testing.T) {
4646
"json output",
4747
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
4848
`[--height=1 --output=json]`,
49-
`{"mint_denom":"","inflation_coef":"0","blocks_per_year":"0"}`,
49+
`{"mint_denom":"","inflation_coef":"0","blocks_per_year":"0","inflation_max":null,"inflation_min":null}`,
5050
},
5151
{
5252
"text output",
5353
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
5454
`[--height=1 --output=text]`,
5555
`blocks_per_year: "0"
5656
inflation_coef: "0"
57+
inflation_max: null
58+
inflation_min: null
5759
mint_denom: ""`,
5860
},
5961
}

x/mint/keeper/msg_server_test.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
)
1010

1111
func (s *IntegrationTestSuite) TestUpdateParams() {
12+
validInfMin := math.LegacyNewDecWithPrec(3, 2)
13+
validInfMax := math.LegacyNewDecWithPrec(20, 2)
14+
invalidInfMin := math.LegacyNewDecWithPrec(-3, 2)
15+
invalidInfMax := math.LegacyNewDecWithPrec(-20, 2)
16+
1217
testCases := []struct {
1318
name string
1419
request *types.MsgUpdateParams
@@ -34,12 +39,70 @@ func (s *IntegrationTestSuite) TestUpdateParams() {
3439
expectErr: true,
3540
},
3641
{
37-
name: "set full valid params",
42+
name: "set invalid params for inflation max (negative value)",
43+
request: &types.MsgUpdateParams{
44+
Authority: s.mintKeeper.GetAuthority(),
45+
Params: types.Params{
46+
MintDenom: sdk.DefaultBondDenom,
47+
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
48+
InflationMax: &invalidInfMax,
49+
InflationMin: nil,
50+
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
51+
},
52+
},
53+
expectErr: true,
54+
},
55+
{
56+
name: "set invalid params for inflation min (negative value)",
57+
request: &types.MsgUpdateParams{
58+
Authority: s.mintKeeper.GetAuthority(),
59+
Params: types.Params{
60+
MintDenom: sdk.DefaultBondDenom,
61+
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
62+
InflationMax: nil,
63+
InflationMin: &invalidInfMin,
64+
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
65+
},
66+
},
67+
expectErr: true,
68+
},
69+
{
70+
name: "set invalid params for inflation min & max (min > max)",
71+
request: &types.MsgUpdateParams{
72+
Authority: s.mintKeeper.GetAuthority(),
73+
Params: types.Params{
74+
MintDenom: sdk.DefaultBondDenom,
75+
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
76+
InflationMax: &validInfMin,
77+
InflationMin: &validInfMax,
78+
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
79+
},
80+
},
81+
expectErr: true,
82+
},
83+
{
84+
name: "set full valid params with boundaries",
85+
request: &types.MsgUpdateParams{
86+
Authority: s.mintKeeper.GetAuthority(),
87+
Params: types.Params{
88+
MintDenom: sdk.DefaultBondDenom,
89+
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
90+
InflationMax: &validInfMax,
91+
InflationMin: &validInfMin,
92+
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
93+
},
94+
},
95+
expectErr: false,
96+
},
97+
{
98+
name: "set full valid params without boundaries",
3899
request: &types.MsgUpdateParams{
39100
Authority: s.mintKeeper.GetAuthority(),
40101
Params: types.Params{
41102
MintDenom: sdk.DefaultBondDenom,
42103
InflationCoef: math.LegacyNewDecWithPrec(73, 2),
104+
InflationMax: nil,
105+
InflationMin: nil,
43106
BlocksPerYear: uint64(60 * 60 * 8766 / 5),
44107
},
45108
},

x/mint/simulation/genesis_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestRandomizedGenState(t *testing.T) {
4545

4646
inflationCoef := math.LegacyNewDecWithPrec(3, 2)
4747
bondedRatio := math.LegacyNewDecWithPrec(2, 1)
48-
minter, _ := types.NewMinterWithInflationCoef(inflationCoef, bondedRatio, simState.InitialStake)
48+
minter, _ := types.NewMinterWithInflationCoef(inflationCoef, bondedRatio, nil, nil, simState.InitialStake)
4949

5050
require.Equal(t, uint64(6311520), mintGenesis.Params.BlocksPerYear)
5151
require.Equal(t, "0.073000000000000000", mintGenesis.Params.InflationCoef.String())

x/mint/types/mint.pb.go

Lines changed: 133 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/mint/types/minter.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ func NewMinterWithInitialInflation(inflation math.LegacyDec) Minter {
2626
}
2727

2828
// NewMinterWithInflationCoef returns a new Minter with updated inflation and annual provisions values.
29-
func NewMinterWithInflationCoef(inflationCoef math.LegacyDec, bondedRatio math.LegacyDec, totalSupply math.Int) (Minter, error) {
30-
inflationRate, err := inflationRate(inflationCoef, bondedRatio)
29+
func NewMinterWithInflationCoef(
30+
inflationCoef, bondedRatio math.LegacyDec,
31+
minBound, maxBound *math.LegacyDec,
32+
totalSupply math.Int,
33+
) (Minter, error) {
34+
inflationRate, err := inflationRate(inflationCoef, bondedRatio, minBound, maxBound)
3135
if err != nil {
3236
return Minter{}, err
3337
}
@@ -55,12 +59,20 @@ func (m Minter) Validate() error {
5559

5660
// inflationRate returns the inflation rate computed from the current bonded ratio
5761
// and the inflation parameter.
58-
func inflationRate(inflationCoef math.LegacyDec, bondedRatio math.LegacyDec) (math.LegacyDec, error) {
62+
func inflationRate(inflationCoef, bondedRatio math.LegacyDec, minBound, maxBound *math.LegacyDec) (math.LegacyDec, error) {
5963
if bondedRatio.IsZero() {
6064
return math.LegacyZeroDec(), ErrBondedRatioIsZero
6165
}
6266

63-
return inflationCoef.Quo(bondedRatio), nil
67+
rate := inflationCoef.Quo(bondedRatio)
68+
if minBound != nil {
69+
rate = math.LegacyMaxDec(rate, *minBound)
70+
}
71+
if maxBound != nil {
72+
rate = math.LegacyMinDec(rate, *maxBound)
73+
}
74+
75+
return rate, nil
6476
}
6577

6678
// BlockProvision returns the provisions for a block based on the annual

0 commit comments

Comments
 (0)