-
Notifications
You must be signed in to change notification settings - Fork 118
/
genesis.go
162 lines (136 loc) · 5.01 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package clp
import (
"fmt"
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/Sifchain/sifnode/x/clp/keeper"
"github.com/Sifchain/sifnode/x/clp/types"
)
func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) (res []abci.ValidatorUpdate) {
// Set initial CLP parameters
k.SetParams(ctx, data.Params)
// Set initial address whitelist
if data.AddressWhitelist == nil || len(data.AddressWhitelist) == 0 {
panic("AddressWhiteList must be set.")
}
wl := make([]sdk.AccAddress, len(data.AddressWhitelist))
for i, entry := range data.AddressWhitelist {
wlAddress, err := sdk.AccAddressFromBech32(entry)
if err != nil {
panic(err)
}
wl[i] = wlAddress
}
k.SetClpWhiteList(ctx, wl)
// Set all the pools
for _, pool := range data.PoolList {
err := k.SetPool(ctx, pool)
if err != nil {
panic(fmt.Sprintf("Pool could not be set : %s", pool.String()))
}
}
// Set all the liquidity providers
for _, lp := range data.LiquidityProviders {
k.SetLiquidityProvider(ctx, lp)
}
// Set all the rewardsBucket
for _, elem := range data.RewardsBucketList {
k.SetRewardsBucket(ctx, elem)
}
// Set initial reward states
k.SetRewardParams(ctx, &data.RewardParams)
// Set initial pmtp states
k.SetPmtpRateParams(ctx, data.PmtpRateParams)
k.SetPmtpEpoch(ctx, data.PmtpEpoch)
k.SetPmtpParams(ctx, &data.PmtpParams)
k.SetPmtpRateParams(ctx, data.PmtpRateParams)
// Set initial liquidity protection states
k.SetLiquidityProtectionParams(ctx, &data.LiquidityProtectionParams)
k.SetLiquidityProtectionRateParams(ctx, data.LiquidityProtectionRateParams)
// Set initial swap fee states
k.SetSwapFeeParams(ctx, &data.SwapFeeParams)
// Set initial provider distribution states
k.SetProviderDistributionParams(ctx, &data.ProviderDistributionParams)
return []abci.ValidatorUpdate{}
}
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
// Retrieve CLP parameters
params := keeper.GetParams(ctx)
// Retrieve all the pools
var poolList []*types.Pool
poolList, _, _ = keeper.GetPoolsPaginated(ctx, &query.PageRequest{
Limit: uint64(math.MaxUint64),
})
// Retrieve all the liquidity providers
liquidityProviders, _, _ := keeper.GetAllLiquidityProvidersPaginated(ctx, &query.PageRequest{
Limit: uint64(math.MaxUint64),
})
// Retrieve all the whitelist addresses
whiteList := keeper.GetClpWhiteList(ctx)
wl := make([]string, len(whiteList))
for i, entry := range whiteList {
wl[i] = entry.String()
}
// Retrieve all the rewardsBucket
rewardsBucketList := keeper.GetAllRewardsBucket(ctx)
// Retrieve all the reward states
rewardParams := keeper.GetRewardsParams(ctx)
if rewardParams == nil {
rewardParams = types.GetDefaultRewardParams()
}
// Retrieve all the pmtp states
pmtpParams := keeper.GetPmtpParams(ctx)
if pmtpParams == nil {
pmtpParams = types.GetDefaultPmtpParams()
}
pmtpEpoch := keeper.GetPmtpEpoch(ctx)
pmtpRateParams := keeper.GetPmtpRateParams(ctx)
// Retrieve all the liquidity protection states
liquidityProtectionParams := keeper.GetLiquidityProtectionParams(ctx)
if liquidityProtectionParams == nil {
liquidityProtectionParams = types.GetDefaultLiquidityProtectionParams()
}
liquidityProtectionRateParams := keeper.GetLiquidityProtectionRateParams(ctx)
// Retrieve all the swap fee states
swapFeeParams := keeper.GetSwapFeeParams(ctx)
// Retrieve all the provider distribution states
providerDistributionParams := keeper.GetProviderDistributionParams(ctx)
if providerDistributionParams == nil {
providerDistributionParams = types.GetDefaultProviderDistributionParams()
}
return types.GenesisState{
Params: params,
AddressWhitelist: wl,
PoolList: poolList,
LiquidityProviders: liquidityProviders,
RewardsBucketList: rewardsBucketList,
RewardParams: *rewardParams,
PmtpParams: *pmtpParams,
PmtpEpoch: pmtpEpoch,
PmtpRateParams: pmtpRateParams,
LiquidityProtectionParams: *liquidityProtectionParams,
LiquidityProtectionRateParams: liquidityProtectionRateParams,
SwapFeeParams: swapFeeParams,
ProviderDistributionParams: *providerDistributionParams,
}
}
// ValidateGenesis validates the clp genesis parameters
func ValidateGenesis(data types.GenesisState) error {
if err := data.Params.Validate(); err != nil {
return sdkerrors.Wrap(types.ErrInvalid, fmt.Sprintf("clp: params are invalid : %s \n %s", err.Error(), data.Params.String()))
}
for _, pool := range data.PoolList {
if !pool.Validate() {
return sdkerrors.Wrap(types.ErrInvalid, fmt.Sprintf("clp: pool is invalid : %s", pool.String()))
}
}
for _, lp := range data.LiquidityProviders {
if !lp.Validate() {
return sdkerrors.Wrap(types.ErrInvalid, fmt.Sprintf("clp: liquidityProvider is invalid : %s", lp.String()))
}
}
return nil
}