-
Notifications
You must be signed in to change notification settings - Fork 1
/
params.go
74 lines (58 loc) · 1.59 KB
/
params.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rarimo/rarimo-core/x/tokenmanager/types"
)
// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.KeyPrefix(types.ParamsKey))
if b == nil {
return types.DefaultParams()
}
k.cdc.MustUnmarshal(b, ¶ms)
return params
}
// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
if err := params.Validate(); err != nil {
panic("failed to set params: " + err.Error())
}
b := k.cdc.MustMarshal(¶ms)
ctx.KVStore(k.storeKey).Set(types.KeyPrefix(types.ParamsKey), b)
}
func (k Keeper) GetNetwork(ctx sdk.Context, name string) (param types.Network, ok bool) {
params := k.GetParams(ctx)
for _, network := range params.Networks {
if network.Name == name {
return *network, true
}
}
return
}
func (k Keeper) SetNetwork(ctx sdk.Context, network types.Network) {
params := k.GetParams(ctx)
defer func() {
k.SetParams(ctx, params)
}()
for index, n := range params.Networks {
if n.Name == network.Name {
params.Networks[index] = &network
return
}
}
params.Networks = append(params.Networks, &network)
}
func (k Keeper) RemoveNetwork(ctx sdk.Context, chain string) {
params := k.GetParams(ctx)
defer func() {
k.SetParams(ctx, params)
}()
networks := make([]*types.Network, 0, len(params.Networks)-1)
for _, network := range params.Networks {
if network.Name != chain {
networks = append(networks, network)
}
}
params.Networks = networks
}