avantis-trader-sdk 0.8.2__py3-none-any.whl → 0.8.3__py3-none-any.whl
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.
- avantis_trader_sdk/__init__.py +5 -5
- avantis_trader_sdk/abis/AggregatorV3Interface.json +606 -606
- avantis_trader_sdk/abis/IPyth.sol/IPyth.dbg.json +4 -4
- avantis_trader_sdk/abis/Referral.sol/ReferralStorage.json +7132 -7132
- avantis_trader_sdk/abis/Sanctions.json +190 -190
- avantis_trader_sdk/abis/USDC.sol/USDC.dbg.json +4 -4
- avantis_trader_sdk/abis/interfaces/ICallbacks.sol/ICallbacks.json +2637 -2637
- avantis_trader_sdk/abis/interfaces/IExecute.sol/IExecute.json +1628 -1628
- avantis_trader_sdk/abis/interfaces/IPairInfos.sol/IPairInfos.json +2781 -2781
- avantis_trader_sdk/abis/interfaces/IPairStorage.sol/IPairStorage.json +3729 -3729
- avantis_trader_sdk/abis/interfaces/IPriceAggregator.sol/IPriceAggregator.json +2330 -2330
- avantis_trader_sdk/abis/interfaces/IReferral.sol/IReferral.json +1890 -1890
- avantis_trader_sdk/abis/interfaces/ITradingStorage.sol/ITradingStorage.json +7022 -7022
- avantis_trader_sdk/abis/interfaces/ITranche.sol/ITranche.json +1283 -1283
- avantis_trader_sdk/abis/interfaces/IVaultManager.sol/IVaultManager.json +2424 -2424
- avantis_trader_sdk/abis/interfaces/IVeTranche.sol/IVeTranche.json +855 -855
- avantis_trader_sdk/abis/library/PositionMath.sol/PositionMath.dbg.json +4 -4
- avantis_trader_sdk/abis/library/PositionMath.sol/PositionMath.json +10 -10
- avantis_trader_sdk/abis/testnet/USDC.sol/USDC.dbg.json +4 -4
- avantis_trader_sdk/abis/testnet/USDC.sol/USDC.json +320 -320
- avantis_trader_sdk/client.py +369 -367
- avantis_trader_sdk/config.py +14 -14
- avantis_trader_sdk/feed/feed_client.py +263 -261
- avantis_trader_sdk/rpc/asset_parameters.py +499 -499
- avantis_trader_sdk/rpc/blended.py +71 -71
- avantis_trader_sdk/rpc/category_parameters.py +216 -216
- avantis_trader_sdk/rpc/fee_parameters.py +237 -237
- avantis_trader_sdk/rpc/pairs_cache.py +130 -130
- avantis_trader_sdk/rpc/rpc_helpers.py +8 -8
- avantis_trader_sdk/rpc/snapshot.py +142 -142
- avantis_trader_sdk/rpc/trade.py +701 -710
- avantis_trader_sdk/rpc/trading_parameters.py +139 -139
- avantis_trader_sdk/types.py +462 -462
- avantis_trader_sdk/utils.py +78 -78
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/METADATA +124 -113
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/RECORD +38 -39
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/WHEEL +1 -1
- avantis_trader_sdk/feed/feedIds.json +0 -214
- {avantis_trader_sdk-0.8.2.dist-info → avantis_trader_sdk-0.8.3.dist-info}/top_level.txt +0 -0
avantis_trader_sdk/rpc/trade.py
CHANGED
|
@@ -1,710 +1,701 @@
|
|
|
1
|
-
from ..
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
self.
|
|
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
|
-
trade_input_order_type == TradeInputOrderType.
|
|
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
|
-
trade_input_order_type == TradeInputOrderType.
|
|
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
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
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
|
-
|
|
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
|
-
"chainId": self.client.chain_id,
|
|
703
|
-
"nonce": await self.client.get_transaction_count(
|
|
704
|
-
self.client.get_signer().get_ethereum_address()
|
|
705
|
-
),
|
|
706
|
-
"value": 1,
|
|
707
|
-
"gas": 1_000_000,
|
|
708
|
-
}
|
|
709
|
-
)
|
|
710
|
-
return delegate_transaction
|
|
1
|
+
from ..feed.feed_client import FeedClient
|
|
2
|
+
from ..types import (
|
|
3
|
+
TradeInput,
|
|
4
|
+
TradeInputOrderType,
|
|
5
|
+
TradeExtendedResponse,
|
|
6
|
+
TradeResponse,
|
|
7
|
+
TradeInfo,
|
|
8
|
+
PendingLimitOrderExtendedResponse,
|
|
9
|
+
MarginUpdateType,
|
|
10
|
+
)
|
|
11
|
+
from typing import Optional
|
|
12
|
+
import math
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TradeRPC:
|
|
16
|
+
"""
|
|
17
|
+
The TradeRPC class contains methods for retrieving trading parameters from the Avantis Protocol.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, client, feed_client: FeedClient):
|
|
21
|
+
"""
|
|
22
|
+
Constructor for the TradeRPC class.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
client: The TraderClient object.
|
|
26
|
+
feed_client: The FeedClient object.
|
|
27
|
+
"""
|
|
28
|
+
self.client = client
|
|
29
|
+
self.feed_client = feed_client
|
|
30
|
+
|
|
31
|
+
async def build_trade_open_tx(
|
|
32
|
+
self,
|
|
33
|
+
trade_input: TradeInput,
|
|
34
|
+
trade_input_order_type: TradeInputOrderType,
|
|
35
|
+
slippage_percentage: int,
|
|
36
|
+
execution_fee: Optional[float] = None,
|
|
37
|
+
):
|
|
38
|
+
"""
|
|
39
|
+
Builds a transaction to open a trade.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
trade: The trade input object.
|
|
43
|
+
trade_input_order_type: The trade input order type.
|
|
44
|
+
slippage_percentage: The slippage percentage.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
A transaction object.
|
|
48
|
+
"""
|
|
49
|
+
Trading = self.client.contracts.get("Trading")
|
|
50
|
+
|
|
51
|
+
if (
|
|
52
|
+
trade_input.trader == "0x1234567890123456789012345678901234567890"
|
|
53
|
+
and self.client.get_signer() is not None
|
|
54
|
+
):
|
|
55
|
+
trade_input.trader = await self.client.get_signer().get_ethereum_address()
|
|
56
|
+
|
|
57
|
+
if execution_fee is not None:
|
|
58
|
+
execution_fee_wei = int(execution_fee * 10**18)
|
|
59
|
+
else:
|
|
60
|
+
execution_fee_wei = await self.get_trade_execution_fee()
|
|
61
|
+
|
|
62
|
+
if (
|
|
63
|
+
trade_input_order_type == TradeInputOrderType.MARKET
|
|
64
|
+
or trade_input_order_type == TradeInputOrderType.MARKET_ZERO_FEE
|
|
65
|
+
) and not trade_input.openPrice:
|
|
66
|
+
pair_name = await self.client.pairs_cache.get_pair_name_from_index(
|
|
67
|
+
trade_input.pairIndex
|
|
68
|
+
)
|
|
69
|
+
price_data = await self.feed_client.get_latest_price_updates([pair_name])
|
|
70
|
+
price = int(price_data.parsed[0].converted_price * 10**10)
|
|
71
|
+
trade_input.openPrice = price
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
trade_input_order_type == TradeInputOrderType.LIMIT
|
|
75
|
+
or trade_input_order_type == TradeInputOrderType.STOP_LIMIT
|
|
76
|
+
) and not trade_input.openPrice:
|
|
77
|
+
raise Exception("Open price is required for LIMIT/STOP LIMIT order type")
|
|
78
|
+
|
|
79
|
+
transaction = await Trading.functions.openTrade(
|
|
80
|
+
trade_input.model_dump(),
|
|
81
|
+
trade_input_order_type.value,
|
|
82
|
+
slippage_percentage * 10**10,
|
|
83
|
+
).build_transaction(
|
|
84
|
+
{
|
|
85
|
+
"from": trade_input.trader,
|
|
86
|
+
"value": execution_fee_wei,
|
|
87
|
+
"chainId": self.client.chain_id,
|
|
88
|
+
"nonce": await self.client.get_transaction_count(trade_input.trader),
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
return transaction
|
|
93
|
+
|
|
94
|
+
async def build_trade_open_tx_delegate(
|
|
95
|
+
self,
|
|
96
|
+
trade_input: TradeInput,
|
|
97
|
+
trade_input_order_type: TradeInputOrderType,
|
|
98
|
+
slippage_percentage: int,
|
|
99
|
+
execution_fee: Optional[float] = None,
|
|
100
|
+
):
|
|
101
|
+
"""
|
|
102
|
+
Builds a transaction to open a trade.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
trade: The trade input object.
|
|
106
|
+
trade_input_order_type: The trade input order type.
|
|
107
|
+
slippage_percentage: The slippage percentage.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
A transaction object.
|
|
111
|
+
"""
|
|
112
|
+
Trading = self.client.contracts.get("Trading")
|
|
113
|
+
|
|
114
|
+
if (
|
|
115
|
+
trade_input.trader == "0x1234567890123456789012345678901234567890"
|
|
116
|
+
and self.client.get_signer() is not None
|
|
117
|
+
):
|
|
118
|
+
trade_input.trader = await self.client.get_signer().get_ethereum_address()
|
|
119
|
+
|
|
120
|
+
if execution_fee is not None:
|
|
121
|
+
execution_fee_wei = int(execution_fee * 10**18)
|
|
122
|
+
else:
|
|
123
|
+
execution_fee_wei = await self.get_trade_execution_fee()
|
|
124
|
+
|
|
125
|
+
if (
|
|
126
|
+
trade_input_order_type == TradeInputOrderType.MARKET
|
|
127
|
+
or trade_input_order_type == TradeInputOrderType.MARKET_ZERO_FEE
|
|
128
|
+
) and not trade_input.openPrice:
|
|
129
|
+
pair_name = await self.client.pairs_cache.get_pair_name_from_index(
|
|
130
|
+
trade_input.pairIndex
|
|
131
|
+
)
|
|
132
|
+
price_data = await self.feed_client.get_latest_price_updates([pair_name])
|
|
133
|
+
price = int(price_data.parsed[0].converted_price * 10**10)
|
|
134
|
+
trade_input.openPrice = price
|
|
135
|
+
|
|
136
|
+
if (
|
|
137
|
+
trade_input_order_type == TradeInputOrderType.LIMIT
|
|
138
|
+
or trade_input_order_type == TradeInputOrderType.STOP_LIMIT
|
|
139
|
+
) and not trade_input.openPrice:
|
|
140
|
+
raise Exception("Open price is required for LIMIT/STOP LIMIT order type")
|
|
141
|
+
|
|
142
|
+
transaction = await Trading.functions.openTrade(
|
|
143
|
+
trade_input.model_dump(),
|
|
144
|
+
trade_input_order_type.value,
|
|
145
|
+
slippage_percentage * 10**10,
|
|
146
|
+
).build_transaction(
|
|
147
|
+
{
|
|
148
|
+
"from": trade_input.trader,
|
|
149
|
+
"value": 0,
|
|
150
|
+
"chainId": self.client.chain_id,
|
|
151
|
+
"nonce": await self.client.get_transaction_count(trade_input.trader),
|
|
152
|
+
}
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
print("transaction: ", trade_input.trader)
|
|
156
|
+
|
|
157
|
+
delegate_transaction = await Trading.functions.delegatedAction(
|
|
158
|
+
trade_input.trader, transaction["data"]
|
|
159
|
+
).build_transaction(
|
|
160
|
+
{
|
|
161
|
+
"from": self.client.get_signer().get_ethereum_address(),
|
|
162
|
+
"value": execution_fee_wei,
|
|
163
|
+
"chainId": self.client.chain_id,
|
|
164
|
+
"nonce": await self.client.get_transaction_count(
|
|
165
|
+
self.client.get_signer().get_ethereum_address()
|
|
166
|
+
),
|
|
167
|
+
}
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
return delegate_transaction
|
|
171
|
+
|
|
172
|
+
async def get_trade_execution_fee(self):
|
|
173
|
+
"""
|
|
174
|
+
Gets the correct trade execution fee.
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
The trade execution fee
|
|
178
|
+
"""
|
|
179
|
+
execution_fee = round(0.00035, 18) # default value
|
|
180
|
+
execution_fee_wei = int(execution_fee * 10**18)
|
|
181
|
+
|
|
182
|
+
try:
|
|
183
|
+
feeScalar = 0.001
|
|
184
|
+
|
|
185
|
+
estimatedL1Gas = math.floor(22676 * feeScalar)
|
|
186
|
+
estimatedL2Gas = math.floor(850000 * 1.1)
|
|
187
|
+
|
|
188
|
+
l2GasPrice = await self.client.async_web3.eth.gas_price
|
|
189
|
+
estimatedL2GasEth = l2GasPrice * estimatedL2Gas
|
|
190
|
+
|
|
191
|
+
l1GasPrice = await self.client.l1_async_web3.eth.gas_price
|
|
192
|
+
estimatedL1GasEth = l1GasPrice * estimatedL1Gas
|
|
193
|
+
|
|
194
|
+
feeEstimate = estimatedL1GasEth + estimatedL2GasEth
|
|
195
|
+
feeEstimate = round(feeEstimate, 18)
|
|
196
|
+
return feeEstimate
|
|
197
|
+
except Exception as e:
|
|
198
|
+
print("Error getting correct trade execution fee. Using fallback: ", e)
|
|
199
|
+
return execution_fee_wei
|
|
200
|
+
|
|
201
|
+
async def get_trades(self, trader: Optional[str] = None):
|
|
202
|
+
"""
|
|
203
|
+
Gets the trades.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
trader: The trader's wallet address.
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
The trades.
|
|
210
|
+
"""
|
|
211
|
+
if trader is None:
|
|
212
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
213
|
+
|
|
214
|
+
result = (
|
|
215
|
+
await self.client.contracts.get("Multicall")
|
|
216
|
+
.functions.getPositions(trader)
|
|
217
|
+
.call()
|
|
218
|
+
)
|
|
219
|
+
trades = []
|
|
220
|
+
pendingOpenLimitOrders = []
|
|
221
|
+
|
|
222
|
+
for aggregated_trade in result[0]: # Access the list of aggregated trades
|
|
223
|
+
(trade, trade_info, margin_fee, liquidation_price) = aggregated_trade
|
|
224
|
+
|
|
225
|
+
if trade[7] <= 0:
|
|
226
|
+
continue
|
|
227
|
+
|
|
228
|
+
# Extract and format the trade data
|
|
229
|
+
trade_details = {
|
|
230
|
+
"trade": {
|
|
231
|
+
"trader": trade[0],
|
|
232
|
+
"pairIndex": trade[1],
|
|
233
|
+
"index": trade[2],
|
|
234
|
+
"initialPosToken": trade[3],
|
|
235
|
+
"positionSizeUSDC": trade[4],
|
|
236
|
+
"openPrice": trade[5],
|
|
237
|
+
"buy": trade[6],
|
|
238
|
+
"leverage": trade[7],
|
|
239
|
+
"tp": trade[8],
|
|
240
|
+
"sl": trade[9],
|
|
241
|
+
"timestamp": trade[10],
|
|
242
|
+
},
|
|
243
|
+
"additional_info": {
|
|
244
|
+
"openInterestUSDC": trade_info[0],
|
|
245
|
+
"tpLastUpdated": trade_info[1],
|
|
246
|
+
"slLastUpdated": trade_info[2],
|
|
247
|
+
"beingMarketClosed": trade_info[3],
|
|
248
|
+
"lossProtectionPercentage": await self.client.trading_parameters.get_loss_protection_percentage_by_tier(
|
|
249
|
+
trade_info[4], trade[1]
|
|
250
|
+
),
|
|
251
|
+
},
|
|
252
|
+
"margin_fee": margin_fee,
|
|
253
|
+
"liquidationPrice": liquidation_price,
|
|
254
|
+
}
|
|
255
|
+
trades.append(
|
|
256
|
+
TradeExtendedResponse(
|
|
257
|
+
trade=TradeResponse(**trade_details["trade"]),
|
|
258
|
+
additional_info=TradeInfo(**trade_details["additional_info"]),
|
|
259
|
+
margin_fee=trade_details["margin_fee"],
|
|
260
|
+
liquidation_price=trade_details["liquidationPrice"],
|
|
261
|
+
)
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
for aggregated_order in result[1]: # Access the list of aggregated orders
|
|
265
|
+
(order, liquidation_price) = aggregated_order
|
|
266
|
+
|
|
267
|
+
if order[5] <= 0:
|
|
268
|
+
continue
|
|
269
|
+
|
|
270
|
+
# Extract and format the order data
|
|
271
|
+
order_details = {
|
|
272
|
+
"trader": order[0],
|
|
273
|
+
"pairIndex": order[1],
|
|
274
|
+
"index": order[2],
|
|
275
|
+
"positionSize": order[3],
|
|
276
|
+
"buy": order[4],
|
|
277
|
+
"leverage": order[5],
|
|
278
|
+
"tp": order[6],
|
|
279
|
+
"sl": order[7],
|
|
280
|
+
"price": order[8],
|
|
281
|
+
"slippageP": order[9],
|
|
282
|
+
"block": order[10],
|
|
283
|
+
# 'executionFee': order[11],
|
|
284
|
+
"liquidation_price": liquidation_price,
|
|
285
|
+
}
|
|
286
|
+
pendingOpenLimitOrders.append(
|
|
287
|
+
PendingLimitOrderExtendedResponse(**order_details)
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
return trades, pendingOpenLimitOrders
|
|
291
|
+
|
|
292
|
+
async def build_trade_close_tx(
|
|
293
|
+
self,
|
|
294
|
+
pair_index: int,
|
|
295
|
+
trade_index: int,
|
|
296
|
+
collateral_to_close: float,
|
|
297
|
+
trader: Optional[str] = None,
|
|
298
|
+
):
|
|
299
|
+
"""
|
|
300
|
+
Builds a transaction to close a trade.
|
|
301
|
+
|
|
302
|
+
Args:
|
|
303
|
+
pair_index: The pair index.
|
|
304
|
+
trade_index: The trade index.
|
|
305
|
+
collateral_to_close: The collateral to close.
|
|
306
|
+
trader (optional): The trader's wallet address.
|
|
307
|
+
|
|
308
|
+
Returns:
|
|
309
|
+
A transaction object.
|
|
310
|
+
"""
|
|
311
|
+
Trading = self.client.contracts.get("Trading")
|
|
312
|
+
|
|
313
|
+
if trader is None:
|
|
314
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
315
|
+
|
|
316
|
+
collateral_to_close = int(collateral_to_close * 10**6)
|
|
317
|
+
|
|
318
|
+
execution_fee = await self.get_trade_execution_fee()
|
|
319
|
+
|
|
320
|
+
transaction = await Trading.functions.closeTradeMarket(
|
|
321
|
+
pair_index,
|
|
322
|
+
trade_index,
|
|
323
|
+
collateral_to_close,
|
|
324
|
+
).build_transaction(
|
|
325
|
+
{
|
|
326
|
+
"from": trader,
|
|
327
|
+
"chainId": self.client.chain_id,
|
|
328
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
329
|
+
"value": execution_fee,
|
|
330
|
+
}
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
return transaction
|
|
334
|
+
|
|
335
|
+
async def build_trade_close_tx_delegate(
|
|
336
|
+
self,
|
|
337
|
+
pair_index: int,
|
|
338
|
+
trade_index: int,
|
|
339
|
+
collateral_to_close: float,
|
|
340
|
+
trader: Optional[str] = None,
|
|
341
|
+
):
|
|
342
|
+
"""
|
|
343
|
+
Builds a transaction to close a trade.
|
|
344
|
+
|
|
345
|
+
Args:
|
|
346
|
+
pair_index: The pair index.
|
|
347
|
+
trade_index: The trade index.
|
|
348
|
+
collateral_to_close: The collateral to close.
|
|
349
|
+
trader (optional): The trader's wallet address.
|
|
350
|
+
|
|
351
|
+
Returns:
|
|
352
|
+
A transaction object.
|
|
353
|
+
"""
|
|
354
|
+
Trading = self.client.contracts.get("Trading")
|
|
355
|
+
|
|
356
|
+
if trader is None:
|
|
357
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
358
|
+
|
|
359
|
+
collateral_to_close = int(collateral_to_close * 10**6)
|
|
360
|
+
|
|
361
|
+
execution_fee = await self.get_trade_execution_fee()
|
|
362
|
+
|
|
363
|
+
transaction = await Trading.functions.closeTradeMarket(
|
|
364
|
+
pair_index,
|
|
365
|
+
trade_index,
|
|
366
|
+
collateral_to_close,
|
|
367
|
+
).build_transaction(
|
|
368
|
+
{
|
|
369
|
+
"from": trader,
|
|
370
|
+
"chainId": self.client.chain_id,
|
|
371
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
372
|
+
"value": 0,
|
|
373
|
+
}
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
delegate_transaction = await Trading.functions.delegatedAction(
|
|
377
|
+
trader, transaction["data"]
|
|
378
|
+
).build_transaction(
|
|
379
|
+
{
|
|
380
|
+
"from": self.client.get_signer().get_ethereum_address(),
|
|
381
|
+
"value": execution_fee,
|
|
382
|
+
"chainId": self.client.chain_id,
|
|
383
|
+
"nonce": await self.client.get_transaction_count(
|
|
384
|
+
self.client.get_signer().get_ethereum_address()
|
|
385
|
+
),
|
|
386
|
+
}
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
return delegate_transaction
|
|
390
|
+
|
|
391
|
+
async def build_order_cancel_tx(
|
|
392
|
+
self, pair_index: int, trade_index: int, trader: Optional[str] = None
|
|
393
|
+
):
|
|
394
|
+
"""
|
|
395
|
+
Builds a transaction to cancel an order.
|
|
396
|
+
|
|
397
|
+
Args:
|
|
398
|
+
pair_index: The pair index.
|
|
399
|
+
trade_index: The trade/order index.
|
|
400
|
+
trader (optional): The trader's wallet address.
|
|
401
|
+
|
|
402
|
+
Returns:
|
|
403
|
+
A transaction object.
|
|
404
|
+
"""
|
|
405
|
+
Trading = self.client.contracts.get("Trading")
|
|
406
|
+
|
|
407
|
+
if trader is None:
|
|
408
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
409
|
+
|
|
410
|
+
transaction = await Trading.functions.cancelOpenLimitOrder(
|
|
411
|
+
pair_index, trade_index
|
|
412
|
+
).build_transaction(
|
|
413
|
+
{
|
|
414
|
+
"from": trader,
|
|
415
|
+
"chainId": self.client.chain_id,
|
|
416
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
417
|
+
}
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
return transaction
|
|
421
|
+
|
|
422
|
+
async def build_order_cancel_tx_delegate(
|
|
423
|
+
self, pair_index: int, trade_index: int, trader: Optional[str] = None
|
|
424
|
+
):
|
|
425
|
+
"""
|
|
426
|
+
Builds a transaction to cancel an order.
|
|
427
|
+
|
|
428
|
+
Args:
|
|
429
|
+
pair_index: The pair index.
|
|
430
|
+
trade_index: The trade/order index.
|
|
431
|
+
trader (optional): The trader's wallet address.
|
|
432
|
+
|
|
433
|
+
Returns:
|
|
434
|
+
A transaction object.
|
|
435
|
+
"""
|
|
436
|
+
Trading = self.client.contracts.get("Trading")
|
|
437
|
+
|
|
438
|
+
if trader is None:
|
|
439
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
440
|
+
|
|
441
|
+
transaction = await Trading.functions.cancelOpenLimitOrder(
|
|
442
|
+
pair_index, trade_index
|
|
443
|
+
).build_transaction(
|
|
444
|
+
{
|
|
445
|
+
"from": trader,
|
|
446
|
+
"chainId": self.client.chain_id,
|
|
447
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
448
|
+
}
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
delegate_transaction = await Trading.functions.delegatedAction(
|
|
452
|
+
trader, transaction["data"]
|
|
453
|
+
).build_transaction(
|
|
454
|
+
{
|
|
455
|
+
"from": self.client.get_signer().get_ethereum_address(),
|
|
456
|
+
"chainId": self.client.chain_id,
|
|
457
|
+
"nonce": await self.client.get_transaction_count(
|
|
458
|
+
self.client.get_signer().get_ethereum_address()
|
|
459
|
+
),
|
|
460
|
+
}
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
return delegate_transaction
|
|
464
|
+
|
|
465
|
+
async def build_trade_margin_update_tx(
|
|
466
|
+
self,
|
|
467
|
+
pair_index: int,
|
|
468
|
+
trade_index: int,
|
|
469
|
+
margin_update_type: MarginUpdateType,
|
|
470
|
+
collateral_change: float,
|
|
471
|
+
trader: Optional[str] = None,
|
|
472
|
+
):
|
|
473
|
+
"""
|
|
474
|
+
Builds a transaction to update the margin of a trade.
|
|
475
|
+
|
|
476
|
+
Args:
|
|
477
|
+
pair_index: The pair index.
|
|
478
|
+
trade_index: The trade index.
|
|
479
|
+
margin_update_type: The margin update type.
|
|
480
|
+
collateral_change: The collateral change.
|
|
481
|
+
trader (optional): The trader's wallet address.
|
|
482
|
+
Returns:
|
|
483
|
+
A transaction object.
|
|
484
|
+
"""
|
|
485
|
+
Trading = self.client.contracts.get("Trading")
|
|
486
|
+
|
|
487
|
+
if trader is None:
|
|
488
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
489
|
+
|
|
490
|
+
collateral_change = int(collateral_change * 10**6)
|
|
491
|
+
|
|
492
|
+
pair_name = await self.client.pairs_cache.get_pair_name_from_index(pair_index)
|
|
493
|
+
|
|
494
|
+
price_data = await self.feed_client.get_latest_price_updates([pair_name])
|
|
495
|
+
|
|
496
|
+
price_update_data = "0x" + price_data.binary.data[0]
|
|
497
|
+
|
|
498
|
+
transaction = await Trading.functions.updateMargin(
|
|
499
|
+
pair_index,
|
|
500
|
+
trade_index,
|
|
501
|
+
margin_update_type.value,
|
|
502
|
+
collateral_change,
|
|
503
|
+
[price_update_data],
|
|
504
|
+
).build_transaction(
|
|
505
|
+
{
|
|
506
|
+
"from": trader,
|
|
507
|
+
"chainId": self.client.chain_id,
|
|
508
|
+
"value": 1,
|
|
509
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
510
|
+
}
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
return transaction
|
|
514
|
+
|
|
515
|
+
async def build_trade_margin_update_tx_delegate(
|
|
516
|
+
self,
|
|
517
|
+
pair_index: int,
|
|
518
|
+
trade_index: int,
|
|
519
|
+
margin_update_type: MarginUpdateType,
|
|
520
|
+
collateral_change: float,
|
|
521
|
+
trader: Optional[str] = None,
|
|
522
|
+
):
|
|
523
|
+
"""
|
|
524
|
+
Builds a transaction to update the margin of a trade.
|
|
525
|
+
|
|
526
|
+
Args:
|
|
527
|
+
pair_index: The pair index.
|
|
528
|
+
trade_index: The trade index.
|
|
529
|
+
margin_update_type: The margin update type.
|
|
530
|
+
collateral_change: The collateral change.
|
|
531
|
+
trader (optional): The trader's wallet address.
|
|
532
|
+
Returns:
|
|
533
|
+
A transaction object.
|
|
534
|
+
"""
|
|
535
|
+
Trading = self.client.contracts.get("Trading")
|
|
536
|
+
|
|
537
|
+
if trader is None:
|
|
538
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
539
|
+
|
|
540
|
+
collateral_change = int(collateral_change * 10**6)
|
|
541
|
+
|
|
542
|
+
pair_name = await self.client.pairs_cache.get_pair_name_from_index(pair_index)
|
|
543
|
+
|
|
544
|
+
price_data = await self.feed_client.get_latest_price_updates([pair_name])
|
|
545
|
+
|
|
546
|
+
price_update_data = "0x" + price_data.binary.data[0]
|
|
547
|
+
|
|
548
|
+
transaction = await Trading.functions.updateMargin(
|
|
549
|
+
pair_index,
|
|
550
|
+
trade_index,
|
|
551
|
+
margin_update_type.value,
|
|
552
|
+
collateral_change,
|
|
553
|
+
[price_update_data],
|
|
554
|
+
).build_transaction(
|
|
555
|
+
{
|
|
556
|
+
"from": trader,
|
|
557
|
+
"chainId": self.client.chain_id,
|
|
558
|
+
"value": 0,
|
|
559
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
560
|
+
}
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
delegate_transaction = await Trading.functions.delegatedAction(
|
|
564
|
+
trader, transaction["data"]
|
|
565
|
+
).build_transaction(
|
|
566
|
+
{
|
|
567
|
+
"from": self.client.get_signer().get_ethereum_address(),
|
|
568
|
+
"chainId": self.client.chain_id,
|
|
569
|
+
"nonce": await self.client.get_transaction_count(
|
|
570
|
+
self.client.get_signer().get_ethereum_address()
|
|
571
|
+
),
|
|
572
|
+
"value": 1,
|
|
573
|
+
}
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
return delegate_transaction
|
|
577
|
+
|
|
578
|
+
async def build_trade_tp_sl_update_tx(
|
|
579
|
+
self,
|
|
580
|
+
pair_index: int,
|
|
581
|
+
trade_index: int,
|
|
582
|
+
take_profit_price: float,
|
|
583
|
+
stop_loss_price: float,
|
|
584
|
+
trader: str = None,
|
|
585
|
+
):
|
|
586
|
+
"""
|
|
587
|
+
Builds a transaction to update the stop loss and take profit of a trade.
|
|
588
|
+
|
|
589
|
+
Args:
|
|
590
|
+
pair_index: The pair index.
|
|
591
|
+
trade_index: The trade index.
|
|
592
|
+
take_profit_price: The take profit price.
|
|
593
|
+
stop_loss_price: The stop loss price. Pass 0 if you want to remove the stop loss.
|
|
594
|
+
trader (optional): The trader's wallet address.
|
|
595
|
+
Returns:
|
|
596
|
+
A transaction object.
|
|
597
|
+
"""
|
|
598
|
+
Trading = self.client.contracts.get("Trading")
|
|
599
|
+
|
|
600
|
+
if take_profit_price == 0:
|
|
601
|
+
raise ValueError("Take profit price cannot be 0")
|
|
602
|
+
|
|
603
|
+
if trader is None:
|
|
604
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
605
|
+
|
|
606
|
+
pair_name = await self.client.pairs_cache.get_pair_name_from_index(pair_index)
|
|
607
|
+
|
|
608
|
+
price_data = await feed_client.get_latest_price_updates([pair_name])
|
|
609
|
+
|
|
610
|
+
price_update_data = "0x" + price_data.binary.data[0]
|
|
611
|
+
|
|
612
|
+
take_profit_price = int(take_profit_price * 10**10)
|
|
613
|
+
stop_loss_price = int(stop_loss_price * 10**10)
|
|
614
|
+
|
|
615
|
+
transaction = await Trading.functions.updateTpAndSl(
|
|
616
|
+
pair_index,
|
|
617
|
+
trade_index,
|
|
618
|
+
stop_loss_price,
|
|
619
|
+
take_profit_price,
|
|
620
|
+
[price_update_data],
|
|
621
|
+
).build_transaction(
|
|
622
|
+
{
|
|
623
|
+
"from": trader,
|
|
624
|
+
"chainId": self.client.chain_id,
|
|
625
|
+
"value": 1,
|
|
626
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
627
|
+
"gas": 1_000_000,
|
|
628
|
+
}
|
|
629
|
+
)
|
|
630
|
+
|
|
631
|
+
return transaction
|
|
632
|
+
|
|
633
|
+
async def build_trade_tp_sl_update_tx_delegate(
|
|
634
|
+
self,
|
|
635
|
+
pair_index: int,
|
|
636
|
+
trade_index: int,
|
|
637
|
+
take_profit_price: float,
|
|
638
|
+
stop_loss_price: float,
|
|
639
|
+
trader: str = None,
|
|
640
|
+
):
|
|
641
|
+
"""
|
|
642
|
+
Builds a transaction to update the stop loss and take profit of a trade.
|
|
643
|
+
|
|
644
|
+
Args:
|
|
645
|
+
pair_index: The pair index.
|
|
646
|
+
trade_index: The trade index.
|
|
647
|
+
take_profit_price: The take profit price.
|
|
648
|
+
stop_loss_price: The stop loss price. Pass 0 if you want to remove the stop loss.
|
|
649
|
+
trader (optional): The trader's wallet address.
|
|
650
|
+
Returns:
|
|
651
|
+
A transaction object.
|
|
652
|
+
"""
|
|
653
|
+
Trading = self.client.contracts.get("Trading")
|
|
654
|
+
|
|
655
|
+
if take_profit_price == 0:
|
|
656
|
+
raise ValueError("Take profit price cannot be 0")
|
|
657
|
+
|
|
658
|
+
if trader is None:
|
|
659
|
+
trader = self.client.get_signer().get_ethereum_address()
|
|
660
|
+
|
|
661
|
+
feed_client = self.FeedClient()
|
|
662
|
+
|
|
663
|
+
pair_name = await self.client.pairs_cache.get_pair_name_from_index(pair_index)
|
|
664
|
+
|
|
665
|
+
price_data = await self.feed_client.get_latest_price_updates([pair_name])
|
|
666
|
+
|
|
667
|
+
price_update_data = "0x" + price_data.binary.data[0]
|
|
668
|
+
|
|
669
|
+
take_profit_price = int(take_profit_price * 10**10)
|
|
670
|
+
stop_loss_price = int(stop_loss_price * 10**10)
|
|
671
|
+
|
|
672
|
+
transaction = await Trading.functions.updateTpAndSl(
|
|
673
|
+
pair_index,
|
|
674
|
+
trade_index,
|
|
675
|
+
stop_loss_price,
|
|
676
|
+
take_profit_price,
|
|
677
|
+
[price_update_data],
|
|
678
|
+
).build_transaction(
|
|
679
|
+
{
|
|
680
|
+
"from": trader,
|
|
681
|
+
"chainId": self.client.chain_id,
|
|
682
|
+
"value": 0,
|
|
683
|
+
"nonce": await self.client.get_transaction_count(trader),
|
|
684
|
+
"gas": 1_000_000,
|
|
685
|
+
}
|
|
686
|
+
)
|
|
687
|
+
|
|
688
|
+
delegate_transaction = await Trading.functions.delegatedAction(
|
|
689
|
+
trader, transaction["data"]
|
|
690
|
+
).build_transaction(
|
|
691
|
+
{
|
|
692
|
+
"from": self.client.get_signer().get_ethereum_address(),
|
|
693
|
+
"chainId": self.client.chain_id,
|
|
694
|
+
"nonce": await self.client.get_transaction_count(
|
|
695
|
+
self.client.get_signer().get_ethereum_address()
|
|
696
|
+
),
|
|
697
|
+
"value": 1,
|
|
698
|
+
"gas": 1_000_000,
|
|
699
|
+
}
|
|
700
|
+
)
|
|
701
|
+
return delegate_transaction
|