/
commands.go
306 lines (264 loc) · 8.84 KB
/
commands.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
package websockets
import (
"encoding/json"
"fmt"
"sync/atomic"
"github.com/anyswap/CrossChain-Router/v3/tokens/ripple/rubblelabs/ripple/data"
)
var counter uint64
type Syncer interface {
Done()
Fail(message string)
}
type CommandError struct {
Name string `json:"error"`
Code int `json:"error_code"`
Message string `json:"error_message"`
}
type Command struct {
*CommandError
Id uint64 `json:"id"`
Name string `json:"command"`
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
Ready chan struct{} `json:"-"`
}
func (c *Command) Done() {
c.Ready <- struct{}{}
}
func (c *Command) Fail(message string) {
c.CommandError = &CommandError{
Name: "Client Error",
Code: -1,
Message: message,
}
c.Ready <- struct{}{}
}
func (c *Command) IncrementId() {
c.Id = atomic.AddUint64(&counter, 1)
}
func (e *CommandError) Error() string {
return fmt.Sprintf("%s %d %s", e.Name, e.Code, e.Message)
}
func newCommand(command string) *Command {
return &Command{
Id: atomic.AddUint64(&counter, 1),
Name: command,
Ready: make(chan struct{}),
}
}
type AccountTxCommand struct {
*Command
Account data.Account `json:"account"`
MinLedger int64 `json:"ledger_index_min"`
MaxLedger int64 `json:"ledger_index_max"`
Binary bool `json:"binary,omitempty"`
Forward bool `json:"forward,omitempty"`
Limit int `json:"limit,omitempty"`
Marker map[string]interface{} `json:"marker,omitempty"`
Result *AccountTxResult `json:"result,omitempty"`
}
type AccountTxResult struct {
Marker map[string]interface{} `json:"marker,omitempty"`
Transactions data.TransactionSlice `json:"transactions,omitempty"`
}
func newAccountTxCommand(account data.Account, pageSize int, marker map[string]interface{}, minLedger, maxLedger int64) *AccountTxCommand {
return &AccountTxCommand{
Command: newCommand("account_tx"),
Account: account,
MinLedger: minLedger,
MaxLedger: maxLedger,
Limit: pageSize,
Marker: marker,
}
}
func newBinaryLedgerDataCommand(ledger interface{}, marker *data.Hash256) *BinaryLedgerDataCommand {
return &BinaryLedgerDataCommand{
Command: newCommand("ledger_data"),
Ledger: ledger,
Binary: true,
Marker: marker,
}
}
type TxCommand struct {
*Command
Transaction data.Hash256 `json:"transaction"`
Result *TxResult `json:"result,omitempty"`
}
type TxResult struct {
data.TransactionWithMetaData
Validated bool `json:"validated"`
}
// A shim to populate the Validated field before passing
// control on to TransactionWithMetaData.UnmarshalJSON
func (txr *TxResult) UnmarshalJSON(b []byte) error {
var extract map[string]interface{}
if err := json.Unmarshal(b, &extract); err != nil {
return err
}
// "validated" can be absent, when tx result is provisional.
validated, ok := extract["validated"]
if !ok {
txr.Validated = false
} else {
txr.Validated = validated.(bool)
}
return json.Unmarshal(b, &txr.TransactionWithMetaData)
}
type SubmitCommand struct {
*Command
TxBlob string `json:"tx_blob"`
Result *SubmitResult `json:"result,omitempty"`
}
type SubmitResult struct {
EngineResult data.TransactionResult `json:"engine_result"`
EngineResultCode int `json:"engine_result_code"`
EngineResultMessage string `json:"engine_result_message"`
TxBlob string `json:"tx_blob"`
Tx interface{} `json:"tx_json"`
}
type LedgerCommand struct {
*Command
LedgerIndex interface{} `json:"ledger_index"`
Accounts bool `json:"accounts"`
Transactions bool `json:"transactions"`
Expand bool `json:"expand"`
Result *LedgerResult `json:"result,omitempty"`
}
type LedgerResult struct {
Ledger data.Ledger
}
type LedgerHeaderCommand struct {
*Command
Ledger interface{} `json:"ledger"`
Result *LedgerHeaderResult
}
type LedgerHeaderResult struct {
Ledger data.Ledger
LedgerSequence uint32 `json:"ledger_index"`
Hash *data.Hash256 `json:"ledger_hash,omitempty"`
LedgerData data.VariableLength `json:"ledger_data"`
}
type LedgerDataCommand struct {
*Command
Ledger interface{} `json:"ledger"`
Marker *data.Hash256 `json:"marker,omitempty"`
Result *LedgerDataResult `json:"result,omitempty"`
}
type BinaryLedgerDataCommand struct {
*Command
Ledger interface{} `json:"ledger"`
Binary bool `json:"binary"`
Marker *data.Hash256 `json:"marker,omitempty"`
Result *BinaryLedgerDataResult `json:"result,omitempty"`
}
type LedgerDataResult struct {
LedgerSequence uint32 `json:"ledger_index"`
Hash data.Hash256 `json:"ledger_hash"`
Marker *data.Hash256 `json:"marker"`
State data.LedgerEntrySlice `json:"state"`
}
type BinaryLedgerData struct {
Data string `json:"data"`
Index string `json:"index"`
}
type BinaryLedgerDataResult struct {
LedgerSequence uint32 `json:"ledger_index"`
Hash data.Hash256 `json:"ledger_hash"`
Marker *data.Hash256 `json:"marker"`
State []BinaryLedgerData `json:"state"`
}
type RipplePathFindCommand struct {
*Command
SrcAccount data.Account `json:"source_account"`
SrcCurrencies *[]data.Currency `json:"source_currencies,omitempty"`
DestAccount data.Account `json:"destination_account"`
DestAmount data.Amount `json:"destination_amount"`
Result *RipplePathFindResult `json:"result,omitempty"`
}
type RipplePathFindResult struct {
Alternatives []struct {
SrcAmount data.Amount `json:"source_amount"`
PathsComputed data.PathSet `json:"paths_computed,omitempty"`
PathsCanonical data.PathSet `json:"paths_canonical,omitempty"`
}
DestAccount data.Account `json:"destination_account"`
DestCurrencies []data.Currency `json:"destination_currencies"`
}
type AccountInfoCommand struct {
*Command
Account data.Account `json:"account"`
Result *AccountInfoResult `json:"result,omitempty"`
}
type AccountInfoResult struct {
LedgerSequence uint32 `json:"ledger_current_index"`
AccountData data.AccountRoot `json:"account_data"`
}
type AccountLinesCommand struct {
*Command
Account data.Account `json:"account"`
Limit uint32 `json:"limit"`
LedgerIndex interface{} `json:"ledger_index,omitempty"`
Marker *data.Hash256 `json:"marker,omitempty"`
Result *AccountLinesResult `json:"result,omitempty"`
}
type AccountLinesResult struct {
LedgerSequence *uint32 `json:"ledger_index"`
Account data.Account `json:"account"`
Marker *string `json:"marker"`
Lines data.AccountLineSlice `json:"lines"`
}
type AccountOffersCommand struct {
*Command
Account data.Account `json:"account"`
Limit uint32 `json:"limit"`
LedgerIndex interface{} `json:"ledger_index,omitempty"`
Marker *data.Hash256 `json:"marker,omitempty"`
Result *AccountOffersResult `json:"result,omitempty"`
}
type AccountOffersResult struct {
LedgerSequence *uint32 `json:"ledger_index"`
Account data.Account `json:"account"`
Marker *data.Hash256 `json:"marker"`
Offers data.AccountOfferSlice `json:"offers"`
}
type BookOffersCommand struct {
*Command
LedgerIndex interface{} `json:"ledger_index,omitempty"`
Taker data.Account `json:"taker"`
TakerPays data.Asset `json:"taker_pays"`
TakerGets data.Asset `json:"taker_gets"`
Limit uint32 `json:"limit"`
Result *BookOffersResult
}
type BookOffersResult struct {
LedgerSequence uint32 `json:"ledger_index"`
Offers []data.OrderBookOffer `json:"offers"`
}
type FeeCommand struct {
*Command
Result *FeeResult
}
type FeeResult struct {
CurrentLedgerSize uint32 `json:"current_ledger_size,string"`
CurrentQueueSize uint32 `json:"current_queue_size,string"`
Drops struct {
BaseFee data.Value `json:"base_fee"`
MedianFee data.Value `json:"median_fee"`
MinimumFee data.Value `json:"minimum_fee"`
OpenLedgerFee data.Value `json:"open_ledger_fee"`
} `json:"drops"`
ExpectedLedgerSize uint32 `json:"expected_ledger_size,string"`
Levels struct {
MedianLevel data.Value `json:"median_level"`
MinimumLevel data.Value `json:"minimum_level"`
OpenLedgerLevel data.Value `json:"open_ledger_level"`
ReferenceLevel data.Value `json:"reference_level"`
} `json:"levels"`
MaxQueueSize uint32 `json:"max_queue_size,string"`
Status string `json:"status"`
}
type LedgerCurrentResult struct {
LedgerSequence uint32 `json:"ledger_current_index"`
Status string `json:"status"`
}