Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a roller config export command #439

Merged
merged 8 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"github.com/dymensionxyz/roller/cmd/config/export"
configInit "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/config/set"
"github.com/dymensionxyz/roller/cmd/config/show"
Expand All @@ -15,5 +16,6 @@ func Cmd() *cobra.Command {
cmd.AddCommand(configInit.InitCmd())
cmd.AddCommand(show.Cmd())
cmd.AddCommand(set.Cmd())
cmd.AddCommand(export.Cmd())
return cmd
}
20 changes: 20 additions & 0 deletions cmd/config/export/bech32.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package export

import (
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"path/filepath"
"strings"
)

func getBech32Prefix(rlpCfg config.RollappConfig) (string, error) {
rollappSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{
Dir: filepath.Join(rlpCfg.Home, consts.ConfigDirName.Rollapp),
ID: consts.KeysIds.RollappSequencer,
}, rlpCfg.RollappBinary)
if err != nil {
return "", err
}
return strings.Split(rollappSeqAddr, "1")[0], nil
}
103 changes: 103 additions & 0 deletions cmd/config/export/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package export

import (
"encoding/json"
"errors"
"fmt"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/relayer"
"github.com/spf13/cobra"
"math/big"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Export the rollapp configurations jsons needed to list your rollapp.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
rlpCfg, err := config.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)
bech32, err := getBech32Prefix(rlpCfg)
utils.PrettifyErrorIfExists(err)
const defaultFaucetUrl = "https://discord.com/channels/956961633165529098/1125047988247593010"
baseDenom := rlpCfg.Denom
coinType := 118
if rlpCfg.VMType == config.EVM_ROLLAPP {
coinType = 60
}
rly := relayer.NewRelayer(rlpCfg.Home, rlpCfg.RollappID, rlpCfg.HubData.ID)
srcChannel, hubChannel, err := rly.LoadChannels()
if err != nil || srcChannel == "" || hubChannel == "" {
utils.PrettifyErrorIfExists(errors.New("no relayer channel was found on your local machine. Please create a channel before listing your rollapp on the portal"))
}
networkJson := NetworkJson{
ChainId: rlpCfg.RollappID,
ChainName: rlpCfg.RollappID,
Rpc: "",
Rest: "",
Bech32Prefix: bech32,
Currencies: []string{
baseDenom,
},
NativeCurrency: baseDenom,
StakeCurrency: baseDenom,
FeeCurrency: baseDenom,
CoinType: coinType,
FaucetUrl: defaultFaucetUrl,
Website: "",
Logo: "",
Ibc: IbcConfig{
HubChannel: hubChannel,
Channel: srcChannel,
Timeout: 172800000,
},
Type: RollApp,
Description: nil,
Analytics: false,
}
if rlpCfg.VMType == config.EVM_ROLLAPP {
evmID := config.GetEthID(rlpCfg.RollappID)
hexEvmID, err := decimalToHexStr(evmID)
utils.PrettifyErrorIfExists(err)
networkJson.Evm = &EvmConfig{
ChainId: hexEvmID,
Rpc: "",
}
}
if rlpCfg.DA == config.Avail {
networkJson.Da = Avail
} else {
networkJson.Da = Celestia
}
currency := Currency{
CoinDenom: baseDenom[1:],
CoinMinimalDenom: baseDenom,
CoinDecimals: rlpCfg.Decimals,
Logo: "",
}

networkJsonString, err := json.MarshalIndent(networkJson, "", " ")
utils.PrettifyErrorIfExists(err)
currencyJsonString, err := json.MarshalIndent(currency, "", " ")
utils.PrettifyErrorIfExists(err)
println("💈 networks.json:")
println(string(networkJsonString))
println()
println("💈 tokens.json:")
println(string(currencyJsonString))
},
}
return cmd
}

func decimalToHexStr(decimalStr string) (string, error) {
num := new(big.Int)
num, ok := num.SetString(decimalStr, 10)
if !ok {
return "", fmt.Errorf("Failed to parse the decimal string: %s", decimalStr)
}
hexStr := fmt.Sprintf("0x%x", num)
return hexStr, nil
}
78 changes: 78 additions & 0 deletions cmd/config/export/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package export

type IbcConfig struct {
HubChannel string `json:"hubChannel"`
Channel string `json:"channel"`
Timeout int `json:"timeout"`
EnabledTokens []string `json:"enabledTokens,omitempty"`
}

