Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add CLI commands: 1) simulate a transaction, 2) query block results #16887

Merged
merged 8 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add simulate command
  • Loading branch information
larry0x committed Jul 7, 2023
commit 82f818e76729f8e643f8493b4f766ed5efaae28d
1 change: 1 addition & 0 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func txCommand() *cobra.Command {
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
authcmd.GetAuxToFeeCommand(),
authcmd.GetSimulateCmd(),
)

return cmd
Expand Down
1 change: 1 addition & 0 deletions simapp/simd/cmd/root_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func txCommand() *cobra.Command {
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
authcmd.GetAuxToFeeCommand(),
authcmd.GetSimulateCmd(),
)

return cmd
Expand Down
107 changes: 107 additions & 0 deletions x/auth/client/cli/tx_simulate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
)

// GetSimulateCmd returns a command that simulates whether a transaction will be
// successful.
func GetSimulateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "simulate /path/to/tx.json --from keyname",
Short: "Simulate the gas usage of a transaction",
Long: strings.TrimSpace(
fmt.Sprintf(`Simulate whether a transaction will be successful:

- if successful, the simulation result is printed, which includes the gas
consumption, message response data, and events emitted;
- if unsuccessful, the error message is printed.

Example:
$ %s query simulate tx.json --from keyname

Where tx.json is a JSON-encoded unsigned transaction, typically generated by any
transaction command with the --generate-only flag. It shoud look like:

{
"body": {
"messages": [
{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": "cosmos1...",
"to_address": "cosmos1...",
"amount": [
{
"denom": "utoken",
"amount": "12345"
}
]
}
],
"memo": "",
"timeout_height": "0",
"extension_options": [],
"non_critical_extension_options": []
},
"auth_info": {
"signer_infos": [],
"fee": {
"amount": [],
"gas_limit": "200000",
"payer": "",
"granter": ""
},
"tip": null
},
"signatures": []
}

The --from flag is mandatory.
`,
version.AppName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

txf, err := tx.NewFactoryCLI(clientCtx, cmd.Flags())
if err != nil {
return err
}

txf, err = txf.Prepare(clientCtx)
if err != nil {
return err
}

stdTx, err := authclient.ReadTxFromFile(clientCtx, args[0])
if err != nil {
return err
}

simRes, _, err := tx.CalculateGas(clientCtx, txf, stdTx.GetMsgs()...)
if err != nil {
return err
}

return clientCtx.PrintProto(simRes)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}