-
Notifications
You must be signed in to change notification settings - Fork 163
/
wizard_bond.go
345 lines (277 loc) · 8.51 KB
/
wizard_bond.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"net/url"
"os"
"strconv"
"github.com/golang/glog"
"github.com/ethereum/go-ethereum/common"
lpcommon "github.com/livepeer/go-livepeer/common"
"github.com/livepeer/go-livepeer/eth"
lpTypes "github.com/livepeer/go-livepeer/eth/types"
"github.com/olekukonko/tablewriter"
)
func (w *wizard) registeredOrchestratorStats() map[int]common.Address {
orchestrators, err := w.getRegisteredOrchestrators()
if err != nil {
glog.Errorf("Error getting registered orchestrators: %v", err)
return nil
}
orchestratorIDs := make(map[int]common.Address)
nextId := 0
fmt.Println("+------------------------+")
fmt.Println("|REGISTERED ORCHESTRATORS|")
fmt.Println("+------------------------+")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Address", "Active", "Delegated Stake", "Reward Cut (%)", "Fee Cut (%)", "Service URI", "Price Per Pixel"})
for _, t := range orchestrators {
table.Append([]string{
strconv.FormatInt(int64(nextId), 10),
t.Address.Hex(),
strconv.FormatBool(t.Active),
eth.FormatUnits(t.DelegatedStake, "LPT"),
eth.FormatPerc(t.RewardCut),
eth.FormatPerc(flipPerc(t.FeeShare)),
t.ServiceURI,
t.PricePerPixel.FloatString(3),
})
orchestratorIDs[nextId] = t.Address
nextId++
}
table.Render()
return orchestratorIDs
}
func (w *wizard) getRegisteredOrchestrators() ([]lpTypes.Transcoder, error) {
resp, err := http.Get(fmt.Sprintf("http://%v:%v/registeredOrchestrators", w.host, w.httpPort))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %d", resp.StatusCode)
}
defer resp.Body.Close()
result, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var orchestrators []lpTypes.Transcoder
err = json.Unmarshal(result, &orchestrators)
if err != nil {
return nil, err
}
return orchestrators, nil
}
func (w *wizard) unbondingLockStats(withdrawable bool) map[int64]bool {
unbondingLocks, err := w.getUnbondingLocks(withdrawable)
if err != nil {
glog.Errorf("Error getting unbonding locks: %v", err)
return nil
}
unbondingLockIDs := make(map[int64]bool)
if len(unbondingLocks) == 0 {
return unbondingLockIDs
}
fmt.Println("+---------------+")
fmt.Println("|UNBONDING LOCKS|")
fmt.Println("+---------------+")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Amount", "Withdraw Round"})
for _, u := range unbondingLocks {
table.Append([]string{
strconv.FormatInt(u.ID, 10),
eth.FormatUnits(u.Amount, "LPT"),
strconv.FormatInt(u.WithdrawRound, 10),
})
unbondingLockIDs[u.ID] = true
}
table.Render()
return unbondingLockIDs
}
func (w *wizard) getUnbondingLocks(withdrawable bool) ([]lpcommon.DBUnbondingLock, error) {
var url string
if withdrawable {
url = fmt.Sprintf("http://%v:%v/unbondingLocks?withdrawable=true", w.host, w.httpPort)
} else {
url = fmt.Sprintf("http://%v:%v/unbondingLocks", w.host, w.httpPort)
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var unbondingLocks []lpcommon.DBUnbondingLock
err = json.Unmarshal(result, &unbondingLocks)
if err != nil {
return nil, err
}
return unbondingLocks, nil
}
func (w *wizard) bond() {
orchestratorIds := w.registeredOrchestratorStats()
var tAddr common.Address
if orchestratorIds == nil {
fmt.Printf("Enter the address of the orchestrator you would like to bond to - ")
strAddr := w.readString()
if err := tAddr.UnmarshalText([]byte(strAddr)); err != nil {
fmt.Println(err)
return
}
} else {
fmt.Printf("Enter the identifier of the orchestrator you would like to bond to - ")
id := w.readInt()
tAddr = orchestratorIds[id]
}
balBigInt, err := lpcommon.ParseBigInt(w.getTokenBalance())
if err != nil {
fmt.Printf("Cannot read token balance: %v", w.getTokenBalance())
return
}
amount := big.NewInt(0)
for amount.Cmp(big.NewInt(0)) == 0 || balBigInt.Cmp(amount) < 0 {
amount = w.readBigInt("Enter bond amount")
if amount.Cmp(big.NewInt(0)) == 0 {
break
}
if balBigInt.Cmp(amount) < 0 {
fmt.Printf("Must enter an amount smaller than the current balance. ")
}
}
val := url.Values{
"amount": {fmt.Sprintf("%v", amount.String())},
"toAddr": {fmt.Sprintf("%v", tAddr.Hex())},
}
httpPostWithParams(fmt.Sprintf("http://%v:%v/bond", w.host, w.httpPort), val)
}
func (w *wizard) rebond() {
dInfo, err := w.getDelegatorInfo()
if err != nil {
glog.Errorf("Error getting delegator info: %v", err)
return
}
fmt.Printf("Current Bonded Amount: %v\n", eth.FormatUnits(dInfo.BondedAmount, "LPT"))
if (dInfo.DelegateAddress != common.Address{}) {
fmt.Printf("Current Delegate: %v\n", dInfo.DelegateAddress.Hex())
}
unbondingLockIDs := w.unbondingLockStats(false)
if unbondingLockIDs == nil || len(unbondingLockIDs) == 0 {
fmt.Printf("No unbonding locks")
return
}
unbondingLockID := int64(-1)
for {
fmt.Printf("Enter the identifier of the unbonding lock you would like to rebond with - ")
unbondingLockID = int64(w.readInt())
if _, ok := unbondingLockIDs[unbondingLockID]; ok {
break
}
fmt.Printf("Must enter a valid unbonding lock ID\n")
}
val := url.Values{
"unbondingLockId": {fmt.Sprintf("%v", strconv.FormatInt(unbondingLockID, 10))},
}
if dInfo.Status == "Unbonded" {
fmt.Printf("You are unbonded - you will need to choose an address to rebond to.\n")
var toAddr common.Address
orchestratorIds := w.registeredOrchestratorStats()
if orchestratorIds == nil {
fmt.Printf("Enter the address of the orchestrator you would like to rebond to - ")
strAddr := w.readString()
if err := toAddr.UnmarshalText([]byte(strAddr)); err != nil {
fmt.Println(err)
return
}
} else {
fmt.Printf("Enter the identifier of the orchestrator you would like to rebond to - ")
orchestratorID := w.readInt()
toAddr = orchestratorIds[orchestratorID]
}
val["toAddr"] = []string{fmt.Sprintf("%v", toAddr.Hex())}
}
httpPostWithParams(fmt.Sprintf("http://%v:%v/rebond", w.host, w.httpPort), val)
}
func (w *wizard) unbond() {
dInfo, err := w.getDelegatorInfo()
if err != nil {
glog.Errorf("Error getting delegator info: %v", err)
return
}
if dInfo.BondedAmount.Cmp(big.NewInt(0)) < 0 {
fmt.Printf("You are not bonded\n")
return
}
fmt.Printf("Current Bonded Amount: %v\n", eth.FormatUnits(dInfo.BondedAmount, "LPT"))
fmt.Printf("Current Delegate: %v\n", dInfo.DelegateAddress.Hex())
fmt.Printf("Would you like to fully unbond? (y/n) - ")
input := ""
for {
input = w.readString()
if input == "y" || input == "n" {
break
}
fmt.Printf("Enter (y)es or (n)o \n")
}
var amount *big.Int
if input == "y" {
amount = dInfo.BondedAmount
} else {
amount = big.NewInt(0)
}
for amount.Cmp(big.NewInt(0)) == 0 || dInfo.BondedAmount.Cmp(amount) < 0 {
amount = w.readBigInt("Enter unbond amount")
if dInfo.BondedAmount.Cmp(amount) < 0 {
fmt.Printf("Must enter an amount less than or equal to the current bonded amount.")
}
}
val := url.Values{
"amount": {fmt.Sprintf("%v", amount.String())},
}
httpPostWithParams(fmt.Sprintf("http://%v:%v/unbond", w.host, w.httpPort), val)
}
func (w *wizard) withdrawStake() {
dInfo, err := w.getDelegatorInfo()
if err != nil {
glog.Errorf("Error getting delegator info: %v", err)
return
}
fmt.Printf("Current Bonded Amount: %v\n", eth.FormatUnits(dInfo.BondedAmount, "LPT"))
if (dInfo.DelegateAddress != common.Address{}) {
fmt.Printf("Current Delegate: %v\n", dInfo.DelegateAddress.Hex())
}
unbondingLockIDs := w.unbondingLockStats(true)
if unbondingLockIDs == nil || len(unbondingLockIDs) == 0 {
fmt.Printf("No withdrawable unbonding locks")
return
}
unbondingLockID := int64(-1)
for {
fmt.Printf("Enter the identifier of the unbonding lock you would like to withdraw with - ")
unbondingLockID = int64(w.readInt())
if _, ok := unbondingLockIDs[unbondingLockID]; ok {
break
}
fmt.Printf("Must enter a valid unbonding lock ID\n")
}
val := url.Values{
"unbondingLockId": {fmt.Sprintf("%v", strconv.FormatInt(unbondingLockID, 10))},
}
httpPostWithParams(fmt.Sprintf("http://%v:%v/withdrawStake", w.host, w.httpPort), val)
}
func (w *wizard) withdrawFees() {
dInfo, err := w.getDelegatorInfo()
if err != nil {
glog.Errorf("Error getting delegator info: %v", err)
return
}
val := url.Values{
"amount": {fmt.Sprintf("%v", dInfo.PendingFees.String())},
}
httpPostWithParams(fmt.Sprintf("http://%v:%v/withdrawFees", w.host, w.httpPort), val)
}