Completed
Push — main ( 5e6d02...339f4c )
by
unknown
15s queued 12s
created

tests.test_tradehub_demex_client   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 420
Duplicated Lines 42.38 %

Importance

Changes 0
Metric Value
eloc 329
dl 178
loc 420
rs 10
c 0
b 0
f 0
wmc 21

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import json
2
import time
3
from tradehub.demex_client import DemexClient
4
import tradehub.types as types
5
from tests import APITestCase, TRADING_TESTNET_WALLET_MNEMONIC
6
7
8
class TestTradeHubDemexClient(APITestCase):
9
10
    def setUp(self) -> None:
11
        self.demex_client = DemexClient(mnemonic=TRADING_TESTNET_WALLET_MNEMONIC, network='testnet')
12
13
    def checkResponse(self, response):
14
        if 'code' in response:
15
            self.skipTest(f"Skip test because of unknown error: {response['code']}")
16
17
    def test_limit_buy(self):
18
        expect: dict = {
19
            'height': str,
20
            'txhash': str,
21
            'raw_log': str,
22
            'logs': [{
23
                'msg_index': int,
24
                'log': str,
25
                'events': [{
26
                    'type': str,
27
                    'attributes': [{
28
                        'key': str,
29
                        'value': str
30
                    }, {
31
                        'key': str,
32
                        'value': str
33
                    }]
34
                }]
35
            }],
36
            'gas_wanted': str,
37
            'gas_used': str
38
        }
39
40
        time.sleep(2)
41
        result: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000091')
42
        self.checkResponse(result)
43
        self.assertDictStructure(expect, result)
44
45
    def test_limit_sell(self):
46
        expect: dict = {
47
            'height': str,
48
            'txhash': str,
49
            'raw_log': str,
50
            'logs': [{
51
                'msg_index': int,
52
                'log': str,
53
                'events': [{
54
                    'type': str,
55
                    'attributes': [{
56
                        'key': str,
57
                        'value': str
58
                    }, {
59
                        'key': str,
60
                        'value': str
61
                    }]
62
                }]
63
            }],
64
            'gas_wanted': str,
65
            'gas_used': str
66
        }
67
68
        time.sleep(2)
69
        result: dict = self.demex_client.limit_sell(pair='swth_eth', quantity='400', price='0.0000227')
70
        self.checkResponse(result)
71
        self.assertDictStructure(expect, result)
72
73
    def test_market_buy(self):
74
        expect: dict = {
75
            'height': str,
76
            'txhash': str,
77
            'raw_log': str,
78
            'logs': [{
79
                'msg_index': int,
80
                'log': str,
81
                'events': [{
82
                    'type': str,
83
                    'attributes': [{
84
                        'key': str,
85
                        'value': str
86
                    }, {
87
                        'key': str,
88
                        'value': str
89
                    }]
90
                }]
91
            }],
92
            'gas_wanted': str,
93
            'gas_used': str
94
        }
95
96
        time.sleep(2)
97
        result: dict = self.demex_client.market_buy(pair='swth_eth', quantity='400')
98
        self.checkResponse(result)
99
        self.assertDictStructure(expect, result)
100
101
    def test_market_sell(self):
102
        expect: dict = {
103
            'height': str,
104
            'txhash': str,
105
            'raw_log': str,
106
            'logs': [{
107
                'msg_index': int,
108
                'log': str,
109
                'events': [{
110
                    'type': str,
111
                    'attributes': [{
112
                        'key': str,
113
                        'value': str
114
                    }, {
115
                        'key': str,
116
                        'value': str
117
                    }]
118
                }]
119
            }],
120
            'gas_wanted': str,
121
            'gas_used': str
122
        }
123
124
        time.sleep(2)
125
        result: dict = self.demex_client.market_sell(pair='swth_eth', quantity='400')
126
        self.checkResponse(result)
127
        self.assertDictStructure(expect, result)
128
129
    def test_stop_limit_buy(self):
130
        expect: dict = {
131
            'height': str,
132
            'txhash': str,
133
            'raw_log': str,
134
            'logs': [{
135
                'msg_index': int,
136
                'log': str,
137
                'events': [{
138
                    'type': str,
139
                    'attributes': [{
140
                        'key': str,
141
                        'value': str
142
                    }, {
143
                        'key': str,
144
                        'value': str
145
                    }]
146
                }]
147
            }],
148
            'gas_wanted': str,
149
            'gas_used': str
150
        }
151
152
        pair = 'swth_eth'
153
        current_price = self.demex_client.tradehub.get_prices(market=pair)["last"]
154
        stop_price = "{:10.8f}".format(float(current_price) + 0.000001)
155
156
        time.sleep(2)
157
        result: dict = self.demex_client.stop_limit_buy(pair=pair, quantity='400', price='0.0000091', stop_price=stop_price)
158
        self.checkResponse(result)
159
        self.assertDictStructure(expect, result)
160
161
    def test_stop_limit_sell(self):
