Skip to content

Commit

Permalink
add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 9, 2024
1 parent c91720b commit 81bbd02
Show file tree
Hide file tree
Showing 24 changed files with 4,166 additions and 647 deletions.
614 changes: 593 additions & 21 deletions api/minievm/evm/v1/auth.pulsar.go

Large diffs are not rendered by default.

1,313 changes: 1,036 additions & 277 deletions api/minievm/evm/v1/query.pulsar.go

Large diffs are not rendered by default.

1,339 changes: 1,250 additions & 89 deletions api/minievm/evm/v1/tx.pulsar.go

Large diffs are not rendered by default.

43 changes: 41 additions & 2 deletions api/minievm/evm/v1/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion proto/minievm/evm/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,20 @@ message QueryCallRequest {
// hex encoded call input
string input = 3;
// whether to trace the call
bool with_trace = 4;
// `nil` means no trace
TraceOptions trace_options = 4;
}

// TraceOption is the option for tracing
message TraceOptions {
// whether to trace memory
bool with_memory = 1;
// whether to trace stack
bool with_stack = 2;
// wtether to trace storage
bool with_storage = 3;
// whether to return data trace
bool with_return_data = 4;
}

// QueryCallResponse is the response type for the Query/Call RPC
Expand Down
88 changes: 88 additions & 0 deletions x/evm/autocli.go
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
package evm

import (
"fmt"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"

"github.com/cosmos/cosmos-sdk/version"
evmv1 "github.com/initia-labs/minievm/api/minievm/evm/v1"
)

// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: &autocliv1.ServiceCommandDescriptor{
Service: evmv1.Query_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Code",
Use: "code [contract_addr]",
Short: "Query contract code bytes",
Example: fmt.Sprintf("%s query evm code 0x1", version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "contract_addr"},
},
},
{
RpcMethod: "State",
Use: "state [contract_addr] [key]",
Short: "Query contract state",
Example: fmt.Sprintf("%s query evm state 0x1 0x123...", version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "contract_addr"},
{ProtoField: "key"},
},
},
{
RpcMethod: "ContractAddrByDenom",
Use: "contract-addr-by-denom [denom]",
Short: "Query corresponding contract address by denom",
Alias: []string{"contract-addr"},
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "denom"},
},
},
{
RpcMethod: "Denom",
Use: "denom [contract_addr]",
Short: "Query corresponding denom by contract address",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "contract_addr"},
},
},
{
RpcMethod: "Params",
Use: "params",
Short: "Query the evm module params",
},
{
RpcMethod: "Call",
Skip: true,
},
},
EnhanceCustomCommand: true, // We still have manual commands in evm that we want to keep
},
Tx: &autocliv1.ServiceCommandDescriptor{
Service: evmv1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "MsgCreate",
Skip: true,
},
{
RpcMethod: "MsgCreate2",
Skip: true,
},
{
RpcMethod: "MsgCall",
Skip: true,
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
},
},
EnhanceCustomCommand: false, // use custom commands only until v0.51
},
}
}
23 changes: 23 additions & 0 deletions x/evm/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
flag "github.com/spf13/pflag"
)

const (
FlagTrace = "trace"
FlagWithStorage = "with-storage"
FlagWithMemory = "with-memory"
FlagWithStack = "with-stack"
FlagWithReturnData = "with-return-data"
)

func FlagTraceOptions() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Bool(FlagTrace, false, `Trace the execution of the transaction`)
fs.Bool(FlagWithStorage, false, `Trace the storage of the contract`)
fs.Bool(FlagWithMemory, false, `Trace the memory of the contract`)
fs.Bool(FlagWithStack, false, `Trace the stack of the contract`)
fs.Bool(FlagWithReturnData, false, `Trace the return data of the contract`)
return fs
}
108 changes: 108 additions & 0 deletions x/evm/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cli

import (
"context"

"cosmossdk.io/core/address"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"

"github.com/initia-labs/minievm/x/evm/types"
)

func GetQueryCmd(ac address.Codec) *cobra.Command {
queryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the move module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
queryCmd.AddCommand(
GetCmdCall(ac),
)
return queryCmd
}

func GetCmdCall(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "call [sender] [contract-addr] [input-hex-string]",
Short: "Call deployed evm contract",
Long: "Call deployed evm contract",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

_, err = ac.StringToBytes(args[0])
if err != nil {
return err
}

_, err = types.ContractAddressFromString(ac, args[1])
if err != nil {
return err
}

_, err = hexutil.Decode(args[2])
if err != nil {
return err
}

trace, err := cmd.Flags().GetBool(FlagTrace)
if err != nil {
return err
}

var traceOption *types.TraceOptions
if trace {
withMemory, err := cmd.Flags().GetBool(FlagWithMemory)
if err != nil {
return err
}
withStack, err := cmd.Flags().GetBool(FlagWithStack)
if err != nil {
return err
}
withStorage, err := cmd.Flags().GetBool(FlagWithStorage)
if err != nil {
return err
}
withReturnData, err := cmd.Flags().GetBool(FlagWithReturnData)
if err != nil {
return err
}
traceOption = &types.TraceOptions{
WithMemory: withMemory,
WithStack: withStack,
WithStorage: withStorage,
WithReturnData: withReturnData,
}
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Call(
context.Background(),
&types.QueryCallRequest{
Sender: args[0],
ContractAddr: args[1],
Input: args[2],
TraceOptions: traceOption,
},
)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
cmd.Flags().AddFlagSet(FlagTraceOptions())
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
Loading

0 comments on commit 81bbd02

Please sign in to comment.