/
hooks.go
141 lines (122 loc) · 4.35 KB
/
hooks.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package keeper
import (
"fmt"
epochstypes "github.com/Canto-Network/Canto/v7/x/epochs/types"
"github.com/Canto-Network/Canto/v7/x/inflation/types"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// BeforeEpochStart: noop, We don't need to do anything here
func (k Keeper) BeforeEpochStart(_ sdk.Context, _ string, _ int64) {
}
// AfterEpochEnd mints and allocates coins at the end of each epoch end
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
params := k.GetParams(ctx)
skippedEpochs := k.GetSkippedEpochs(ctx)
// Skip inflation if it is disabled and increment number of skipped epochs
if !params.EnableInflation {
// check if the epochIdentifier is "day" before incrementing.
if epochIdentifier != epochstypes.DayEpochID {
return
}
skippedEpochs++
k.SetSkippedEpochs(ctx, skippedEpochs)
k.Logger(ctx).Debug(
"skipping inflation mint and allocation",
"height", ctx.BlockHeight(),
"epoch-id", epochIdentifier,
"epoch-number", epochNumber,
"skipped-epochs", skippedEpochs,
)
return
}
expEpochID := k.GetEpochIdentifier(ctx)
if epochIdentifier != expEpochID {
return
}
// mint coins, update supply
epochMintProvision, found := k.GetEpochMintProvision(ctx)
if !found {
panic("the epochMintProvision was not found")
}
mintedCoin := sdk.NewCoin(params.MintDenom, epochMintProvision.TruncateInt())
staking, communityPool, err := k.MintAndAllocateInflation(ctx, mintedCoin)
if err != nil {
panic(err)
}
period := k.GetPeriod(ctx)
epochsPerPeriod := k.GetEpochsPerPeriod(ctx)
newProvision := epochMintProvision
// If period is passed, update the period and epochMintProvision. A period is
// passed if the current epoch number surpasses the epochsPerPeriod for the
// current period. Skipped epochs are subtracted to only account for epochs
// where inflation minted tokens.
//
// Examples:
// Given, epochNumber = 1, period = 0, epochPerPeriod = 365, skippedEpochs = 0
// => 1 - 365 * 0 - 0 < 365 --- nothing to do here
// Given, epochNumber = 741, period = 1, epochPerPeriod = 365, skippedEpochs = 10
// => 741 - 1 * 365 - 10 > 365 --- a period has passed! we change the epochMintProvision and set a new period
if epochNumber-epochsPerPeriod*int64(period)-int64(skippedEpochs) > epochsPerPeriod {
period++
k.SetPeriod(ctx, period)
period = k.GetPeriod(ctx)
bondedRatio := k.BondedRatio(ctx)
newProvision = types.CalculateEpochMintProvision(
params,
period,
epochsPerPeriod,
bondedRatio,
)
k.SetEpochMintProvision(ctx, newProvision)
}
defer func() {
if mintedCoin.Amount.IsInt64() {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, "allocate", "total"},
float32(mintedCoin.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", mintedCoin.Denom)},
)
}
if staking.AmountOf(mintedCoin.Denom).IsInt64() {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, "allocate", "staking", "total"},
float32(staking.AmountOf(mintedCoin.Denom).Int64()),
[]metrics.Label{telemetry.NewLabel("denom", mintedCoin.Denom)},
)
}
if communityPool.AmountOf(mintedCoin.Denom).IsInt64() {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, "allocate", "community_pool", "total"},
float32(communityPool.AmountOf(mintedCoin.Denom).Int64()),
[]metrics.Label{telemetry.NewLabel("denom", mintedCoin.Denom)},
)
}
}()
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeEpochNumber, fmt.Sprintf("%d", epochNumber)),
sdk.NewAttribute(types.AttributeKeyEpochProvisions, newProvision.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)
}
// ___________________________________________________________________________________________________
// Hooks wrapper struct for incentives keeper
type Hooks struct {
k Keeper
}
var _ epochstypes.EpochHooks = Hooks{}
// Return the wrapper struct
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
// epochs hooks
func (h Hooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
h.k.BeforeEpochStart(ctx, epochIdentifier, epochNumber)
}
func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) {
h.k.AfterEpochEnd(ctx, epochIdentifier, epochNumber)
}