forked from crescent-network/crescent
/
abci.go
70 lines (59 loc) 路 2.25 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
package mint
import (
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
utils "github.com/jesse-pinkman-cre/crescent/types"
"github.com/jesse-pinkman-cre/crescent/x/mint/keeper"
"github.com/jesse-pinkman-cre/crescent/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 params
params := k.GetParams(ctx)
lastBlockTime := k.GetLastBlockTime(ctx)
// if not set LastBlockTime(e.g. fist block), skip minting inflation
if lastBlockTime == nil {
k.SetLastBlockTime(ctx, ctx.BlockTime())
return
}
inflationSchedules := k.GetInflationSchedules(ctx)
blockInflation := sdk.ZeroInt()
var blockDurationForInflation time.Duration
for _, schedule := range inflationSchedules {
if utils.DateRangeIncludes(schedule.StartTime, schedule.EndTime, ctx.BlockTime()) {
blockDurationForInflation = ctx.BlockTime().Sub(*lastBlockTime)
if blockDurationForInflation > params.BlockTimeThreshold {
blockDurationForInflation = params.BlockTimeThreshold
}
// blockInflation = InflationAmountThisPeriod * min(CurrentBlockTime-LastBlockTime,BlockTimeThreshold)/(InflationPeriodEndDate-InflationPeriodStartDate)
blockInflation = schedule.Amount.MulRaw(blockDurationForInflation.Nanoseconds()).QuoRaw(schedule.EndTime.Sub(schedule.StartTime).Nanoseconds())
break
}
}
if blockInflation.IsPositive() {
mintedCoin := sdk.NewCoin(params.MintDenom, blockInflation)
mintedCoins := sdk.NewCoins(mintedCoin)
err := k.MintCoins(ctx, mintedCoins)
if err != nil {
panic(err)
}
// send the minted coins to the mint pool
err = k.SendInflationToMintPool(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(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
sdk.NewAttribute(types.AttributeKeyBlockDuration, blockDurationForInflation.String()),
),
)
}
k.SetLastBlockTime(ctx, ctx.BlockTime())
}