Skip to content

Commit

Permalink
Add a cli to convert module name to address (#1462)
Browse files Browse the repository at this point in the history
Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
  • Loading branch information
shrenujb committed May 3, 2024
1 parent 25dbefe commit 648bb3c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
22 changes: 20 additions & 2 deletions protocol/cmd/dydxprotocold/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -283,6 +300,7 @@ func queryCommand() *cobra.Command {
server.QueryBlockCmd(),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
CmdModuleNameToAddress(),
)

// Module specific query sub-commands are added by AutoCLI
Expand Down
24 changes: 24 additions & 0 deletions protocol/cmd/dydxprotocold/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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())
},
)
}
}

0 comments on commit 648bb3c

Please sign in to comment.