Skip to content

Commit

Permalink
fix scaling of rate values -- should be yearly rates
Browse files Browse the repository at this point in the history
  • Loading branch information
nddeluca committed Dec 8, 2022
1 parent ebf2f4c commit 3053443
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion x/kavamint/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type genesisTestSuite struct {

func (suite *genesisTestSuite) Test_InitGenesis_ValidationPanic() {
invalidState := types.NewGenesisState(
types.NewParams(sdk.OneDec(), sdk.OneDec()), // rate over max
types.NewParams(sdk.OneDec(), types.MaxMintingRate.Add(sdk.OneDec())), // rate over max
time.Time{},
)

Expand Down
36 changes: 18 additions & 18 deletions x/kavamint/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"testing"

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/stretchr/testify/suite"

"github.com/kava-labs/kava/x/kavamint/testutil"
"github.com/kava-labs/kava/x/kavamint/types"
Expand Down Expand Up @@ -59,39 +60,39 @@ func (suite *grpcQueryTestSuite) TestGRPCInflationQuery() {
{
name: "no community inflation means only staking contributes",
communityInflation: sdk.NewDec(0),
stakingApy: sdk.NewDecWithPrec(1, 10),
stakingApy: sdk.NewDec(1),
bondedRatio: sdk.NewDecWithPrec(34, 2),
expectedInflation: sdk.NewDecWithPrec(34, 12),
expectedInflation: sdk.NewDecWithPrec(34, 2),
},
{
name: "no staking apy means only inflation contributes",
communityInflation: sdk.NewDecWithPrec(75, 10),
communityInflation: sdk.NewDecWithPrec(75, 2),
stakingApy: sdk.NewDec(0),
bondedRatio: sdk.NewDecWithPrec(40, 2),
expectedInflation: sdk.NewDecWithPrec(75, 10),
expectedInflation: sdk.NewDecWithPrec(75, 2),
},
{
name: "staking and community inflation combines (100 percent bonded)",
communityInflation: sdk.NewDecWithPrec(1, 10),
stakingApy: sdk.NewDecWithPrec(50, 12),
communityInflation: sdk.NewDec(1),
stakingApy: sdk.NewDecWithPrec(50, 2),
bondedRatio: sdk.NewDec(1),
expectedInflation: sdk.NewDecWithPrec(150, 12),
expectedInflation: sdk.NewDecWithPrec(150, 2),
},
{
name: "staking and community inflation combines (40 percent bonded)",
communityInflation: sdk.NewDecWithPrec(90, 10),
stakingApy: sdk.NewDecWithPrec(25, 10),
communityInflation: sdk.NewDecWithPrec(90, 2),
stakingApy: sdk.NewDecWithPrec(25, 2),
bondedRatio: sdk.NewDecWithPrec(40, 2),
// 90 + .4*25 = 100
expectedInflation: sdk.NewDecWithPrec(1, 8),
expectedInflation: sdk.NewDec(1),
},
{
name: "staking and community inflation combines (25 percent bonded)",
communityInflation: sdk.NewDecWithPrec(90, 10),
stakingApy: sdk.NewDecWithPrec(20, 10),
communityInflation: sdk.NewDecWithPrec(90, 2),
stakingApy: sdk.NewDecWithPrec(20, 2),
bondedRatio: sdk.NewDecWithPrec(25, 2),
// 90 + .25*20 = 95
expectedInflation: sdk.NewDecWithPrec(95, 10),
expectedInflation: sdk.NewDecWithPrec(95, 2),
},
}

Expand All @@ -111,11 +112,10 @@ func (suite *grpcQueryTestSuite) TestGRPCInflationQuery() {
staking.EndBlocker(ctx, suite.StakingKeeper)

// query inflation & check for expected results
response, err := queryClient.Inflation(context.Background(), &types.QueryInflationRequest{})
inflation, err := queryClient.Inflation(context.Background(), &types.QueryInflationRequest{})
suite.Require().NoError(err)

suite.Require().Equal(kavamintKeeper.CumulativeInflation(ctx), response.Inflation)
suite.Require().Equal(tc.expectedInflation, response.Inflation)
suite.Require().Equal(inflation.Inflation, kavamintKeeper.CumulativeInflation(ctx))
suite.Require().Equal(inflation.Inflation, tc.expectedInflation)
})
}
}
4 changes: 2 additions & 2 deletions x/kavamint/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestGenesis_Validation(t *testing.T) {
types.NewParams(types.MaxMintingRate.Add(sdk.NewDecWithPrec(1, 18)), sdk.ZeroDec()),
time.Now(),
),
"invalid rate: 0.000000146028999311",
"invalid rate: 100.000000000000000001",
},
{
"invalid - staking reward inflation param too big",
Expand All @@ -73,7 +73,7 @@ func TestGenesis_Validation(t *testing.T) {
types.NewParams(sdk.ZeroDec(), types.MaxMintingRate.Add(sdk.NewDecWithPrec(1, 18))),
time.Now(),
),
"invalid rate: 0.000000146028999311",
"invalid rate: 100.000000000000000001",
},
{
"invalid - negative community inflation param",
Expand Down
2 changes: 1 addition & 1 deletion x/kavamint/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
DefaultStakingRewardsApy = sdk.ZeroDec()

// MaxMintingRate returns the per second rate equivalent to 10,000% per year
MaxMintingRate = sdk.MustNewDecFromStr("0.000000146028999310")
MaxMintingRate = sdk.NewDec(100)
)

