generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mint): add new mint function calculation
- Loading branch information
Showing
2 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters