-
Notifications
You must be signed in to change notification settings - Fork 322
/
actiondeposit.go
87 lines (80 loc) · 2.73 KB
/
actiondeposit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2022 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package action
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
_depositCmdShorts = map[config.Language]string{
config.English: "Deposit rewards to rewarding fund",
config.Chinese: "将奖励存入奖励基金",
}
_depositCmdUses = map[config.Language]string{
config.English: "deposit AMOUNT_IOTX [DATA] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
config.Chinese: "deposit IOTX数量 [数据] [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
}
)
// NewActionDepositCmd represents the action deposit command
func NewActionDepositCmd(client ioctl.Client) *cobra.Command {
use, _ := client.SelectTranslation(_depositCmdUses)
short, _ := client.SelectTranslation(_depositCmdShorts)
cmd := &cobra.Command{
Use: use,
Short: short,
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
amount, err := util.StringToRau(args[0], util.IotxDecimalNum)
if err != nil {
return errors.Wrap(err, "invalid amount")
}
payload := make([]byte, 0)
if len(args) == 2 {
payload = []byte(args[1])
}
gasPrice, signer, password, nonce, gasLimit, assumeYes, err := GetWriteCommandFlag(cmd)
if err != nil {
return err
}
sender, err := Signer(client, signer)
if err != nil {
return errors.Wrap(err, "failed to get signer address")
}
if gasLimit == 0 {
gasLimit = action.DepositToRewardingFundBaseGas +
action.DepositToRewardingFundGasPerByte*uint64(len(payload))
}
gasPriceRau, err := gasPriceInRau(client, gasPrice)
if err != nil {
return errors.Wrap(err, "failed to get gasPriceRau")
}
nonce, err = checkNonce(client, nonce, sender)
if err != nil {
return errors.Wrap(err, "failed to get nonce")
}
act := (&action.DepositToRewardingFundBuilder{}).SetAmount(amount).SetData(payload).Build()
return SendAction(
client,
cmd,
(&action.EnvelopeBuilder{}).SetNonce(nonce).
SetGasPrice(gasPriceRau).
SetGasLimit(gasLimit).
SetAction(&act).Build(),
sender,
password,
nonce,
assumeYes,
)
},
}
RegisterWriteCommand(client, cmd)
return cmd
}