github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2Transfer.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 "github.com/iotexproject/iotex-core/ioctl/util" 18 ) 19 20 // Multi-language support 21 var ( 22 _stake2TransferCmdUses = map[config.Language]string{ 23 config.English: "transfer (ALIAS|VOTE_ADDRESS) BUCKET_INDEX [DATA]" + 24 " [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]", 25 config.Chinese: "transfer (别名|投票地址) 票索引 [数据]" + 26 " [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]", 27 } 28 29 _stake2TransferCmdShorts = map[config.Language]string{ 30 config.English: "Transfer bucket ownership on IoTeX blockchain", 31 config.Chinese: "在IoTeX区块链上转移投票所有权", 32 } 33 ) 34 35 // _stake2TransferCmd represents the stake2 transfer command 36 var _stake2TransferCmd = &cobra.Command{ 37 Use: config.TranslateInLang(_stake2TransferCmdUses, config.UILanguage), 38 Short: config.TranslateInLang(_stake2TransferCmdShorts, config.UILanguage), 39 Args: cobra.RangeArgs(2, 3), 40 RunE: func(cmd *cobra.Command, args []string) error { 41 cmd.SilenceUsage = true 42 err := stake2Transfer(args) 43 return output.PrintError(err) 44 45 }, 46 } 47 48 func init() { 49 RegisterWriteCommand(_stake2TransferCmd) 50 } 51 52 func stake2Transfer(args []string) error { 53 voterAddrStr, err := util.Address(args[0]) 54 if err != nil { 55 return output.NewError(output.AddressError, "failed to get voter address", err) 56 } 57 58 bucketIndex, err := strconv.ParseUint(args[1], 10, 64) 59 if err != nil { 60 return output.NewError(output.ConvertError, "failed to convert bucket index", nil) 61 } 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.MoveStakeBaseIntrinsicGas + 79 action.MoveStakePayloadGas*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 s2t, err := action.NewTransferStake(nonce, voterAddrStr, bucketIndex, payload, gasLimit, gasPriceRau) 91 if err != nil { 92 return output.NewError(output.InstantiationError, "failed to make a transferStake instance", err) 93 } 94 return SendAction( 95 (&action.EnvelopeBuilder{}). 96 SetNonce(nonce). 97 SetGasPrice(gasPriceRau). 98 SetGasLimit(gasLimit). 99 SetAction(s2t).Build(), 100 sender) 101 }