162
        expect: dict = {
163
            'height': str,
164
            'txhash': str,
165
            'raw_log': str,
166
            'logs': [{
167
                'msg_index': int,
168
                'log': str,
169
                'events': [{
170
                    'type': str,
171
                    'attributes': [{
172
                        'key': str,
173
                        'value': str
174
                    }, {
175
                        'key': str,
176
                        'value': str
177
                    }]
178
                }]
179
            }],
180
            'gas_wanted': str,
181
            'gas_used': str
182
        }
183
184
        pair = 'swth_eth'
185
        current_price = self.demex_client.tradehub.get_prices(market=pair)["last"]
186
        stop_price = "{:10.8f}".format(float(current_price) - 0.000001)
187
188
        time.sleep(2)
189
        result: dict = self.demex_client.stop_limit_sell(pair=pair, quantity='400', price='0.0000227', stop_price=stop_price)
190
        self.checkResponse(result)
191
        self.assertDictStructure(expect, result)
192
193
    def test_stop_market_buy(self):
194
        expect: dict = {
195
            'height': str,
196
            'txhash': str,
197
            'raw_log': str,
198
            'logs': [{
199
                'msg_index': int,
200
                'log': str,
201
                'events': [{
202
                    'type': str,
203
                    'attributes': [{
204
                        'key': str,
205
                        'value': str
206
                    }, {
207
                        'key': str,
208
                        'value': str
209
                    }]
210
                }]
211
            }],
212
            'gas_wanted': str,
213
            'gas_used': str
214
        }
215
216
        pair = 'swth_eth'
217
        current_price = self.demex_client.tradehub.get_prices(market=pair)["last"]
218
        stop_price = "{:10.8f}".format(float(current_price) + 0.000001)
219
220
        time.sleep(2)
221
        result: dict = self.demex_client.stop_market_buy(pair=pair, quantity='400', stop_price=stop_price)
222
        self.checkResponse(result)
223
        self.assertDictStructure(expect, result)
224
225
    def test_stop_market_sell(self):
226
        expect: dict = {
227
            'height': str,
228
            'txhash': str,
229
            'raw_log': str,
230
            'logs': [{
231
                'msg_index': int,
232
                'log': str,
233
                'events': [{
234
                    'type': str,
235
                    'attributes': [{
236
                        'key': str,
237
                        'value': str
238
                    }, {
239
                        'key': str,
240
                        'value': str
241
                    }]
242
                }]
243
            }],
244
            'gas_wanted': str,
245
            'gas_used': str
246
        }
247
248
        pair = 'swth_eth'
249
        current_price = self.demex_client.tradehub.get_prices(market=pair)["last"]
250
        stop_price = "{:10.8f}".format(float(current_price) - 0.000001)
251
252
        time.sleep(2)
253
        result: dict = self.demex_client.stop_market_sell(pair=pair, quantity='400', stop_price=stop_price)
254
        self.checkResponse(result)
255
        self.assertDictStructure(expect, result)
256
257
    def test_cancel_order(self):
258
        expect: dict = {
259
            'height': str,
260
            'txhash': str,
261
            'raw_log': str,
262
            'logs': [{
263
                'msg_index': int,
264
                'log': str,
265
                'events': [{
266
                    'type': str,
267
                    'attributes': [{
268
                        'key': str,
269
                        'value': str
270
                    }]
271
                }]
272
            }],
273
            'gas_wanted': str,
274
            'gas_used': str
275
        }
276
        time.sleep(2)
277
        limit_order: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075')
278
        self.checkResponse(limit_order)
279
        order_id: str = json.loads(limit_order["logs"][0]['log'])["order"]["order_id"]
280
        result: dict = self.demex_client.cancel_order(order_id=order_id)
281
        self.checkResponse(result)
282
        self.assertDictStructure(expect, result)
283
284
    def test_cancel_orders(self):
285
        expect: dict = {
286
            'height': str,
287
            'txhash': str,
288
            'raw_log': str,
289
            'logs': [{
290
                'msg_index': int,
291
                'log': str,
292
                'events': [{
293
                    'type': str,
294
                    'attributes': [{
295
                        'key': str,
296
                        'value': str
297
                    }]
298
                }]
299
            }],
300
            'gas_wanted': str,
301
            'gas_used': str
302
        }
303
304
        time.sleep(2)
305
        limit_order_1: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075')
306
        self.checkResponse(limit_order_1)
307
        order_id_1: str = json.loads(limit_order_1["logs"][0]['log'])["order"]["order_id"]
308
        time.sleep(2)
309
        limit_order_2: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075')
310
        self.checkResponse(limit_order_2)
311
        order_id_2: str = json.loads(limit_order_2["logs"][0]['log'])["order"]["order_id"]
312
        order_ids = [order_id_1, order_id_2]
313
        result: dict = self.demex_client.cancel_orders(order_ids=order_ids)
314
        self.checkResponse(result)
315
        self.assertDictStructure(expect, result)
316
317
    def test_cancel_all_open_orders_for_pair(self):
318
        pair = 'swth_btc'
319
        time.sleep(2)
320
        self.demex_client.limit_buy(pair=pair, quantity='400', price='0.0000010')
321
        time.sleep(2)
322
        self.demex_client.limit_sell(pair=pair, quantity='400', price='0.0000020')
