From 01d531fe518c96c7ed6870bc82b940ea2f7f4e70 Mon Sep 17 00:00:00 2001 From: shrenujb <98204323+shrenujb@users.noreply.github.com> Date: Fri, 3 May 2024 16:39:12 -0400 Subject: [PATCH] Add a cli to convert module name to address (#1462) Signed-off-by: Shrenuj Bansal --- protocol/cmd/dydxprotocold/cmd/root.go | 22 +++++++++++++++++-- protocol/cmd/dydxprotocold/cmd/root_test.go | 24 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/protocol/cmd/dydxprotocold/cmd/root.go b/protocol/cmd/dydxprotocold/cmd/root.go index c4cae453795..f65934aacc0 100644 --- a/protocol/cmd/dydxprotocold/cmd/root.go +++ b/protocol/cmd/dydxprotocold/cmd/root.go @@ -32,7 +32,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" - "github.com/cosmos/cosmos-sdk/x/auth/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/crisis" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" @@ -106,7 +106,7 @@ func NewRootCmdWithInterceptors( WithTxConfig(tempApp.TxConfig()). WithLegacyAmino(tempApp.LegacyAmino()). WithInput(os.Stdin). - WithAccountRetriever(types.AccountRetriever{}). + WithAccountRetriever(authtypes.AccountRetriever{}). WithBroadcastMode(flags.BroadcastSync). WithHomeDir(homeDir). WithViper(EnvPrefix) @@ -267,6 +267,23 @@ func addModuleInitFlags(startCmd *cobra.Command) { crisis.AddModuleInitFlags(startCmd) } +func CmdModuleNameToAddress() *cobra.Command { + cmd := &cobra.Command{ + Use: "module-name-to-address [module-name]", + Short: "module name to address", + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx := client.GetClientContextFromCmd(cmd) + address := authtypes.NewModuleAddress(args[0]) + return clientCtx.PrintString(address.String()) + }, + } + + flags.AddPaginationFlagsToCmd(cmd, cmd.Use) + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + // queryCommand adds transaction and account querying commands. func queryCommand() *cobra.Command { cmd := &cobra.Command{ @@ -283,6 +300,7 @@ func queryCommand() *cobra.Command { server.QueryBlockCmd(), authcmd.QueryTxsByEventsCmd(), authcmd.QueryTxCmd(), + CmdModuleNameToAddress(), ) // Module specific query sub-commands are added by AutoCLI diff --git a/protocol/cmd/dydxprotocold/cmd/root_test.go b/protocol/cmd/dydxprotocold/cmd/root_test.go index 686b1f9981e..4dd4a03822f 100644 --- a/protocol/cmd/dydxprotocold/cmd/root_test.go +++ b/protocol/cmd/dydxprotocold/cmd/root_test.go @@ -1,8 +1,13 @@ package cmd_test import ( + "fmt" "testing" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + + "github.com/cosmos/cosmos-sdk/client" + svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" "github.com/dydxprotocol/v4-chain/protocol/app/config" "github.com/dydxprotocol/v4-chain/protocol/app/constants" @@ -46,3 +51,22 @@ func TestNewRootCmd_UsesClientConfig(t *testing.T) { rootCmd.SetArgs([]string{"query", "auth", "params"}) require.ErrorContains(t, svrcmd.Execute(rootCmd, constants.AppDaemonName, tempDir), "fakeTestAddress") } + +func TestCmdModuleNameToAddress(t *testing.T) { + expectedModuleNameAddress := map[string]string{ + "subaccounts": "dydx1v88c3xv9xyv3eetdx0tvcmq7ung3dywp5upwc6", + "subaccounts:37": "dydx16lwrx54mh9aru9ulzpknd429wldkhdwekhlswf", + "insurance_fund": "dydx1c7ptc87hkd54e3r7zjy92q29xkq7t79w64slrq", + "insurance_fund:37": "dydx10mlrxmaquwjwsj59ywp8xttc8rfxn9jfvzswtn", + } + for moduleName, expectedAddress := range expectedModuleNameAddress { + t.Run( + fmt.Sprintf("ModuleNameToAddress %s", moduleName), func(t *testing.T) { + ctx := client.Context{} + out, err := clitestutil.ExecTestCLICmd(ctx, cmd.CmdModuleNameToAddress(), []string{moduleName}) + require.NoError(t, err) + require.Equal(t, expectedAddress, out.String()) + }, + ) + } +}