github.com/cosmos/cosmos-sdk@v0.50.10/client/flags/flags.go (about) 1 package flags 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/spf13/cobra" 8 "github.com/spf13/pflag" 9 10 "github.com/cosmos/cosmos-sdk/crypto/keyring" 11 ) 12 13 const ( 14 // DefaultGasAdjustment is applied to gas estimates to avoid tx execution 15 // failures due to state changes that might occur between the tx simulation 16 // and the actual run. 17 DefaultGasAdjustment = 1.0 18 DefaultGasLimit = 200000 19 GasFlagAuto = "auto" 20 21 // DefaultKeyringBackend 22 DefaultKeyringBackend = keyring.BackendOS 23 24 // BroadcastSync defines a tx broadcasting mode where the client waits for 25 // a CheckTx execution response only. 26 BroadcastSync = "sync" 27 // BroadcastAsync defines a tx broadcasting mode where the client returns 28 // immediately. 29 BroadcastAsync = "async" 30 31 // SignModeDirect is the value of the --sign-mode flag for SIGN_MODE_DIRECT 32 SignModeDirect = "direct" 33 // SignModeLegacyAminoJSON is the value of the --sign-mode flag for SIGN_MODE_LEGACY_AMINO_JSON 34 SignModeLegacyAminoJSON = "amino-json" 35 // SignModeDirectAux is the value of the --sign-mode flag for SIGN_MODE_DIRECT_AUX 36 SignModeDirectAux = "direct-aux" 37 // SignModeTextual is the value of the --sign-mode flag for SIGN_MODE_TEXTUAL. 38 SignModeTextual = "textual" 39 // SignModeEIP191 is the value of the --sign-mode flag for SIGN_MODE_EIP_191 40 SignModeEIP191 = "eip-191" 41 ) 42 43 // List of CLI flags 44 const ( 45 FlagHome = "home" 46 FlagKeyringDir = "keyring-dir" 47 FlagUseLedger = "ledger" 48 FlagChainID = "chain-id" 49 FlagNode = "node" 50 FlagGRPC = "grpc-addr" 51 FlagGRPCInsecure = "grpc-insecure" 52 FlagHeight = "height" 53 FlagGasAdjustment = "gas-adjustment" 54 FlagFrom = "from" 55 FlagName = "name" 56 FlagAccountNumber = "account-number" 57 FlagSequence = "sequence" 58 FlagNote = "note" 59 FlagFees = "fees" 60 FlagGas = "gas" 61 FlagGasPrices = "gas-prices" 62 FlagBroadcastMode = "broadcast-mode" 63 FlagDryRun = "dry-run" 64 FlagGenerateOnly = "generate-only" 65 FlagOffline = "offline" 66 FlagOutputDocument = "output-document" // inspired by wget -O 67 FlagSkipConfirmation = "yes" 68 FlagProve = "prove" 69 FlagKeyringBackend = "keyring-backend" 70 FlagPage = "page" 71 FlagLimit = "limit" 72 FlagSignMode = "sign-mode" 73 FlagPageKey = "page-key" 74 FlagOffset = "offset" 75 FlagCountTotal = "count-total" 76 FlagTimeoutHeight = "timeout-height" 77 FlagKeyAlgorithm = "algo" 78 FlagKeyType = "key-type" 79 FlagFeePayer = "fee-payer" 80 FlagFeeGranter = "fee-granter" 81 FlagReverse = "reverse" 82 FlagTip = "tip" 83 FlagAux = "aux" 84 FlagInitHeight = "initial-height" 85 // FlagOutput is the flag to set the output format. 86 // This differs from FlagOutputDocument that is used to set the output file. 87 FlagOutput = "output" 88 // Logging flags 89 FlagLogLevel = "log_level" 90 FlagLogFormat = "log_format" 91 FlagLogNoColor = "log_no_color" 92 ) 93 94 // List of supported output formats 95 const ( 96 OutputFormatJSON = "json" 97 OutputFormatText = "text" 98 ) 99 100 // LineBreak can be included in a command list to provide a blank line 101 // to help with readability 102 var LineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} 103 104 // AddQueryFlagsToCmd adds common flags to a module query command. 105 func AddQueryFlagsToCmd(cmd *cobra.Command) { 106 cmd.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to CometBFT RPC interface for this chain") 107 cmd.Flags().String(FlagGRPC, "", "the gRPC endpoint to use for this chain") 108 cmd.Flags().Bool(FlagGRPCInsecure, false, "allow gRPC over insecure channels, if not the server must use TLS") 109 cmd.Flags().Int64(FlagHeight, 0, "Use a specific height to query state at (this can error if the node is pruning state)") 110 cmd.Flags().StringP(FlagOutput, "o", "text", "Output format (text|json)") 111 112 // some base commands does not require chainID e.g `simd testnet` while subcommands do 113 // hence the flag should not be required for those commands 114 _ = cmd.MarkFlagRequired(FlagChainID) 115 } 116 117 // AddTxFlagsToCmd adds common flags to a module tx command. 118 func AddTxFlagsToCmd(cmd *cobra.Command) { 119 f := cmd.Flags() 120 f.StringP(FlagOutput, "o", OutputFormatJSON, "Output format (text|json)") 121 if cmd.Flag(FlagFrom) == nil { // avoid flag redefinition when it's already been added by AutoCLI 122 f.String(FlagFrom, "", "Name or address of private key with which to sign") 123 } 124 f.Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)") 125 f.Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)") 126 f.String(FlagNote, "", "Note to add a description to the transaction (previously --memo)") 127 f.String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom") 128 f.String(FlagGasPrices, "", "Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom)") 129 f.String(FlagNode, "tcp://localhost:26657", "<host>:<port> to CometBFT rpc interface for this chain") 130 f.Bool(FlagUseLedger, false, "Use a connected Ledger device") 131 f.Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ") 132 f.StringP(FlagBroadcastMode, "b", BroadcastSync, "Transaction broadcasting mode (sync|async)") 133 f.Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it (when enabled, the local Keybase is not accessible)") 134 f.Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase only accessed when providing a key name)") 135 f.Bool(FlagOffline, false, "Offline mode (does not allow any online functionality)") 136 f.BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation") 137 f.String(FlagSignMode, "", "Choose sign mode (direct|amino-json|direct-aux|textual), this is an advanced feature") 138 f.Uint64(FlagTimeoutHeight, 0, "Set a block timeout height to prevent the tx from being committed past a certain height") 139 f.String(FlagFeePayer, "", "Fee payer pays fees for the transaction instead of deducting from the signer") 140 f.String(FlagFeeGranter, "", "Fee granter grants fees for the transaction") 141 f.String(FlagTip, "", "Tip is the amount that is going to be transferred to the fee payer on the target chain. This flag is only valid when used with --aux, and is ignored if the target chain didn't enable the TipDecorator") 142 f.Bool(FlagAux, false, "Generate aux signer data instead of sending a tx") 143 f.String(FlagChainID, "", "The network chain ID") 144 // --gas can accept integers and "auto" 145 f.String(FlagGas, "", fmt.Sprintf("gas limit to set per-transaction; set to %q to calculate sufficient gas automatically. Note: %q option doesn't always report accurate results. Set a valid coin value to adjust the result. Can be used instead of %q. (default %d)", 146 GasFlagAuto, GasFlagAuto, FlagFees, DefaultGasLimit)) 147 148 AddKeyringFlags(f) 149 } 150 151 // AddKeyringFlags sets common keyring flags 152 func AddKeyringFlags(flags *pflag.FlagSet) { 153 flags.String(FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used") 154 flags.String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test|memory)") 155 } 156 157 // AddPaginationFlagsToCmd adds common pagination flags to cmd 158 func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) { 159 cmd.Flags().Uint64(FlagPage, 1, fmt.Sprintf("pagination page of %s to query for. This sets offset to a multiple of limit", query)) 160 cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query)) 161 cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query)) 162 cmd.Flags().Uint64(FlagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query)) 163 cmd.Flags().Bool(FlagCountTotal, false, fmt.Sprintf("count total number of records in %s to query for", query)) 164 cmd.Flags().Bool(FlagReverse, false, "results are sorted in descending order") 165 } 166 167 // GasSetting encapsulates the possible values passed through the --gas flag. 168 type GasSetting struct { 169 Simulate bool 170 Gas uint64 171 } 172 173 func (v *GasSetting) String() string { 174 if v.Simulate { 175 return GasFlagAuto 176 } 177 178 return strconv.FormatUint(v.Gas, 10) 179 } 180 181 // ParseGasSetting parses a string gas value. The value may either be 'auto', 182 // which indicates a transaction should be executed in simulate mode to 183 // automatically find a sufficient gas value, or a string integer. It returns an 184 // error if a string integer is provided which cannot be parsed. 185 func ParseGasSetting(gasStr string) (GasSetting, error) { 186 switch gasStr { 187 case "": 188 return GasSetting{false, DefaultGasLimit}, nil 189 190 case GasFlagAuto: 191 return GasSetting{true, 0}, nil 192 193 default: 194 gas, err := strconv.ParseUint(gasStr, 10, 64) 195 if err != nil { 196 return GasSetting{}, fmt.Errorf("gas must be either integer or %s", GasFlagAuto) 197 } 198 199 return GasSetting{false, gas}, nil 200 } 201 }