2019-07-25 20:38:55 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-07 16:41:15 +00:00
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2020-04-07 20:00:06 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-11-15 17:02:13 +00:00
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
2019-09-07 16:41:15 +00:00
|
|
|
// GetQueryCmd defines evm module queries through the cli
|
2019-07-25 20:38:55 +00:00
|
|
|
func GetQueryCmd(moduleName string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
evmQueryCmd := &cobra.Command{
|
|
|
|
Use: types.ModuleName,
|
|
|
|
Short: "Querying commands for the evm module",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
2020-04-07 20:00:06 +00:00
|
|
|
evmQueryCmd.AddCommand(flags.GetCommands(
|
2019-07-25 20:38:55 +00:00
|
|
|
GetCmdGetStorageAt(moduleName, cdc),
|
|
|
|
GetCmdGetCode(moduleName, cdc),
|
|
|
|
)...)
|
|
|
|
return evmQueryCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdGetStorageAt queries a key in an accounts storage
|
|
|
|
func GetCmdGetStorageAt(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "storage [account] [key]",
|
|
|
|
Short: "Gets storage for an account at a given key",
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-10-22 20:39:51 +00:00
|
|
|
clientCtx := context.NewCLIContext().WithCodec(cdc)
|
2019-07-25 20:38:55 +00:00
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
account, err := accountToHex(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not parse account address")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := formatKeyToHash(args[1])
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
res, _, err := clientCtx.Query(
|
2019-11-15 17:02:13 +00:00
|
|
|
fmt.Sprintf("custom/%s/storage/%s/%s", queryRoute, account, key))
|
2019-07-25 20:38:55 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-11-15 17:02:13 +00:00
|
|
|
return fmt.Errorf("could not resolve: %s", err)
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2019-09-26 15:36:23 +00:00
|
|
|
var out types.QueryResStorage
|
2019-07-25 20:38:55 +00:00
|
|
|
cdc.MustUnmarshalJSON(res, &out)
|
2020-10-22 20:39:51 +00:00
|
|
|
return clientCtx.PrintOutput(out)
|
2019-07-25 20:38:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdGetCode queries the code field of a given address
|
|
|
|
func GetCmdGetCode(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "code [account]",
|
|
|
|
Short: "Gets code from an account",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-10-22 20:39:51 +00:00
|
|
|
clientCtx := context.NewCLIContext().WithCodec(cdc)
|
2019-07-25 20:38:55 +00:00
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
account, err := accountToHex(args[0])
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-11-15 17:02:13 +00:00
|
|
|
return errors.Wrap(err, "could not parse account address")
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2019-09-07 16:41:15 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
res, _, err := clientCtx.Query(
|
2019-11-15 17:02:13 +00:00
|
|
|
fmt.Sprintf("custom/%s/code/%s", queryRoute, account))
|
2019-09-07 16:41:15 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-11-15 17:02:13 +00:00
|
|
|
return fmt.Errorf("could not resolve: %s", err)
|
2019-09-07 16:41:15 +00:00
|
|
|
}
|
2019-11-15 17:02:13 +00:00
|
|
|
|
|
|
|
var out types.QueryResCode
|
2019-09-07 16:41:15 +00:00
|
|
|
cdc.MustUnmarshalJSON(res, &out)
|
2020-10-22 20:39:51 +00:00
|
|
|
return clientCtx.PrintOutput(out)
|
2019-09-07 16:41:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|