tradehub.demex_client.DemexClient.cancel_orders()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 38
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 2
dl 0
loc 38
rs 10
c 0
b 0
f 0
1
"""
2
Description:
3
4
    Demex Client for the Switcheo Tradehub decentralized exchange.
5
    This is the client that you should use to start trading with Demex.
6
    You will find the necessary trading functions to trade on the exchange that you would find in the UI.
7
    To get started you will need to use your mnemonic to access your wallet and define the network you want to use.
8
9
Usage::
10
11
    from tradehub.demex_client import DemexClient
12
"""
13
from decimal import Decimal
14
15
import tradehub.types as types
16
from tradehub.authenticated_client import AuthenticatedClient as TradehubAuthenticatedClient
17
from tradehub.wallet import Wallet
18
19
20
class DemexClient(object):
21
    """
22
    This class allows the user to interact with the Demex API to manage and trade the users Tradehub account.
23
    Execution of this function is as follows::
24
25
        DemexClient(mnemonic='lorem ipsum dolor consectetur adipiscing eiusmod tempor incididunt labore magna',
26
                    network='mainnet',
27
                    trusted_ips=None,
28
                    trusted_uris=['http://175.41.151.35:5001', 'http://54.255.5.46:5001'])
29
    """
30
31
    def __init__(self, mnemonic: str, network: str = "testnet", trusted_ips: list = None, trusted_uris: list = None):
32
        """
33
        :param mnemonic: The 12 or 24 word seed required to access your wallet and trade on Demex
34
        :type mnemonic: str
35
        :param network: The network you want to interact with. Accepts "testnet" or "mainnet".
36
        :type network: str
37
        :param trusted_ips: A list of Validator IP addresses that the user trusts, providing this value bypasses the network crawler.
38
        :type trusted_ips: list
39
        :param trusted_uris: A list of Validator URIs that the user trusts, providing this value bypasses the network crawler.
40
        :type trusted_uris: list
41
        """
42
        self.wallet = Wallet(mnemonic=mnemonic, network=network)
43
        self.tradehub = TradehubAuthenticatedClient(wallet=self.wallet, network=network, trusted_ips=trusted_ips, trusted_uris=trusted_uris)
44
45
    def limit_buy(self, pair: str, quantity: str, price: str):
46
        """
47
        Function to place a limit buy order on Demex.
48
49
        Execution of this function is as follows::
50
51
            limit_buy(pair='swth_eth1', quantity=1000, price='0.0001')
52
53
        The expected return result for this function is as follows::
54
55
            {
56
                'height': str,
57
                'txhash': str,
58
                `'raw_log': str,
59
                'logs': [{
60
                    'msg_index': int,
61
                    'log': str,
62
                    'events': [{
63
                        'type': str,
64
                        'attributes': [{
65
                            'key': str,
66
                            'value': str
67
                        }, {
68
                            'key': str,
69
                            'value': str
70
                        }]
71
                    }]
72
                }],
73
                'gas_wanted': str,
74
                'gas_used': str
75
            }
76
77
        :return: Dictionary in the form of a JSON message with the limit order details.
78
        """
79
        create_order_msg = types.CreateOrderMessage(market=pair,
80
                                                    side="buy",
81
                                                    quantity=quantity,
82
                                                    price=price,
83
                                                    type="limit")
84
        return self.tradehub.create_order(message=create_order_msg)
85
86
    def limit_sell(self, pair: str, quantity: str, price: str):
87
        """
88
        Function to place a limit sell order on Demex.
89
90
        Execution of this function is as follows::
91
92
            limit_sell(pair='swth_eth1', quantity=1000, price='0.0002')
93
94
        The expected return result for this function is as follows::
95
96
            {
97
                'height': str,
98
                'txhash': str,
99
                `'raw_log': str,
100
                'logs': [{
101
                    'msg_index': int,
102
                    'log': str,
103
                    'events': [{
104
                        'type': str,
105
                        'attributes': [{
106
                            'key': str,
107
                            'value': str
108
                        }, {
109
                            'key': str,
110
                            'value': str
111
                        }]
112
                    }]
113
                }],
114
                'gas_wanted': str,
115
                'gas_used': str
116
            }
117
118
        :return: Dictionary in the form of a JSON message with the limit order details.
119
        """
120
        create_order_msg = types.CreateOrderMessage(market=pair,
121
                                                    side="sell",
122
                                                    quantity=quantity,
123
                                                    price=price,
124
                                                    type="limit")
