github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2renew.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 _stake2RenewCmdUses = map[config.Language]string{ 22 config.English: "renew BUCKET_INDEX STAKE_DURATION [DATA] [--auto-stake]" + 23 " [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]", 24 config.Chinese: "renew 票索引 投票持续时间 [数据] [--auto-stake]" + 25 " [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]", 26 } 27 28 _stake2RenewCmdShorts = map[config.Language]string{ 29 config.English: "Renew bucket on IoTeX blockchain", 30 config.Chinese: "更新IoTeX区块链上的投票", 31 } 32 ) 33 34 // _stake2RenewCmd represents the stake2 renew command 35 var _stake2RenewCmd = &cobra.Command{ 36 Use: config.TranslateInLang(_stake2RenewCmdUses, config.UILanguage), 37 Short: config.TranslateInLang(_stake2RenewCmdShorts, config.UILanguage), 38 Args: cobra.RangeArgs(2, 3), 39 RunE: func(cmd *cobra.Command, args []string) error { 40 cmd.SilenceUsage = true 41 err := stake2Renew(args) 42 return output.PrintError(err) 43 }} 44 45 func init() { 46 RegisterWriteCommand(_stake2RenewCmd) 47 _stake2RenewCmd.Flags().BoolVar(&_stake2AutoStake, "auto-stake", false, 48 config.TranslateInLang(_stake2FlagAutoStakeUsages, config.UILanguage)) 49 } 50 51 func stake2Renew(args []string) error { 52 bucketIndex, err := strconv.ParseUint(args[0], 10, 64) 53 if err != nil { 54 return output.NewError(output.ConvertError, "failed to convert bucket index", nil) 55 } 56 57 stakeDuration, err := parseStakeDuration(args[1]) 58 if err != nil { 59 return output.NewError(0, "", err) 60 } 61 duration := uint32(stakeDuration.Uint64()) 62 63 var payload []byte 64 if len(args) == 3 { 65 payload, err = hex.DecodeString(args[2]) 66 if err != nil { 67 return output.NewError(output.ConvertError, "failed to decode data", err) 68 } 69 } 70 71 sender, err := Signer() 72 if err != nil { 73 return output.NewError(output.AddressError, "failed to get signed address", err) 74 } 75 76 gasLimit := _gasLimitFlag.Value().(uint64) 77 if gasLimit == 0 { 78 gasLimit = action.RestakeBaseIntrinsicGas + 79 action.RestakePayloadGas*uint64(len(payload)) 80 } 81 82 gasPriceRau, err := gasPriceInRau() 83 if err != nil { 84 return output.NewError(0, "failed to get gas price", err) 85 } 86 nonce, err := nonce(sender) 87 if err != nil { 88 return output.NewError(0, "failed to get nonce ", err) 89 } 90 s2r, err := action.NewRestake(nonce, bucketIndex, duration, _stake2AutoStake, payload, gasLimit, gasPriceRau) 91 if err != nil { 92 return output.NewError(output.InstantiationError, "failed to make a restake instance", err) 93 } 94 return SendAction( 95 (&action.EnvelopeBuilder{}). 96 SetNonce(nonce). 97 SetGasPrice(gasPriceRau). 98 SetGasLimit(gasLimit). 99 SetAction(s2r).Build(), 100 sender) 101 }