This commit is contained in:
Federico Kunze
2021-04-18 19:23:26 +02:00
parent cb2ab3d95d
commit 6f7470c2e0
29 changed files with 650 additions and 1549 deletions
-94
View File
@@ -1,19 +1,12 @@
package cli
import (
"math/big"
"strings"
rpctypes "github.com/cosmos/ethermint/ethereum/rpc/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/InjectiveLabs/sdk-go/ethereum/rpc"
"github.com/InjectiveLabs/sdk-go/wrappers"
"github.com/cosmos/ethermint/x/evm/types"
)
@@ -30,8 +23,6 @@ func GetQueryCmd() *cobra.Command {
cmd.AddCommand(
GetStorageCmd(),
GetCodeCmd(),
GetErc20Balance(),
GetAccount(),
)
return cmd
}
@@ -112,88 +103,3 @@ func GetCodeCmd() *cobra.Command {
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// 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
}