125
        return self.tradehub.create_order(message=create_order_msg)
126
127
    def market_buy(self, pair: str, quantity: str):
128
        """
129
        Function to place a market buy order on Demex.
130
131
        Execution of this function is as follows::
132
133
            market_buy(pair='swth_eth1', quantity=1000)
134
135
        The expected return result for this function is as follows::
136
137
            {
138
                'height': str,
139
                'txhash': str,
140
                `'raw_log': str,
141
                'logs': [{
142
                    'msg_index': int,
143
                    'log': str,
144
                    'events': [{
145
                        'type': str,
146
                        'attributes': [{
147
                            'key': str,
148
                            'value': str
149
                        }, {
150
                            'key': str,
151
                            'value': str
152
                        }]
153
                    }]
154
                }],
155
                'gas_wanted': str,
156
                'gas_used': str
157
            }
158
159
        :return: Dictionary in the form of a JSON message with the market order details.
160
        """
161
        create_order_msg = types.CreateOrderMessage(market=pair,
162
                                                    side="buy",
163
                                                    quantity=quantity,
164
                                                    type="market")
165
        return self.tradehub.create_order(message=create_order_msg)
166
167
    def market_sell(self, pair: str, quantity: str):
168
        """
169
        Function to place a market sell order on Demex.
170
171
        Execution of this function is as follows::
172
173
            market_sell(pair='swth_eth1', quantity=1000)
174
175
        The expected return result for this function is as follows::
176
177
            {
178
                'height': str,
179
                'txhash': str,
180
                `'raw_log': str,
181
                'logs': [{
182
                    'msg_index': int,
183
                    'log': str,
184
                    'events': [{
185
                        'type': str,
186
                        'attributes': [{
187
                            'key': str,
188
                            'value': str
189
                        }, {
190
                            'key': str,
191
                            'value': str
192
                        }]
193
                    }]
194
                }],
195
                'gas_wanted': str,
196
                'gas_used': str
197
            }
198
199
        :return: Dictionary in the form of a JSON message with the market order details.
200
        """
201
        create_order_msg = types.CreateOrderMessage(market=pair,
202
                                                    side="sell",
203
                                                    quantity=quantity,
204
                                                    type="market")
205
        return self.tradehub.create_order(message=create_order_msg)
206
207 View Code Duplication
    def stop_limit_buy(self, pair: str, price: str, quantity: str, stop_price: str):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
208
        """
209
        Function to place a stop limit buy order on Demex.
210
211
        Execution of this function is as follows::
212
213
            stop_limit_buy(pair='swth_eth1', quantity=1000, price='0.0001', stop_price='0.00015')
214
215
        The expected return result for this function is as follows::
216
217
            {
218
                'height': str,
219
                'txhash': str,
220
                `'raw_log': str,
221
                'logs': [{
222
                    'msg_index': int,
223
                    'log': str,
224
                    'events': [{
225
                        'type': str,
226
                        'attributes': [{
227
                            'key': str,
228
                            'value': str
229
                        }, {
230
                            'key': str,
231
                            'value': str
232
                        }]
233
                    }]
234
                }],
235
                'gas_wanted': str,
236
                'gas_used': str
237
            }
238
239
        :return: Dictionary in the form of a JSON message with the stop limit order details.
240
        """
241
        current_price = self.tradehub.get_prices(market=pair)["last"]
242
        if Decimal(stop_price) > Decimal(current_price):
243
            create_order_msg = types.CreateOrderMessage(market=pair,
244
                                                        side="buy",
245
                                                        quantity=quantity,
246
                                                        price=price,
247
                                                        type="stop-limit",
248
                                                        stop_price=stop_price,
249
                                                        trigger_type="last_price")
250
            return self.tradehub.create_order(message=create_order_msg)
251
        else:
252
            raise ValueError("Stop Price target {} is required to be higher than the current market price {} to trigger a stop order.".format(stop_price, current_price))
253
254 View Code Duplication
    def stop_limit_sell(self, pair: str, price: str, quantity: str, stop_price: str):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
