github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2endorse.go (about)

     1  package action
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/iotexproject/iotex-core/action"
     9  	"github.com/iotexproject/iotex-core/ioctl/config"
    10  	"github.com/iotexproject/iotex-core/ioctl/output"
    11  )
    12  
    13  // Multi-language support
    14  var (
    15  	_stake2EndorseCmdUses = map[config.Language]string{
    16  		config.English: "endorse BUCKET_INDEX" +
    17  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    18  		config.Chinese: "endorse 票索引" +
    19  			" [-s 签署人] [-n NONCE] [-l GAS 限制] [-p GAS 价格] [-P 密码] [-y]",
    20  	}
    21  	_stake2UnEndorseCmdUses = map[config.Language]string{
    22  		config.English: "unendorse BUCKET_INDEX" +
    23  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    24  		config.Chinese: "unendorse 票索引" +
    25  			" [-s 签署人] [-n NONCE] [-l GAS 限制] [-p GAS 价格] [-P 密码] [-y]",
    26  	}
    27  
    28  	_stake2EndorseCmdShorts = map[config.Language]string{
    29  		config.English: "Endorse bucket's candidate on IoTeX blockchain",
    30  		config.Chinese: "在 IoTeX 区块链上背书候选人",
    31  	}
    32  	_stake2UnEndorseCmdShorts = map[config.Language]string{
    33  		config.English: "UnEndorse bucket's candidate on IoTeX blockchain",
    34  		config.Chinese: "在 IoTeX 区块链上撤销背书",
    35  	}
    36  )
    37  
    38  var (
    39  	// _stake2EndorseCmd represents the stake2 transfer command
    40  	_stake2EndorseCmd = &cobra.Command{
    41  		Use:   config.TranslateInLang(_stake2EndorseCmdUses, config.UILanguage),
    42  		Short: config.TranslateInLang(_stake2EndorseCmdShorts, config.UILanguage),
    43  		Args:  cobra.RangeArgs(1, 2),
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			cmd.SilenceUsage = true
    46  			err := stake2Endorse(args)
    47  			return output.PrintError(err)
    48  		},
    49  	}
    50  	_stake2UnEndorseCmd = &cobra.Command{
    51  		Use:   config.TranslateInLang(_stake2UnEndorseCmdUses, config.UILanguage),
    52  		Short: config.TranslateInLang(_stake2UnEndorseCmdShorts, config.UILanguage),
    53  		Args:  cobra.RangeArgs(1, 2),
    54  		RunE: func(cmd *cobra.Command, args []string) error {
    55  			cmd.SilenceUsage = true
    56  			err := stake2UnEndorse(args)
    57  			return output.PrintError(err)
    58  		},
    59  	}
    60  )
    61  
    62  func init() {
    63  	RegisterWriteCommand(_stake2EndorseCmd)
    64  	RegisterWriteCommand(_stake2UnEndorseCmd)
    65  }
    66  
    67  func stake2UnEndorse(args []string) error {
    68  	bucketIndex, err := strconv.ParseUint(args[0], 10, 64)
    69  	if err != nil {
    70  		return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
    71  	}
    72  
    73  	return doEndorsement(bucketIndex, false)
    74  }
    75  
    76  func stake2Endorse(args []string) error {
    77  	bucketIndex, err := strconv.ParseUint(args[0], 10, 64)
    78  	if err != nil {
    79  		return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
    80  	}
    81  
    82  	return doEndorsement(bucketIndex, true)
    83  }
    84  
    85  func doEndorsement(bucketIndex uint64, isEndorse bool) error {
    86  	sender, err := Signer()
    87  	if err != nil {
    88  		return output.NewError(output.AddressError, "failed to get signed address", err)
    89  	}
    90  
    91  	gasLimit := _gasLimitFlag.Value().(uint64)
    92  	if gasLimit == 0 {
    93  		gasLimit = action.CandidateEndorsementBaseIntrinsicGas
    94  	}
    95  
    96  	gasPriceRau, err := gasPriceInRau()
    97  	if err != nil {
    98  		return output.NewError(0, "failed to get gas price", err)
    99  	}
   100  	nonce, err := nonce(sender)
   101  	if err != nil {
   102  		return output.NewError(0, "failed to get nonce ", err)
   103  	}
   104  	s2t := action.NewCandidateEndorsement(nonce, gasLimit, gasPriceRau, bucketIndex, isEndorse)
   105  	return SendAction(
   106  		(&action.EnvelopeBuilder{}).
   107  			SetNonce(nonce).
   108  			SetGasPrice(gasPriceRau).
   109  			SetGasLimit(gasLimit).
   110  			SetAction(s2t).Build(),
   111  		sender)
   112  }