github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/client/cli/tx_simulate.go (about) 1 package cli 2 3 import ( 4 "strings" 5 6 "github.com/spf13/cobra" 7 8 "github.com/cosmos/cosmos-sdk/client" 9 "github.com/cosmos/cosmos-sdk/client/flags" 10 "github.com/cosmos/cosmos-sdk/client/tx" 11 authclient "github.com/cosmos/cosmos-sdk/x/auth/client" 12 ) 13 14 // GetSimulateCmd returns a command that simulates whether a transaction will be 15 // successful. 16 func GetSimulateCmd() *cobra.Command { 17 cmd := &cobra.Command{ 18 Use: "simulate /path/to/unsigned-tx.json --from keyname", 19 Short: "Simulate the gas usage of a transaction", 20 Long: strings.TrimSpace(`Simulate whether a transaction will be successful: 21 22 - if successful, the simulation result is printed, which includes the gas 23 consumption, message response data, and events emitted; 24 - if unsuccessful, the error message is printed. 25 26 The user must provide the path to a JSON-encoded unsigned transaction, typically 27 generated by any transaction command with the --generate-only flag. It should 28 look like below. Note that the "signer_infos" and "signatures" fields are left 29 empty; they will be auto-populated by dummy data for simulation purpose. 30 31 { 32 "body": { 33 "messages": [ 34 { 35 "@type": "/cosmos.bank.v1beta1.MsgSend", 36 "from_address": "cosmos1...", 37 "to_address": "cosmos1...", 38 "amount": [ 39 { 40 "denom": "utoken", 41 "amount": "12345" 42 } 43 ] 44 } 45 ], 46 "memo": "", 47 "timeout_height": "0", 48 "extension_options": [], 49 "non_critical_extension_options": [] 50 }, 51 "auth_info": { 52 "signer_infos": [], 53 "fee": { 54 "amount": [], 55 "gas_limit": "200000", 56 "payer": "", 57 "granter": "" 58 }, 59 "tip": null 60 }, 61 "signatures": [] 62 } 63 64 The --from flag is mandatory, as the signer account's correct sequence number is 65 necessary for simulation. 66 `), 67 Args: cobra.ExactArgs(1), 68 RunE: func(cmd *cobra.Command, args []string) error { 69 clientCtx, err := client.GetClientTxContext(cmd) 70 if err != nil { 71 return err 72 } 73 74 txf, err := tx.NewFactoryCLI(clientCtx, cmd.Flags()) 75 if err != nil { 76 return err 77 } 78 79 txf, err = txf.Prepare(clientCtx) 80 if err != nil { 81 return err 82 } 83 84 stdTx, err := authclient.ReadTxFromFile(clientCtx, args[0]) 85 if err != nil { 86 return err 87 } 88 89 simRes, _, err := tx.CalculateGas(clientCtx, txf, stdTx.GetMsgs()...) 90 if err != nil { 91 return err 92 } 93 94 return clientCtx.PrintProto(simRes) 95 }, 96 } 97 98 flags.AddTxFlagsToCmd(cmd) 99 100 return cmd 101 }