github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/actiontransfer.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/cmd/account" 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 _actionTransferCmdShorts = map[config.Language]string{ 23 config.English: "Transfer tokens on IoTeX blokchain", 24 config.Chinese: "在IoTeX区块链上转移令牌", 25 } 26 _actionTransferCmdUses = map[config.Language]string{ 27 config.English: "transfer (ALIAS|RECIPIENT_ADDRESS) AMOUNT_IOTX [DATA] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]", 28 config.Chinese: "transfer (别名|接收人地址) IOTX数量 [数据] [-s 签署人] [-n NONCE] [-l GAS限制] [-P GAS" + 29 "价格] [-P 密码] [-y]", 30 } 31 ) 32 33 // _actionTransferCmd represents the action transfer command 34 var _actionTransferCmd = &cobra.Command{ 35 Use: config.TranslateInLang(_actionTransferCmdUses, config.UILanguage), 36 Short: config.TranslateInLang(_actionTransferCmdShorts, config.UILanguage), 37 Args: cobra.RangeArgs(2, 3), 38 RunE: func(cmd *cobra.Command, args []string) error { 39 cmd.SilenceUsage = true 40 err := transfer(args) 41 return output.PrintError(err) 42 }, 43 } 44 45 func init() { 46 RegisterWriteCommand(_actionTransferCmd) 47 } 48 49 func transfer(args []string) error { 50 recipient, err := util.Address(args[0]) 51 if err != nil { 52 return output.NewError(output.AddressError, "failed to get recipient address", err) 53 } 54 55 accountMeta, err := account.GetAccountMeta(recipient) 56 if err != nil { 57 return output.NewError(0, "failed to get account meta", err) 58 } 59 if accountMeta.IsContract { 60 return output.NewError(output.RuntimeError, "use 'ioctl contract' command instead", err) 61 } 62 63 amount, err := util.StringToRau(args[1], util.IotxDecimalNum) 64 if err != nil { 65 return output.NewError(output.ConvertError, "invalid amount", err) 66 } 67 var payload []byte 68 if len(args) == 3 { 69 payload, err = hex.DecodeString(args[2]) 70 if err != nil { 71 return output.NewError(output.ConvertError, "failed to decode data", err) 72 } 73 } 74 sender, err := Signer() 75 if err != nil { 76 return output.NewError(output.AddressError, "failed to get signed address", err) 77 } 78 gasLimit := _gasLimitFlag.Value().(uint64) 79 if gasLimit == 0 { 80 gasLimit = action.TransferBaseIntrinsicGas + 81 action.TransferPayloadGas*uint64(len(payload)) 82 } 83 gasPriceRau, err := gasPriceInRau() 84 if err != nil { 85 return output.NewError(0, "failed to get gas price", err) 86 } 87 nonce, err := nonce(sender) 88 if err != nil { 89 return output.NewError(0, "failed to get nonce ", err) 90 } 91 tx, err := action.NewTransfer(nonce, amount, 92 recipient, payload, gasLimit, gasPriceRau) 93 if err != nil { 94 return output.NewError(output.InstantiationError, "failed to make a Transfer instance", err) 95 } 96 return SendAction( 97 (&action.EnvelopeBuilder{}). 98 SetNonce(nonce). 99 SetGasPrice(gasPriceRau). 100 SetGasLimit(gasLimit). 101 SetAction(tx).Build(), 102 sender, 103 ) 104 105 }