github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/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/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/ioctl"
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/newcmd/alias"
    17  )
    18  
    19  // Multi-language support
    20  var (
    21  	_xrc20TransferFromCmdUses = map[config.Language]string{
    22  		config.English: "transferFrom (ALIAS|OWNER_ADDRESS) (ALIAS|RECIPIENT_ADDRESS) AMOUNT -c (ALIAS|CONTRACT_ADDRESS)" +
    23  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    24  		config.Chinese: "transferFrom (别名|所有人地址) (别名|接收人地址) 数量 -c (别名|合约地址) [-s 签署] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    25  	}
    26  	_xrc20TransferFromCmdShorts = map[config.Language]string{
    27  		config.English: "Send amount of tokens from owner address to target address",
    28  		config.Chinese: "将通证数量从所有者地址发送到目标地址",
    29  	}
    30  )
    31  
    32  // NewXrc20TransferFromCmd represent xrc20TransferFrom command
    33  func NewXrc20TransferFromCmd(client ioctl.Client) *cobra.Command {
    34  	cmd := &cobra.Command{
    35  		Use:   selectTranslation(client, _xrc20TransferFromCmdUses),
    36  		Short: selectTranslation(client, _xrc20TransferFromCmdShorts),
    37  		Args:  cobra.ExactArgs(3),
    38  		RunE: func(cmd *cobra.Command, args []string) error {
    39  			cmd.SilenceUsage = true
    40  			owner, err := alias.EtherAddress(client, args[0])
    41  			if err != nil {
    42  				return errors.Wrap(err, "failed to get owner address")
    43  			}
    44  			recipient, err := alias.EtherAddress(client, args[1])
    45  			if err != nil {
    46  				return errors.Wrap(err, "failed to get recipient address")
    47  			}
    48  			contractAddr, err := cmd.Flags().GetString("contract-address")
    49  			if err != nil {
    50  				return errors.Wrap(err, "failed to get contract address")
    51  			}
    52  			contract, err := xrc20Contract(client, contractAddr)
    53  			if err != nil {
    54  				return errors.Wrap(err, "failed to get contract address")
    55  			}
    56  			amount, err := parseAmount(client, cmd, contract, args[2])
    57  			if err != nil {
    58  				return errors.Wrap(err, "failed to parse amount")
    59  			}
    60  			bytecode, err := xrc20abi.Pack("transferFrom", owner, recipient, amount)
    61  			if err != nil {
    62  				return errors.Wrap(err, "cannot generate bytecode from given command")
    63  			}
    64  			gasPrice, signer, password, nonce, gasLimit, assumeYes, err := GetWriteCommandFlag(cmd)
    65  			if err != nil {
    66  				return err
    67  			}
    68  			return Execute(client, cmd, contract.String(), big.NewInt(0), bytecode, gasPrice, signer, password, nonce, gasLimit, assumeYes)
    69  		},
    70  	}
    71  	RegisterWriteCommand(client, cmd)
    72  	return cmd
    73  }