laconicd/x/evm/client/cli/query.go

106 lines
2.5 KiB
Go
Raw Normal View History

package cli
import (
"github.com/spf13/cobra"
rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/tharsis/ethermint/x/evm/types"
)
2021-04-17 10:00:07 +00:00
// GetQueryCmd returns the parent command for all x/bank CLi query commands.
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the evm module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
2021-04-17 10:00:07 +00:00
cmd.AddCommand(
GetStorageCmd(),
GetCodeCmd(),
)
return cmd
}
2021-04-17 10:00:07 +00:00
// GetStorageCmd queries a key in an accounts storage
func GetStorageCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "storage [address] [key]",
Short: "Gets storage for an account with a given key and height",
Long: "Gets storage for an account with a given key and height. If the height is not provided, it will use the latest height from context.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
2021-04-17 10:00:07 +00:00
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
2021-04-17 10:00:07 +00:00
queryClient := types.NewQueryClient(clientCtx)
address, err := accountToHex(args[0])
if err != nil {
2021-04-17 10:00:07 +00:00
return err
}
key := formatKeyToHash(args[1])
2021-04-17 10:00:07 +00:00
req := &types.QueryStorageRequest{
Address: address,
Key: key,
}
2021-04-17 10:00:07 +00:00
res, err := queryClient.Storage(rpctypes.ContextWithHeight(clientCtx.Height), req)
if err != nil {
2021-04-17 10:00:07 +00:00
return err
}
2021-04-17 10:00:07 +00:00
return clientCtx.PrintProto(res)
},
}
2021-04-17 10:00:07 +00:00
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
2021-04-17 10:00:07 +00:00
// GetCodeCmd queries the code field of a given address
func GetCodeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "code [address]",
Short: "Gets code from an account",
2021-04-17 10:00:07 +00:00
Long: "Gets code from an account. If the height is not provided, it will use the latest height from context.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
2021-04-17 10:00:07 +00:00
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
2021-04-17 10:00:07 +00:00
queryClient := types.NewQueryClient(clientCtx)
address, err := accountToHex(args[0])
if err != nil {
2021-04-17 10:00:07 +00:00
return err
}
2021-04-17 10:00:07 +00:00
req := &types.QueryCodeRequest{
Address: address,
}
2021-04-17 10:00:07 +00:00
res, err := queryClient.Code(rpctypes.ContextWithHeight(clientCtx.Height), req)
if err != nil {
2021-04-17 10:00:07 +00:00
return err
}
2021-04-17 10:00:07 +00:00
return clientCtx.PrintProto(res)
},
}
2021-04-17 10:00:07 +00:00
flags.AddQueryFlagsToCmd(cmd)
return cmd
}