-
Notifications
You must be signed in to change notification settings - Fork 23
/
writepool.go
118 lines (97 loc) · 2.81 KB
/
writepool.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
package cmd
import (
"fmt"
"log"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
)
// wpLock locks tokens in write pool
var wpLock = &cobra.Command{
Use: "wp-lock",
Short: "Lock some tokens in write pool.",
Long: `Lock some tokens in write pool.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
allocID string // required
tokens float64
fee float64
err error
)
if !flags.Changed("allocation") {
log.Fatal("missing required 'allocation' flag")
}
if !flags.Changed("tokens") {
log.Fatal("missing required 'tokens' flag")
}
if allocID, err = flags.GetString("allocation"); err != nil {
log.Fatal("invalid 'allocation' flag: ", err)
}
if tokens, err = flags.GetFloat64("tokens"); err != nil {
log.Fatal("invalid 'tokens' flag: ", err)
}
if tokens < 0 {
log.Fatal("invalid token amount: negative")
}
if flags.Changed("fee") {
if fee, err = flags.GetFloat64("fee"); err != nil {
log.Fatal("invalid 'fee' flag: ", err)
}
}
_, _, err = sdk.WritePoolLock(allocID, zcncore.ConvertToValue(tokens), zcncore.ConvertToValue(fee))
if err != nil {
log.Fatalf("Failed to lock tokens in write pool: %v", err)
}
fmt.Println("locked")
},
}
// wpUnlock unlocks tokens in a write pool
var wpUnlock = &cobra.Command{
Use: "wp-unlock",
Short: "Unlock some expired tokens in a write pool.",
Long: `Unlock some expired tokens in a write pool.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
flags = cmd.Flags()
allocID string
fee float64
err error
)
if !flags.Changed("allocation") {
log.Fatal("missing required 'allocation' flag")
}
if allocID, err = flags.GetString("allocation"); err != nil {
log.Fatal("invalid 'allocation' flag: ", err)
}
if flags.Changed("fee") {
if fee, err = flags.GetFloat64("fee"); err != nil {
log.Fatal("invalid 'fee' flag: ", err)
}
}
_, _, err = sdk.WritePoolUnlock(allocID, zcncore.ConvertToValue(fee))
if err != nil {
log.Fatalf("Failed to unlock tokens in write pool: %v", err)
}
fmt.Println("unlocked")
},
}
func init() {
rootCmd.AddCommand(wpLock)
rootCmd.AddCommand(wpUnlock)
wpLock.PersistentFlags().String("allocation", "",
"allocation id to lock for, required")
wpLock.PersistentFlags().Float64("tokens", 0.0,
"lock tokens number, required")
wpLock.PersistentFlags().Float64("fee", 0.0,
"transaction fee, default 0")
wpLock.MarkFlagRequired("allocation")
wpLock.MarkFlagRequired("tokens")
wpUnlock.PersistentFlags().String("allocation", "",
"allocation id from which to unlock tokens")
wpUnlock.PersistentFlags().Float64("fee", 0.0,
"transaction fee, default 0")
wpUnlock.MarkFlagRequired("allocation")
}