-
Notifications
You must be signed in to change notification settings - Fork 55
/
genesis.go
90 lines (72 loc) · 2.44 KB
/
genesis.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
package keeper
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crescent-network/crescent/v5/x/liquidfarming/types"
)
// InitGenesis initializes the capability module's state from a provided genesis state.
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
if err := genState.Validate(); err != nil {
panic(err)
}
k.SetParams(ctx, genState.Params)
for _, liquidFarm := range genState.LiquidFarms {
k.SetLiquidFarm(ctx, liquidFarm)
}
for _, record := range genState.LastRewardsAuctionIdRecord {
k.SetLastRewardsAuctionId(ctx, record.AuctionId, record.PoolId)
}
for _, auction := range genState.RewardsAuctions {
k.SetRewardsAuction(ctx, auction)
}
for _, bid := range genState.Bids {
k.SetBid(ctx, bid)
}
for _, record := range genState.WinningBidRecords {
k.SetWinningBid(ctx, record.AuctionId, record.WinningBid)
}
}
// ExportGenesis returns the module's exported genesis.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
params := k.GetParams(ctx)
// Initialize objects to prevent from having nil slice
if params.LiquidFarms == nil {
params.LiquidFarms = []types.LiquidFarm{}
}
poolIds := []uint64{}
for _, liquidFarm := range params.LiquidFarms {
poolIds = append(poolIds, liquidFarm.PoolId)
}
lastRewardsAuctionIdRecords := []types.LastRewardsAuctionIdRecord{}
bids := []types.Bid{}
winningBidRecords := []types.WinningBidRecord{}
for _, poolId := range poolIds {
lastRewardsAuctionIdRecords = append(lastRewardsAuctionIdRecords, types.LastRewardsAuctionIdRecord{
PoolId: poolId,
AuctionId: k.GetLastRewardsAuctionId(ctx, poolId),
})
bids = append(bids, k.GetBidsByPoolId(ctx, poolId)...)
auctionId := k.GetLastRewardsAuctionId(ctx, poolId)
winningBid, found := k.GetWinningBid(ctx, auctionId, poolId)
if found {
winningBidRecords = append(winningBidRecords, types.WinningBidRecord{
AuctionId: auctionId,
WinningBid: winningBid,
})
}
}
var endTime *time.Time
tempEndTime, found := k.GetLastRewardsAuctionEndTime(ctx)
if found {
endTime = &tempEndTime
}
return &types.GenesisState{
Params: params,
LastRewardsAuctionIdRecord: lastRewardsAuctionIdRecords,
LiquidFarms: k.GetLiquidFarmsInStore(ctx),
RewardsAuctions: k.GetAllRewardsAuctions(ctx),
Bids: bids,
WinningBidRecords: winningBidRecords,
LastRewardsAuctionEndTime: endTime,
}
}