/
genesis.go
81 lines (69 loc) 路 2.14 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
package keeper
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crescent-network/crescent/v4/x/lpfarm/types"
)
// InitGenesis initializes the module's state from a provided genesis state.
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
k.SetParams(ctx, genState.Params)
if genState.LastBlockTime != nil {
k.SetLastBlockTime(ctx, *genState.LastBlockTime)
}
if genState.LastPlanId > 0 {
k.SetLastPlanId(ctx, genState.LastPlanId)
}
k.SetNumPrivatePlans(ctx, genState.NumPrivatePlans)
for _, plan := range genState.Plans {
k.SetPlan(ctx, plan)
}
for _, farm := range genState.Farms {
k.SetFarm(ctx, farm.Denom, farm.Farm)
}
for _, position := range genState.Positions {
k.SetPosition(ctx, position)
}
for _, hist := range genState.HistoricalRewards {
k.SetHistoricalRewards(ctx, hist.Denom, hist.Period, hist.HistoricalRewards)
}
}
// ExportGenesis returns the module's exported genesis.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
var lastBlockTimePtr *time.Time
lastBlockTime, found := k.GetLastBlockTime(ctx)
if found {
lastBlockTimePtr = &lastBlockTime
}
lastPlanId, _ := k.GetLastPlanId(ctx)
plans := []types.Plan{}
k.IterateAllPlans(ctx, func(plan types.Plan) (stop bool) {
plans = append(plans, plan)
return false
})
farms := []types.FarmRecord{}
k.IterateAllFarms(ctx, func(denom string, farm types.Farm) (stop bool) {
farms = append(farms, types.FarmRecord{
Denom: denom,
Farm: farm,
})
return false
})
positions := []types.Position{}
k.IterateAllPositions(ctx, func(position types.Position) (stop bool) {
positions = append(positions, position)
return false
})
hists := []types.HistoricalRewardsRecord{}
k.IterateAllHistoricalRewards(
ctx, func(denom string, period uint64, hist types.HistoricalRewards) (stop bool) {
hists = append(hists, types.HistoricalRewardsRecord{
Denom: denom,
Period: period,
HistoricalRewards: hist,
})
return false
})
return types.NewGenesisState(
k.GetParams(ctx), lastBlockTimePtr, lastPlanId, k.GetNumPrivatePlans(ctx),
plans, farms, positions, hists)
}