From 1e012603530eeb13d2cb83036a5788bcdc1bc7e1 Mon Sep 17 00:00:00 2001 From: Solovyov1796 Date: Sun, 9 Jun 2024 15:40:37 +0800 Subject: [PATCH] custom inflation calculation function --- app/app.go | 2 +- chaincfg/mint.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 chaincfg/mint.go diff --git a/app/app.go b/app/app.go index a6b8b9f0..4a4293ac 100644 --- a/app/app.go +++ b/app/app.go @@ -588,7 +588,7 @@ func NewApp( committee.NewAppModule(app.committeeKeeper, app.accountKeeper), evmutil.NewAppModule(app.evmutilKeeper, app.bankKeeper, app.accountKeeper), // nil InflationCalculationFn, use SDK's default inflation function - mint.NewAppModule(appCodec, app.mintKeeper, app.accountKeeper, nil), + mint.NewAppModule(appCodec, app.mintKeeper, app.accountKeeper, chaincfg.CustomInflationCalculateFn), council.NewAppModule(app.CouncilKeeper, app.stakingKeeper), das.NewAppModule(app.DasKeeper), ) diff --git a/chaincfg/mint.go b/chaincfg/mint.go new file mode 100644 index 00000000..1ecfe409 --- /dev/null +++ b/chaincfg/mint.go @@ -0,0 +1,48 @@ +package chaincfg + +import ( + "github.com/tendermint/tendermint/libs/log" + + sdk "github.com/cosmos/cosmos-sdk/types" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" +) + +func CustomInflationCalculateFn(ctx sdk.Context, minter minttypes.Minter, params minttypes.Params, bondedRatio sdk.Dec) sdk.Dec { + logger := ctx.Logger() + if logger == nil { + panic("logger is nil") + } + return customInflationCalculateFn(logger, minter, params, bondedRatio) +} + +func customInflationCalculateFn(logger log.Logger, minter minttypes.Minter, params minttypes.Params, bondedRatio sdk.Dec) sdk.Dec { + // The target annual inflation rate is recalculated for each previsions cycle. The + // inflation is also subject to a rate change (positive or negative) depending on + // the distance from the desired ratio (67%). The maximum rate change possible is + // defined to be 13% per year, however the annual inflation is capped as between + // 7% and 20%. + + // (1 - bondedRatio/GoalBonded) * InflationRateChange + inflationRateChangePerYear := sdk.OneDec(). + Sub(bondedRatio.Quo(params.GoalBonded)). + Mul(params.InflationRateChange) + inflationRateChange := inflationRateChangePerYear.Quo(sdk.NewDec(int64(params.BlocksPerYear))) + + // adjust the new annual inflation for this next cycle + inflation := minter.Inflation.Add(inflationRateChange) // note inflationRateChange may be negative + if inflation.GT(params.InflationMax) { + inflation = params.InflationMax + } + if inflation.LT(params.InflationMin) { + inflation = params.InflationMin + } + + logger.Info( + "calculated new annual inflation", + "bondedRatio", bondedRatio, + "inflation", inflation, + "params", params, + "minter", minter, + ) + return inflation +}