additions

This commit is contained in:
Federico Kunze
2021-04-18 17:54:18 +02:00
parent 5a3d514ba0
commit 614e62fb7e
138 changed files with 11348 additions and 2906 deletions
+95 -1
View File
@@ -1,12 +1,19 @@
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"
rpctypes "github.com/cosmos/ethermint/rpc/types"
"github.com/InjectiveLabs/sdk-go/ethereum/rpc"
"github.com/InjectiveLabs/sdk-go/wrappers"
"github.com/cosmos/ethermint/x/evm/types"
)
@@ -23,6 +30,8 @@ func GetQueryCmd() *cobra.Command {
cmd.AddCommand(
GetStorageCmd(),
GetCodeCmd(),
GetErc20Balance(),
GetAccount(),
)
return cmd
}
@@ -103,3 +112,88 @@ 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
}
+16
View File
@@ -45,3 +45,19 @@ func formatKeyToHash(key string) string {
return ethkey.Hex()
}
func cosmosAddressFromArg(addr string) (sdk.AccAddress, error) {
if strings.HasPrefix(addr, sdk.GetConfig().GetBech32AccountAddrPrefix()) {
// Check to see if address is Cosmos bech32 formatted
toAddr, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return nil, errors.Wrap(err, "invalid bech32 formatted address")
}
return toAddr, nil
}
// Strip 0x prefix if exists
addr = strings.TrimPrefix(addr, "0x")
return sdk.AccAddressFromHex(addr)
}
+21
View File
@@ -61,3 +61,24 @@ func TestCosmosToEthereumTypes(t *testing.T) {
require.NoError(t, err)
require.Equal(t, hexString, ethDecoded)
}
func TestAddressToCosmosAddress(t *testing.T) {
baseAddr, err := sdk.AccAddressFromHex("6A98D72760f7bbA69d62Ed6F48278451251948E7")
require.NoError(t, err)
// Test cosmos string back to address
cosmosFormatted, err := cosmosAddressFromArg(baseAddr.String())
require.NoError(t, err)
require.Equal(t, baseAddr, cosmosFormatted)
// Test account address from Ethereum address
ethAddr := common.BytesToAddress(baseAddr.Bytes())
ethFormatted, err := cosmosAddressFromArg(ethAddr.Hex())
require.NoError(t, err)
require.Equal(t, baseAddr, ethFormatted)
// Test encoding without the 0x prefix
ethFormatted, err = cosmosAddressFromArg(ethAddr.Hex()[2:])
require.NoError(t, err)
require.Equal(t, baseAddr, ethFormatted)
}
+2 -2
View File
@@ -14,7 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/rest"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
rpctypes "github.com/cosmos/ethermint/rpc/types"
rpctypes "github.com/cosmos/ethermint/ethereum/rpc/types"
"github.com/ethereum/go-ethereum/common"
)
@@ -81,7 +81,7 @@ func getEthTransactionByHash(clientCtx client.Context, hashHex string) ([]byte,
return nil, err
}
blockHash := common.BytesToHash(block.Block.Hash())
blockHash := common.BytesToHash(block.Block.Header.Hash())
ethTx, err := rpctypes.RawTxToEthTx(clientCtx, tx.Tx)
if err != nil {