Skip to content

Commit

Permalink
add CLI query command
Browse files Browse the repository at this point in the history
  • Loading branch information
pirtleshell committed May 30, 2023
1 parent 0bfef0f commit 2ff9996
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
42 changes: 42 additions & 0 deletions x/evmutil/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"

Expand All @@ -24,6 +25,7 @@ func GetQueryCmd() *cobra.Command {

cmds := []*cobra.Command{
QueryParamsCmd(),
QueryDeployedCosmosCoinContractsCmd(),
}

for _, cmd := range cmds {
Expand Down Expand Up @@ -61,3 +63,43 @@ func QueryParamsCmd() *cobra.Command {
},
}
}

func QueryDeployedCosmosCoinContractsCmd() *cobra.Command {
var cosmosDenoms []string
cmdName := "deployed-cosmos-coin-contracts"
q := fmt.Sprintf("%[1]s q %[2]s %s", version.AppName, types.ModuleName, cmdName)
cmd := &cobra.Command{
Use: fmt.Sprintf("%s [--denoms denom1,denom2] [flags]", cmdName),
Short: "Query for deployed ERC20 contract addresses representing cosmos coins in the EVM",
Example: fmt.Sprintf("Query all:\n %s\n\nQuery by denom:\n %s --denoms denom1,denom2", q, q),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

page, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
request := types.QueryDeployedCosmosCoinContractsRequest{
CosmosDenoms: cosmosDenoms,
Pagination: page,
}
res, err := queryClient.DeployedCosmosCoinContracts(context.Background(), &request)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddPaginationFlagsToCmd(cmd, cmdName)
cmd.Flags().StringSliceVar(&cosmosDenoms, "denoms", []string{}, fmt.Sprintf("(optional) Cosmos denoms to get addresses for. If no contract is deployed, the result will be omitted. Limit %d per query.", query.DefaultLimit))

return cmd
}
6 changes: 6 additions & 0 deletions x/evmutil/types/address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/json"
"errors"
"fmt"

Expand Down Expand Up @@ -54,6 +55,11 @@ func (addr InternalEVMAddress) MarshalTo(data []byte) (int, error) {
return copy(data, addressBytes[:]), nil
}

// MarshalJSON allows PrintProto to handle InternalEVMAddress
func (addr InternalEVMAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(addr.Hex())
}

// Size implements protobuf Unmarshaler interface.
func (a InternalEVMAddress) Size() int {
return common.AddressLength
Expand Down
8 changes: 4 additions & 4 deletions x/evmutil/types/conversion_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@ func NewAllowedCosmosCoinERC20Token(
func (token AllowedCosmosCoinERC20Token) Validate() error {
// disallow empty string fields
if err := sdk.ValidateDenom(token.CosmosDenom); err != nil {
return fmt.Errorf("allowed cosomos coin erc20 token's sdk denom is invalid: %v", err)
return fmt.Errorf("allowed cosmos coin erc20 token's sdk denom is invalid: %v", err)
}

if token.Name == "" {
return errors.New("allowed cosomos coin erc20 token's name cannot be empty")
return errors.New("allowed cosmos coin erc20 token's name cannot be empty")
}

if token.Symbol == "" {
return errors.New("allowed cosomos coin erc20 token's symbol cannot be empty")
return errors.New("allowed cosmos coin erc20 token's symbol cannot be empty")
}

// ensure decimals will properly cast to uint8 of erc20 spec
if token.Decimals > math.MaxUint8 {
return fmt.Errorf("allowed cosomos coin erc20 token's decimals must be less than 256, found %d", token.Decimals)
return fmt.Errorf("allowed cosmos coin erc20 token's decimals must be less than 256, found %d", token.Decimals)
}

return nil
Expand Down

0 comments on commit 2ff9996

Please sign in to comment.