github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/stake2withdraw.go (about) 1 // Copyright (c) 2022 IoTeX 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/pkg/errors" 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/iotex-core/action" 16 "github.com/iotexproject/iotex-core/ioctl" 17 "github.com/iotexproject/iotex-core/ioctl/config" 18 ) 19 20 // Multi-language support 21 var ( 22 _stake2WithDrawCmdUses = map[config.Language]string{ 23 config.English: "withdraw BUCKET_INDEX [DATA] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]", 24 config.Chinese: "withdraw 票索引 [数据] [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]", 25 } 26 _stake2WithDrawCmdShorts = map[config.Language]string{ 27 config.English: "Withdraw bucket from IoTeX blockchain", 28 config.Chinese: "提取IoTeX区块链上的投票", 29 } 30 ) 31 32 // NewStake2WithdrawCmd represents the stake2 withdraw command 33 func NewStake2WithdrawCmd(client ioctl.Client) *cobra.Command { 34 use, _ := client.SelectTranslation(_stake2WithDrawCmdUses) 35 short, _ := client.SelectTranslation(_stake2WithDrawCmdShorts) 36 37 cmd := &cobra.Command{ 38 Use: use, 39 Short: short, 40 Args: cobra.RangeArgs(1, 2), 41 RunE: func(cmd *cobra.Command, args []string) error { 42 cmd.SilenceUsage = true 43 bucketIndex, err := strconv.ParseUint(args[0], 10, 64) 44 if err != nil { 45 return errors.Wrap(err, "failed to convert bucket index") 46 } 47 48 var data []byte 49 if len(args) == 2 { 50 data, err = hex.DecodeString(args[1]) 51 if err != nil { 52 return errors.Wrap(err, "failed to decode data") 53 } 54 } 55 56 gasPrice, signer, password, nonce, gasLimit, assumeYes, err := GetWriteCommandFlag(cmd) 57 if err != nil { 58 return err 59 } 60 sender, err := Signer(client, signer) 61 if err != nil { 62 return errors.Wrap(err, "failed to get signed address") 63 } 64 if gasLimit == 0 { 65 gasLimit = action.ReclaimStakeBaseIntrinsicGas + action.ReclaimStakePayloadGas*uint64(len(data)) 66 } 67 gasPriceRau, err := gasPriceInRau(client, gasPrice) 68 if err != nil { 69 return errors.Wrap(err, "failed to get gas price") 70 } 71 nonce, err = checkNonce(client, nonce, sender) 72 if err != nil { 73 return errors.Wrap(err, "failed to get nonce") 74 } 75 s2w, err := action.NewWithdrawStake(nonce, bucketIndex, data, gasLimit, gasPriceRau) 76 if err != nil { 77 return errors.Wrap(err, "failed to make a changeCandidate instance") 78 } 79 return SendAction( 80 client, 81 cmd, 82 (&action.EnvelopeBuilder{}). 83 SetNonce(nonce). 84 SetGasPrice(gasPriceRau). 85 SetGasLimit(gasLimit). 86 SetAction(s2w).Build(), 87 sender, 88 password, 89 nonce, 90 assumeYes, 91 ) 92 }, 93 } 94 RegisterWriteCommand(client, cmd) 95 return cmd 96 }