/
abci.go
78 lines (62 loc) · 2.07 KB
/
abci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package mint
import (
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/exfury/furya/v18/x/mint/keeper"
"github.com/exfury/furya/v18/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
minter := k.GetMinter(ctx)
// inflation phase end
if minter.Inflation.Equal(sdk.ZeroDec()) {
return
}
// fetch stored params
params := k.GetParams(ctx)
currentBlock := uint64(ctx.BlockHeight())
// fetch current total supply
totalSupply := k.TokenSupply(ctx, params.MintDenom)
// check if we need to change phase
nextPhase := minter.NextPhase(params, totalSupply)
if nextPhase != minter.Phase {
// store new inflation rate by phase
newInflation := minter.PhaseInflationRate(nextPhase)
minter.Inflation = newInflation
minter.Phase = nextPhase
minter.StartPhaseBlock = currentBlock
minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply)
minter.TargetSupply = totalSupply.Add(minter.AnnualProvisions.TruncateInt())
k.SetMinter(ctx, minter)
// inflation phase end
if minter.Inflation.Equal(sdk.ZeroDec()) {
return
}
}
// mint coins, update supply
mintedCoin := minter.BlockProvision(params, totalSupply)
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.AttributeKeyInflation, minter.Inflation.String()),
sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)
}