From a2bc105de2898a23e743b33c51138a6387328677 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 23 Aug 2023 11:26:14 +0200 Subject: [PATCH] feat(mint): add new mint function calculation --- x/mint/abci.go | 44 ++++++++++++++++++++++++++++++++++++++++++ x/mint/types/minter.go | 5 +++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index 1c33e81d..a5ed50ec 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -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()), + ), + ) } diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index 6daf9253..c9db82f6 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -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