github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/xrc20transfer.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  	"math/big"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/iotexproject/iotex-core/ioctl/cmd/alias"
    14  	"github.com/iotexproject/iotex-core/ioctl/config"
    15  	"github.com/iotexproject/iotex-core/ioctl/output"
    16  )
    17  
    18  // Multi-language support
    19  var (
    20  	_xrc20TransferCmdUses = map[config.Language]string{
    21  		config.English: "transfer (ALIAS|TARGET_ADDRESS) AMOUNT -c ALIAS|CONTRACT_ADDRESS [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    22  		config.Chinese: "transfer (别名|目标地址) 数量 -c 别名|合约地址 [-s 签署人" +
    23  			"] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    24  	}
    25  	_xrc20TransferCmdShorts = map[config.Language]string{
    26  		config.English: "Transfer token to the target address",
    27  		config.Chinese: "将通证转移至目标地址",
    28  	}
    29  )
    30  
    31  // _xrc20TransferCmd could do transfer action
    32  var _xrc20TransferCmd = &cobra.Command{
    33  	Use:   config.TranslateInLang(_xrc20TransferCmdUses, config.UILanguage),
    34  	Short: config.TranslateInLang(_xrc20TransferCmdShorts, config.UILanguage),
    35  	Args:  cobra.ExactArgs(2),
    36  	RunE: func(cmd *cobra.Command, args []string) error {
    37  		cmd.SilenceUsage = true
    38  		err := xrc20Transfer(args)
    39  		return output.PrintError(err)
    40  	},
    41  }
    42  
    43  func init() {
    44  	RegisterWriteCommand(_xrc20TransferCmd)
    45  }
    46  
    47  func xrc20Transfer(args []string) error {
    48  	recipient, err := alias.EtherAddress(args[0])
    49  	if err != nil {
    50  		return output.NewError(output.AddressError, "failed to get recipient address", err)
    51  	}
    52  	contract, err := xrc20Contract()
    53  	if err != nil {
    54  		return output.NewError(output.AddressError, "failed to get contract address", err)
    55  	}
    56  	amount, err := parseAmount(contract, args[1])
    57  	if err != nil {
    58  		return output.NewError(0, "failed to parse amount", err)
    59  	}
    60  	bytecode, err := _xrc20ABI.Pack("transfer", recipient, amount)
    61  	if err != nil {
    62  		return output.NewError(output.ConvertError, "cannot generate bytecode from given command", err)
    63  	}
    64  	return Execute(contract.String(), big.NewInt(0), bytecode)
    65  }