-
Notifications
You must be signed in to change notification settings - Fork 21
/
stakepool.go
329 lines (280 loc) · 9.13 KB
/
stakepool.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package cmd
import (
"fmt"
"log"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
func printStakePoolInfo(info *sdk.StakePoolInfo) {
fmt.Println("blobber_id: ", info.ID)
fmt.Println("total stake: ", info.Balance)
fmt.Println("going to unlock, total:", info.Unstake)
fmt.Println("capacity:")
fmt.Println(" free: ", info.Free, "(for current write price)")
fmt.Println(" capacity: ", info.Capacity, "(blobber bid)")
fmt.Println(" write_price:", info.WritePrice, "(blobber write price)")
if len(info.Offers) == 0 {
fmt.Println("offers: no opened offers")
} else {
fmt.Println("offers:")
for _, off := range info.Offers {
fmt.Println("- lock: ", off.Lock)
fmt.Println(" expire: ", off.Expire.ToTime())
fmt.Println(" allocation:", off.AllocationID)
fmt.Println(" expired: ", off.IsExpired)
}
fmt.Println("offers_total:", info.OffersTotal, "(held by opened offers)")
}
if len(info.Delegate) == 0 {
fmt.Println("delegate_pools: no delegate pools")
} else {
fmt.Println("delegate_pools:")
for _, dp := range info.Delegate {
fmt.Println("- id: ", dp.ID)
fmt.Println(" balance: ", dp.Balance)
fmt.Println(" delegate_id: ", dp.DelegateID)
fmt.Println(" rewards: ", dp.Rewards)
fmt.Println(" penalty: ", dp.Penalty)
fmt.Println(" interests: ", dp.Interests, "(payed)")
fmt.Println(" pending_interests:", dp.PendingInterests, "(not payed yet, can be given by 'sp-pay-interests' command)")
var gtu string
if dp.Unstake > 0 {
gtu = dp.Unstake.ToTime().String()
} else {
gtu = "<not going>"
}
fmt.Println(" going to unlock at:", gtu)
}
}
fmt.Println("penalty: ", info.Penalty, "(total blobber penalty for all time)")
fmt.Println("rewards:")
fmt.Println(" charge: ", info.Rewards.Charge, "(for all time)")
fmt.Println(" blobber: ", info.Rewards.Blobber, "(for all time)")
fmt.Println(" validator:", info.Rewards.Validator, "(for all time)")
// settings
fmt.Println("settings:")
fmt.Println(" delegate_wallet:", info.Settings.DelegateWallet)
fmt.Println(" min_stake: ", info.Settings.MinStake.String())
fmt.Println(" max_stake: ", info.Settings.MaxStake.String())
fmt.Println(" num_delegates: ", info.Settings.NumDelegates)
}
func printStakePoolUserInfo(info *sdk.StakePoolUserInfo) {
if len(info.Pools) == 0 {
fmt.Print("no delegate pools")
return
}
for blobberID, dps := range info.Pools {
fmt.Println("- blobber_id: ", blobberID)
for _, dp := range dps {
fmt.Println(" - id: ", dp.ID)
fmt.Println(" balance: ", dp.Balance)
fmt.Println(" delegate_id: ", dp.DelegateID)
fmt.Println(" rewards: ", dp.Rewards)
fmt.Println(" penalty: ", dp.Penalty)
fmt.Println(" interests: ", dp.Interests, "(payed)")
fmt.Println(" pending_interests:", dp.PendingInterests,
"(not payed yet, can be given by 'sp-pay-interests' command)")
}
}
}
// spInfo information
var spInfo = &cobra.Command{
Use: "sp-info",
Short: "Stake pool information.",
Long: `Stake pool information.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
blobberID string
err error
)
doJSON, _ := cmd.Flags().GetBool("json")
if flags.Changed("blobber_id") {
if blobberID, err = flags.GetString("blobber_id"); err != nil {
log.Fatalf("can't get 'blobber_id' flag: %v", err)
}
}
var info *sdk.StakePoolInfo
if info, err = sdk.GetStakePoolInfo(blobberID); err != nil {
log.Fatalf("Failed to get stake pool info: %v", err)
}
if doJSON {
util.PrintJSON(info)
} else {
printStakePoolInfo(info)
}
},
}
// spUserInfo information per user
var spUserInfo = &cobra.Command{
Use: "sp-user-info",
Short: "Stake pool information for a user.",
Long: `Stake pool information for a user.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
clientID string
err error
)
doJSON, _ := cmd.Flags().GetBool("json")
if flags.Changed("client_id") {
if clientID, err = flags.GetString("client_id"); err != nil {
log.Fatalf("can't get 'client_id' flag: %v", err)
}
}
var info *sdk.StakePoolUserInfo
if info, err = sdk.GetStakePoolUserInfo(clientID); err != nil {
log.Fatalf("Failed to get stake pool info: %v", err)
}
if doJSON {
util.PrintJSON(info)
} else {
printStakePoolUserInfo(info)
}
},
}
// spLock locks tokens a stake pool lack
var spLock = &cobra.Command{
Use: "sp-lock",
Short: "Lock tokens lacking in stake pool.",
Long: `Lock tokens lacking in stake pool.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
blobberID string
tokens float64
fee float64
err error
)
if flags.Changed("blobber_id") {
if blobberID, err = flags.GetString("blobber_id"); err != nil {
log.Fatalf("invalid 'blobber_id' flag: %v", err)
}
}
if !flags.Changed("tokens") {
log.Fatal("missing required 'tokens' flag")
}
if tokens, err = flags.GetFloat64("tokens"); err != nil {
log.Fatal("invalid 'tokens' flag: ", err)
}
if flags.Changed("fee") {
if fee, err = flags.GetFloat64("fee"); err != nil {
log.Fatal("invalid 'fee' flag: ", err)
}
}
var poolID string
poolID, err = sdk.StakePoolLock(blobberID,
zcncore.ConvertToValue(tokens), zcncore.ConvertToValue(fee))
if err != nil {
log.Fatalf("Failed to lock tokens in stake pool: %v", err)
}
fmt.Println("tokens locked, pool id:", poolID)
},
}
// spUnlock unlocks tokens in stake pool
var spUnlock = &cobra.Command{
Use: "sp-unlock",
Short: "Unlock tokens in stake pool.",
Long: `Unlock tokens in stake pool.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
blobberID, poolID string
fee float64
err error
)
if flags.Changed("blobber_id") {
if blobberID, err = flags.GetString("blobber_id"); err != nil {
log.Fatalf("invalid 'blobber_id' flag: %v", err)
}
}
if !flags.Changed("pool_id") {
log.Fatal("missing required 'pool_id' flag")
}
if poolID, err = flags.GetString("pool_id"); err != nil {
log.Fatal("invalid 'pool_id' flag: ", err)
}
if flags.Changed("fee") {
if fee, err = flags.GetFloat64("fee"); err != nil {
log.Fatal("invalid 'fee' flag: ", err)
}
}
var unstake common.Timestamp
unstake, err = sdk.StakePoolUnlock(blobberID, poolID,
zcncore.ConvertToValue(fee))
// an error
if err != nil {
log.Fatalf("Failed to unlock tokens in stake pool: %v", err)
}
// can't unlock for now
if unstake > 0 {
fmt.Println("tokens can't be unlocked due to opened offers")
fmt.Printf("the pool marked as releasing, wait %s and retry to succeed", unstake.ToTime())
fmt.Println()
return
}
// success
fmt.Println("tokens has unlocked, pool deleted")
},
}
// spPayInterests pays interests not payed yet. A stake pool changes
// pays all interests can be payed. But if stake pool is not changed,
// then user can manually pay the interests.
var spPayInterests = &cobra.Command{
Use: "sp-pay-interests",
Short: "Pay interests not payed yet.",
Long: `Pay interests not payed.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
blobberID string
err error
)
if flags.Changed("blobber_id") {
if blobberID, err = flags.GetString("blobber_id"); err != nil {
log.Fatalf("invalid 'blobber_id' flag: %v", err)
}
}
if err = sdk.StakePoolPayInterests(blobberID); err != nil {
log.Fatalf("Failed to pay interests: %v", err)
}
fmt.Println("interests has payed")
},
}
func init() {
rootCmd.AddCommand(spInfo)
rootCmd.AddCommand(spUserInfo)
rootCmd.AddCommand(spLock)
rootCmd.AddCommand(spUnlock)
rootCmd.AddCommand(spPayInterests)
spInfo.PersistentFlags().String("blobber_id", "",
"for given blobber, default is current client")
spInfo.PersistentFlags().Bool("json", false, "pass this option to print response as json data")
spUserInfo.PersistentFlags().Bool("json", false, "pass this option to print response as json data")
spLock.PersistentFlags().String("blobber_id", "",
"for given blobber, default is current client")
spLock.PersistentFlags().Float64("tokens", 0.0,
"tokens to lock, required")
spLock.PersistentFlags().Float64("fee", 0.0,
"transaction fee, default 0")
spLock.MarkFlagRequired("tokens")
spLock.MarkFlagRequired("blobber_id")
spUnlock.PersistentFlags().String("blobber_id", "",
"for given blobber, default is current client")
spUnlock.PersistentFlags().String("pool_id", "",
"pool id to unlock")
spUnlock.PersistentFlags().Float64("fee", 0.0,
"transaction fee, default 0")
spUnlock.MarkFlagRequired("tokens")
spUnlock.MarkFlagRequired("pool_id")
spPayInterests.PersistentFlags().String("blobber_id", "",
"for given blobber, default is current client")
}