-
Notifications
You must be signed in to change notification settings - Fork 3
/
abci.go
81 lines (68 loc) · 2.58 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
73
74
75
76
77
78
79
80
81
package liquidfarming
import (
"sort"
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmosquad-labs/squad/v3/x/liquidfarming/keeper"
"github.com/cosmosquad-labs/squad/v3/x/liquidfarming/types"
)
// BeginBlocker compares all LiquidFarms stored in KVstore with all LiquidFarms registered in params.
// Execute an appropriate operation when either new LiquidFarm is added or existing LiquidFarm is removed
// by going through governance proposal.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
liquidFarmsInStore := k.GetLiquidFarmsInStore(ctx)
liquidFarmsInParams := k.GetLiquidFarmsInParams(ctx)
liquidFarmByPoolId := map[uint64]types.LiquidFarm{} // PoolId => LiquidFarm
for _, liquidFarm := range liquidFarmsInStore {
liquidFarmByPoolId[liquidFarm.PoolId] = liquidFarm
}
// Iterate through all liquid farms in params.
// Store new one or delete the existing one from liquidFarmByPoolId
// when it is added or removed in params by governance proposal.
for _, liquidFarm := range liquidFarmsInParams {
if _, found := liquidFarmByPoolId[liquidFarm.PoolId]; !found {
k.SetLiquidFarm(ctx, liquidFarm)
} else {
delete(liquidFarmByPoolId, liquidFarm.PoolId)
}
}
// Sort map keys for deterministic execution
var poolIds []uint64
for poolId := range liquidFarmByPoolId {
poolIds = append(poolIds, poolId)
}
sort.Slice(poolIds, func(i, j int) bool {
return poolIds[i] < poolIds[j]
})
for _, poolId := range poolIds {
k.HandleRemovedLiquidFarm(ctx, liquidFarmByPoolId[poolId])
}
y, m, d := ctx.BlockTime().Date()
endTime, found := k.GetLastRewardsAuctionEndTime(ctx)
if !found {
initialEndTime := time.Date(y, m, d+1, 0, 0, 0, 0, time.UTC) // the next day 00:00 UTC
k.SetLastRewardsAuctionEndTime(ctx, initialEndTime)
} else {
currentTime := ctx.BlockTime()
if !currentTime.Before(endTime) {
duration := k.GetRewardsAuctionDuration(ctx)
nextEndTime := endTime.Add(duration)
// Handle a case when a chain is halted for a long time
if !currentTime.Before(nextEndTime) {
nextEndTime = time.Date(y, m, d+1, 0, 0, 0, 0, time.UTC) // the next day 00:00 UTC
}
for _, l := range liquidFarmsInStore {
auction, found := k.GetLastRewardsAuction(ctx, l.PoolId)
if found {
if err := k.FinishRewardsAuction(ctx, auction, l.FeeRate); err != nil {
panic(err)
}
}
k.CreateRewardsAuction(ctx, l.PoolId, nextEndTime)
}
k.SetLastRewardsAuctionEndTime(ctx, nextEndTime)
}
}
}