Skip to content

Commit

Permalink
feat(mint): add new mint function calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux authored and ccamel committed Dec 4, 2023
1 parent 6484ce4 commit a2bc105
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
44 changes: 44 additions & 0 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
package mint

import (
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/okp4/okp4d/x/mint/keeper"
"github.com/okp4/okp4d/x/mint/types"
)

// BeginBlocker mints new tokens for the previous block.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

// fetch stored minter & params
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)

// recalculate inflation rate
totalSupply := k.TokenSupply(ctx, params.MintDenom)
bondedRatio := k.BondedRatio(ctx)
minter.Inflation = minter.NextInflation(params, bondedRatio)
minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply)
k.SetMinter(ctx, minter)

// mint coins, update supply
mintedCoin := minter.BlockProvision(params)
mintedCoins := sdk.NewCoins(mintedCoin)

err := k.MintCoins(ctx, mintedCoins)
if err != nil {
panic(err)
}

// send the minted coins to the fee collector account
err = k.AddCollectedFees(ctx, mintedCoins)
if err != nil {
panic(err)
}

if mintedCoin.Amount.IsInt64() {
defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens")
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()),
sdk.NewAttribute(types.AttributeKeyInflation, minter.Inflation.String()),
sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)
}
5 changes: 3 additions & 2 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func ValidateMinter(minter Minter) error {

// NextInflation return the new inflation rate for the next year
// Get the current inflation and multiply by (1 - annual reduction factor).
func (m Minter) NextInflation(params Params) sdk.Dec {
return m.Inflation
func (m Minter) NextInflation(params Params, boundedRatio sdk.Dec) sdk.Dec {
bounded := params.BoundingAdjustment.Sub(boundedRatio.Quo(params.TargetBoundingRatio))
return params.InflationCoef.Mul(bounded).Mul(sdk.NewDec(100))
}

// NextAnnualProvisions returns the annual provisions based on current total
Expand Down

0 comments on commit a2bc105

Please sign in to comment.