323
        result: list = self.demex_client.get_open_orders_by_pair(pair=pair)
324
        self.assertTrue(result)
325
        time.sleep(4)
326
        result: dict = self.demex_client.cancel_all_open_orders_for_pair(pair=pair)
327
        self.checkResponse(result)
328
        time.sleep(2)
329
        result: list = self.demex_client.get_open_orders_by_pair(pair=pair)
330
        self.assertFalse(result)
331
332
    def test_edit_orders(self):
333
        time.sleep(2)
334
        limit_order_1: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075')
335
        self.checkResponse(limit_order_1)
336
        order_id_1: str = json.loads(limit_order_1["logs"][0]['log'])["order"]["order_id"]
337
        time.sleep(2)
338
        limit_order_2: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075')
339
        self.checkResponse(limit_order_2)
340
        order_id_2: str = json.loads(limit_order_2["logs"][0]['log'])["order"]["order_id"]
341
342
        edit_order_1: dict = types.EditOrderMessage(id=order_id_1, quantity='500')
343
        edit_order_2: dict = types.EditOrderMessage(id=order_id_2, quantity='800')
344
        edit_orders: list = [edit_order_1, edit_order_2]
345
346
        time.sleep(2)
347
        self.demex_client.edit_orders(orders=edit_orders)
348
349
        check_order_1: dict = self.demex_client.tradehub.get_order(order_id=order_id_1)
350
        check_order_2: dict = self.demex_client.tradehub.get_order(order_id=order_id_2)
351
        self.assertEqual(check_order_1["quantity"], '500')
352
        self.assertEqual(check_order_2["quantity"], '800')
353
354
    def test_edit_limit_order(self):
355
        time.sleep(2)
356
        limit_order_1: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075')
357
        self.checkResponse(limit_order_1)
358
        order_id_1: str = json.loads(limit_order_1["logs"][0]['log'])["order"]["order_id"]
359
360
        time.sleep(2)
361
        self.demex_client.edit_limit_order(order_id=order_id_1, quantity='600')
362
363
        time.sleep(2)
364
        check_order_1: dict = self.demex_client.tradehub.get_order(order_id=order_id_1)
365
        self.assertEqual(check_order_1["quantity"], '600')
366
367
    def test_edit_stop_order(self):
368
        pair = 'swth_eth'
369
        current_price = self.demex_client.tradehub.get_prices(market=pair)["last"]
370
        stop_price = "{:10.8f}".format(float(current_price) + 0.000001)
371
372
        time.sleep(2)
373
        limit_order_1: dict = self.demex_client.stop_limit_buy(pair=pair, quantity='400', price='0.0000091', stop_price=stop_price)
374
        self.checkResponse(limit_order_1)
375
        order_id_1: str = json.loads(limit_order_1["logs"][0]['log'])["order"]["order_id"]
376
377
        time.sleep(2)
378
        stop_price = "{:10.8f}".format(float(current_price) + 0.000002)
379
        self.demex_client.edit_stop_order(order_id=order_id_1, quantity='600', price='0.0000081', stop_price=stop_price)
380
381
        time.sleep(2)
382
        check_order_1: dict = self.demex_client.tradehub.get_order(order_id=order_id_1)
383
        self.assertEqual(check_order_1["quantity"], '600')
384
385
    def test_get_open_orders(self):
386
        pair = 'swth_btc'
387
        time.sleep(2)
388
        self.demex_client.limit_buy(pair=pair, quantity='400', price='0.0000010')
389
        time.sleep(2)
390
        self.demex_client.limit_sell(pair=pair, quantity='400', price='0.0000020')
391
        result: list = self.demex_client.get_open_orders()
392
        self.assertTrue(result)
393
394
    def test_get_open_orders_by_pair(self):
395
        pair = 'swth_btc'
396
        time.sleep(2)
397
        self.demex_client.limit_buy(pair=pair, quantity='400', price='0.0000010')
398
        time.sleep(2)
399
        self.demex_client.limit_sell(pair=pair, quantity='400', price='0.0000020')
400
        result: list = self.demex_client.get_open_orders_by_pair(pair=pair)
401
        self.assertTrue(result)
402
403
    def test_get_open_limit_orders(self):
404
        pair = 'swth_btc'
405
        time.sleep(2)
406
        self.demex_client.limit_buy(pair=pair, quantity='400', price='0.0000010')
407
        time.sleep(2)
408
        self.demex_client.limit_sell(pair=pair, quantity='400', price='0.0000020')
409
        result: list = self.demex_client.get_open_limit_orders()
410
        self.assertTrue(result)
411
412
    def test_get_open_stop_orders(self):
413
        pair = 'swth_eth'
414
        current_price = self.demex_client.tradehub.get_prices(market=pair)["last"]
415
        stop_price = "{:10.8f}".format(float(current_price) + 0.000001)
416
        time.sleep(2)
417
        self.demex_client.stop_limit_buy(pair=pair, quantity='400', price='0.0000091', stop_price=stop_price)
418
        result: list = self.demex_client.get_open_stop_orders()
419
        self.assertTrue(result)
420