-
Notifications
You must be signed in to change notification settings - Fork 0
/
abci.go
72 lines (50 loc) · 1.7 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
package mint
import (
sdk "github.com/ayher/anatha/types"
"github.com/ayher/project-anatha/config"
"github.com/ayher/project-anatha/x/mint/internal/types"
)
func BeginBlocker(ctx sdk.Context, k Keeper) {
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)
if ctx.BlockHeight() == 1 {
minter.PreviousMintTime = ctx.BlockTime()
k.SetMinter(ctx, minter)
return
}
currentBlockTime := ctx.BlockTime()
previousBlockTime := minter.PreviousMintTime
if previousBlockTime.Equal(currentBlockTime) || previousBlockTime.After(currentBlockTime) {
panic("Block Time Sequence Invalid")
}
elapsed := int(currentBlockTime.Sub(previousBlockTime).Seconds())
totalSupply := k.TotalSupply(ctx)
inflationPerSecond := params.PerSecondInflationRate
totalSupplyDec := totalSupply.AmountOf(config.DefaultDenom).ToDec()
toMintDec := minter.Leftover // initialize minting amount with previous iterations leftover
for i := 0; i < elapsed; i++ {
currentSecondInflationAmountDec := totalSupplyDec.MulTruncate(inflationPerSecond)
toMintDec = toMintDec.Add(currentSecondInflationAmountDec)
totalSupplyDec = totalSupplyDec.Add(currentSecondInflationAmountDec)
}
toMintInt := toMintDec.TruncateInt()
leftover := toMintDec.Sub(toMintInt.ToDec())
minter.PreviousMintTime = currentBlockTime
minter.Leftover = leftover
k.SetMinter(ctx, minter)
toMintCoins := sdk.NewCoins(sdk.NewCoin(params.MintDenom, toMintInt))
err := k.MintCoins(ctx, toMintCoins)
if err != nil {
panic(err)
}
err = k.DistributeCollectedFees(ctx, toMintCoins)
if err != nil {
panic(err)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(sdk.AttributeKeyAmount, toMintCoins.String()),
),
)
}