@axis-markets/client 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -27
- package/lib/axis-client.cjs +1 -1
- package/lib/axis-client.cjs.map +1 -1
- package/package.json +1 -1
- package/src/allowance.js +40 -8
- package/src/axis-account.js +1376 -1015
- package/src/axis.js +470 -465
- package/src/errors.js +150 -21
- package/src/index.d.ts +1721 -1632
- package/src/index.js +175 -21
package/src/axis-account.js
CHANGED
|
@@ -1,1015 +1,1376 @@
|
|
|
1
|
-
import {Emitter} from './emitter.js'
|
|
2
|
-
import {pairKey, invertPrice} from './asset-pair.js'
|
|
3
|
-
import {parseApiDate} from './api-dates.js'
|
|
4
|
-
import {planApproval, maxQuoteSpend} from './allowance.js'
|
|
5
|
-
import {OrderKind, TradeDirection} from './constants.js'
|
|
6
|
-
|
|
7
|
-
/** Orders per `update` transaction */
|
|
8
|
-
const UPDATE_BATCH = 90
|
|
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
|
-
* @type {
|
|
76
|
-
* @readonly
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
*
|
|
81
|
-
* @type {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*
|
|
93
|
-
* @
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
*
|
|
98
|
-
* @type {Map<string, number>}
|
|
99
|
-
* @private
|
|
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
|
-
this.
|
|
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
|
-
* @return {
|
|
173
|
-
*/
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
*
|
|
181
|
-
* @param {string}
|
|
182
|
-
* @return {
|
|
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
|
-
return
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
*
|
|
261
|
-
* @param {
|
|
262
|
-
* @return {
|
|
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
|
-
const
|
|
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
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
return
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
* @param {
|
|
478
|
-
* @param {
|
|
479
|
-
* @return {Promise<
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
const
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
this.
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
if (
|
|
523
|
-
//
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
*
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
*
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
* @
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
const
|
|
736
|
-
const
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
* @
|
|
767
|
-
* @private
|
|
768
|
-
*/
|
|
769
|
-
|
|
770
|
-
const
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
/**
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
* @
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
})
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
*
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1
|
+
import {Emitter} from './emitter.js'
|
|
2
|
+
import {pairKey, invertPrice} from './asset-pair.js'
|
|
3
|
+
import {parseApiDate} from './api-dates.js'
|
|
4
|
+
import {planApproval, planReduction, maxQuoteSpend, APPROVAL_TTL_LEDGERS} from './allowance.js'
|
|
5
|
+
import {OrderKind, TradeDirection} from './constants.js'
|
|
6
|
+
|
|
7
|
+
/** Orders per `update` transaction */
|
|
8
|
+
const UPDATE_BATCH = 90
|
|
9
|
+
/** Orders an approval takes the place of in an `update` batch: the token `approve` event is about twice the size of the
|
|
10
|
+
* `mod` event of a removal, within the 16 KB of events a transaction may emit */
|
|
11
|
+
const APPROVAL_BATCH_WEIGHT = 2
|
|
12
|
+
/** Time to wait for the account state before trading, in milliseconds */
|
|
13
|
+
const READY_TIMEOUT = 15_000
|
|
14
|
+
/** How long an order created by this client counts as committed before the indexer reports it, in milliseconds */
|
|
15
|
+
const LOCAL_ORDER_TTL = 120_000
|
|
16
|
+
/** How long an allowance set by this client overrides pushed records still showing the allowance it replaced, in
|
|
17
|
+
* milliseconds: the indexer may reload an allowance only with its periodic sweep */
|
|
18
|
+
const APPROVAL_CONFIRM_TIMEOUT = 360_000
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Trader wrapper that tracks every open order of an account across all markets and the account backing.
|
|
22
|
+
* Create it with {@link import('./axis.js').Axis#account}.
|
|
23
|
+
*
|
|
24
|
+
* Emits:
|
|
25
|
+
* - `ready` - the account state was loaded
|
|
26
|
+
* - `new` {@link AccountOrder} - an order was created (confirmed by the indexer)
|
|
27
|
+
* - `pending` {@link AccountOrder} - an order created by this client, not reported by the indexer yet
|
|
28
|
+
* - `fill` {@link AccountFill} - an order was partially filled
|
|
29
|
+
* - `filled` {@link AccountFill} - an order was filled in full
|
|
30
|
+
* - `update` {@link AccountOrder} - an order changed (amount, price, expiration, revived)
|
|
31
|
+
* - `cancel` {@link AccountOrder} - an order was removed by the owner
|
|
32
|
+
* - `expire` {@link AccountOrder} - an order expired
|
|
33
|
+
* - `remove` {@link AccountOrder} - an order left the open orders (after `filled`, `cancel`, `expire`)
|
|
34
|
+
* - `order` `{action, order, fill?}` - any order change
|
|
35
|
+
* - `backing` `{asset, backing}` - the balance, allowance or the backing split of the orders selling a token changed
|
|
36
|
+
* - `approve` `{asset, amount, liveUntil}` - a call is about to set an allowance (grant, renew, lower or revoke it)
|
|
37
|
+
* - `trade` {ApiTrade} - the account performed a trade
|
|
38
|
+
* - `swap` {ApiSwap} - the account performed a swap
|
|
39
|
+
* - `change` - the open orders or the backing changed
|
|
40
|
+
* - `error` `Error` - the push API rejected the account subscription
|
|
41
|
+
*/
|
|
42
|
+
export class AxisAccount extends Emitter {
|
|
43
|
+
/**
|
|
44
|
+
* @param {Axis} axis
|
|
45
|
+
* @param {string} address - Trader address
|
|
46
|
+
* @param {{signTransaction?: SignTransactionCallback, approvalTtl?: number}} [options]
|
|
47
|
+
*/
|
|
48
|
+
constructor(axis, address, {signTransaction, approvalTtl} = {}) {
|
|
49
|
+
super()
|
|
50
|
+
this.axis = axis
|
|
51
|
+
this.address = address
|
|
52
|
+
this.signTransaction = signTransaction
|
|
53
|
+
this.approvalTtl = approvalTtl || APPROVAL_TTL_LEDGERS
|
|
54
|
+
this.ready = new Promise(resolve => this.resolveReady = resolve)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Trader address
|
|
59
|
+
* @type {string}
|
|
60
|
+
* @readonly
|
|
61
|
+
*/
|
|
62
|
+
address
|
|
63
|
+
/**
|
|
64
|
+
* Wallet signing callback, required to trade
|
|
65
|
+
* @type {SignTransactionCallback|undefined}
|
|
66
|
+
*/
|
|
67
|
+
signTransaction
|
|
68
|
+
/**
|
|
69
|
+
* Lifetime of the allowances granted by trading calls, in ledgers
|
|
70
|
+
* @type {number}
|
|
71
|
+
*/
|
|
72
|
+
approvalTtl
|
|
73
|
+
/**
|
|
74
|
+
* Open orders by id
|
|
75
|
+
* @type {Map<string, AccountOrder>}
|
|
76
|
+
* @readonly
|
|
77
|
+
*/
|
|
78
|
+
orders = new Map()
|
|
79
|
+
/**
|
|
80
|
+
* Account backing by token contract id
|
|
81
|
+
* @type {Map<string, AccountBacking>}
|
|
82
|
+
* @readonly
|
|
83
|
+
*/
|
|
84
|
+
backing = new Map()
|
|
85
|
+
/**
|
|
86
|
+
* Resolves once the account state has been loaded
|
|
87
|
+
* @type {Promise<void>}
|
|
88
|
+
* @readonly
|
|
89
|
+
*/
|
|
90
|
+
ready
|
|
91
|
+
/**
|
|
92
|
+
* Whether the account state has been loaded
|
|
93
|
+
* @type {boolean}
|
|
94
|
+
*/
|
|
95
|
+
loaded = false
|
|
96
|
+
/**
|
|
97
|
+
* Orders created by this client that the indexer has not reported yet
|
|
98
|
+
* @type {Map<string, {order: AccountOrder, expires: number}>}
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
local = new Map()
|
|
102
|
+
/**
|
|
103
|
+
* Whether the backing in memory get updated from the push API
|
|
104
|
+
* @type {boolean}
|
|
105
|
+
* @private
|
|
106
|
+
*/
|
|
107
|
+
live = false
|
|
108
|
+
/**
|
|
109
|
+
* Last taker spend of this client by token, UNIX milliseconds
|
|
110
|
+
* @type {Map<string, number>}
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
spent = new Map() //TODO: drop this
|
|
114
|
+
/**
|
|
115
|
+
* Calls submitting transactions, run one at a time
|
|
116
|
+
* @type {Promise<void>}
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
queue = Promise.resolve()
|
|
120
|
+
/**
|
|
121
|
+
* Orders removed by this client that the indexer has not reported yet: expiration by order id, UNIX milliseconds
|
|
122
|
+
* @type {Map<string, number>}
|
|
123
|
+
* @private
|
|
124
|
+
*/
|
|
125
|
+
removed = new Map()
|
|
126
|
+
/**
|
|
127
|
+
* Allowances set by this client that the pushed state may not reflect yet, by token contract id
|
|
128
|
+
* @type {Map<string, {amount: bigint, liveUntil: number, replaced?: {allowance: bigint, liveUntil: number}, until: number}>}
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
approved = new Map()
|
|
132
|
+
/**
|
|
133
|
+
* @type {function(): void}
|
|
134
|
+
* @private
|
|
135
|
+
*/
|
|
136
|
+
resolveReady
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Start tracking this account
|
|
140
|
+
* @internal
|
|
141
|
+
*/
|
|
142
|
+
start() {
|
|
143
|
+
const {stream} = this.axis
|
|
144
|
+
this.unsubscribe = stream.subscribe('account', {address: this.address}, message => this.handle(message))
|
|
145
|
+
this.offOpen = stream.on('open', () => this.stopFallback())
|
|
146
|
+
this.offClose = stream.on('close', () => {
|
|
147
|
+
this.live = false //changes are missed until the next snapshot
|
|
148
|
+
this.startFallback()
|
|
149
|
+
})
|
|
150
|
+
//load over REST if the push API does not deliver the snapshot soon
|
|
151
|
+
this.initTimer = setTimeout(() => {
|
|
152
|
+
if (!this.loaded) {
|
|
153
|
+
this.refresh().catch(() => this.startFallback())
|
|
154
|
+
}
|
|
155
|
+
}, 3_000)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Stop tracking this account
|
|
160
|
+
*/
|
|
161
|
+
close() {
|
|
162
|
+
clearTimeout(this.initTimer)
|
|
163
|
+
this.stopFallback()
|
|
164
|
+
this.unsubscribe?.()
|
|
165
|
+
this.offOpen?.()
|
|
166
|
+
this.offClose?.()
|
|
167
|
+
this.axis.releaseAccount(this)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Reload current account state from the REST API
|
|
172
|
+
* @return {Promise<void>}
|
|
173
|
+
*/
|
|
174
|
+
async refresh() {
|
|
175
|
+
//the REST state lists only the tokens of the orders once nobody subscribes: keep the other balances
|
|
176
|
+
this.applySnapshot(await this.axis.api.getAccount(this.address), true)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Retrieve open orders, newest first
|
|
181
|
+
* @param {{market?: string|{base: string, quote: string}|string[], selling?: string, buying?: string}} [filter] - `market` is an {AxisMarket} or a pair of assets in any order
|
|
182
|
+
* @return {AccountOrder[]}
|
|
183
|
+
*/
|
|
184
|
+
getOrders({market, selling, buying} = {}) { //TODO: simplify market argument
|
|
185
|
+
const key = toMarketKey(market)
|
|
186
|
+
const res = []
|
|
187
|
+
for (const order of this.allOrders()) {
|
|
188
|
+
if (key && order.market !== key)
|
|
189
|
+
continue
|
|
190
|
+
if (selling && order.selling !== selling)
|
|
191
|
+
continue
|
|
192
|
+
if (buying && order.buying !== buying)
|
|
193
|
+
continue
|
|
194
|
+
res.push(order)
|
|
195
|
+
}
|
|
196
|
+
return res.sort(newestFirst)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Get open order by id
|
|
201
|
+
* @param {bigint|string} id
|
|
202
|
+
* @return {AccountOrder|undefined}
|
|
203
|
+
*/
|
|
204
|
+
getOrder(id) {
|
|
205
|
+
const key = id.toString()
|
|
206
|
+
if (this.isRemoved(key))
|
|
207
|
+
return undefined
|
|
208
|
+
return this.orders.get(key) ?? this.localOrders().find(order => order.id === key)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Calculate the remaining amount of the open orders selling the token across every market (what the allowance must cover)
|
|
213
|
+
* @param {string} asset - Token contract id
|
|
214
|
+
* @return {bigint}
|
|
215
|
+
*/
|
|
216
|
+
committed(asset) {
|
|
217
|
+
let sum = 0n
|
|
218
|
+
for (const order of this.allOrders()) {
|
|
219
|
+
if (order.selling === asset) {
|
|
220
|
+
sum += order.amount
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return sum
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Get spendable balance of the account in a token
|
|
228
|
+
* @param {string} asset - Token contract id
|
|
229
|
+
* @return {bigint|undefined} - Undefined if not tracked
|
|
230
|
+
*/
|
|
231
|
+
getBalance(asset) {
|
|
232
|
+
const backing = this.backing.get(asset)
|
|
233
|
+
return backing?.updated ? backing.balance : undefined
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Get spendable balances in every tracked token by token contract id
|
|
238
|
+
* @return {Map<string, bigint>}
|
|
239
|
+
*/
|
|
240
|
+
get balances() {
|
|
241
|
+
const res = new Map()
|
|
242
|
+
for (const [asset, backing] of this.backing) {
|
|
243
|
+
if (backing.updated) {
|
|
244
|
+
res.set(asset, backing.balance)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return res
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Whether the account exists on the ledger
|
|
252
|
+
* @return {boolean|undefined}
|
|
253
|
+
*/
|
|
254
|
+
get funded() {
|
|
255
|
+
const backing = this.backing.get(this.axis.nativeAsset)
|
|
256
|
+
return backing?.updated ? backing.authorized : undefined
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Get balance, allowance and commitments of the account in a token
|
|
261
|
+
* @param {string} asset - Token contract id
|
|
262
|
+
* @return {AccountAllowance}
|
|
263
|
+
*/
|
|
264
|
+
getAllowance(asset) {
|
|
265
|
+
const backing = this.backing.get(asset)
|
|
266
|
+
const committed = this.committed(asset)
|
|
267
|
+
if (!backing?.updated)
|
|
268
|
+
return {asset, known: false, committed}
|
|
269
|
+
return {
|
|
270
|
+
asset,
|
|
271
|
+
known: true,
|
|
272
|
+
balance: backing.balance,
|
|
273
|
+
allowance: backing.allowance,
|
|
274
|
+
liveUntil: backing.liveUntil,
|
|
275
|
+
authorized: backing.authorized,
|
|
276
|
+
pending: backing.pending,
|
|
277
|
+
available: backing.budget,
|
|
278
|
+
committed
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Sell tokens creating a limit remainder order if not executed in full
|
|
284
|
+
* @param {TradeParams} params - `amount` of `selling` to sell; `price` is the minimum `buying` per 1 `selling`
|
|
285
|
+
* @return {Promise<TradeResult>}
|
|
286
|
+
*/
|
|
287
|
+
async sell(params) {
|
|
288
|
+
return this.serialize(() => this.trade(TradeDirection.Sell, params))
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Buy tokens creating a limit remainder order if not executed in full
|
|
293
|
+
* @param {TradeParams} params - `amount` of `buying` to acquire; `price` is the maximum `selling` per 1 `buying`
|
|
294
|
+
* @return {Promise<TradeResult>}
|
|
295
|
+
*/
|
|
296
|
+
async buy(params) {
|
|
297
|
+
return this.serialize(() => this.trade(TradeDirection.Buy, params))
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Simulate a sell with the same crossing and approval as `sell`, without signing or submitting it
|
|
302
|
+
* @param {TradeParams} params - Same as `sell`
|
|
303
|
+
* @return {Promise<import('./index.js').TradeEstimate>} - Network fee and expected result
|
|
304
|
+
*/
|
|
305
|
+
async estimateSell(params) {
|
|
306
|
+
const {payload} = await this.prepareTrade(TradeDirection.Sell, params)
|
|
307
|
+
const client = await this.client()
|
|
308
|
+
return client.estimateSell(payload)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Simulate a buy with the same crossing and approval as `buy`, without signing or submitting it
|
|
313
|
+
* @param {TradeParams} params - Same as `buy`
|
|
314
|
+
* @return {Promise<import('./index.js').TradeEstimate>} - Network fee and expected result
|
|
315
|
+
*/
|
|
316
|
+
async estimateBuy(params) {
|
|
317
|
+
const {payload} = await this.prepareTrade(TradeDirection.Buy, params)
|
|
318
|
+
const client = await this.client()
|
|
319
|
+
return client.estimateBuy(payload)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Change the amount, price or expiration of open orders (omitted fields keep their current values). The allowance of
|
|
324
|
+
* every sold token follows what its open orders need: raised when they grow, or when it does not back the changed
|
|
325
|
+
* orders, lowered when they shrink
|
|
326
|
+
* @param {Array<{id: bigint|string, amount?: bigint, price?: bigint, expires?: number}>} updates
|
|
327
|
+
* @return {Promise<bigint[]>} - Updated order ids
|
|
328
|
+
*/
|
|
329
|
+
async update(updates) {
|
|
330
|
+
return this.serialize(async () => {
|
|
331
|
+
await this.whenReady()
|
|
332
|
+
const changes = updates.map(update => {
|
|
333
|
+
const current = this.getOrder(update.id)
|
|
334
|
+
if (!current && (update.amount === undefined || update.price === undefined))
|
|
335
|
+
throw new Error(`Order ${update.id} is not an open order of ${this.address}`)
|
|
336
|
+
return {
|
|
337
|
+
current,
|
|
338
|
+
update: {
|
|
339
|
+
id: BigInt(update.id),
|
|
340
|
+
amount: update.amount ?? current.amount,
|
|
341
|
+
price: update.price ?? current.price,
|
|
342
|
+
expires: update.expires ?? current?.expires ?? 0
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
})
|
|
346
|
+
return this.submitUpdates(changes, {grow: true})
|
|
347
|
+
})
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Cancel open orders, lowering the allowance of every sold token to what its remaining open orders need
|
|
352
|
+
* @param {Array<bigint|string>} ids
|
|
353
|
+
* @return {Promise<void>}
|
|
354
|
+
*/
|
|
355
|
+
async cancel(ids) {
|
|
356
|
+
await this.serialize(() => this.removeOrders(ids))
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Cancel all open orders, lowering the allowances like `cancel`
|
|
361
|
+
* @param {{market?: string|{base: string, quote: string}|string[]}} [filter]
|
|
362
|
+
* @return {Promise<string[]>} - Canceled order ids
|
|
363
|
+
*/
|
|
364
|
+
async cancelAll({market} = {}) {
|
|
365
|
+
return this.serialize(async () => {
|
|
366
|
+
await this.whenReady()
|
|
367
|
+
const ids = this.getOrders({market}).map(order => order.id)
|
|
368
|
+
if (ids.length) {
|
|
369
|
+
await this.removeOrders(ids)
|
|
370
|
+
}
|
|
371
|
+
return ids
|
|
372
|
+
})
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Set the allowances granted to the AXIS contract on tokens to zero, optionally canceling the open orders selling them
|
|
377
|
+
* in the same transactions. The revocation goes with the first transaction. Works while the contract is frozen
|
|
378
|
+
* @param {string[]} assets - Token contract ids
|
|
379
|
+
* @param {{cancelOrders?: boolean}} [options] - Also cancel every open order selling these tokens
|
|
380
|
+
* @return {Promise<AccountRevokeResult>}
|
|
381
|
+
* @throws {Error} - The submission error, with the `revoked` tokens and the `canceled` order ids done before it
|
|
382
|
+
*/
|
|
383
|
+
async revoke(assets, {cancelOrders = false} = {}) {
|
|
384
|
+
return this.serialize(async () => {
|
|
385
|
+
const tokens = [...new Set(assets)]
|
|
386
|
+
let ids = []
|
|
387
|
+
if (cancelOrders) {
|
|
388
|
+
await this.whenReady()
|
|
389
|
+
ids = [...this.allOrders()].filter(order => tokens.includes(order.selling)).map(order => order.id)
|
|
390
|
+
}
|
|
391
|
+
try {
|
|
392
|
+
const canceled = await this.removeOrders(ids, tokens)
|
|
393
|
+
return {revoked: tokens, canceled: canceled.map(id => id.toString())}
|
|
394
|
+
} catch (e) {
|
|
395
|
+
if (e instanceof Error) {
|
|
396
|
+
e.revoked = e.progress?.batches > 0 ? tokens : []
|
|
397
|
+
e.canceled = (e.progress?.updated ?? []).map(id => id.toString())
|
|
398
|
+
}
|
|
399
|
+
throw e
|
|
400
|
+
}
|
|
401
|
+
})
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Fill an open order against the book while keeping profits from price inefficiencies
|
|
406
|
+
* @param {bigint|string} takerOrderId - Order acting as the taker
|
|
407
|
+
* @param {Array<bigint|string>} [orders] - Counter orders, looked up automatically when omitted
|
|
408
|
+
* @return {Promise<{sold: bigint, bought: bigint, surplus: bigint}>}
|
|
409
|
+
*/
|
|
410
|
+
async crossfill(takerOrderId, orders) {
|
|
411
|
+
return this.serialize(async () => {
|
|
412
|
+
if (!orders) {
|
|
413
|
+
const taker = this.getOrder(takerOrderId) ?? await this.axis.getOrder(takerOrderId)
|
|
414
|
+
if (!taker)
|
|
415
|
+
throw new Error(`Order ${takerOrderId} not found`)
|
|
416
|
+
orders = (await this.findQuote(TradeDirection.Sell, taker.selling, taker.buying, taker.amount)).orders
|
|
417
|
+
}
|
|
418
|
+
const client = await this.client()
|
|
419
|
+
const [sold, bought, surplus] = await client.crossfill(this.address, BigInt(takerOrderId), orders.map(id => BigInt(id)))
|
|
420
|
+
return {sold, bought, surplus}
|
|
421
|
+
})
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Swap across one or several markets along the best route found by the AXIS API
|
|
426
|
+
* @param {SwapParams} params
|
|
427
|
+
* @return {Promise<{sold: bigint, bought: bigint, approve?: import('./index.js').Approval}>}
|
|
428
|
+
*/
|
|
429
|
+
async swap(params) {
|
|
430
|
+
return this.serialize(() => this.trySwap(params))
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* @param {SwapParams} params
|
|
435
|
+
* @return {Promise<{sold: bigint, bought: bigint, approve?: import('./index.js').Approval}>}
|
|
436
|
+
* @private
|
|
437
|
+
*/
|
|
438
|
+
async trySwap({selling, buying, amount, direction = TradeDirection.Sell, slippage = 0}) {
|
|
439
|
+
await this.whenReady()
|
|
440
|
+
const params = {sellingAsset: selling, buyingAsset: buying, amount: amount.toString()}
|
|
441
|
+
const quote = direction === TradeDirection.Buy ?
|
|
442
|
+
await this.axis.api.quoteBuy(params) :
|
|
443
|
+
await this.axis.api.quoteSell(params)
|
|
444
|
+
const best = quote.paths?.[0]
|
|
445
|
+
if (quote.status !== 'success' || !best?.path?.length)
|
|
446
|
+
throw new Error(quote.error || 'No route available')
|
|
447
|
+
const path = best.path.map(hop => ({asset: hop.buying, orders: hop.orders.filter(Boolean).map(id => BigInt(id))}))
|
|
448
|
+
const tolerance = BigInt(Math.round(slippage * 10_000))
|
|
449
|
+
let sellingAmount = BigInt(best.sold)
|
|
450
|
+
let buyingAmount = BigInt(best.bought)
|
|
451
|
+
if (direction === TradeDirection.Buy) {
|
|
452
|
+
sellingAmount = (sellingAmount * (10_000n + tolerance) + 9_999n) / 10_000n
|
|
453
|
+
} else {
|
|
454
|
+
buyingAmount = buyingAmount * (10_000n - tolerance) / 10_000n
|
|
455
|
+
}
|
|
456
|
+
//`sellingAmount` is the exact spend of a sell and the most a buy may spend
|
|
457
|
+
const approve = await this.planApproval(selling, sellingAmount)
|
|
458
|
+
if (approve) {
|
|
459
|
+
this.emit('approve', {asset: selling, ...approve})
|
|
460
|
+
}
|
|
461
|
+
const client = await this.client()
|
|
462
|
+
const [sold, bought] = await client.swap({
|
|
463
|
+
direction,
|
|
464
|
+
trader: this.address,
|
|
465
|
+
selling,
|
|
466
|
+
sellingAmount,
|
|
467
|
+
buyingAmount,
|
|
468
|
+
path,
|
|
469
|
+
approve
|
|
470
|
+
})
|
|
471
|
+
this.spent.set(selling, Date.now())
|
|
472
|
+
return {sold, bought, approve}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Compute approval for a call
|
|
477
|
+
* @param {string} asset - Token contract id
|
|
478
|
+
* @param {bigint} required - Max amount the call may spend
|
|
479
|
+
* @return {Promise<{amount: bigint, liveUntil: number}|undefined>}
|
|
480
|
+
*/
|
|
481
|
+
async planApproval(asset, required) {
|
|
482
|
+
await this.whenReady()
|
|
483
|
+
const allowance = await this.readAllowance(asset) ?? 0n //unknown: a redundant approval, never a shortfall
|
|
484
|
+
return planApproval({
|
|
485
|
+
required,
|
|
486
|
+
committed: this.committed(asset),
|
|
487
|
+
allowance,
|
|
488
|
+
liveUntil: this.backing.get(asset)?.liveUntil ?? 0,
|
|
489
|
+
ledger: await this.axis.getLedger(),
|
|
490
|
+
ttl: this.approvalTtl
|
|
491
|
+
})
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Current allowance of a token: the pushed value while it can be trusted, otherwise read over RPC
|
|
496
|
+
* @param {string} asset - Token contract id
|
|
497
|
+
* @return {Promise<bigint|undefined>} - Undefined when the RPC read fails
|
|
498
|
+
* @throws {Error} - If the token is not tracked and there is no RPC to read it from
|
|
499
|
+
* @private
|
|
500
|
+
*/
|
|
501
|
+
async readAllowance(asset) {
|
|
502
|
+
const backing = this.backing.get(asset)
|
|
503
|
+
if (this.isAllowanceCurrent(asset) || (backing?.updated && !this.axis.tokenBalances))
|
|
504
|
+
return backing.allowance
|
|
505
|
+
const {tokenBalances} = this.axis
|
|
506
|
+
if (!tokenBalances)
|
|
507
|
+
throw new Error(`The allowance of ${this.address} in ${asset} is not tracked: set the \`rpcUrl\` option to read it`)
|
|
508
|
+
await this.axis.getContractId() //the allowance spender
|
|
509
|
+
return tokenBalances.getAllowance(asset, this.address).catch(() => undefined)
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Check whether the pushed allowance of a token can be used as is
|
|
514
|
+
* @param {string} asset - Token contract id
|
|
515
|
+
* @return {boolean}
|
|
516
|
+
* @private
|
|
517
|
+
*/
|
|
518
|
+
isAllowanceCurrent(asset) {
|
|
519
|
+
const backing = this.backing.get(asset)
|
|
520
|
+
if (!this.live || !this.axis.stream.connected || !backing?.updated || backing.pending)
|
|
521
|
+
return false
|
|
522
|
+
if (this.pendingApproval(asset))
|
|
523
|
+
return false //set by this client, not confirmed by the push yet
|
|
524
|
+
const spent = this.spent.get(asset)
|
|
525
|
+
if (spent === undefined)
|
|
526
|
+
return true
|
|
527
|
+
if (Date.now() - spent < 30_000) //the spend lowers the allowance before the push reporting it arrives
|
|
528
|
+
return false //TODO: try to read allowance change from tx result meta changes
|
|
529
|
+
this.spent.delete(asset)
|
|
530
|
+
return true
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* @param {number} direction
|
|
535
|
+
* @param {TradeParams} params
|
|
536
|
+
* @return {Promise<TradeResult>}
|
|
537
|
+
* @private
|
|
538
|
+
*/
|
|
539
|
+
async trade(direction, params) {
|
|
540
|
+
const {payload, planned} = await this.prepareTrade(direction, params)
|
|
541
|
+
const {selling, buying, amount, price, expires, approve} = payload
|
|
542
|
+
if (planned && approve) {
|
|
543
|
+
this.emit('approve', {asset: selling, ...approve})
|
|
544
|
+
}
|
|
545
|
+
const client = await this.client()
|
|
546
|
+
const [sold, bought, createdId] = direction === TradeDirection.Buy ?
|
|
547
|
+
await client.buy(payload) :
|
|
548
|
+
await client.sell(payload)
|
|
549
|
+
this.spent.set(selling, Date.now())
|
|
550
|
+
let order
|
|
551
|
+
if (createdId) {
|
|
552
|
+
order = await this.trackCreated(createdId, {direction, selling, buying, amount, price, sold, bought, expires})
|
|
553
|
+
}
|
|
554
|
+
return {sold, bought, orderId: createdId ? createdId.toString() : undefined, order, approve}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Resolve the crossing orders, the price of a market order and the approval of a trade
|
|
559
|
+
* @param {number} direction
|
|
560
|
+
* @param {TradeParams} params
|
|
561
|
+
* @return {Promise<{payload: import('./index.js').TradeArguments, planned: boolean}>} - Contract call arguments,
|
|
562
|
+
* `planned` if the approval was planned by the account rather than supplied
|
|
563
|
+
* @private
|
|
564
|
+
*/
|
|
565
|
+
async prepareTrade(direction, {selling, buying, amount, price, kind, expires, orders, approve}) {
|
|
566
|
+
await this.whenReady()
|
|
567
|
+
amount = BigInt(amount)
|
|
568
|
+
const market = price === undefined || price === null
|
|
569
|
+
if (kind === undefined) {
|
|
570
|
+
kind = market ? OrderKind.Fill : OrderKind.Limit
|
|
571
|
+
}
|
|
572
|
+
let crossing
|
|
573
|
+
if (!orders || market) {
|
|
574
|
+
//a limit order crosses only the orders its price reaches (the contract threshold: the price of a buy, the
|
|
575
|
+
//inverted price of a sell) and rests with the remainder, so a partial crossing is quoted as well
|
|
576
|
+
const maxPrice = market ? undefined : direction === TradeDirection.Buy ? BigInt(price) : invertPrice(BigInt(price))
|
|
577
|
+
crossing = await this.findQuote(direction, selling, buying, amount, maxPrice)
|
|
578
|
+
}
|
|
579
|
+
if (market) {
|
|
580
|
+
//cross everything the quote found: the limit is the worst maker price (makers price `selling` per
|
|
581
|
+
//`buying` of a buyer; seen from a seller, invert it)
|
|
582
|
+
if (!crossing.worstPrice)
|
|
583
|
+
throw new Error('Not enough liquidity for a market order')
|
|
584
|
+
price = direction === TradeDirection.Buy ? crossing.worstPrice : invertPrice(crossing.worstPrice)
|
|
585
|
+
}
|
|
586
|
+
price = BigInt(price)
|
|
587
|
+
const planned = approve === undefined
|
|
588
|
+
if (planned) {
|
|
589
|
+
const required = direction === TradeDirection.Buy ? maxQuoteSpend(amount, price) : amount
|
|
590
|
+
approve = await this.planApproval(selling, required)
|
|
591
|
+
}
|
|
592
|
+
const payload = {
|
|
593
|
+
kind,
|
|
594
|
+
trader: this.address,
|
|
595
|
+
amount,
|
|
596
|
+
selling,
|
|
597
|
+
buying,
|
|
598
|
+
price,
|
|
599
|
+
orders: (orders ?? crossing.orders).map(id => BigInt(id)),
|
|
600
|
+
expires,
|
|
601
|
+
approve: approve || undefined
|
|
602
|
+
}
|
|
603
|
+
return {payload, planned}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Count an order this client created as committed until the indexer reports it
|
|
608
|
+
* @param {bigint} id
|
|
609
|
+
* @param {{}} trade - Trade parameters and result, for an estimate if the order cannot be read
|
|
610
|
+
* @return {Promise<AccountOrder|undefined>}
|
|
611
|
+
* @private
|
|
612
|
+
*/
|
|
613
|
+
async trackCreated(id, {direction, selling, buying, amount, price, sold, bought, expires}) {
|
|
614
|
+
const key = id.toString()
|
|
615
|
+
if (this.orders.has(key))
|
|
616
|
+
return this.orders.get(key) //already reported
|
|
617
|
+
let stored
|
|
618
|
+
try {
|
|
619
|
+
const client = await this.client()
|
|
620
|
+
stored = await client.order(id)
|
|
621
|
+
} catch (e) {
|
|
622
|
+
//estimate below
|
|
623
|
+
}
|
|
624
|
+
if (this.orders.has(key))
|
|
625
|
+
return this.orders.get(key)
|
|
626
|
+
let orderAmount
|
|
627
|
+
let orderPrice
|
|
628
|
+
if (stored) {
|
|
629
|
+
orderAmount = stored.amount
|
|
630
|
+
orderPrice = stored.price
|
|
631
|
+
} else if (direction === TradeDirection.Sell) {
|
|
632
|
+
orderAmount = amount - sold
|
|
633
|
+
orderPrice = price
|
|
634
|
+
} else {
|
|
635
|
+
//the remainder of a buy sells `selling` for the rest of `buying` at the limit
|
|
636
|
+
orderAmount = maxQuoteSpend(amount - bought, price)
|
|
637
|
+
orderPrice = invertPrice(price)
|
|
638
|
+
}
|
|
639
|
+
const now = Date.now()
|
|
640
|
+
/** @type {AccountOrder} */
|
|
641
|
+
const order = {
|
|
642
|
+
id: key,
|
|
643
|
+
owner: this.address,
|
|
644
|
+
selling,
|
|
645
|
+
buying,
|
|
646
|
+
market: pairKey(selling, buying),
|
|
647
|
+
status: 'ACTIVE',
|
|
648
|
+
price: orderPrice,
|
|
649
|
+
amount: orderAmount,
|
|
650
|
+
quote: orderAmount,
|
|
651
|
+
expires: Number(stored?.expires ?? expires ?? 0),
|
|
652
|
+
created: now,
|
|
653
|
+
updated: now,
|
|
654
|
+
cursor: undefined,
|
|
655
|
+
backed: null,
|
|
656
|
+
backedPct: null,
|
|
657
|
+
backingPending: true,
|
|
658
|
+
filledPct: 0,
|
|
659
|
+
pending: true
|
|
660
|
+
}
|
|
661
|
+
this.local.set(key, {order, expires: now + LOCAL_ORDER_TTL})
|
|
662
|
+
this.emit('pending', order)
|
|
663
|
+
this.emit('change')
|
|
664
|
+
return order
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Crossing orders of a direct-market trade and the worst price among them
|
|
669
|
+
* @param {number} direction
|
|
670
|
+
* @param {string} selling
|
|
671
|
+
* @param {string} buying
|
|
672
|
+
* @param {bigint} amount - `selling` amount of a sell, `buying` amount of a buy
|
|
673
|
+
* @param {bigint} [maxPrice] - Highest maker price to cross, for a limit order
|
|
674
|
+
* @return {Promise<{orders: string[], worstPrice: bigint|null}>}
|
|
675
|
+
* @private
|
|
676
|
+
*/
|
|
677
|
+
async findQuote(direction, selling, buying, amount, maxPrice) {
|
|
678
|
+
const params = {sellingAsset: selling, buyingAsset: buying, amount: amount.toString(), direct: true, maxPrice}
|
|
679
|
+
try {
|
|
680
|
+
const quote = direction === TradeDirection.Buy ?
|
|
681
|
+
await this.axis.api.quoteBuy(params) :
|
|
682
|
+
await this.axis.api.quoteSell(params)
|
|
683
|
+
const hop = quote.status === 'success' ? quote.paths?.[0]?.path?.[0] : null
|
|
684
|
+
if (hop)
|
|
685
|
+
return {orders: hop.orders ?? [], worstPrice: hop.worstPrice ? BigInt(hop.worstPrice) : null}
|
|
686
|
+
} catch (e) {
|
|
687
|
+
//nothing to cross: a limit order simply rests
|
|
688
|
+
}
|
|
689
|
+
return {orders: [], worstPrice: null}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Run calls submitting transactions one at a time: each one reads the sequence number and plans its approvals only
|
|
694
|
+
* after the previous one settled, so concurrent calls neither collide on the sequence number nor overwrite each
|
|
695
|
+
* other's approvals
|
|
696
|
+
* @template T
|
|
697
|
+
* @param {function(): Promise<T>} call
|
|
698
|
+
* @return {Promise<T>}
|
|
699
|
+
* @private
|
|
700
|
+
*/
|
|
701
|
+
serialize(call) {
|
|
702
|
+
const result = this.queue.then(call)
|
|
703
|
+
this.queue = result.then(() => undefined, () => undefined)
|
|
704
|
+
return result
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Remove orders, lowering the allowances to what the remaining orders need
|
|
709
|
+
* @param {Array<bigint|string>} ids
|
|
710
|
+
* @param {string[]} [revoke] - Tokens to revoke the allowance on with the first transaction
|
|
711
|
+
* @return {Promise<bigint[]>} - Removed order ids
|
|
712
|
+
* @private
|
|
713
|
+
*/
|
|
714
|
+
removeOrders(ids, revoke) {
|
|
715
|
+
const changes = ids.map(id => ({current: this.getOrder(id), update: {id: BigInt(id), amount: 0n, price: 0n, expires: 0}}))
|
|
716
|
+
return this.submitUpdates(changes, {revoke})
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Send order changes in `update` batches. Each batch carries the approvals that keep the allowance of every token it
|
|
721
|
+
* changes at what the open orders selling it need once the batch applies: raised when they grow (`grow` only), or
|
|
722
|
+
* when the allowance does not back the changed orders, lowered when they shrink. The `revoke` tokens are set to zero
|
|
723
|
+
* with the first batch
|
|
724
|
+
* @param {Array<{current?: AccountOrder, update: OrderUpdate}>} changes - New values per order, with the order in
|
|
725
|
+
* memory when known
|
|
726
|
+
* @param {{grow?: boolean, revoke?: string[]}} [options] - `grow`: allowances may be raised (updates, never cancels)
|
|
727
|
+
* @return {Promise<bigint[]>} - Updated or removed order ids
|
|
728
|
+
* @throws {Error} - The submission error, with `progress` `{updated, batches}` done before it
|
|
729
|
+
* @private
|
|
730
|
+
*/
|
|
731
|
+
async submitUpdates(changes, {grow = false, revoke = []} = {}) {
|
|
732
|
+
const batches = splitBatches(changes, revoke)
|
|
733
|
+
if (!batches.length)
|
|
734
|
+
return []
|
|
735
|
+
const plan = await this.snapshotAllowances(changes, revoke, grow)
|
|
736
|
+
const client = await this.client()
|
|
737
|
+
const ids = []
|
|
738
|
+
for (let i = 0; i < batches.length; i++) {
|
|
739
|
+
const batch = batches[i]
|
|
740
|
+
const approvals = i === 0 ? revoke.map(asset => ({asset, amount: 0n, liveUntil: 0})) : []
|
|
741
|
+
if (plan) {
|
|
742
|
+
approvals.push(...planBatch(plan, batch, grow))
|
|
743
|
+
}
|
|
744
|
+
for (const approval of approvals) {
|
|
745
|
+
this.emit('approve', approval)
|
|
746
|
+
}
|
|
747
|
+
try {
|
|
748
|
+
ids.push(...await client.update({trader: this.address, updates: batch.map(change => change.update), approvals}))
|
|
749
|
+
} catch (e) {
|
|
750
|
+
if (e instanceof Error) {
|
|
751
|
+
e.progress = {updated: ids, batches: i}
|
|
752
|
+
}
|
|
753
|
+
throw e
|
|
754
|
+
}
|
|
755
|
+
this.applyBatch(batch, approvals)
|
|
756
|
+
}
|
|
757
|
+
return ids
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Committed amounts and allowances of the tokens the changed orders sell, read once before the first batch: every
|
|
762
|
+
* batch target derives from them, as pushes arriving between batches already drop the removed orders
|
|
763
|
+
* @param {Array<{current?: AccountOrder}>} changes
|
|
764
|
+
* @param {string[]} revoke - Tokens revoked anyway
|
|
765
|
+
* @param {boolean} grow - Whether allowances may be raised: a failed read throws then, otherwise nothing is planned
|
|
766
|
+
* @return {Promise<AllowancePlan|null>} - Null when no allowance is planned
|
|
767
|
+
* @private
|
|
768
|
+
*/
|
|
769
|
+
async snapshotAllowances(changes, revoke, grow) {
|
|
770
|
+
const assets = new Set()
|
|
771
|
+
for (const {current} of changes) {
|
|
772
|
+
if (current && !revoke.includes(current.selling)) {
|
|
773
|
+
assets.add(current.selling)
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
if (!assets.size || !this.loaded)
|
|
777
|
+
return null
|
|
778
|
+
try {
|
|
779
|
+
const ledger = await this.axis.getLedger()
|
|
780
|
+
const tokens = new Map()
|
|
781
|
+
for (const asset of assets) {
|
|
782
|
+
const liveUntil = this.backing.get(asset)?.liveUntil ?? 0
|
|
783
|
+
let allowance = await this.readAllowance(asset)
|
|
784
|
+
if (allowance !== undefined && liveUntil > 0 && liveUntil < ledger) {
|
|
785
|
+
allowance = 0n //an expired allowance reads as zero
|
|
786
|
+
}
|
|
787
|
+
tokens.set(asset, {committed: this.committed(asset), allowance, liveUntil, change: 0n})
|
|
788
|
+
}
|
|
789
|
+
return {ledger, ttl: this.approvalTtl, tokens}
|
|
790
|
+
} catch (e) {
|
|
791
|
+
if (grow)
|
|
792
|
+
throw e
|
|
793
|
+
return null //a cancel goes through without lowering the allowances
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Reflect a confirmed `update` batch before the indexer reports it: removed orders leave the open orders, changed
|
|
799
|
+
* ones take their new values, approved allowances replace the pushed ones
|
|
800
|
+
* @param {Array<{current?: AccountOrder, update: OrderUpdate}>} batch
|
|
801
|
+
* @param {import('./index.js').Approval[]} approvals
|
|
802
|
+
* @private
|
|
803
|
+
*/
|
|
804
|
+
applyBatch(batch, approvals) {
|
|
805
|
+
const until = Date.now() + LOCAL_ORDER_TTL
|
|
806
|
+
for (const {current, update} of batch) {
|
|
807
|
+
const key = update.id.toString()
|
|
808
|
+
if (update.amount === 0n) {
|
|
809
|
+
this.local.delete(key)
|
|
810
|
+
if (this.orders.has(key)) {
|
|
811
|
+
this.removed.set(key, until)
|
|
812
|
+
}
|
|
813
|
+
} else if (current) {
|
|
814
|
+
Object.assign(current, {amount: update.amount, price: update.price, expires: Number(update.expires)})
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
for (const {asset, amount, liveUntil} of approvals) {
|
|
818
|
+
const backing = this.backing.get(asset)
|
|
819
|
+
this.approved.set(asset, {
|
|
820
|
+
amount,
|
|
821
|
+
liveUntil,
|
|
822
|
+
replaced: backing && {allowance: backing.allowance, liveUntil: backing.liveUntil},
|
|
823
|
+
until: Date.now() + APPROVAL_CONFIRM_TIMEOUT
|
|
824
|
+
})
|
|
825
|
+
if (backing) {
|
|
826
|
+
const updated = withAllowance(backing, amount, liveUntil)
|
|
827
|
+
this.backing.set(asset, updated)
|
|
828
|
+
this.emit('backing', {asset, backing: updated})
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
this.emit('change')
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Keep an allowance this client set over an incoming record that still shows the allowance it replaced
|
|
836
|
+
* @param {string} asset
|
|
837
|
+
* @param {AccountBacking} backing - Incoming record
|
|
838
|
+
* @return {AccountBacking}
|
|
839
|
+
* @private
|
|
840
|
+
*/
|
|
841
|
+
reconcileBacking(asset, backing) {
|
|
842
|
+
const expected = this.pendingApproval(asset)
|
|
843
|
+
if (!expected)
|
|
844
|
+
return backing
|
|
845
|
+
const {replaced} = expected
|
|
846
|
+
if (replaced && backing.allowance === replaced.allowance && backing.liveUntil === replaced.liveUntil)
|
|
847
|
+
return withAllowance(backing, expected.amount, expected.liveUntil)
|
|
848
|
+
this.approved.delete(asset) //the record shows the approval, or a later change
|
|
849
|
+
return backing
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Allowance set by this client that the pushed state did not confirm yet
|
|
854
|
+
* @param {string} asset
|
|
855
|
+
* @return {{amount: bigint, liveUntil: number, replaced?: {allowance: bigint, liveUntil: number}}|undefined}
|
|
856
|
+
* @private
|
|
857
|
+
*/
|
|
858
|
+
pendingApproval(asset) {
|
|
859
|
+
const expected = this.approved.get(asset)
|
|
860
|
+
if (expected && expected.until < Date.now()) {
|
|
861
|
+
this.approved.delete(asset)
|
|
862
|
+
return undefined
|
|
863
|
+
}
|
|
864
|
+
return expected
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* Whether an order was removed by this client and the indexer has not reported it yet
|
|
869
|
+
* @param {string} id
|
|
870
|
+
* @return {boolean}
|
|
871
|
+
* @private
|
|
872
|
+
*/
|
|
873
|
+
isRemoved(id) {
|
|
874
|
+
const until = this.removed.get(id)
|
|
875
|
+
if (until === undefined)
|
|
876
|
+
return false
|
|
877
|
+
if (until < Date.now()) {
|
|
878
|
+
this.removed.delete(id)
|
|
879
|
+
return false
|
|
880
|
+
}
|
|
881
|
+
return true
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* @return {Promise<AxisContractClient>}
|
|
886
|
+
* @private
|
|
887
|
+
*/
|
|
888
|
+
async client() {
|
|
889
|
+
if (!this.signTransaction)
|
|
890
|
+
throw new Error(`No signTransaction callback for ${this.address}: pass it to axis.account()`)
|
|
891
|
+
return this.axis.contractClient({publicKey: this.address, signTransaction: this.signTransaction})
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* @param {number} [timeout]
|
|
896
|
+
* @return {Promise<void>}
|
|
897
|
+
* @private
|
|
898
|
+
*/
|
|
899
|
+
async whenReady(timeout = READY_TIMEOUT) {
|
|
900
|
+
if (this.loaded)
|
|
901
|
+
return
|
|
902
|
+
let timer
|
|
903
|
+
try {
|
|
904
|
+
await Promise.race([
|
|
905
|
+
this.ready,
|
|
906
|
+
new Promise((resolve, reject) => {
|
|
907
|
+
timer = setTimeout(() => reject(new Error('The account state is not available: check the AXIS API connection')), timeout)
|
|
908
|
+
})
|
|
909
|
+
])
|
|
910
|
+
} finally {
|
|
911
|
+
clearTimeout(timer)
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* @param {StreamMessage} message
|
|
917
|
+
* @private
|
|
918
|
+
*/
|
|
919
|
+
handle(message) {
|
|
920
|
+
switch (message.type) {
|
|
921
|
+
case 'account':
|
|
922
|
+
this.live = true
|
|
923
|
+
return this.applySnapshot(message)
|
|
924
|
+
case 'order':
|
|
925
|
+
return this.applyOrder(message)
|
|
926
|
+
case 'backing':
|
|
927
|
+
return this.applyBacking(message)
|
|
928
|
+
case 'trade':
|
|
929
|
+
return this.emit('trade', message.trade)
|
|
930
|
+
case 'swap':
|
|
931
|
+
return this.emit('swap', message.swap)
|
|
932
|
+
case 'error':
|
|
933
|
+
return this.emit('error', new Error(message.error))
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Replace the state with a snapshot
|
|
939
|
+
* @param {ApiAccount} state
|
|
940
|
+
* @param {boolean} [mergeBacking] - Keep the tokens missing from the snapshot
|
|
941
|
+
* @private
|
|
942
|
+
*/
|
|
943
|
+
applySnapshot(state, mergeBacking = false) {
|
|
944
|
+
const previous = this.orders
|
|
945
|
+
const next = new Map()
|
|
946
|
+
for (const raw of state.orders || []) {
|
|
947
|
+
const order = parseApiOrder(raw)
|
|
948
|
+
next.set(order.id, order)
|
|
949
|
+
this.local.delete(order.id)
|
|
950
|
+
}
|
|
951
|
+
this.orders = next
|
|
952
|
+
for (const id of this.removed.keys()) {
|
|
953
|
+
if (!next.has(id)) {
|
|
954
|
+
this.removed.delete(id)
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
const backing = mergeBacking ? new Map(this.backing) : new Map()
|
|
958
|
+
for (const [asset, raw] of Object.entries(state.backing || {})) {
|
|
959
|
+
backing.set(asset, this.reconcileBacking(asset, parseBacking(asset, raw)))
|
|
960
|
+
}
|
|
961
|
+
this.backing = backing
|
|
962
|
+
if (this.loaded) {
|
|
963
|
+
for (const [id, order] of previous) {
|
|
964
|
+
if (!next.has(id)) {
|
|
965
|
+
this.emit('remove', order)
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
for (const [id, order] of next) {
|
|
969
|
+
if (!previous.has(id)) {
|
|
970
|
+
this.emit('new', order)
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
if (!this.loaded) {
|
|
975
|
+
this.loaded = true
|
|
976
|
+
this.resolveReady()
|
|
977
|
+
this.emit('ready')
|
|
978
|
+
}
|
|
979
|
+
this.emit('change')
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/**
|
|
983
|
+
* @param {{action: string, order: import('./api-client.js').ApiOrder, fill?: {}}} message
|
|
984
|
+
* @private
|
|
985
|
+
*/
|
|
986
|
+
applyOrder({action, order: raw, fill: rawFill}) {
|
|
987
|
+
const order = parseApiOrder(raw)
|
|
988
|
+
this.local.delete(order.id)
|
|
989
|
+
const previous = this.orders.get(order.id)
|
|
990
|
+
const removed = action === 'filled' || action === 'cancel' || action === 'expire'
|
|
991
|
+
if (removed) {
|
|
992
|
+
this.orders.delete(order.id)
|
|
993
|
+
this.removed.delete(order.id)
|
|
994
|
+
} else {
|
|
995
|
+
if (previous && order.backed === null) {
|
|
996
|
+
//the event serialization may lack the split; keep the last known one until the backing update
|
|
997
|
+
order.backed = previous.backed
|
|
998
|
+
order.backedPct = previous.backedPct
|
|
999
|
+
order.backingPending = previous.backingPending
|
|
1000
|
+
}
|
|
1001
|
+
this.orders.set(order.id, order)
|
|
1002
|
+
}
|
|
1003
|
+
const fill = rawFill && {
|
|
1004
|
+
order,
|
|
1005
|
+
sold: BigInt(rawFill.sold),
|
|
1006
|
+
bought: BigInt(rawFill.bought),
|
|
1007
|
+
taker: rawFill.taker,
|
|
1008
|
+
trade: rawFill.trade?.toString(),
|
|
1009
|
+
ts: Number(rawFill.ts) * 1000,
|
|
1010
|
+
partial: action === 'fill'
|
|
1011
|
+
}
|
|
1012
|
+
this.emit(action, fill || order)
|
|
1013
|
+
if (removed) {
|
|
1014
|
+
this.emit('remove', order)
|
|
1015
|
+
}
|
|
1016
|
+
this.emit('order', {action, order, fill})
|
|
1017
|
+
this.emit('change')
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* @param {{asset: string, backing: import('./api-client.js').ApiBacking|null, backed?: Object<string, string>}} message
|
|
1022
|
+
* @private
|
|
1023
|
+
*/
|
|
1024
|
+
applyBacking({asset, backing: raw, backed}) {
|
|
1025
|
+
const backing = raw ? this.reconcileBacking(asset, parseBacking(asset, raw)) : undefined
|
|
1026
|
+
if (backing) {
|
|
1027
|
+
this.backing.set(asset, backing)
|
|
1028
|
+
} else {
|
|
1029
|
+
this.backing.delete(asset)
|
|
1030
|
+
}
|
|
1031
|
+
for (const [id, amount] of Object.entries(backed || {})) {
|
|
1032
|
+
const order = this.orders.get(id)
|
|
1033
|
+
if (!order)
|
|
1034
|
+
continue
|
|
1035
|
+
order.backed = BigInt(amount)
|
|
1036
|
+
order.backedPct = share(order.backed, order.amount)
|
|
1037
|
+
order.backingPending = !backing || backing.pending || !backing.updated
|
|
1038
|
+
}
|
|
1039
|
+
this.emit('backing', {asset, backing})
|
|
1040
|
+
this.emit('change')
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* @return {Iterable<AccountOrder>}
|
|
1045
|
+
* @private
|
|
1046
|
+
*/
|
|
1047
|
+
* allOrders() {
|
|
1048
|
+
for (const order of this.orders.values()) {
|
|
1049
|
+
if (!this.isRemoved(order.id)) {
|
|
1050
|
+
yield order
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
yield* this.localOrders()
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Get local orders that haven't expired
|
|
1058
|
+
* @return {AccountOrder[]}
|
|
1059
|
+
* @private
|
|
1060
|
+
*/
|
|
1061
|
+
localOrders() {
|
|
1062
|
+
const now = Date.now()
|
|
1063
|
+
const res = []
|
|
1064
|
+
for (const [id, {order, expires}] of this.local) {
|
|
1065
|
+
if (expires < now || this.orders.has(id)) {
|
|
1066
|
+
this.local.delete(id)
|
|
1067
|
+
} else {
|
|
1068
|
+
res.push(order)
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
return res
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* @private
|
|
1076
|
+
*/
|
|
1077
|
+
startFallback() {
|
|
1078
|
+
if (this.fallbackTimer)
|
|
1079
|
+
return
|
|
1080
|
+
this.fallbackTimer = setInterval(() => {
|
|
1081
|
+
this.refresh().catch(() => {
|
|
1082
|
+
//retried on the next tick
|
|
1083
|
+
})
|
|
1084
|
+
}, this.axis.fallbackPollInterval)
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
/**
|
|
1088
|
+
* @private
|
|
1089
|
+
*/
|
|
1090
|
+
stopFallback() {
|
|
1091
|
+
clearInterval(this.fallbackTimer)
|
|
1092
|
+
this.fallbackTimer = undefined
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
/**
|
|
1097
|
+
* Convert an API order to the client form (bigint amounts and prices, numeric timestamps)
|
|
1098
|
+
* @param {ApiOrder} raw
|
|
1099
|
+
* @return {AccountOrder}
|
|
1100
|
+
*/
|
|
1101
|
+
export function parseApiOrder(raw) {
|
|
1102
|
+
const amount = BigInt(raw.amount)
|
|
1103
|
+
const quote = BigInt(raw.quote ?? raw.amount)
|
|
1104
|
+
//unconfirmed until loaded, and while a reload confirming the latest event is due
|
|
1105
|
+
const confirmed = raw.backed !== undefined && !!raw.backing?.updated && !raw.backing?.pending
|
|
1106
|
+
const backed = raw.backed !== undefined ? BigInt(raw.backed) : null
|
|
1107
|
+
return {
|
|
1108
|
+
id: raw.id,
|
|
1109
|
+
owner: raw.owner,
|
|
1110
|
+
selling: raw.selling,
|
|
1111
|
+
buying: raw.buying,
|
|
1112
|
+
market: pairKey(raw.selling, raw.buying),
|
|
1113
|
+
status: raw.status,
|
|
1114
|
+
price: BigInt(raw.price),
|
|
1115
|
+
amount,
|
|
1116
|
+
quote,
|
|
1117
|
+
expires: raw.expires ? Math.floor(parseApiDate(raw.expires) / 1000) : 0,
|
|
1118
|
+
created: parseApiDate(raw.created),
|
|
1119
|
+
updated: parseApiDate(raw.updated),
|
|
1120
|
+
cursor: raw.cursor !== undefined ? BigInt(raw.cursor) : undefined,
|
|
1121
|
+
backed,
|
|
1122
|
+
backedPct: backed === null ? null : share(backed, amount),
|
|
1123
|
+
backingPending: !confirmed,
|
|
1124
|
+
filledPct: quote > 0n ? Math.max(0, Math.min(1, 1 - Number(amount * 10_000n / quote) / 10_000)) : 0,
|
|
1125
|
+
pending: false
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/**
|
|
1130
|
+
* @param {string} asset
|
|
1131
|
+
* @param {ApiBacking} raw
|
|
1132
|
+
* @return {AccountBacking}
|
|
1133
|
+
*/
|
|
1134
|
+
function parseBacking(asset, raw) {
|
|
1135
|
+
return {
|
|
1136
|
+
asset,
|
|
1137
|
+
balance: BigInt(raw.balance),
|
|
1138
|
+
allowance: BigInt(raw.allowance),
|
|
1139
|
+
liveUntil: raw.liveUntil || 0,
|
|
1140
|
+
authorized: raw.authorized !== false,
|
|
1141
|
+
budget: BigInt(raw.budget ?? 0),
|
|
1142
|
+
updated: parseApiDate(raw.updated),
|
|
1143
|
+
skipped: parseApiDate(raw.skipped),
|
|
1144
|
+
pending: raw.pending === true
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* @param {bigint} part
|
|
1150
|
+
* @param {bigint} total
|
|
1151
|
+
* @return {number} - 0..1
|
|
1152
|
+
*/
|
|
1153
|
+
function share(part, total) {
|
|
1154
|
+
if (total <= 0n)
|
|
1155
|
+
return 1
|
|
1156
|
+
return Math.max(0, Math.min(1, Number(part * 10_000n / total) / 10_000))
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* Split order changes into `update` batches, leaving event room for the approvals a batch may carry
|
|
1161
|
+
* @param {Array<{current?: AccountOrder, update: OrderUpdate}>} changes
|
|
1162
|
+
* @param {string[]} revoke - Tokens revoked with the first batch
|
|
1163
|
+
* @return {Array<Array<{current?: AccountOrder, update: OrderUpdate}>>}
|
|
1164
|
+
*/
|
|
1165
|
+
function splitBatches(changes, revoke) {
|
|
1166
|
+
const batches = []
|
|
1167
|
+
let batch
|
|
1168
|
+
let tokens
|
|
1169
|
+
for (const change of changes) {
|
|
1170
|
+
const asset = change.current?.selling
|
|
1171
|
+
if (batch) {
|
|
1172
|
+
const approvals = tokens.size + (asset && !tokens.has(asset) ? 1 : 0)
|
|
1173
|
+
if (batch.length + 1 + APPROVAL_BATCH_WEIGHT * approvals > UPDATE_BATCH) {
|
|
1174
|
+
batch = undefined
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
if (!batch) {
|
|
1178
|
+
batch = []
|
|
1179
|
+
tokens = new Set(batches.length ? [] : revoke)
|
|
1180
|
+
batches.push(batch)
|
|
1181
|
+
}
|
|
1182
|
+
batch.push(change)
|
|
1183
|
+
if (asset) {
|
|
1184
|
+
tokens.add(asset)
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
if (!batches.length && revoke.length) {
|
|
1188
|
+
batches.push([]) //a revocation alone
|
|
1189
|
+
}
|
|
1190
|
+
return batches
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* Approvals of one `update` batch, advancing the planned allowances: an allowance grows only when the batch
|
|
1195
|
+
* grows the orders selling the token, or when it does not back the changed orders (the contract checks that), and
|
|
1196
|
+
* lowered when the batch shrinks them
|
|
1197
|
+
* @param {AllowancePlan} plan
|
|
1198
|
+
* @param {Array<{current?: AccountOrder, update: OrderUpdate}>} batch
|
|
1199
|
+
* @param {boolean} grow - Whether allowances may be raised
|
|
1200
|
+
* @return {import('./index.js').Approval[]}
|
|
1201
|
+
*/
|
|
1202
|
+
function planBatch({ledger, ttl, tokens}, batch, grow) {
|
|
1203
|
+
//change of the committed amount and the new amounts of the changed orders, per sold token
|
|
1204
|
+
const changed = new Map()
|
|
1205
|
+
for (const {current, update} of batch) {
|
|
1206
|
+
if (!current || !tokens.has(current.selling))
|
|
1207
|
+
continue
|
|
1208
|
+
const entry = changed.get(current.selling) ?? {delta: 0n, need: 0n}
|
|
1209
|
+
entry.delta += update.amount - current.amount
|
|
1210
|
+
entry.need += update.amount
|
|
1211
|
+
changed.set(current.selling, entry)
|
|
1212
|
+
}
|
|
1213
|
+
const approvals = []
|
|
1214
|
+
for (const [asset, {delta, need}] of changed) {
|
|
1215
|
+
const token = tokens.get(asset)
|
|
1216
|
+
token.change += delta
|
|
1217
|
+
let approval
|
|
1218
|
+
if (grow && (delta > 0n || (token.allowance ?? 0n) < need)) {
|
|
1219
|
+
//unknown: a redundant approval, never a shortfall
|
|
1220
|
+
approval = planApproval({required: token.change, committed: token.committed, allowance: token.allowance ?? 0n, liveUntil: token.liveUntil, ledger, ttl})
|
|
1221
|
+
} else if (delta < 0n && token.allowance !== undefined) {
|
|
1222
|
+
approval = planReduction({target: token.committed + token.change, allowance: token.allowance, liveUntil: token.liveUntil, ledger, ttl})
|
|
1223
|
+
}
|
|
1224
|
+
if (approval) {
|
|
1225
|
+
approvals.push({asset, ...approval})
|
|
1226
|
+
token.allowance = approval.amount
|
|
1227
|
+
token.liveUntil = approval.liveUntil
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
return approvals
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
/**
|
|
1234
|
+
* @param {AccountBacking} backing
|
|
1235
|
+
* @param {bigint} allowance
|
|
1236
|
+
* @param {number} liveUntil
|
|
1237
|
+
* @return {AccountBacking} - The record with another allowance
|
|
1238
|
+
*/
|
|
1239
|
+
function withAllowance(backing, allowance, liveUntil) {
|
|
1240
|
+
const budget = !backing.authorized ? 0n : backing.balance < allowance ? backing.balance : allowance
|
|
1241
|
+
return {...backing, allowance, liveUntil, budget}
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/**
|
|
1245
|
+
* @param {string|{base: string, quote: string}|string[]|undefined} market
|
|
1246
|
+
* @return {string|undefined}
|
|
1247
|
+
*/
|
|
1248
|
+
function toMarketKey(market) {
|
|
1249
|
+
if (!market)
|
|
1250
|
+
return undefined
|
|
1251
|
+
if (typeof market === 'string') {
|
|
1252
|
+
const [x, y] = market.split('/')
|
|
1253
|
+
return pairKey(x, y)
|
|
1254
|
+
}
|
|
1255
|
+
if (Array.isArray(market))
|
|
1256
|
+
return pairKey(market[0], market[1])
|
|
1257
|
+
return pairKey(market.base, market.quote)
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* Newest first: unconfirmed local orders, then by creation position
|
|
1262
|
+
* @param {AccountOrder} x
|
|
1263
|
+
* @param {AccountOrder} y
|
|
1264
|
+
* @return {number}
|
|
1265
|
+
*/
|
|
1266
|
+
function newestFirst(x, y) {
|
|
1267
|
+
if (x.cursor === undefined || y.cursor === undefined)
|
|
1268
|
+
return (x.cursor === undefined ? 0 : 1) - (y.cursor === undefined ? 0 : 1) || y.created - x.created
|
|
1269
|
+
return x.cursor < y.cursor ? 1 : x.cursor > y.cursor ? -1 : 0
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* @typedef {{}} AccountOrder - Open order of a tracked account
|
|
1274
|
+
* @property {string} id - Order id (decimal u128)
|
|
1275
|
+
* @property {string} owner - Maker address
|
|
1276
|
+
* @property {string} selling - Sold token
|
|
1277
|
+
* @property {string} buying - Bought token
|
|
1278
|
+
* @property {string} market - Market key
|
|
1279
|
+
* @property {'ACTIVE'|'FILLED'|'CANCELED'|'EXPIRED'} status - Current status
|
|
1280
|
+
* @property {bigint} price - `buying` per 1 `selling`, contract scale (18 decimals)
|
|
1281
|
+
* @property {bigint} amount - Amount of `selling` left
|
|
1282
|
+
* @property {bigint} quote - Amount of `selling` at creation
|
|
1283
|
+
* @property {number} expires - Expiration UNIX timestamp in seconds, 0 = none
|
|
1284
|
+
* @property {number} created - Creation time, UNIX milliseconds
|
|
1285
|
+
* @property {number} updated - Last change, UNIX milliseconds
|
|
1286
|
+
* @property {bigint} [cursor] - Paging cursor (undefined for unconfirmed local orders)
|
|
1287
|
+
* @property {bigint|null} backed - Share of the account budget in `selling` that backs the order
|
|
1288
|
+
* @property {number|null} backedPct - `backed / amount` (0..1), null when unknown
|
|
1289
|
+
* @property {boolean} backingPending - The backing is not confirmed yet
|
|
1290
|
+
* @property {number} filledPct - Filled share of the initial amount (0..1)
|
|
1291
|
+
* @property {boolean} pending - Created by this client, not reported by the indexer yet
|
|
1292
|
+
*/
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* @typedef {{}} AccountBacking - Balance and allowance of the account in a token, as tracked by the indexer
|
|
1296
|
+
* @property {string} asset
|
|
1297
|
+
* @property {bigint} balance - Spendable amount (balance minus reserve and liabilities)
|
|
1298
|
+
* @property {bigint} allowance - Allowance granted to the AXIS contract
|
|
1299
|
+
* @property {number} liveUntil - Ledger sequence the allowance lives until
|
|
1300
|
+
* @property {boolean} authorized - Whether the account can send and receive the token
|
|
1301
|
+
* @property {bigint} budget - min(balance, allowance), 0 when unauthorized or expired
|
|
1302
|
+
* @property {number} updated - Last load, UNIX milliseconds (0 = not loaded)
|
|
1303
|
+
* @property {number} skipped - Last skipped fill of an order selling the token, UNIX milliseconds (0 = none)
|
|
1304
|
+
* @property {boolean} pending - A reload confirming the latest change is due
|
|
1305
|
+
*/
|
|
1306
|
+
|
|
1307
|
+
/**
|
|
1308
|
+
* @typedef {{}} AccountAllowance
|
|
1309
|
+
* @property {string} asset
|
|
1310
|
+
* @property {boolean} known - Whether the backing is tracked and loaded
|
|
1311
|
+
* @property {bigint} [balance]
|
|
1312
|
+
* @property {bigint} [allowance]
|
|
1313
|
+
* @property {number} [liveUntil]
|
|
1314
|
+
* @property {boolean} [authorized]
|
|
1315
|
+
* @property {boolean} [pending]
|
|
1316
|
+
* @property {bigint} [available] - min(balance, allowance)
|
|
1317
|
+
* @property {bigint} committed - Remaining amount of the open orders selling the token
|
|
1318
|
+
*/
|
|
1319
|
+
|
|
1320
|
+
/**
|
|
1321
|
+
* @typedef {{}} AccountFill
|
|
1322
|
+
* @property {AccountOrder} order - Order after the fill
|
|
1323
|
+
* @property {bigint} sold - Amount of `selling` delivered
|
|
1324
|
+
* @property {bigint} bought - Amount of `buying` received
|
|
1325
|
+
* @property {string} taker - Taker address
|
|
1326
|
+
* @property {string} trade - Trade id
|
|
1327
|
+
* @property {number} ts - Trade time, UNIX milliseconds
|
|
1328
|
+
* @property {boolean} partial - Whether the order is still open
|
|
1329
|
+
*/
|
|
1330
|
+
|
|
1331
|
+
/**
|
|
1332
|
+
* @typedef {{}} TradeParams
|
|
1333
|
+
* @property {string} selling - Token sent
|
|
1334
|
+
* @property {string} buying - Token received
|
|
1335
|
+
* @property {bigint|string} amount - `selling` amount of a sell, `buying` amount of a buy (raw units)
|
|
1336
|
+
* @property {bigint|string} [price] - Limit price, contract scale; omitted for a market order
|
|
1337
|
+
* @property {number} [kind] - {@link OrderKind}: `Limit` by default, `Fill` for a market order
|
|
1338
|
+
* @property {number} [expires] - Expiration UNIX timestamp (seconds) of the resting remainder
|
|
1339
|
+
* @property {Array<bigint|string>} [orders] - Counter orders to cross, looked up automatically when omitted
|
|
1340
|
+
* @property {Approval|null} [approve] - Explicit approval (`null` for none)
|
|
1341
|
+
*/
|
|
1342
|
+
|
|
1343
|
+
/**
|
|
1344
|
+
* @typedef {{}} TradeResult
|
|
1345
|
+
* @property {bigint} sold - Amount of `selling` sent
|
|
1346
|
+
* @property {bigint} bought - Amount of `buying` received
|
|
1347
|
+
* @property {string} [orderId] - ID of the order created for a limit remainder
|
|
1348
|
+
* @property {AccountOrder} [order] - The created order (unconfirmed until the indexer reports it)
|
|
1349
|
+
* @property {Approval} [approve] - Approval granted with the trade
|
|
1350
|
+
*/
|
|
1351
|
+
|
|
1352
|
+
/**
|
|
1353
|
+
* @typedef {{}} AccountRevokeResult
|
|
1354
|
+
* @property {string[]} revoked - Tokens whose allowance was set to zero
|
|
1355
|
+
* @property {string[]} canceled - Canceled order ids
|
|
1356
|
+
*/
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* @typedef {{}} AllowancePlan - Allowances planned across the `update` batches of a call
|
|
1360
|
+
* @property {number} ledger - Current ledger sequence
|
|
1361
|
+
* @property {number} ttl - Lifetime of a new allowance, in ledgers
|
|
1362
|
+
* @property {Map<string, {committed: bigint, allowance?: bigint, liveUntil: number, change: bigint}>} tokens - Open amount before the call, the current allowance (undefined when unknown) and the committed change of the batches planned so far for each sold token
|
|
1363
|
+
*/
|
|
1364
|
+
|
|
1365
|
+
/**
|
|
1366
|
+
* @typedef {import('./index.js').OrderUpdate} OrderUpdate
|
|
1367
|
+
*/
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* @typedef {{}} SwapParams
|
|
1371
|
+
* @property {string} selling - Token sent
|
|
1372
|
+
* @property {string} buying - Token received
|
|
1373
|
+
* @property {bigint|string} amount - `selling` amount of a sell, `buying` amount of a buy (raw units)
|
|
1374
|
+
* @property {number} [direction] - {@link TradeDirection}, `Sell` by default
|
|
1375
|
+
* @property {number} [slippage] - Price tolerance as a fraction (0.005 = 0.5%), 0 by default
|
|
1376
|
+
*/
|