// NewParams returns new Params with inflation rates set
Expand Down
33 changes: 17 additions & 16 deletions x/kavamint/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ const secondsPerYear = 31536000

func newValidParams(t *testing.T) types.Params {
// 50%
poolRate, err := sdk.MustNewDecFromStr("1.5").ApproxRoot(secondsPerYear)
require.NoError(t, err)
poolRate = poolRate.Sub(sdk.OneDec())
poolRate := sdk.MustNewDecFromStr("0.5")

// 10%
stakingRate, err := sdk.MustNewDecFromStr("1.1").ApproxRoot(secondsPerYear)
require.NoError(t, err)
stakingRate = stakingRate.Sub(sdk.OneDec())
stakingRate := sdk.MustNewDecFromStr("0.1")

params := types.NewParams(poolRate, stakingRate)
require.NoError(t, params.Validate())
Expand All @@ -47,25 +43,30 @@ func TestParams_Default(t *testing.T) {
assert.Equal(t, defaultParams.CommunityPoolInflation, sdk.ZeroDec(), "expected default staking inflation to be zero")
}

func TestParams_MaxInflationRate_DerivedCorrectly(t *testing.T) {
maxYearlyRate := sdk.NewDec(100) // 10,000%, should never be a reason to exceed this value

expectedMaxRate, err := maxYearlyRate.ApproxRoot(secondsPerYear)
require.NoError(t, err)
expectedMaxRate = expectedMaxRate.Sub(sdk.OneDec())
func TestParams_MaxInflationRate_ApproxRootDoesNotPanic(t *testing.T) {
require.Equal(t, sdk.NewDec(100), types.MaxMintingRate) // 10,000%, should never be a reason to exceed this value
maxYearlyRate := types.MaxMintingRate

require.Equal(t, expectedMaxRate, types.MaxMintingRate)
require.NotPanics(t, func() {
expectedMaxRate, err := maxYearlyRate.ApproxRoot(secondsPerYear)
require.NoError(t, err)
expectedMaxRate = expectedMaxRate.Sub(sdk.OneDec())
})
}

func TestParams_MaxInflationRate_DoesNotOverflow(t *testing.T) {
maxRate := types.MaxMintingRate // use the max minting rate
totalSupply := sdk.NewDec(1e14) // 100 trillion starting supply
years := uint64(25) // calculate over 50 years

perSecondMaxRate, err := maxRate.ApproxRoot(secondsPerYear)
require.NoError(t, err)
perSecondMaxRate = perSecondMaxRate.Sub(sdk.OneDec())

var finalSupply sdk.Int

require.NotPanics(t, func() {
compoundedRate := maxRate.Power(years * secondsPerYear)
compoundedRate := perSecondMaxRate.Power(years * secondsPerYear)
finalSupply = totalSupply.Mul(compoundedRate).RoundInt()

})
Expand Down Expand Up @@ -168,7 +169,7 @@ func TestParams_Validation(t *testing.T) {
testFn: func(params *types.Params) {
params.CommunityPoolInflation = types.MaxMintingRate.Add(sdk.NewDecWithPrec(1, 18))
},
expectedErr: "invalid rate: 0.000000146028999311",
expectedErr: "invalid rate: 100.000000000000000001",
},
{
name: "nil staking inflation",
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestParams_Validation(t *testing.T) {
testFn: func(params *types.Params) {
params.StakingRewardsApy = types.MaxMintingRate.Add(sdk.NewDecWithPrec(1, 18))
},
expectedErr: "invalid rate: 0.000000146028999311",
expectedErr: "invalid rate: 100.000000000000000001",
},
}

Expand Down

0 comments on commit 3053443

Please sign in to comment.