255
        """
256
        Function to place a stop limit sell order on Demex.
257
258
        Execution of this function is as follows::
259
260
            stop_limit_sell(pair='swth_eth1', quantity=1000, price='0.0002', stop_price='0.00015')
261
262
        The expected return result for this function is as follows::
263
264
            {
265
                'height': str,
266
                'txhash': str,
267
                `'raw_log': str,
268
                'logs': [{
269
                    'msg_index': int,
270
                    'log': str,
271
                    'events': [{
272
                        'type': str,
273
                        'attributes': [{
274
                            'key': str,
275
                            'value': str
276
                        }, {
277
                            'key': str,
278
                            'value': str
279
                        }]
280
                    }]
281
                }],
282
                'gas_wanted': str,
283
                'gas_used': str
284
            }
285
286
        :return: Dictionary in the form of a JSON message with the stop limit order details.
287
        """
288
        current_price = self.tradehub.get_prices(market=pair)["last"]
289
        if Decimal(stop_price) < Decimal(current_price):
290
            create_order_msg = types.CreateOrderMessage(market=pair,
291
                                                        side="sell",
292
                                                        quantity=quantity,
293
                                                        price=price,
294
                                                        type="stop-limit",
295
                                                        stop_price=stop_price,
296
                                                        trigger_type="last_price")
297
            return self.tradehub.create_order(message=create_order_msg)
298
        else:
299
            raise ValueError("Stop Price target {} is required to be below the current market price {} to trigger a stop order.".format(stop_price, current_price))
300
301 View Code Duplication
    def stop_market_buy(self, pair: str, quantity: str, stop_price: str):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
302
        """
303
        Function to place a stop market buy order on Demex.
304
305
        Execution of this function is as follows::
306
307
            stop_market_buy(pair='swth_eth1', quantity=1000, stop_price='0.00015')
308
309
        The expected return result for this function is as follows::
310
311
            {
312
                'height': str,
313
                'txhash': str,
314
                `'raw_log': str,
315
                'logs': [{
316
                    'msg_index': int,
317
                    'log': str,
318
                    'events': [{
319
                        'type': str,
320
                        'attributes': [{
321
                            'key': str,
322
                            'value': str
323
                        }, {
324
                            'key': str,
325
                            'value': str
326
                        }]
327
                    }]
328
                }],
329
                'gas_wanted': str,
330
                'gas_used': str
331
            }
332
333
        :return: Dictionary in the form of a JSON message with the stop market order details.
334
        """
335
        current_price = self.tradehub.get_prices(market=pair)["last"]
336
        if Decimal(stop_price) > Decimal(current_price):
337
            create_order_msg = types.CreateOrderMessage(market=pair,
338
                                                        side="buy",
339
                                                        quantity=quantity,
340
                                                        type="stop-market",
341
                                                        stop_price=stop_price,
342
                                                        trigger_type="last_price")
343
            return self.tradehub.create_order(message=create_order_msg)
344
        else:
345
            raise ValueError("Stop Price target {} is required to be higher than the current market price {} to trigger a stop order.".format(stop_price, current_price))
346
347 View Code Duplication
    def stop_market_sell(self, pair: str, quantity: str, stop_price: str):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
348
        """
349
        Function to place a stop market sell order on Demex.
350
351
        Execution of this function is as follows::
352
353
            stop_market_sell(pair='swth_eth1', quantity=1000, stop_price='0.00015')
354
355
        The expected return result for this function is as follows::
356
357
            {
358
                'height': str,
359
                'txhash': str,
360
                `'raw_log': str,
361
                'logs': [{
362
                    'msg_index': int,
363
                    'log': str,
364
                    'events': [{
365
                        'type': str,
366
                        'attributes': [{
367
                            'key': str,
368
                            'value': str
369
                        }, {
370
                            'key': str,
371
                            'value': str
372
                        }]
373
                    }]
374
                }],
375
                'gas_wanted': str,
376
                'gas_used': str
377
            }
378
379
        :return: Dictionary in the form of a JSON message with the stop market order details.
380
        """
381
        current_price = self.tradehub.get_prices(market=pair)["last"]
382
        if Decimal(stop_price) < Decimal(current_price):
383
            create_order_msg = types.CreateOrderMessage(market=pair,
384
                                                        side="sell",
385
                                                        quantity=quantity,
386
                                                        type="stop-market",
387
                                                        stop_price=stop_price,
388
                                                        trigger_type="last_price")
389
            return self.tradehub.create_order(message=create_order_msg)
390
        else:
391
            raise ValueError("Stop Price target {} is required to be below the current market price {} to trigger a stop order.".format(stop_price, current_price))
392
393
    def cancel_order(self, order_id: str):
