github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/xrc20transferfrom.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  	_xrc20TransferFromCmdUses = map[config.Language]string{
    21  		config.English: "transferFrom (ALIAS|OWNER_ADDRESS) (ALIAS|RECIPIENT_ADDRESS) AMOUNT -c (ALIAS|CONTRACT_ADDRESS)" +
    22  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    23  		config.Chinese: "transferFrom (别名|所有人地址) (别名|接收人地址) 数量 -c (" +
    24  			"别名|合约地址)" +
    25  			" [-s 签署] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    26  	}
    27  	_xrc20TransferFromCmdShorts = map[config.Language]string{
    28  		config.English: "Send amount of tokens from owner address to target address",
    29  		config.Chinese: "将通证数量从所有者地址发送到目标地址",
    30  	}
    31  )
    32  
    33  // _xrc20TransferFromCmd could transfer from owner address to target address
    34  var _xrc20TransferFromCmd = &cobra.Command{
    35  	Use:   config.TranslateInLang(_xrc20TransferFromCmdUses, config.UILanguage),
    36  	Short: config.TranslateInLang(_xrc20TransferFromCmdShorts, config.UILanguage),
    37  	Args:  cobra.ExactArgs(3),
    38  	RunE: func(cmd *cobra.Command, args []string) error {
    39  		cmd.SilenceUsage = true
    40  		err := transferFrom(args)
    41  		return output.PrintError(err)
    42  	},
    43  }
    44  
    45  func init() {
    46  	RegisterWriteCommand(_xrc20TransferFromCmd)
    47  }
    48  
    49  func transferFrom(args []string) error {
    50  	owner, err := alias.EtherAddress(args[0])
    51  	if err != nil {
    52  		return output.NewError(output.AddressError, "failed to get owner address", err)
    53  	}
    54  	recipient, err := alias.EtherAddress(args[1])
    55  	if err != nil {
    56  		return output.NewError(output.AddressError, "failed to get recipient address", err)
    57  	}
    58  	contract, err := xrc20Contract()
    59  	if err != nil {
    60  		return output.NewError(output.AddressError, "failed to get contract address", err)
    61  	}
    62  	amount, err := parseAmount(contract, args[2])
    63  	if err != nil {
    64  		return output.NewError(0, "failed to parse amount", err)
    65  	}
    66  	bytecode, err := _xrc20ABI.Pack("transferFrom", owner, recipient, amount)
    67  	if err != nil {
    68  		return output.NewError(output.ConvertError, "cannot generate bytecode from given command", err)
    69  	}
    70  	return Execute(contract.String(), big.NewInt(0), bytecode)
    71  }