Skip to content

Commit

Permalink
feat(mint): add optional inflation bounds params
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Feb 26, 2024
1 parent 0dd8526 commit fd77aab
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 30 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
];
}
2 changes: 1 addition & 1 deletion x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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
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.

16 changes: 12 additions & 4 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ 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 +55,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
37 changes: 35 additions & 2 deletions x/mint/types/minter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import (
)

func TestNextInflation(t *testing.T) {
infMin := math.LegacyNewDecWithPrec(7, 2)
infMax := math.LegacyNewDecWithPrec(10, 2)

Convey("Given a test cases", t, func() {
cases := []struct {
name string
inflationRatio math.LegacyDec
bondedRatio math.LegacyDec
minBound *math.LegacyDec
maxBound *math.LegacyDec
totalSupply math.Int
expectedInflation math.LegacyDec
expectedAnnualProvisions math.LegacyDec
Expand All @@ -26,6 +31,8 @@ func TestNextInflation(t *testing.T) {
name: "inflation ratio is 0",
inflationRatio: math.LegacyNewDec(0),
bondedRatio: math.LegacyNewDecWithPrec(20, 2),
minBound: nil,
maxBound: nil,
totalSupply: math.NewInt(1000),
expectedInflation: math.LegacyNewDec(0),
expectedAnnualProvisions: math.LegacyNewDec(0),
Expand All @@ -34,21 +41,47 @@ func TestNextInflation(t *testing.T) {
name: "inflation ratio is 0.03",
inflationRatio: math.LegacyNewDecWithPrec(3, 2),
bondedRatio: math.LegacyNewDecWithPrec(2, 1),
minBound: nil,
maxBound: nil,
totalSupply: math.NewInt(1000),
expectedInflation: math.LegacyNewDecWithPrec(15, 2),
expectedAnnualProvisions: math.LegacyNewDec(150),
},
{
name: "inflation max is 0.1",
inflationRatio: math.LegacyNewDecWithPrec(3, 2),
bondedRatio: math.LegacyNewDecWithPrec(2, 1),
minBound: nil,
maxBound: &infMax,
totalSupply: math.NewInt(1000),
expectedInflation: math.LegacyNewDecWithPrec(10, 2),
expectedAnnualProvisions: math.LegacyNewDec(100),
},
{
name: "inflation min is 0.07",
inflationRatio: math.LegacyNewDecWithPrec(3, 2),
bondedRatio: math.LegacyNewDecWithPrec(7, 1),
minBound: &infMin,
maxBound: &infMax,
totalSupply: math.NewInt(1000),
expectedInflation: math.LegacyNewDecWithPrec(7, 2),
expectedAnnualProvisions: math.LegacyNewDec(70),
},
{
name: "bonded ratio is 0",
inflationRatio: math.LegacyNewDecWithPrec(3, 2),
bondedRatio: math.LegacyNewDec(0),
minBound: nil,
maxBound: nil,
totalSupply: math.NewInt(1000),
expectedErr: fmt.Errorf("bonded ratio is zero"),
},
{
name: "negative inflation ratio",
inflationRatio: math.LegacyNewDecWithPrec(3, 2),
bondedRatio: math.LegacyNewDecWithPrec(-2, 1),
minBound: nil,
maxBound: nil,
totalSupply: math.NewInt(1000),
expectedErr: fmt.Errorf("mint parameter Inflation should be positive, is -0.150000000000000000"),
},
Expand All @@ -58,7 +91,7 @@ func TestNextInflation(t *testing.T) {
Convey(
fmt.Sprintf("Given test case #%d: %v", nc, tc.name), func() {
Convey("when calling NewMinterWithInflationCoef function", func() {
minter, err := NewMinterWithInflationCoef(tc.inflationRatio, tc.bondedRatio, tc.totalSupply)
minter, err := NewMinterWithInflationCoef(tc.inflationRatio, tc.bondedRatio, tc.minBound, tc.maxBound, tc.totalSupply)
if tc.expectedErr != nil {
Convey("then an error should occur", func() {
So(err, ShouldNotBeNil)
Expand Down Expand Up @@ -109,7 +142,7 @@ func BenchmarkNextInflation(b *testing.B) {

// run the NextInflationRate function b.N times
for n := 0; n < b.N; n++ {
_, err := NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, totalSupply)
_, err := NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, nil, nil, totalSupply)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit fd77aab

Please sign in to comment.