forked from forbole/callisto
/
migrate.go
88 lines (72 loc) · 2.28 KB
/
migrate.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
package migrate
import (
"fmt"
"io/ioutil"
"github.com/pylons-tech/juno/types/config"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
v1 "github.com/pylons-tech/bdjuno/cmd/migrate/v1"
"github.com/pylons-tech/bdjuno/modules/distribution"
"github.com/pylons-tech/bdjuno/modules/pricefeed"
"github.com/pylons-tech/bdjuno/types"
junomigrate "github.com/pylons-tech/juno/cmd/migrate"
junov1 "github.com/pylons-tech/juno/cmd/migrate/v1"
)
type Config struct {
junomigrate.Config `yaml:",inline"`
PricefeedConfig *pricefeed.Config `yaml:"pricefeed"`
DistributionConfig *distribution.Config `yaml:"distribution"`
}
// NewMigrateCmd returns the command to be run when migrating the config from v1 to v2
func NewMigrateCmd() *cobra.Command {
return &cobra.Command{
Use: "migrate",
RunE: func(cmd *cobra.Command, args []string) error {
v2Config, err := migrateConfig()
if err != nil {
return nil
}
bz, err := yaml.Marshal(&v2Config)
if err != nil {
return fmt.Errorf("error while serializing v2 config: %s", err)
}
v2File := config.GetConfigFilePath()
return ioutil.WriteFile(v2File, bz, 0600)
},
}
}
func migrateConfig() (Config, error) {
bz, err := junov1.ReadConfig()
if err != nil {
return Config{}, err
}
v1JunoCfg, err := junomigrate.MigrateConfig()
if err != nil {
return Config{}, err
}
v1BDJunoCfg, err := v1.ParseConfig(bz)
if err != nil {
return Config{}, err
}
var pricefeedConfig *pricefeed.Config
if v1BDJunoCfg.PricefeedConfig != nil {
var tokens = make([]types.Token, len(v1BDJunoCfg.PricefeedConfig.Tokens))
for tokenIndex, token := range v1BDJunoCfg.PricefeedConfig.Tokens {
var units = make([]types.TokenUnit, len(token.Units))
for unitIndex, unit := range token.Units {
units[unitIndex] = types.NewTokenUnit(unit.Denom, unit.Exponent, unit.Aliases, unit.PriceID)
}
tokens[tokenIndex] = types.NewToken(token.Name, units)
}
pricefeedConfig = pricefeed.NewConfig(tokens)
}
var distributionConfig = distribution.DefaultConfig()
if v1BDJunoCfg.DistributionConfig != nil {
distributionConfig = distribution.NewConfig(v1BDJunoCfg.DistributionConfig.DistributionFrequency)
}
return Config{
Config: v1JunoCfg,
PricefeedConfig: pricefeedConfig,
DistributionConfig: distributionConfig,
}, nil
}