Skip to content

Commit

Permalink
refactor(x/mint): initialize inflation decimals once (backport #2407) (
Browse files Browse the repository at this point in the history
…#2513)

This is an automatic backport of pull request #2407 done by
[Mergify](https://mergify.com).


---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the
[documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on
`<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com) you
can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>

Co-authored-by: Rootul P <[email protected]>
  • Loading branch information
mergify[bot] and rootulp authored Sep 18, 2023
1 parent 57b2b9f commit 10fb48d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 9 additions & 3 deletions x/mint/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ const (
TargetInflationRate = 0.015
)

var (
initialInflationRateAsDec = sdk.NewDecWithPrec(InitialInflationRate*1000, 3)
disinflationRateAsDec = sdk.NewDecWithPrec(DisinflationRate*1000, 3)
targetInflationRateAsDec = sdk.NewDecWithPrec(TargetInflationRate*1000, 3)
)

func InitialInflationRateAsDec() sdk.Dec {
return sdk.NewDecWithPrec(InitialInflationRate*1000, 3)
return initialInflationRateAsDec
}

func DisinflationRateAsDec() sdk.Dec {
return sdk.NewDecWithPrec(DisinflationRate*1000, 3)
return disinflationRateAsDec
}

func TargetInflationRateAsDec() sdk.Dec {
return sdk.NewDecWithPrec(TargetInflationRate*1000, 3)
return targetInflationRateAsDec
}
12 changes: 4 additions & 8 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ func NewMinter(inflationRate sdk.Dec, annualProvisions sdk.Dec, bondDenom string

// DefaultMinter returns a Minter object with default values.
func DefaultMinter() Minter {
inflationRate := InitialInflationRateAsDec()
annualProvisions := sdk.NewDec(0)
return NewMinter(inflationRate, annualProvisions, DefaultBondDenom)
return NewMinter(InitialInflationRateAsDec(), annualProvisions, DefaultBondDenom)
}

// Validate returns an error if the minter is invalid.
Expand All @@ -44,13 +43,10 @@ func (m Minter) Validate() error {
// decrease every year according to the schedule specified in the README.
func (m Minter) CalculateInflationRate(ctx sdk.Context, genesis time.Time) sdk.Dec {
years := yearsSinceGenesis(genesis, ctx.BlockTime())
initialInflationRate := InitialInflationRateAsDec()
disinflationRate := DisinflationRateAsDec()
inflationRate := initialInflationRate.Mul(sdk.OneDec().Sub(disinflationRate).Power(uint64(years)))
targetInflationRate := TargetInflationRateAsDec()
inflationRate := InitialInflationRateAsDec().Mul(sdk.OneDec().Sub(DisinflationRateAsDec()).Power(uint64(years)))

if inflationRate.LT(targetInflationRate) {
return targetInflationRate
if inflationRate.LT(TargetInflationRateAsDec()) {
return TargetInflationRateAsDec()
}
return inflationRate
}
Expand Down
10 changes: 4 additions & 6 deletions x/mint/types/minter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ func TestCalculateBlockProvision(t *testing.T) {
minter := DefaultMinter()
current := time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC)
blockInterval := 15 * time.Second
initialInflationRate := InitialInflationRateAsDec()
totalSupply := sdk.NewDec(1_000_000_000_000) // 1 trillion utia
annualProvisions := totalSupply.Mul(initialInflationRate) // 80 billion utia
totalSupply := sdk.NewDec(1_000_000_000_000) // 1 trillion utia
annualProvisions := totalSupply.Mul(InitialInflationRateAsDec()) // 80 billion utia

type testCase struct {
name string
Expand Down Expand Up @@ -138,9 +137,8 @@ func TestCalculateBlockProvisionError(t *testing.T) {
oneYear := time.Duration(NanosecondsPerYear)
end := current.Add(oneYear)

initialInflationRate := InitialInflationRateAsDec()
totalSupply := sdk.NewDec(1_000_000_000_000) // 1 trillion utia
annualProvisions := totalSupply.Mul(initialInflationRate) // 80 billion utia
totalSupply := sdk.NewDec(1_000_000_000_000) // 1 trillion utia
annualProvisions := totalSupply.Mul(InitialInflationRateAsDec()) // 80 billion utia
minter.AnnualProvisions = annualProvisions
totalBlockProvisions := sdk.NewDec(0)
for current.Before(end) {
Expand Down

0 comments on commit 10fb48d

Please sign in to comment.