2019-07-25 20:38:55 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2021-04-18 15:54:18 +00:00
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
rpctypes "github.com/cosmos/ethermint/ethereum/rpc/types"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-11-15 17:02:13 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-04-07 20:00:06 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-11-15 17:02:13 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
"github.com/InjectiveLabs/sdk-go/ethereum/rpc"
|
|
|
|
"github.com/InjectiveLabs/sdk-go/wrappers"
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/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{
|
2019-07-25 20:38:55 +00:00
|
|
|
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(),
|
2021-04-18 15:54:18 +00:00
|
|
|
GetErc20Balance(),
|
|
|
|
GetAccount(),
|
2021-04-17 10:00:07 +00:00
|
|
|
)
|
|
|
|
return cmd
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
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.",
|
2019-07-25 20:38:55 +00:00
|
|
|
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
|
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
|
|
|
|
address, err := accountToHex(args[0])
|
2019-11-15 17:02:13 +00:00
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return err
|
2019-11-15 17:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key := formatKeyToHash(args[1])
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
req := &types.QueryStorageRequest{
|
|
|
|
Address: address,
|
|
|
|
Key: key,
|
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
res, err := queryClient.Storage(rpctypes.ContextWithHeight(clientCtx.Height), req)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return err
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return clientCtx.PrintProto(res)
|
2019-07-25 20:38:55 +00:00
|
|
|
},
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
|
|
return cmd
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
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]",
|
2019-07-25 20:38:55 +00:00
|
|
|
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.",
|
2019-07-25 20:38:55 +00:00
|
|
|
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
|
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
|
|
|
|
address, err := accountToHex(args[0])
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return err
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2019-09-07 16:41:15 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
req := &types.QueryCodeRequest{
|
|
|
|
Address: address,
|
|
|
|
}
|
2019-09-07 16:41:15 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
res, err := queryClient.Code(rpctypes.ContextWithHeight(clientCtx.Height), req)
|
2019-09-07 16:41:15 +00:00
|
|
|
if err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return err
|
2019-09-07 16:41:15 +00:00
|
|
|
}
|
2019-11-15 17:02:13 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
return clientCtx.PrintProto(res)
|
2019-09-07 16:41:15 +00:00
|
|
|
},
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
|
|
return cmd
|
2019-09-07 16:41:15 +00:00
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
|
|
|
|
// GetErc20Balance queries the erc20 balance of an address
|
|
|
|
func GetErc20Balance() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "erc20balance [contract] [address]",
|
|
|
|
Short: "Gets erc20 balance of an account",
|
|
|
|
Long: "Gets erc20 balance of an account.",
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
|
|
|
|
contract := args[0]
|
|
|
|
address := args[1]
|
|
|
|
|
|
|
|
erc20ABI, err := abi.JSON(strings.NewReader(wrappers.ERC20ABI))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
input, err := erc20ABI.Pack("balanceOf", common.HexToAddress(address))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &types.QueryStaticCallRequest{
|
|
|
|
Address: contract,
|
|
|
|
Input: input,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := queryClient.StaticCall(rpc.ContextWithHeight(clientCtx.Height), req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ret := big.NewInt(0)
|
|
|
|
err = erc20ABI.UnpackIntoInterface(&ret, "balanceOf", res.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientCtx.PrintString(ret.String())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccount queries the account by address
|
|
|
|
func GetAccount() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "account [address]",
|
|
|
|
Short: "Get an account by address",
|
|
|
|
Long: "Get an account by address",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
|
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
queryClient := types.NewQueryClient(clientCtx)
|
|
|
|
address := args[0]
|
|
|
|
|
|
|
|
req := &types.QueryAccountRequest{
|
|
|
|
Address: address,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := queryClient.Account(rpc.ContextWithHeight(clientCtx.Height), req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientCtx.PrintProto(res)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.AddQueryFlagsToCmd(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|