github.com/Finschia/finschia-sdk@v0.48.1/client/flags/flags.go (about)

     1  package flags
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	ostcli "github.com/Finschia/ostracon/libs/cli"
    10  
    11  	"github.com/Finschia/finschia-sdk/crypto/keyring"
    12  )
    13  
    14  const (
    15  	// DefaultGasAdjustment is applied to gas estimates to avoid tx execution
    16  	// failures due to state changes that might occur between the tx simulation
    17  	// and the actual run.
    18  	DefaultGasAdjustment = 1.0
    19  	DefaultGasLimit      = 200000
    20  	GasFlagAuto          = "auto"
    21  
    22  	// DefaultKeyringBackend
    23  	DefaultKeyringBackend = keyring.BackendOS
    24  
    25  	// BroadcastBlock defines a tx broadcasting mode where the client waits for
    26  	// the tx to be committed in a block.
    27  	BroadcastBlock = "block"
    28  	// BroadcastSync defines a tx broadcasting mode where the client waits for
    29  	// a CheckTx execution response only.
    30  	BroadcastSync = "sync"
    31  	// BroadcastAsync defines a tx broadcasting mode where the client returns
    32  	// immediately.
    33  	BroadcastAsync = "async"
    34  
    35  	// SignModeDirect is the value of the --sign-mode flag for SIGN_MODE_DIRECT
    36  	SignModeDirect = "direct"
    37  	// SignModeLegacyAminoJSON is the value of the --sign-mode flag for SIGN_MODE_LEGACY_AMINO_JSON
    38  	SignModeLegacyAminoJSON = "amino-json"
    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             = ostcli.HomeFlag
    46  	FlagKeyringDir       = "keyring-dir"
    47  	FlagUseLedger        = "ledger"
    48  	FlagChainID          = "chain-id"
    49  	FlagNode             = "node"
    50  	FlagHeight           = "height"
    51  	FlagGasAdjustment    = "gas-adjustment"
    52  	FlagFrom             = "from"
    53  	FlagName             = "name"
    54  	FlagAccountNumber    = "account-number"
    55  	FlagSequence         = "sequence"
    56  	FlagNote             = "note"
    57  	FlagFees             = "fees"
    58  	FlagGas              = "gas"
    59  	FlagGasPrices        = "gas-prices"
    60  	FlagBroadcastMode    = "broadcast-mode"
    61  	FlagDryRun           = "dry-run"
    62  	FlagGenerateOnly     = "generate-only"
    63  	FlagOffline          = "offline"
    64  	FlagOutputDocument   = "output-document" // inspired by wget -O
    65  	FlagSkipConfirmation = "yes"
    66  	FlagProve            = "prove"
    67  	FlagKeyringBackend   = "keyring-backend"
    68  	FlagPage             = "page"
    69  	FlagLimit            = "limit"
    70  	FlagSignMode         = "sign-mode"
    71  	FlagPageKey          = "page-key"
    72  	FlagOffset           = "offset"
    73  	FlagCountTotal       = "count-total"
    74  	FlagTimeoutHeight    = "timeout-height"
    75  	FlagKeyAlgorithm     = "algo"
    76  	FlagFeeAccount       = "fee-account"
    77  	FlagReverse          = "reverse"
    78  
    79  	// Tendermint logging flags
    80  	FlagLogLevel      = "log_level"
    81  	FlagLogFormat     = "log_format"
    82  	FlagLogPath       = "log_path"
    83  	FlagLogMaxAge     = "log_max_age"
    84  	FlagLogMaxSize    = "log_max_size"
    85  	FlagLogMaxBackups = "log_max_backups"
    86  )
    87  
    88  // LineBreak can be included in a command list to provide a blank line
    89  // to help with readability
    90  var LineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}}
    91  
    92  // AddQueryFlagsToCmd adds common flags to a module query command.
    93  func AddQueryFlagsToCmd(cmd *cobra.Command) {
    94  	cmd.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to Tendermint RPC interface for this chain")
    95  	cmd.Flags().Int64(FlagHeight, 0, "Use a specific height to query state at (this can error if the node is pruning state)")
    96  	cmd.Flags().StringP(ostcli.OutputFlag, "o", "text", "Output format (text|json)")
    97  
    98  	cmd.MarkFlagRequired(FlagChainID)
    99  }
   100  
   101  // AddTxFlagsToCmd adds common flags to a module tx command.
   102  func AddTxFlagsToCmd(cmd *cobra.Command) {
   103  	cmd.Flags().StringP(ostcli.OutputFlag, "o", "json", "Output format (text|json)")
   104  	cmd.Flags().String(FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used")
   105  	cmd.Flags().String(FlagFrom, "", "Name or address of private key with which to sign")
   106  	cmd.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)")
   107  	cmd.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)")
   108  	cmd.Flags().String(FlagNote, "", "Note to add a description to the transaction (previously --memo)")
   109  	cmd.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom")
   110  	cmd.Flags().String(FlagGasPrices, "", "Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom)")
   111  	cmd.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to ostracon rpc interface for this chain")
   112  	cmd.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device")
   113  	cmd.Flags().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 ")
   114  	cmd.Flags().StringP(FlagBroadcastMode, "b", BroadcastSync, "Transaction broadcasting mode (sync|async|block)")
   115  	cmd.Flags().Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it")
   116  	cmd.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible)")
   117  	cmd.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality")
   118  	cmd.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation")
   119  	cmd.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test|memory)")
   120  	cmd.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json), this is an advanced feature")
   121  	cmd.Flags().Uint64(FlagTimeoutHeight, 0, "Set a block timeout height to prevent the tx from being committed past a certain height")
   122  	cmd.Flags().String(FlagFeeAccount, "", "Fee account pays fees for the transaction instead of deducting from the signer")
   123  
   124  	// --gas can accept integers and "auto"
   125  	cmd.Flags().String(FlagGas, "", fmt.Sprintf("gas limit to set per-transaction; set to %q to calculate sufficient gas automatically (default %d)", GasFlagAuto, DefaultGasLimit))
   126  
   127  	cmd.MarkFlagRequired(FlagChainID)
   128  }
   129  
   130  // AddPaginationFlagsToCmd adds common pagination flags to cmd
   131  func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) {
   132  	cmd.Flags().Uint64(FlagPage, 1, fmt.Sprintf("pagination page of %s to query for. This sets offset to a multiple of limit", query))
   133  	cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query))
   134  	cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query))
   135  	cmd.Flags().Uint64(FlagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query))
   136  	cmd.Flags().Bool(FlagCountTotal, false, fmt.Sprintf("count total number of records in %s to query for", query))
   137  	cmd.Flags().Bool(FlagReverse, false, "results are sorted in descending order")
   138  }
   139  
   140  // GasSetting encapsulates the possible values passed through the --gas flag.
   141  type GasSetting struct {
   142  	Simulate bool
   143  	Gas      uint64
   144  }
   145  
   146  func (v *GasSetting) String() string {
   147  	if v.Simulate {
   148  		return GasFlagAuto
   149  	}
   150  
   151  	return strconv.FormatUint(v.Gas, 10)
   152  }
   153  
   154  // ParseGasSetting parses a string gas value. The value may either be 'auto',
   155  // which indicates a transaction should be executed in simulate mode to
   156  // automatically find a sufficient gas value, or a string integer. It returns an
   157  // error if a string integer is provided which cannot be parsed.
   158  func ParseGasSetting(gasStr string) (GasSetting, error) {
   159  	switch gasStr {
   160  	case "":
   161  		return GasSetting{false, DefaultGasLimit}, nil
   162  
   163  	case GasFlagAuto:
   164  		return GasSetting{true, 0}, nil
   165  
   166  	default:
   167  		gas, err := strconv.ParseUint(gasStr, 10, 64)
   168  		if err != nil {
   169  			return GasSetting{}, fmt.Errorf("gas must be either integer or %s", GasFlagAuto)
   170  		}
   171  
   172  		return GasSetting{false, gas}, nil
   173  	}
   174  }