394
        """
395
        Function to place a cancel order on Demex.
396
397
        Execution of this function is as follows::
398
399
            cancel_order(order_id='86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6')
400
401
        The expected return result for this function is as follows::
402
403
            {
404
                'height': str,
405
                'txhash': str,
406
                `'raw_log': str,
407
                'logs': [{
408
                    'msg_index': int,
409
                    'log': str,
410
                    'events': [{
411
                        'type': str,
412
                        'attributes': [{
413
                            'key': str,
414
                            'value': str
415
                        }, {
416
                            'key': str,
417
                            'value': str
418
                        }]
419
                    }]
420
                }],
421
                'gas_wanted': str,
422
                'gas_used': str
423
            }
424
425
        :return: Dictionary in the form of a JSON message with the cancel order details.
426
        """
427
        cancel_order_msg = types.CancelOrderMessage(id=order_id)
428
        return self.tradehub.cancel_order(message=cancel_order_msg)
429
430
    def cancel_orders(self, order_ids: list):
431
        """
432
        Function to place a cancel orders on Demex.
433
434
        Execution of this function is as follows::
435
436
            cancel_order(order_ids=['86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6'])
437
438
        The expected return result for this function is as follows::
439
440
            {
441
                'height': str,
442
                'txhash': str,
443
                `'raw_log': str,
444
                'logs': [{
445
                    'msg_index': int,
446
                    'log': str,
447
                    'events': [{
448
                        'type': str,
449
                        'attributes': [{
450
                            'key': str,
451
                            'value': str
452
                        }, {
453
                            'key': str,
454
                            'value': str
455
                        }]
456
                    }]
457
                }],
458
                'gas_wanted': str,
459
                'gas_used': str
460
            }
461
462
        :return: Dictionary in the form of a JSON message with the cancel orders details.
463
        """
464
        cancel_order_msgs = []
465
        for order_id in order_ids:
466
            cancel_order_msgs.append(types.CancelOrderMessage(id=order_id))
467
        return self.tradehub.cancel_orders(messages=cancel_order_msgs)
468
469
    def cancel_all_open_orders_for_pair(self, pair: str):
470
        """
471
        Function to place a cancel all open orders for a trading pair on Demex.
472
473
        Execution of this function is as follows::
474
475
            cancel_all_open_orders_for_pair(pair='swth_eth1')
476
477
        The expected return result for this function is as follows::
478
479
            {
480
                'height': str,
481
                'txhash': str,
482
                'raw_log': str,
483
                'logs': [{
484
                    'msg_index': int,
485
                    'log': str,
486
                    'events': [{
487
                        'type': str,
488
                        'attributes': [{
489
                            'key': str,
490
                            'value': str
491
                        }, {
492
                            'key': str,
493
                            'value': str
494
                        }]
495
                    }]
496
                }],
497
                'gas_wanted': str,
498
                'gas_used': str
499
            }
500
501
        :return: Dictionary in the form of a JSON message with the cancel all orders for pair details.
502
        """
503
        cancel_all_order_msg = types.CancelAllMessage(market=pair)
504
        return self.tradehub.cancel_all(message=cancel_all_order_msg)
505
506
    def edit_orders(self, orders: [types.EditOrderMessage]):
507
        return self.tradehub.edit_orders(messages=orders)
508
509
    def edit_limit_order(self, order_id: str, quantity: str = None, price: str = None):
510
        """
511
        Function to edit an open limit order on Demex.
512
513
        Execution of this function is as follows::
514
515
            edit_limit_order(order_id='86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6',
516
                             quantity='10000',
517
                             price='0.00011')
518
519
        The expected return result for this function is as follows::
520
521
            {
522
                'height': str,
523
                'txhash': str,
524
                `'raw_log': str,
525
                'logs': [{
526
                    'msg_index': int,
527
                    'log': str,
528
                    'events': [{
529
                        'type': str,
530
                        'attributes': [{
531
                            'key': str,
532
                            'value': str
533
                        }, {
534
                            'key': str,
535
                            'value': str
536
                        }]
537
                    }]
538
                }],
539
                'gas_wanted': str,
540
                'gas_used': str
541
            }
542
543
        :return: Dictionary in the form of a JSON message with the edit limit order details.
544
        """
545
        if order_id in self.get_open_limit_orders():
546
            edit_order_msg = types.EditOrderMessage(id=order_id,
547
                                                    quantity=quantity,
548
                                                    price=price)
