Skip to content

Commit

Permalink
fix: manually set autocli for non app wire modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamu committed Nov 2, 2023
1 parent 929d843 commit 242c77b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 91 deletions.
34 changes: 29 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/desmos-labs/desmos/v6/app/upgrades"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/server"
Expand All @@ -23,21 +25,22 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/spf13/cast"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
tmos "github.com/cometbft/cometbft/libs/os"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
"github.com/cosmos/cosmos-sdk/x/params"

"github.com/cosmos/cosmos-sdk/x/auth/ante"
Expand Down Expand Up @@ -739,3 +742,24 @@ func (app *DesmosApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
func (app *DesmosApp) GetTxConfig() client.TxConfig {
return app.txConfig
}

// AutoCliOpts returns the autocli options for the app.
func (app *DesmosApp) AutoCliOpts() autocli.AppOptions {
modules := make(map[string]appmodule.AppModule, 0)
for _, m := range app.ModuleManager.Modules {
if moduleWithName, ok := m.(module.HasName); ok {
moduleName := moduleWithName.Name()
if appModule, ok := moduleWithName.(appmodule.AppModule); ok {
modules[moduleName] = appModule
}
}
}

return autocli.AppOptions{
Modules: modules,
ModuleOptions: runtimeservices.ExtractAutoCLIOptions(app.ModuleManager.Modules),
AddressCodec: authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
ValidatorAddressCodec: authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
ConsensusAddressCodec: authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
}
}
22 changes: 10 additions & 12 deletions app/app_module_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -254,15 +253,14 @@ func GetAppConfig() depinject.Config {
Config: appconfig.WrapAny(&tokenfactorymodulev1.Module{}),
},
},
}), depinject.Supply(
// supply custom module basics
map[string]module.AppModuleBasic{
genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
govtypes.ModuleName: gov.NewAppModuleBasic(
[]govclient.ProposalHandler{
paramsclient.ProposalHandler,
},
),
},
))
}),
depinject.Supply(
// supply custom module basics
map[string]module.AppModuleBasic{
genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
govtypes.ModuleName: gov.NewAppModuleBasic(
[]govclient.ProposalHandler{},
),
},
))
}
119 changes: 45 additions & 74 deletions app/desmos/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package cmd
import (
"os"

"github.com/spf13/cobra"

"cosmossdk.io/client/v2/autocli"
clientv2keyring "cosmossdk.io/client/v2/autocli/keyring"
"cosmossdk.io/core/address"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/simapp/params"
dbm "github.com/cosmos/cosmos-db"
"github.com/desmos-labs/desmos/v6/app"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -23,58 +20,49 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/tx"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
"github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/desmos-labs/desmos/v6/app"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/cosmos/cosmos-sdk/x/gov"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
)

// NewRootCmd creates a new root command for desmos. It is called once in the
// NewRootCmd creates a new root command for simd. It is called once in the
// main function.
func NewRootCmd() *cobra.Command {
// Read in the configuration file for the sdk
cfg := sdk.GetConfig()
app.SetupConfig(cfg)
cfg.Seal()

var (
interfaceRegistry codectypes.InterfaceRegistry
appCodec codec.Codec
txConfig client.TxConfig
autoCliOpts autocli.AppOptions
moduleBasicManager module.BasicManager
clientCtx *client.Context
)

if err := depinject.Inject(
depinject.Configs(
app.GetAppConfig(),
depinject.Supply(
log.NewNopLogger(),
simtestutil.NewAppOptionsWithFlagHome(tempDir()),
),
depinject.Provide(
ProvideClientContext,
ProvideKeyring,
),
),
&interfaceRegistry,
&appCodec,
&txConfig,
&autoCliOpts,
&moduleBasicManager,
&clientCtx,
); err != nil {
panic(err)
// we "pre"-instantiate the application for getting the injected/configured encoding configuration
tempApp := app.NewDesmosApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(tempDir()))
encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
TxConfig: tempApp.TxConfig(),
Amino: tempApp.LegacyAmino(),
}

initClientCtx := client.Context{}.
WithCodec(encodingConfig.Codec).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithLegacyAmino(encodingConfig.Amino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(app.DefaultNodeHome).
WithViper("") // In desmos, we don't use any prefix for env variables.

rootCmd := &cobra.Command{
Use: "desmos",
Short: "Desmos application",
Use: "desmos",
Short: "Desmos application",
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// set the default command outputs
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())

initClientCtx := *clientCtx
initClientCtx = initClientCtx.WithCmdContext(cmd.Context())
initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags())
if err != nil {
Expand All @@ -88,20 +76,20 @@ func NewRootCmd() *cobra.Command {

// This needs to go after ReadFromClientConfig, as that function
// sets the RPC client needed for SIGN_MODE_TEXTUAL.
enabledSignModes := tx.DefaultSignModes
enabledSignModes = append(enabledSignModes, signing.SignMode_SIGN_MODE_TEXTUAL)
enabledSignModes := append(tx.DefaultSignModes, signing.SignMode_SIGN_MODE_TEXTUAL)

Check failure on line 79 in app/desmos/cmd/root.go

View workflow job for this annotation

GitHub Actions / golangci-lint

appendAssign: append result not assigned to the same slice (gocritic)
txConfigOpts := tx.ConfigOptions{
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: txmodule.NewGRPCCoinMetadataQueryFn(initClientCtx),
}
txConfigWithTextual, err := tx.NewTxConfigWithOptions(
codec.NewProtoCodec(interfaceRegistry),
codec.NewProtoCodec(encodingConfig.InterfaceRegistry),
txConfigOpts,
)
if err != nil {
return err
}
initClientCtx = initClientCtx.WithTxConfig(txConfigWithTextual)

if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}
Expand All @@ -113,39 +101,22 @@ func NewRootCmd() *cobra.Command {
},
}

initRootCmd(rootCmd, txConfig, moduleBasicManager)
initRootCmd(rootCmd, encodingConfig.TxConfig, module.NewBasicManagerFromManager(tempApp.ModuleManager, map[string]module.AppModuleBasic{
genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator),
govtypes.ModuleName: gov.NewAppModuleBasic(
[]govclient.ProposalHandler{
paramsclient.ProposalHandler,
},
),
}))

// add keyring to autocli opts
autoCliOpts := tempApp.AutoCliOpts()
autoCliOpts.Keyring = initClientCtx.Keyring

if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil {
panic(err)
}

return rootCmd
}

func ProvideClientContext(appCodec codec.Codec, interfaceRegistry codectypes.InterfaceRegistry, legacyAmino *codec.LegacyAmino) *client.Context {
initClientCtx := client.Context{}.
WithCodec(appCodec).
WithInterfaceRegistry(interfaceRegistry).
WithLegacyAmino(legacyAmino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(app.DefaultNodeHome).
WithViper("DESMOS")

// Read the config again to overwrite the default values with the values from the config file
initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
if err != nil {
panic(err)
}

return &initClientCtx
}

func ProvideKeyring(clientCtx *client.Context, addressCodec address.Codec) (clientv2keyring.Keyring, error) {
kb, err := client.NewKeyringFromBackend(*clientCtx, clientCtx.Keyring.Backend())
if err != nil {
return nil, err
}

return kb, nil
}

0 comments on commit 242c77b

Please sign in to comment.