Skip to content

Commit

Permalink
feat: add rollapp init (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Jul 8, 2024
1 parent f492636 commit 19a455a
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 543 deletions.
100 changes: 23 additions & 77 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package initconfig

import (
"encoding/json"
"fmt"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
global_utils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/version"
)

const (
Expand Down Expand Up @@ -44,99 +44,45 @@ func AddFlags(cmd *cobra.Command) error {
return nil
}

func GetInitConfig(initCmd *cobra.Command, args []string) (*config.RollappConfig, error) {
home := initCmd.Flag(utils.FlagNames.Home).Value.String()
rollerConfigFilePath := filepath.Join(home, "roller.toml")
// interactive, _ := initCmd.Flags().GetBool(FlagNames.Interactive)
raType, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "execution")
if err != nil {
return nil, err
}
raID, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "rollapp_id")
if err != nil {
return nil, err
}
raBaseDenom, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "base_denom")
if err != nil {
return nil, err
}
da, err := global_utils.GetKeyFromTomlFile(rollerConfigFilePath, "da")
if err != nil {
func GetInitConfig(
initCmd *cobra.Command,
args []string,
) (*config.RollappConfig, error) {
var cfg config.RollappConfig
rollerConfigFilePath := filepath.Join(utils.GetRollerRootDir(), config.RollerConfigFileName)
if _, err := toml.DecodeFile(rollerConfigFilePath, &cfg); err != nil {
return nil, err
}

fmt.Println(home, raType, raID, raBaseDenom, da)

// load initial config if exists
var cfg config.RollappConfig
// load from flags
cfg.Home = home
cfg.Home = utils.GetRollerRootDir()

// TODO: support wasm, make the bainry name generic, like 'rollappd'
// for both RollApp types
cfg.RollappBinary = consts.Executables.RollappEVM
cfg.VMType = config.VMType(raType)

// token supply is provided in the pre-created genesis
// cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String()
// decimals, _ := initCmd.Flags().GetUint(FlagNames.Decimals)
cfg.Decimals = 18
cfg.DA = config.DAType(strings.ToLower(da))
cfg.DA = config.DAType(strings.ToLower(string(cfg.DA)))

hubID := initCmd.Flag(FlagNames.HubID).Value.String()
if hub, ok := consts.Hubs[hubID]; ok {
cfg.HubData = hub
}
cfg.RollerVersion = version.TrimVersionStr(version.BuildVersion)
cfg.RollappID = raID
cfg.Denom = raBaseDenom

return formatBaseCfg(cfg, initCmd)
}
// cfg.RollerVersion = version.TrimVersionStr(version.BuildVersion)
// cfg.RollappID = raID
// cfg.Denom = raBaseDenom

func formatBaseCfg(
cfg config.RollappConfig,
initCmd *cobra.Command,
) (*config.RollappConfig, error) {
setDecimals(initCmd, &cfg)
return &cfg, nil
}
fmt.Println(cfg.VMType == config.EVM_ROLLAPP)

// there's no need for a custom chain id as the rollapp id is set via the genesis-creator
// func generateRollappId(rlpCfg config.RollappConfig) (string, error) {
// for {
// RandEthId, err := generateRandEthId()
// if err != nil {
// return "", err
// }
// if rlpCfg.HubData.ID == consts.LocalHubID {
// return fmt.Sprintf("%s_%s-1", rlpCfg.RollappID, RandEthId), nil
// }
// isUnique, err := isEthIdentifierUnique(RandEthId, rlpCfg)
// if err != nil {
// return "", err
// }
// if isUnique {
// return fmt.Sprintf("%s_%s-1", rlpCfg.RollappID, RandEthId), nil
// }
// }
// }

// func generateRandEthId() (string, error) {
// max := big.NewInt(9000000)
// n, err := rand.Int(rand.Reader, max)
// if err != nil {
// return "", err
// }
// return fmt.Sprintf("%d", n), nil
// }

func setDecimals(initCmd *cobra.Command, cfg *config.RollappConfig) {
decimals, _ := initCmd.Flags().GetUint(FlagNames.Decimals)
if cfg.VMType == config.EVM_ROLLAPP || initCmd.Flags().Lookup(FlagNames.Decimals).Changed {
cfg.Decimals = decimals
if cfg.VMType == config.EVM_ROLLAPP {
cfg.Decimals = 18
} else {
cfg.Decimals = 6
}

j, _ := json.MarshalIndent(cfg, "", " ")
fmt.Println(string(j))

return &cfg, nil
}

func getAvailableHubsMessage() string {
Expand Down
2 changes: 1 addition & 1 deletion cmd/config/init/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func FormatTokenSupplyLine(rollappConfig config.RollappConfig) string {
return fmt.Sprintf(
"💰 Total Token Supply: %s %s. Note that 1 %s == 1 * 10^%d %s (like 1 ETH == 1 * 10^18 wei).",
addCommasToNum(
rollappConfig.TokenSupply,
"1000000000",
),
displayDenom,
displayDenom,
Expand Down
Loading

0 comments on commit 19a455a

Please sign in to comment.