Skip to content

Commit

Permalink
fix: gas prices flags
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir committed Oct 25, 2022
1 parent 570d19d commit f44aea0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
18 changes: 18 additions & 0 deletions app/cli/collect_gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,21 @@ func CollectTxs(cdc codec.Codec, txJSONDecoder sdk.TxDecoder, moniker, genTxsDir

return appGenTxs, persistentPeers, nil
}

type PrintInfo struct {
Moniker string `json:"moniker" yaml:"moniker"`
ChainID string `json:"chain_id" yaml:"chain_id"`
NodeID string `json:"node_id" yaml:"node_id"`
GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"`
AppMessage json.RawMessage `json:"app_message" yaml:"app_message"`
}

func NewPrintInfo(moniker, chainID, nodeID, genTxsDir string, appMessage json.RawMessage) PrintInfo {
return PrintInfo{
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
GenTxsDir: genTxsDir,
AppMessage: appMessage,
}
}
50 changes: 9 additions & 41 deletions app/cli/init.go → cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package cmd

import (
"bufio"
Expand All @@ -9,7 +9,6 @@ import (
"path/filepath"

"github.com/cosmos/cosmos-sdk/codec"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -24,6 +23,9 @@ import (
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/types"
bip39 "github.com/tyler-smith/go-bip39"

"github.com/pundix/pundix/app/cli"
pxtypes "github.com/pundix/pundix/types"
)

const (
Expand All @@ -32,35 +34,11 @@ const (

// FlagRecover defines a flag to initialize the private validator key from a specific seed.
FlagRecover = "recover"

// FlagStakingDenom defines a flag to set the default coin denomination
FlagStakingDenom = "denom"

// FlagMintDistributionDenom x/mint module mint token denom.
FlagMintDistributionDenom = "mint-denom"
)

type PrintInfo struct {
Moniker string `json:"moniker" yaml:"moniker"`
ChainID string `json:"chain_id" yaml:"chain_id"`
NodeID string `json:"node_id" yaml:"node_id"`
GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"`
AppMessage json.RawMessage `json:"app_message" yaml:"app_message"`
}

func NewPrintInfo(moniker, chainID, nodeID, genTxsDir string, appMessage json.RawMessage) PrintInfo {
return PrintInfo{
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
GenTxsDir: genTxsDir,
AppMessage: appMessage,
}
}

// InitCmd returns a command that initializes all files needed for Tendermint
// and the respective application.
func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func(stakingDenom, mintDenom string, cdc codec.JSONCodec) map[string]json.RawMessage, consensusParams *tmproto.ConsensusParams) *cobra.Command {
func InitCmd(nodeHome string, genGenesis func(stakingDenom, mintDenom string, cdc codec.JSONCodec) map[string]json.RawMessage, consensusParams *tmproto.ConsensusParams) *cobra.Command {
cmd := &cobra.Command{
Use: "init [moniker]",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
Expand All @@ -76,7 +54,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func(

chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
if chainID == "" {
chainID = fmt.Sprintf("test-chain-%v", tmrand.Str(6))
return fmt.Errorf("chain id cannot be empty")
}

// Get bip39 mnemonic
Expand Down Expand Up @@ -110,15 +88,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func(
if !overwrite && tmos.FileExists(genFile) {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}
flagStakingDenom, err := cmd.Flags().GetString(FlagStakingDenom)
if err != nil || flagStakingDenom == "" {
return fmt.Errorf("invalid staking denom: %v", err)
}
flagMintDistributionDenom, err := cmd.Flags().GetString(FlagMintDistributionDenom)
if err != nil || flagMintDistributionDenom == "" {
return fmt.Errorf("invalid mint denom: %v", err)
}
appState, err := json.MarshalIndent(genGenesis(flagStakingDenom, flagMintDistributionDenom, clientCtx.Codec), "", " ")
appState, err := json.MarshalIndent(genGenesis(pxtypes.StakingBondDenom(), pxtypes.MintDenom(), clientCtx.Codec), "", " ")
if err != nil {
return fmt.Errorf("failed to marshall default genesis state: %s", err.Error())
}
Expand All @@ -143,7 +113,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func(
return fmt.Errorf("failed to export gensis file: %s", err.Error())
}

toPrint := NewPrintInfo(config.Moniker, chainID, nodeID, "", appState)
toPrint := cli.NewPrintInfo(config.Moniker, chainID, nodeID, "", appState)

cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)

Expand All @@ -158,9 +128,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func(
cmd.Flags().String(tmcli.HomeFlag, nodeHome, "node's home directory")
cmd.Flags().Bool(FlagOverwrite, false, "overwrite the genesis.json file")
cmd.Flags().Bool(FlagRecover, false, "provide seed phrase to recover existing key instead of creating")
cmd.Flags().String(flags.FlagChainID, chainId, "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().String(FlagStakingDenom, stakingDenom, "set the default staking coin denomination")
cmd.Flags().String(FlagMintDistributionDenom, mintDenom, "set the default mint coin denomination")
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().StringP(tmcli.OutputFlag, "o", "json", "Output format (text|json)")

return cmd
Expand Down
25 changes: 18 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -91,6 +90,17 @@ func NewRootCmd() *cobra.Command {
pxtypes.SetChainId(initClientCtx.ChainID)
}

if f := cmd.Flags().Lookup(flags.FlagGasPrices); f != nil {
gasPricesStr, _ := cmd.Flags().GetString(flags.FlagGasPrices)
gasPrices, err := sdk.ParseCoinsNormalized(gasPricesStr)
if err != nil {
return err
}
if err := f.Value.Set(gasPrices.String()); err != nil {
panic(err)
}
}

defConfig := config.DefaultConfig()
defConfig.MinGasPrices = fmt.Sprintf("2000000000000%s", pxtypes.StakingBondDenom())
if err := server.InterceptConfigsPreRunHandler(cmd, config.DefaultConfigTemplate, defConfig); err != nil {
Expand All @@ -105,7 +115,7 @@ func NewRootCmd() *cobra.Command {
flags.FlagKeyringBackend: keyring.BackendOS,
flags.FlagGas: "auto",
flags.FlagGasAdjustment: "1.5",
flags.FlagGasPrices: fmt.Sprintf("2000000000000%s", pxtypes.StakingBondDenom()),
flags.FlagGasPrices: "0.000002PUNDIX",
})
return rootCmd
}
Expand All @@ -115,7 +125,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig) {
sdkCfgCmd.AddCommand(cli.AppTomlCmd(), cli.ConfigTomlCmd())

rootCmd.AddCommand(
cli.InitCmd(app.DefaultNodeHome, pxtypes.ChainId(), pxtypes.StakingBondDenom(), pxtypes.MintDenom(), app.NewDefAppGenesisByDenom, app.CustomConsensusParams()),
InitCmd(app.DefaultNodeHome, app.NewDefAppGenesisByDenom, app.CustomConsensusParams()),
cli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
//genutilcli.MigrateGenesisCmd(),
cli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
Expand Down Expand Up @@ -265,9 +275,10 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a
panic(err)
}

gasPrice := cast.ToString(appOpts.Get(server.FlagMinGasPrices))
if strings.Contains(gasPrice, ".") {
panic("Invalid gas price, cannot contain decimals")
gasPricesStr := cast.ToString(appOpts.Get(server.FlagMinGasPrices))
gasPrices, err := sdk.ParseCoinsNormalized(gasPricesStr)
if err != nil {
panic(err)
}

return app.New(
Expand All @@ -278,7 +289,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a
// this line is used by starport scaffolding # stargate/root/appArgument
appOpts,
baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(gasPrice),
baseapp.SetMinGasPrices(gasPrices.String()),
baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))),
baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))),
baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))),
Expand Down
7 changes: 7 additions & 0 deletions types/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ func TestRegisterDenom(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(StakingBondDenom(), sdk.NewInt(1e18))), coin)
}

func TestParseGasPrices(t *testing.T) {
SetChainId(MainnetChainId)
coin, err := sdk.ParseCoinsNormalized("0.000002PUNDIX")
assert.NoError(t, err)
t.Log(coin.String())
}

0 comments on commit f44aea0

Please sign in to comment.