/
hooks.go
104 lines (86 loc) · 3.62 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
package keeper
import (
"fmt"
"github.com/percosis-labs/percosis/v16/x/mint/types"
epochstypes "github.com/percosis-labs/percosis/x/epochs/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// BeforeEpochStart is a hook which is executed before the start of an epoch. It is a no-op for mint module.
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
// no-op
return nil
}
// AfterEpochEnd is a hook which is executed after the end of an epoch.
// This hook should attempt to mint and distribute coins according to
// the configuration set via parameters. In addition, it handles the logic
// for reducing minted coins according to the parameters.
// For an attempt to mint to occur:
// - given epochIdentifier must be equal to the mint epoch identifier set via parameters.
// - given epochNumber must be greater than or equal to the mint start epoch set via parameters.
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
params := k.GetParams(ctx)
if epochIdentifier == params.EpochIdentifier {
// not distribute rewards if it's not time yet for rewards distribution
if epochNumber < params.MintingRewardsDistributionStartEpoch {
return nil
} else if epochNumber == params.MintingRewardsDistributionStartEpoch {
k.setLastReductionEpochNum(ctx, epochNumber)
}
// fetch stored minter & params
minter := k.GetMinter(ctx)
// Check if we have hit an epoch where we update the inflation parameter.
// We measure time between reductions in number of epochs.
// This avoids issues with measuring in block numbers, as epochs have fixed intervals, with very
// low variance at the relevant sizes. As a result, it is safe to store the epoch number
// of the last reduction to be later retrieved for comparison.
if epochNumber >= params.ReductionPeriodInEpochs+k.getLastReductionEpochNum(ctx) {
// Reduce the reward per reduction period
minter.EpochProvisions = minter.NextEpochProvisions(params)
k.SetMinter(ctx, minter)
k.setLastReductionEpochNum(ctx, epochNumber)
}
// mint coins, update supply
mintedCoin := minter.EpochProvision(params)
mintedCoins := sdk.NewCoins(mintedCoin)
// We over-allocate by the developer vesting portion, and burn this later
err := k.mintCoins(ctx, mintedCoins)
if err != nil {
return err
}
// send the minted coins to the fee collector account
err = k.DistributeMintedCoin(ctx, mintedCoin)
if err != nil {
return err
}
if mintedCoin.Amount.IsInt64() {
defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.ModuleName,
sdk.NewAttribute(types.AttributeEpochNumber, fmt.Sprintf("%d", epochNumber)),
sdk.NewAttribute(types.AttributeKeyEpochProvisions, minter.EpochProvisions.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)
}
return nil
}
// ___________________________________________________________________________________________________
// 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) error {
return h.k.BeforeEpochStart(ctx, epochIdentifier, epochNumber)
}
func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
return h.k.AfterEpochEnd(ctx, epochIdentifier, epochNumber)
}