Skip to content

Commit

Permalink
feat: Add configurable token-supply flag on roller config init (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Jun 26, 2023
1 parent ae57f48 commit e8344e4
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 40 deletions.
4 changes: 2 additions & 2 deletions cmd/config/init/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import "github.com/dymensionxyz/roller/cmd/utils"

var FlagNames = struct {
Home string
Decimals string
TokenSupply string
RollappBinary string
HubID string
}{
Home: "home",
Decimals: "decimals",
TokenSupply: "token-supply",
RollappBinary: "rollapp-binary",
HubID: "hub",
}
Expand Down
22 changes: 9 additions & 13 deletions cmd/config/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func addFlags(cmd *cobra.Command) {
cmd.Flags().StringP(FlagNames.HubID, "", TestnetHubID, fmt.Sprintf("The ID of the Dymension hub. %s", getAvailableHubsMessage()))
cmd.Flags().StringP(FlagNames.RollappBinary, "", "", "The rollapp binary. Should be passed only if you built a custom rollapp")
cmd.Flags().Uint64P(FlagNames.Decimals, "", 18, "The number of decimal places a rollapp token supports")
cmd.Flags().StringP(FlagNames.TokenSupply, "", "1000000000", "The total token supply of the RollApp")

cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
hubID, err := cmd.Flags().GetString(FlagNames.HubID)
Expand All @@ -36,14 +36,6 @@ func getValidRollappIdMessage() string {
"version' is a 1 to 5 digit number representing the version. For example: 'mars_9721_1'"
}

func getDecimals(cmd *cobra.Command) uint64 {
decimals, err := cmd.Flags().GetUint64(FlagNames.Decimals)
if err != nil {
panic(err)
}
return decimals
}

func getRollappBinaryPath(cmd *cobra.Command) string {
rollappBinaryPath := cmd.Flag(FlagNames.RollappBinary).Value.String()
if rollappBinaryPath == "" {
Expand All @@ -52,21 +44,25 @@ func getRollappBinaryPath(cmd *cobra.Command) string {
return rollappBinaryPath
}

func GetInitConfig(initCmd *cobra.Command, args []string) utils.RollappConfig {
func getTokenSupply(cmd *cobra.Command) string {
return cmd.Flag(FlagNames.TokenSupply).Value.String()
}

func GetInitConfig(initCmd *cobra.Command, args []string) (utils.RollappConfig, error) {
rollappId := args[0]
denom := args[1]
home := initCmd.Flag(utils.FlagNames.Home).Value.String()
rollappBinaryPath := getRollappBinaryPath(initCmd)
decimals := getDecimals(initCmd)
hubID := initCmd.Flag(FlagNames.HubID).Value.String()
tokenSupply := getTokenSupply(initCmd)
return utils.RollappConfig{
Home: home,
RollappID: rollappId,
RollappBinary: rollappBinaryPath,
Denom: denom,
Decimals: decimals,
HubData: Hubs[hubID],
}
TokenSupply: tokenSupply,
}, nil
}

func getAvailableHubsMessage() string {
Expand Down
10 changes: 5 additions & 5 deletions cmd/config/init/genesis.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package initconfig

import (
"fmt"
"github.com/dymensionxyz/roller/cmd/utils"
"io/ioutil"

"fmt"
"os/exec"
"path/filepath"

Expand All @@ -13,11 +13,11 @@ import (
)

func initializeRollappGenesis(initConfig utils.RollappConfig) error {
zeros := initConfig.Decimals + 9
tokenAmount := "1" + fmt.Sprintf("%0*d", zeros, 0) + initConfig.Denom
tokenSupply := fmt.Sprintf("%s%s", initConfig.TokenSupply, initConfig.Denom)
rollappConfigDirPath := filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp)
genesisSequencerAccountCmd := exec.Command(initConfig.RollappBinary, "add-genesis-account", consts.KeyNames.RollappSequencer, tokenAmount, "--keyring-backend", "test", "--home", rollappConfigDirPath)
err := genesisSequencerAccountCmd.Run()
genesisSequencerAccountCmd := exec.Command(initConfig.RollappBinary, "add-genesis-account",
consts.KeyNames.RollappSequencer, tokenSupply, "--keyring-backend", "test", "--home", rollappConfigDirPath)
_, err := utils.ExecBashCommand(genesisSequencerAccountCmd)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func InitCmd() *cobra.Command {
Use: "init <chain-id> <denom>",
Short: "Initialize a RollApp configuration on your local machine.",
Run: func(cmd *cobra.Command, args []string) {
initConfig := GetInitConfig(cmd, args)
initConfig, err := GetInitConfig(cmd, args)
utils.PrettifyErrorIfExists(err)
utils.PrettifyErrorIfExists(VerifyUniqueRollappID(initConfig.RollappID, initConfig))
isRootExist, err := dirNotEmpty(initConfig.Home)
utils.PrettifyErrorIfExists(err)
Expand Down Expand Up @@ -45,7 +46,7 @@ func InitCmd() *cobra.Command {
daAddress, err := utils.GetCelestiaAddress(initConfig.Home)
utils.PrettifyErrorIfExists(err)
addresses[consts.KeyNames.DALightNode] = daAddress
initializeRollappConfig(initConfig)
utils.PrettifyErrorIfExists(initializeRollappConfig(initConfig))
utils.PrettifyErrorIfExists(initializeRollappGenesis(initConfig))
utils.PrettifyErrorIfExists(utils.WriteConfigToTOML(initConfig))
printInitOutput(addresses, initConfig.RollappID)
Expand Down
30 changes: 21 additions & 9 deletions cmd/config/init/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,38 @@ import (
toml "github.com/pelletier/go-toml"
)

func initializeRollappConfig(initConfig utils.RollappConfig) {
func initializeRollappConfig(initConfig utils.RollappConfig) error {
initRollappCmd := exec.Command(initConfig.RollappBinary, "init", consts.KeyNames.HubSequencer, "--chain-id",
initConfig.RollappID, "--home", filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp))
err := initRollappCmd.Run()
_, err := utils.ExecBashCommand(initRollappCmd)
if err != nil {
panic(err)
return err
}
setRollappAppConfig(filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp, "config/app.toml"), initConfig.Denom)
err = setRollappAppConfig(filepath.Join(initConfig.Home, consts.ConfigDirName.Rollapp, "config/app.toml"),
initConfig.Denom)
if err != nil {
return err
}
return nil
}

func setRollappAppConfig(appConfigFilePath string, denom string) {
func setRollappAppConfig(appConfigFilePath string, denom string) error {
config, _ := toml.LoadFile(appConfigFilePath)
config.Set("minimum-gas-prices", "0"+denom)
config.Set("api.enable", "true")
file, _ := os.Create(appConfigFilePath)
_, err := file.WriteString(config.String())
file, err := os.Create(appConfigFilePath)
if err != nil {
return err
}
_, err = file.WriteString(config.String())
if err != nil {
return err
}
err = file.Close()
if err != nil {
panic(err)
return err
}
file.Close()
return nil
}

func RollappConfigDir(root string) string {
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type RollappConfig struct {
RollappID string
RollappBinary string
Denom string
Decimals uint64
TokenSupply string
HubData HubData
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@
"coins": [
{
"denom": "udym",
"amount": "1000000000000000"
"amount": "1000"
}
]
}
],
"supply": [
{
"denom": "udym",
"amount": "1000000000000000"
"amount": "1000"
}
],
"denom_metadata": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@
"coins": [
{
"denom": "udym",
"amount": "1000000000000000000000000000"
"amount": "1000000000"
}
]
}
],
"supply": [
{
"denom": "udym",
"amount": "1000000000000000000000000000"
"amount": "1000000000"
}
],
"denom_metadata": []
Expand Down
7 changes: 4 additions & 3 deletions test/config/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestInitCmd(t *testing.T) {
decimals := "6"
tokenSupply := "1000"
testCases := []struct {
name string
goldenDirPath string
Expand All @@ -29,7 +29,7 @@ func TestInitCmd(t *testing.T) {
name: "Roller config init with custom flags",
goldenDirPath: "./goldens/init_with_flags",
optionalFlags: []string{
"--" + initconfig.FlagNames.Decimals, decimals,
"--" + initconfig.FlagNames.TokenSupply, tokenSupply,
},
},
}
Expand All @@ -52,7 +52,8 @@ func TestInitCmd(t *testing.T) {
"--" + utils.FlagNames.Home, tempDir,
}, tc.optionalFlags...))
assert.NoError(initCmd.Execute())
initConfig := initconfig.GetInitConfig(initCmd, []string{rollappID, denom})
initConfig, err := initconfig.GetInitConfig(initCmd, []string{rollappID, denom})
assert.NoError(err)
assert.NoError(testutils.VerifyRollerConfig(initConfig))
assert.NoError(os.Remove(filepath.Join(tempDir, utils.RollerConfigFileName)))
assert.NoError(testutils.VerifyRollappKeys(tempDir))
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/config/init/testutils/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func VerifyRollerConfig(rollappConfig utils.RollappConfig) error {
if err != nil {
return err
}
if rollappConfig == existingConfig {
if existingConfig == rollappConfig {
return nil
}
return errors.New("roller config does not match")
Expand Down

0 comments on commit e8344e4

Please sign in to comment.