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