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

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package action
     7  
     8  import (
     9  	"encoding/hex"
    10  	"strconv"
    11  
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/action"
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/output"
    17  )
    18  
    19  // Multi-language support
    20  var (
    21  	_stake2ReleaseCmdUses = map[config.Language]string{
    22  		config.English: "release BUCKET_INDEX [DATA]" +
    23  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    24  		config.Chinese: "release 票索引 [数据]" +
    25  			" [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    26  	}
    27  	_stake2ReleaseCmdShorts = map[config.Language]string{
    28  		config.English: "Release bucket on IoTeX blockchain",
    29  		config.Chinese: "撤回IoTeX区块链上的投票",
    30  	}
    31  )
    32  
    33  // _stake2ReleaseCmd represents the stake2 release command
    34  var _stake2ReleaseCmd = &cobra.Command{
    35  	Use:   config.TranslateInLang(_stake2ReleaseCmdUses, config.UILanguage),
    36  	Short: config.TranslateInLang(_stake2ReleaseCmdShorts, config.UILanguage),
    37  	Args:  cobra.RangeArgs(1, 2),
    38  	RunE: func(cmd *cobra.Command, args []string) error {
    39  		cmd.SilenceUsage = true
    40  		err := stake2Release(args)
    41  		return output.PrintError(err)
    42  	},
    43  }
    44  
    45  func init() {
    46  	RegisterWriteCommand(_stake2ReleaseCmd)
    47  }
    48  
    49  func stake2Release(args []string) error {
    50  	bucketIndex, err := strconv.ParseUint(args[0], 10, 64)
    51  	if err != nil {
    52  		return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
    53  	}
    54  
    55  	var data []byte
    56  	if len(args) == 2 {
    57  		data, err = hex.DecodeString(args[1])
    58  		if err != nil {
    59  			return output.NewError(output.ConvertError, "failed to decode data", err)
    60  		}
    61  	}
    62  	gasPriceRau, err := gasPriceInRau()
    63  	if err != nil {
    64  		return output.NewError(0, "failed to get gas price", err)
    65  	}
    66  	sender, err := Signer()
    67  	if err != nil {
    68  		return output.NewError(output.AddressError, "failed to get signer address", err)
    69  	}
    70  	nonce, err := nonce(sender)
    71  	if err != nil {
    72  		return output.NewError(0, "failed to get nonce", err)
    73  	}
    74  	gasLimit := _gasLimitFlag.Value().(uint64)
    75  	if gasLimit == 0 {
    76  		gasLimit = action.ReclaimStakeBaseIntrinsicGas + action.ReclaimStakePayloadGas*uint64(len(data))
    77  	}
    78  	s2r, err := action.NewUnstake(nonce, bucketIndex, data, gasLimit, gasPriceRau)
    79  	if err != nil {
    80  		return output.NewError(output.InstantiationError, "failed to make a Unstake  instance", err)
    81  	}
    82  
    83  	return SendAction(
    84  		(&action.EnvelopeBuilder{}).
    85  			SetNonce(nonce).
    86  			SetGasPrice(gasPriceRau).
    87  			SetGasLimit(gasLimit).
    88  			SetAction(s2r).Build(),
    89  		sender)
    90  }