type EvmConfig struct {
ChainId string `json:"chainId"`
Rpc string `json:"rpc"`
}

type NetworkType string

const (
Hub NetworkType = "Hub"
RollApp NetworkType = "RollApp"
Regular NetworkType = "Regular"
)

type DataAvailability string

const (
Celestia DataAvailability = "Celestia"
Avail DataAvailability = "Avail"
)

type GasPriceSteps struct {
Low int `json:"low"`
Average int `json:"average"`
High int `json:"high"`
}

type App struct {
Name string `json:"name"`
Url string `json:"url"`
Logo string `json:"logo"`
}

type NetworkJson struct {
ChainId string `json:"chainId"`
ChainName string `json:"chainName"`
Rpc string `json:"rpc"`
Rest string `json:"rest"`
Bech32Prefix string `json:"bech32Prefix"`
Currencies []string `json:"currencies"`
NativeCurrency string `json:"nativeCurrency"`
StakeCurrency string `json:"stakeCurrency"`
FeeCurrency string `json:"feeCurrency"`
GasPriceSteps *GasPriceSteps `json:"gasPriceSteps,omitempty"`
GasAdjustment *float64 `json:"gasAdjustment,omitempty"`
CoinType int `json:"coinType"`
ExplorerUrl *string `json:"explorerUrl,omitempty"`
ExploreTxUrl *string `json:"exploreTxUrl,omitempty"`
FaucetUrl string `json:"faucetUrl,omitempty"`
Website string `json:"website,omitempty"`
ValidatorsLogosStorageDir *string `json:"validatorsLogosStorageDir,omitempty"`
Logo string `json:"logo"`
Disabled *bool `json:"disabled,omitempty"`
Custom *bool `json:"custom,omitempty"`
Ibc IbcConfig `json:"ibc"`
Evm *EvmConfig `json:"evm,omitempty"`
Type NetworkType `json:"type"`
Da DataAvailability `json:"da,omitempty"`
Apps []App `json:"apps,omitempty"`
Description *string `json:"description,omitempty"`
IsValidator *bool `json:"isValidator,omitempty"`
Analytics bool `json:"analytics"`
}

type Currency struct {
CoinDenom string `json:"coinDenom"`
CoinMinimalDenom string `json:"coinMinimalDenom"`
CoinDecimals uint `json:"coinDecimals"`
Logo string `json:"logo"`
}
14 changes: 2 additions & 12 deletions cmd/config/init/unique_rollapp_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/dymensionxyz/roller/config"
"net/http"
"os/exec"
"regexp"
"strings"
)

Expand Down Expand Up @@ -67,22 +66,13 @@ func verifyUniqueEthIdentifier(rollappID string, rlpCfg config.RollappConfig) (b
return false, err
}
for _, rollapp := range rollappsListResponse.Rollapps {
if getEthID(rollapp.ID) == getEthID(rollappID) {
if config.GetEthID(rollapp.ID) == config.GetEthID(rollappID) {
return false, nil
}
}
return true, nil
}

func getEthID(rollappID string) string {
re := regexp.MustCompile(`_(\d+)-`)
matches := re.FindStringSubmatch(rollappID)
if len(matches) > 1 {
return matches[1]
}
return ""
}

func VerifyUniqueRollappID(rollappID string, initConfig config.RollappConfig) error {
isUniqueRollapp, err := IsRollappIDUnique(rollappID, initConfig)
if err != nil {
Expand All @@ -96,7 +86,7 @@ func VerifyUniqueRollappID(rollappID string, initConfig config.RollappConfig) er
if initConfig.VMType == config.SDK_ROLLAPP {
return fmt.Errorf("rollapp ID \"%s\" already exists on the hub. Please use a unique ID", rollappID)
} else {
ethID := getEthID(rollappID)
ethID := config.GetEthID(rollappID)
return fmt.Errorf("EIP155 ID \"%s\" already exists on the hub (%s). Please use a unique EIP155 ID",
ethID, strings.Replace(rollappID, ethID, fmt.Sprintf("*%s*", ethID), 1))
}
Expand Down
9 changes: 9 additions & 0 deletions config/chainid.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ func ValidateRollAppID(id string) error {
}
return nil
}

func GetEthID(rollappID string) string {
re := regexp.MustCompile(`_(\d+)-`)
matches := re.FindStringSubmatch(rollappID)
if len(matches) > 1 {
return matches[1]
}
return ""
}