/
hooks.go
53 lines (42 loc) · 1.52 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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
epochTypes "github.com/fibonacci-chain/core/x/epoch/types"
)
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epoch epochTypes.Epoch) {}
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epoch epochTypes.Epoch) {
latestMinter := k.GetOrUpdateLatestMinter(ctx, epoch)
coinsToMint := latestMinter.GetReleaseAmountToday(epoch.CurrentEpochStartTime.UTC())
if coinsToMint.IsZero() || latestMinter.GetRemainingMintAmount() == 0 {
k.Logger(ctx).Debug("No coins to mint", "minter", latestMinter)
return
}
// mint coins, update supply
if err := k.MintCoins(ctx, coinsToMint); err != nil {
panic(err)
}
// send the minted coins to the fee collector account
if err := k.AddCollectedFees(ctx, coinsToMint); err != nil {
panic(err)
}
// Released Succssfully, decrement the remaining amount by the daily release amount and update minter
amountMinted := coinsToMint.AmountOf(latestMinter.GetDenom())
latestMinter.RecordSuccessfulMint(ctx, epoch, amountMinted.Uint64())
k.Logger(ctx).Info("Minted coins", "minter", latestMinter, "amount", coinsToMint.String())
k.SetMinter(ctx, latestMinter)
}
type Hooks struct {
k Keeper
}
var _ epochTypes.EpochHooks = Hooks{}
// Return the wrapper struct.
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
// epochs hooks.
func (h Hooks) BeforeEpochStart(ctx sdk.Context, epoch epochTypes.Epoch) {
h.k.BeforeEpochStart(ctx, epoch)
}
func (h Hooks) AfterEpochEnd(ctx sdk.Context, epoch epochTypes.Epoch) {
h.k.AfterEpochEnd(ctx, epoch)
}