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