-
Notifications
You must be signed in to change notification settings - Fork 6
/
params.go
70 lines (63 loc) · 2.21 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
package v2
import (
"github.com/chain4energy/c4e-chain/x/cfedistributor/migrations/v1"
"github.com/chain4energy/c4e-chain/x/cfedistributor/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// MigrateParams performs in-place store migrations from v1.0.1 to v1.1.0.
// The migration includes:
// - SubDistributor params structure changed.
// - BurnShare and Share now must be set between 0 and 1, not 0 and 100.
func MigrateParams(ctx sdk.Context, paramStore *paramtypes.Subspace) error {
var oldSubDistributors []v1.SubDistributor
oldSubDistributorsRaw := paramStore.GetRaw(ctx, types.KeySubDistributors)
if err := codec.NewLegacyAmino().UnmarshalJSON(oldSubDistributorsRaw, &oldSubDistributors); err != nil {
panic(err)
}
var newSubDistributors []types.SubDistributor
for _, oldSubDistributor := range oldSubDistributors {
var newShares []*types.DestinationShare
for _, oldShare := range oldSubDistributor.Destination.Share {
newShare := types.DestinationShare{
Share: oldShare.Percent.Quo(sdk.NewDec(100)),
Destination: types.Account{
Id: oldShare.Account.Id,
Type: oldShare.Account.Type,
},
Name: oldShare.Name,
}
newShares = append(newShares, &newShare)
}
var newSources []*types.Account
for _, oldSource := range oldSubDistributor.Sources {
newSource := types.Account{
Id: oldSource.Id,
Type: oldSource.Type,
}
newSources = append(newSources, &newSource)
}
newSubDistributor := types.SubDistributor{
Name: oldSubDistributor.Name,
Destinations: types.Destinations{
Shares: newShares,
BurnShare: oldSubDistributor.Destination.BurnShare.Percent.Quo(sdk.NewDec(100)),
PrimaryShare: types.Account{
Id: oldSubDistributor.Destination.Account.Id,
Type: oldSubDistributor.Destination.Account.Type,
},
},
Sources: newSources,
}
if err := newSubDistributor.Validate(); err != nil {
return err
}
newSubDistributors = append(newSubDistributors, newSubDistributor)
}
if err := types.ValidateSubDistributors(newSubDistributors); err != nil {
return err
}
paramStore.Set(ctx, types.KeySubDistributors, newSubDistributors)
return nil
}