github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2register.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 11 "github.com/spf13/cobra" 12 13 "github.com/iotexproject/iotex-core/action" 14 "github.com/iotexproject/iotex-core/ioctl/config" 15 "github.com/iotexproject/iotex-core/ioctl/output" 16 "github.com/iotexproject/iotex-core/ioctl/util" 17 ) 18 19 // Multi-language support 20 var ( 21 _registerCmdUses = map[config.Language]string{ 22 config.English: "register NAME (ALIAS|OPERATOR_ADDRESS) (ALIAS|REWARD_ADDRESS) (ALIAS|OWNER_ADDRESS) AMOUNT_IOTX STAKE_DURATION [DATA] [--auto-stake] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]", 23 config.Chinese: "register 名字 (别名|操作者地址)(别名|奖励地址)(别名|所有者地址)IOTX数量 质押持续时间 [数据] [--auto-stake] [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]", 24 } 25 26 _registerCmdShorts = map[config.Language]string{ 27 config.English: "Register a candidate", 28 config.Chinese: "在IoTeX区块链上注册候选人", 29 } 30 ) 31 32 // _stake2RegisterCmd represents the stake2 register a candidate command 33 var _stake2RegisterCmd = &cobra.Command{ 34 Use: config.TranslateInLang(_registerCmdUses, config.UILanguage), 35 Short: config.TranslateInLang(_registerCmdShorts, config.UILanguage), 36 Args: cobra.RangeArgs(6, 7), 37 RunE: func(cmd *cobra.Command, args []string) error { 38 cmd.SilenceUsage = true 39 err := register(args) 40 return output.PrintError(err) 41 }, 42 } 43 44 func init() { 45 RegisterWriteCommand(_stake2RegisterCmd) 46 _stake2RegisterCmd.Flags().BoolVar(&_stake2AutoStake, "auto-stake", false, config.TranslateInLang(_stake2FlagAutoStakeUsages, config.UILanguage)) 47 } 48 49 func register(args []string) error { 50 name := args[0] 51 if !action.IsValidCandidateName(name) { 52 return output.NewError(output.ValidationError, "", action.ErrInvalidCanName) 53 } 54 55 operatorAddrStr, err := util.Address(args[1]) 56 if err != nil { 57 return output.NewError(output.AddressError, "failed to get operator address", err) 58 } 59 rewardAddrStr, err := util.Address(args[2]) 60 if err != nil { 61 return output.NewError(output.AddressError, "failed to get reward address", err) 62 } 63 ownerAddrStr, err := util.Address(args[3]) 64 if err != nil { 65 return output.NewError(output.AddressError, "failed to get owner address", err) 66 } 67 68 amountInRau, err := util.StringToRau(args[4], util.IotxDecimalNum) 69 if err != nil { 70 return output.NewError(output.ConvertError, "invalid amount", err) 71 } 72 73 stakeDuration, err := parseStakeDuration(args[5]) 74 if err != nil { 75 return output.NewError(0, "", err) 76 } 77 duration := uint32(stakeDuration.Uint64()) 78 79 var payload []byte 80 if len(args) == 7 { 81 payload, err = hex.DecodeString(args[6]) 82 if err != nil { 83 return output.NewError(output.ConvertError, "failed to decode data", err) 84 } 85 } 86 87 sender, err := Signer() 88 if err != nil { 89 return output.NewError(output.AddressError, "failed to get signed address", err) 90 } 91 92 gasLimit := _gasLimitFlag.Value().(uint64) 93 if gasLimit == 0 { 94 gasLimit = action.CandidateRegisterBaseIntrinsicGas + 95 action.CandidateRegisterPayloadGas*uint64(len(payload)) 96 } 97 98 gasPriceRau, err := gasPriceInRau() 99 if err != nil { 100 return output.NewError(0, "failed to get gas price", err) 101 } 102 nonce, err := nonce(sender) 103 if err != nil { 104 return output.NewError(0, "failed to get nonce ", err) 105 } 106 cr, err := action.NewCandidateRegister(nonce, name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountInRau.String(), duration, _stake2AutoStake, payload, gasLimit, gasPriceRau) 107 108 if err != nil { 109 return output.NewError(output.InstantiationError, "failed to make a candidateRegister instance", err) 110 } 111 112 return SendAction( 113 (&action.EnvelopeBuilder{}). 114 SetNonce(nonce). 115 SetGasPrice(gasPriceRau). 116 SetGasLimit(gasLimit). 117 SetAction(cr).Build(), 118 sender) 119 }