-
Notifications
You must be signed in to change notification settings - Fork 321
/
nodereward.go
178 lines (156 loc) · 5.28 KB
/
nodereward.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 node
import (
"context"
"fmt"
"math/big"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
rewardUses = map[config.Language]string{
config.English: "reward [ALIAS|DELEGATE_ADDRESS]",
config.Chinese: "reward [别名|委托地址]",
}
rewardShorts = map[config.Language]string{
config.English: "Query rewards",
config.Chinese: "查询奖励",
}
rewardPoolMessageTranslations = map[config.Language]string{
config.English: "Available Reward: %s IOTX Total Reward: %s IOTX",
config.Chinese: "可用奖金: %s IOTX 总奖金: %s IOTX",
}
)
// NewNodeRewardCmd represents the node reward command
func NewNodeRewardCmd(c ioctl.Client) *cobra.Command {
var endpoint string
var insecure bool
use, _ := c.SelectTranslation(rewardUses)
short, _ := c.SelectTranslation(rewardShorts)
rewardPoolMessageTranslation, _ := c.SelectTranslation(rewardPoolMessageTranslations)
nc := &cobra.Command{
Use: use,
Short: short,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
var err error
if len(args) == 0 {
apiClient, err := c.APIServiceClient(ioctl.APIServiceConfig{Endpoint: endpoint, Insecure: insecure})
if err != nil {
return err
}
response, err := apiClient.ReadState(
context.Background(),
&iotexapi.ReadStateRequest{
ProtocolID: []byte("rewarding"),
MethodName: []byte("AvailableBalance"),
},
)
if err != nil {
sta, ok := status.FromError(err)
if ok {
return output.NewError(output.APIError, sta.Message(), nil)
}
return output.NewError(output.NetworkError, "failed to invoke ReadState api", err)
}
availableRewardRau, ok := big.NewInt(0).SetString(string(response.Data), 10)
if !ok {
return output.NewError(output.ConvertError, "failed to convert string into big int", err)
}
response, err = apiClient.ReadState(
context.Background(),
&iotexapi.ReadStateRequest{
ProtocolID: []byte("rewarding"),
MethodName: []byte("TotalBalance"),
},
)
if err != nil {
sta, ok := status.FromError(err)
if ok {
return output.NewError(output.APIError, sta.Message(), nil)
}
return output.NewError(output.NetworkError, "failed to invoke ReadState api", err)
}
totalRewardRau, ok := big.NewInt(0).SetString(string(response.Data), 10)
if !ok {
return output.NewError(output.ConvertError, "failed to convert string into big int", err)
}
message := rewardPoolMessage{
AvailableReward: util.RauToString(availableRewardRau, util.IotxDecimalNum),
TotalReward: util.RauToString(totalRewardRau, util.IotxDecimalNum),
}
fmt.Println(message.String(rewardPoolMessageTranslation))
} else {
arg := args[0]
address, err := c.Address(arg)
if err != nil {
return output.NewError(output.AddressError, "failed to get address", err)
}
apiClient, err := c.APIServiceClient(ioctl.APIServiceConfig{
Endpoint: endpoint,
Insecure: insecure,
})
if err != nil {
return err
}
response, err := apiClient.ReadState(
context.Background(),
&iotexapi.ReadStateRequest{
ProtocolID: []byte("rewarding"),
MethodName: []byte("UnclaimedBalance"),
Arguments: [][]byte{[]byte(address)},
},
)
if err != nil {
sta, ok := status.FromError(err)
if ok {
return output.NewError(output.APIError, sta.Message(), nil)
}
return output.NewError(output.NetworkError, "failed to get version from server", err)
}
rewardRau, ok := big.NewInt(0).SetString(string(response.Data), 10)
if !ok {
return output.NewError(output.ConvertError, "failed to convert string into big int", err)
}
message := rewardMessage{Address: address, Reward: util.RauToString(rewardRau, util.IotxDecimalNum)}
fmt.Println(message.String())
}
return output.PrintError(err)
},
}
return nc
}
type rewardPoolMessage struct {
AvailableReward string `json:"availableReward"`
TotalReward string `json:"totalReward"`
}
func (m *rewardPoolMessage) String(trans ...string) string {
if output.Format == "" {
message := fmt.Sprintf(trans[0],
m.AvailableReward, m.TotalReward)
return message
}
return output.FormatStringWithTrans(output.Result, m)
}
type rewardMessage struct {
Address string `json:"address"`
Reward string `json:"reward"`
}
func (m *rewardMessage) String(trans ...string) string {
if output.Format == "" {
message := fmt.Sprintf("%s: %s IOTX", m.Address, m.Reward)
return message
}
return output.FormatStringWithTrans(output.Result, m)
}