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

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