forked from qiwitech/qdp
/
processing.go
187 lines (147 loc) · 3.11 KB
/
processing.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
package client
import (
"context"
"math/rand"
"strconv"
"github.com/pkg/errors"
cli "gopkg.in/urfave/cli.v2"
"github.com/qiwitech/qdp/proto/apipb"
)
var (
MetaKey string
)
func Transfer(cx *cli.Context) error {
args := cx.Args()
u, err := accountFromArgs(args)
if err != nil {
cli.ShowSubcommandHelp(cx)
return err
}
req := &apipb.TransferRequest{Sender: u}
if err := parseTransferItems(req, args.Slice()[1:]); err != nil {
cli.ShowSubcommandHelp(cx)
return err
}
if MetaKey != "" {
key, err := MetaKeyDecode(MetaKey)
if err != nil {
return errors.Wrap(err, "meta key decode")
}
phases := [...]string{"new_moon", "first_quarter", "full_moon", "third_quarter"}
req.Metadata = &apipb.Meta{
Key: key,
Index: map[string][]byte{
"moon": []byte(phases[rand.Intn(4)]),
},
}
}
if err := connect(); err != nil {
return err
}
h, err := getPrevHash(req.Sender)
if err != nil {
return errors.Wrap(err, "prev hash")
}
req.PrevHash = h
sett, err := api.GetLastSettings(context.TODO(), &apipb.GetLastSettingsRequest{Account: u})
if err != nil {
return err
}
err = inspectStatus(sett.Status)
if err != nil {
return err
}
req.SettingsId = sett.Id
for i := 0; i < RepeatFlag; i++ {
err = SignTransfer(req)
if err != nil {
return errors.Wrap(err, "sign")
}
resp, err := api.ProcessTransfer(context.TODO(), req)
if err != nil {
return err
}
err = inspectStatus(resp.Status)
if err != nil {
return err
}
printResponse(cx, resp)
req.PrevHash = resp.Hash
}
return nil
}
func PrevHash(cx *cli.Context) error {
args := cx.Args()
u, err := accountFromArgs(args)
if err != nil {
cli.ShowSubcommandHelp(cx)
return err
}
if err := connect(); err != nil {
return err
}
resp, err := api.GetPrevHash(context.TODO(), &apipb.GetPrevHashRequest{Account: u})
if err != nil {
return err
}
err = inspectStatus(resp.Status)
if err != nil {
return err
}
printResponse(cx, resp)
return nil
}
func Balance(cx *cli.Context) error {
args := cx.Args()
u, err := accountFromArgs(args)
if err != nil {
cli.ShowSubcommandHelp(cx)
return err
}
if err := connect(); err != nil {
return err
}
resp, err := api.GetBalance(context.TODO(), &apipb.GetBalanceRequest{Account: u})
if err != nil {
return err
}
err = inspectStatus(resp.Status)
if err != nil {
return err
}
printResponse(cx, resp)
return nil
}
func getPrevHash(u uint64) (string, error) {
req := &apipb.GetPrevHashRequest{Account: u}
resp, err := api.GetPrevHash(context.TODO(), req)
if err != nil {
return "", err
}
err = inspectStatus(resp.Status)
if err != nil {
return "", err
}
return resp.Hash, nil
}
func parseTransferItems(req *apipb.TransferRequest, a []string) error {
if len(a) < 2 || len(a)%2 != 0 {
return ErrArguments
}
base := 10
if HexFlag {
base = 16
}
for i := 0; i < len(a); i += 2 {
u, err := strconv.ParseUint(a[i], base, 64)
if err != nil {
return err
}
am, err := strconv.ParseInt(a[i+1], 10, 64)
if err != nil {
return err
}
req.Batch = append(req.Batch, &apipb.TransferItem{Receiver: u, Amount: am})
}
return nil
}