github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/actionclaim.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  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/iotexproject/iotex-core/action"
    13  	"github.com/iotexproject/iotex-core/ioctl"
    14  	"github.com/iotexproject/iotex-core/ioctl/config"
    15  	"github.com/iotexproject/iotex-core/ioctl/util"
    16  )
    17  
    18  // Multi-language support
    19  var (
    20  	_claimCmdShorts = map[config.Language]string{
    21  		config.English: "Claim rewards from rewarding fund",
    22  		config.Chinese: "从奖励基金中获取奖励",
    23  	}
    24  	_claimCmdUses = map[config.Language]string{
    25  		config.English: "claim AMOUNT_IOTX [DATA] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    26  		config.Chinese: "claim IOTX数量 [数据] [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    27  	}
    28  )
    29  
    30  // NewActionClaimCmd represents the action claim command
    31  func NewActionClaimCmd(client ioctl.Client) *cobra.Command {
    32  	use, _ := client.SelectTranslation(_claimCmdUses)
    33  	short, _ := client.SelectTranslation(_claimCmdShorts)
    34  
    35  	cmd := &cobra.Command{
    36  		Use:   use,
    37  		Short: short,
    38  		Args:  cobra.RangeArgs(1, 2),
    39  		RunE: func(cmd *cobra.Command, args []string) error {
    40  			cmd.SilenceUsage = true
    41  			amount, err := util.StringToRau(args[0], util.IotxDecimalNum)
    42  			if err != nil {
    43  				return errors.Wrap(err, "invalid amount")
    44  			}
    45  			payload := make([]byte, 0)
    46  			if len(args) == 2 {
    47  				payload = []byte(args[1])
    48  			}
    49  			gasPrice, signer, password, nonce, gasLimit, assumeYes, err := GetWriteCommandFlag(cmd)
    50  			if err != nil {
    51  				return err
    52  			}
    53  			sender, err := Signer(client, signer)
    54  			if err != nil {
    55  				return errors.Wrap(err, "failed to get signer address")
    56  			}
    57  			if gasLimit == 0 {
    58  				gasLimit = action.ClaimFromRewardingFundBaseGas +
    59  					action.ClaimFromRewardingFundGasPerByte*uint64(len(payload))
    60  			}
    61  			gasPriceRau, err := gasPriceInRau(client, gasPrice)
    62  			if err != nil {
    63  				return errors.Wrap(err, "failed to get gasPriceRau")
    64  			}
    65  			nonce, err = checkNonce(client, nonce, sender)
    66  			if err != nil {
    67  				return errors.Wrap(err, "failed to get nonce")
    68  			}
    69  			act := (&action.ClaimFromRewardingFundBuilder{}).SetAmount(amount).SetData(payload).Build()
    70  			return SendAction(
    71  				client,
    72  				cmd,
    73  				(&action.EnvelopeBuilder{}).SetNonce(nonce).
    74  					SetGasPrice(gasPriceRau).
    75  					SetGasLimit(gasLimit).
    76  					SetAction(&act).Build(),
    77  				sender,
    78  				password,
    79  				nonce,
    80  				assumeYes,
    81  			)
    82  		},
    83  	}
    84  	RegisterWriteCommand(client, cmd)
    85  	return cmd
    86  }