549
            return self.tradehub.edit_order(message=edit_order_msg)
550
        else:
551
            raise ValueError("The Order ID - {} - is not a valid limit order; is open or a stop or leveraged order?".format(order_id))
552
553
    def edit_stop_order(self, order_id: str, quantity: str = None, price: str = None, stop_price: str = None):
554
        """
555
        Function to edit an open stop order on Demex.
556
557
        Execution of this function is as follows::
558
559
            edit_stop_order(order_id='86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6',
560
                            quantity='10000',
561
                            price='0.00011')
562
563
        The expected return result for this function is as follows::
564
565
            {
566
                'height': str,
567
                'txhash': str,
568
                `'raw_log': str,
569
                'logs': [{
570
                    'msg_index': int,
571
                    'log': str,
572
                    'events': [{
573
                        'type': str,
574
                        'attributes': [{
575
                            'key': str,
576
                            'value': str
577
                        }, {
578
                            'key': str,
579
                            'value': str
580
                        }]
581
                    }]
582
                }],
583
                'gas_wanted': str,
584
                'gas_used': str
585
            }
586
587
        :return: Dictionary in the form of a JSON message with the edit stop order details.
588
        """
589
        if order_id in self.get_open_stop_orders():
590
            edit_order_msg = types.EditOrderMessage(id=order_id,
591
                                                    quantity=quantity,
592
                                                    price=price,
593
                                                    stop_price=stop_price)
594
            return self.tradehub.edit_order(message=edit_order_msg)
595
        else:
596
            raise ValueError("The Order ID - {} - is not a valid stop order; is open or a limit or leveraged order?".format(order_id))
597
598
    def get_open_orders(self):
599
        """
600
        Function to get all open orders for the wallet attached to the Demex Client.
601
602
        Execution of this function is as follows::
603
604
            get_open_orders()
605
606
        The expected return result for this function is as follows::
607
608
            [
609
                '86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6',
610
                ...
611
            ]
612
613
        :return: List of order IDs.
614
        """
615
        orders = self.tradehub.get_orders(swth_address=self.wallet.address, order_status='open')
616
        order_dict = {}
617
        for order in orders:
618
            order_dict[order["order_id"]] = order
619
        return order_dict
620
621
    def get_open_orders_by_pair(self, pair: str):
622
        """
623
        Function to get all open orders for a specific pair for the wallet attached to the Demex Client.
624
625
        Execution of this function is as follows::
626
627
            get_open_orders(pair='swth_eth1')
628
629
        The expected return result for this function is as follows::
630
631
            [
632
                '86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6',
633
                ...
634
            ]
635
636
        :return: List of order IDs.
637
        """
638
        orders = self.tradehub.get_orders(swth_address=self.wallet.address, order_status='open', market=pair)
639
        order_dict = {}
640
        for order in orders:
641
            order_dict[order["order_id"]] = order
642
        return order_dict
643
644
    def get_open_limit_orders(self):
645
        """
646
        Function to get all open limit orders for the wallet attached to the Demex Client.
647
648
        Execution of this function is as follows::
649
650
            get_open_limit_orders()
651
652
        The expected return result for this function is as follows::
653
654
            [
655
                '86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6',
656
                ...
657
            ]
658
659
        :return: List of order IDs.
660
        """
661
        orders = self.tradehub.get_orders(swth_address=self.wallet.address, order_status='open', order_type='limit')
662
        order_dict = {}
663
        for order in orders:
664
            order_dict[order["order_id"]] = order
665
        return order_dict
666
667
    def get_open_stop_orders(self):
668
        """
669
        Function to get all open stop orders for the wallet attached to the Demex Client.
670
671
        Execution of this function is as follows::
672
673
            get_open_stop_orders()
674
675
        The expected return result for this function is as follows::
676
677
            [
678
                '86BE018C4691E1495DA439647A2246ADFB6101D6292A0C9D365AD88E4A6285B6',
679
                ...
680
            ]
681
682
        :return: List of order IDs.
683
        """
684
        triggerred_stops = self.tradehub.get_orders(swth_address=self.wallet.address, order_status='triggered')
685
        untriggerred_stops = self.tradehub.get_orders(swth_address=self.wallet.address, order_status='untriggered')
686
        stops = triggerred_stops + untriggerred_stops
687
        stop_dict = {}
688
        for stop in stops:
689
            stop_dict[stop["order_id"]] = stop
690
        return stop_dict
691