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

fix(inflation): small fixes and tests #1765

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 x/inflation/types/inflation_calculation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func polynomial(factors []sdk.Dec, x sdk.Dec) sdk.Dec {
}

// Multiply by 1 million to get the value in unibi
// 1 unibi = 1e6 nibi and the polynomial was fit on nibi token curve.
return result.Mul(sdk.NewDec(1_000_000))
}
36 changes: 31 additions & 5 deletions x/inflation/types/inflation_calculation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestCalculateEpochMintProvision(t *testing.T) {
epochId := uint64(0)
totalInflation := sdk.ZeroDec()

// Only the first 8 years have inflation with default params
// Only the first 8 years have inflation with default params but we run
// for 10 years expecting 0 inflation
for year := uint64(0); year < 10; year++ {
yearlyInflation := sdk.ZeroDec()
for month := uint64(0); month < 12; month++ {
Expand All @@ -41,13 +42,38 @@ func TestCalculateEpochMintProvision(t *testing.T) {
}
// Should be within 0.0098%
if year < uint64(len(ExpectedYearlyInflation)) {
require.NoError(t, withingRange(yearlyInflation, ExpectedYearlyInflation[year]))
require.NoError(t, withinRange(yearlyInflation, ExpectedYearlyInflation[year]))
} else {
require.Equal(t, yearlyInflation, sdk.ZeroDec())
}
totalInflation = totalInflation.Add(yearlyInflation)
}
require.NoError(t, withingRange(totalInflation, ExpectedTotalInflation))
require.NoError(t, withinRange(totalInflation, ExpectedTotalInflation))
}

func TestCalculateEpochMintProvisionInflationNotEnabled(t *testing.T) {
params := DefaultParams()
params.InflationEnabled = false

epochId := uint64(0)
totalInflation := sdk.ZeroDec()

// Only the first 8 years have inflation with default params but we run
// for 10 years expecting 0 inflation
for year := uint64(0); year < 10; year++ {
yearlyInflation := sdk.ZeroDec()
for month := uint64(0); month < 12; month++ {
for day := uint64(0); day < 30; day++ {
epochMintProvisions := CalculateEpochMintProvision(params, epochId)
yearlyInflation = yearlyInflation.Add(epochMintProvisions)
}
epochId++
}

require.Equal(t, yearlyInflation, sdk.ZeroDec())
totalInflation = totalInflation.Add(yearlyInflation)
}
require.Equal(t, totalInflation, sdk.ZeroDec())
}

func TestCalculateEpochMintProvision_ZeroEpochs(t *testing.T) {
Expand All @@ -58,9 +84,9 @@ func TestCalculateEpochMintProvision_ZeroEpochs(t *testing.T) {
require.Equal(t, epochMintProvisions, sdk.ZeroDec())
}

// withingRange returns an error if the actual value is not within the expected value +/- tolerance
// withinRange returns an error if the actual value is not within the expected value +/- tolerance
// tolerance is a percentage set to 0.01% by default
func withingRange(expected, actual sdk.Dec) error {
func withinRange(expected, actual sdk.Dec) error {
tolerance := sdk.NewDecWithPrec(1, 4)
is_within := expected.Sub(actual).Abs().Quo(expected).LTE(tolerance)
if !is_within {
Expand Down
2 changes: 1 addition & 1 deletion x/inflation/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewParams(
}
}

// default minting module parameters
// default inflation module parameters
func DefaultParams() Params {
return Params{
PolynomialFactors: DefaultPolynomialFactors,
Expand Down
Loading