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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
self.demex_client.cancel_all_open_orders_for_pair(pair=pair) |
327
|
|
|
time.sleep(2) |
328
|
|
|
result: list = self.demex_client.get_open_orders_by_pair(pair=pair) |
329
|
|
|
self.assertFalse(result) |
330
|
|
|
|
331
|
|
|
def test_edit_orders(self): |
332
|
|
|
time.sleep(2) |
333
|
|
|
limit_order_1: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075') |
334
|
|
|
self.checkResponse(limit_order_1) |
335
|
|
|
order_id_1: str = json.loads(limit_order_1["logs"][0]['log'])["order"]["order_id"] |
336
|
|
|
time.sleep(2) |
337
|
|
|
limit_order_2: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075') |
338
|
|
|
self.checkResponse(limit_order_2) |
339
|
|
|
order_id_2: str = json.loads(limit_order_2["logs"][0]['log'])["order"]["order_id"] |
340
|
|
|
|
341
|
|
|
edit_order_1: dict = types.EditOrderMessage(id=order_id_1, quantity='500') |
342
|
|
|
edit_order_2: dict = types.EditOrderMessage(id=order_id_2, quantity='800') |
343
|
|
|
edit_orders: list = [edit_order_1, edit_order_2] |
344
|
|
|
|
345
|
|
|
time.sleep(2) |
346
|
|
|
self.demex_client.edit_orders(orders=edit_orders) |
347
|
|
|
|
348
|
|
|
check_order_1: dict = self.demex_client.tradehub.get_order(order_id=order_id_1) |
349
|
|
|
check_order_2: dict = self.demex_client.tradehub.get_order(order_id=order_id_2) |
350
|
|
|
self.assertEqual(check_order_1["quantity"], '500') |
351
|
|
|
self.assertEqual(check_order_2["quantity"], '800') |
352
|
|
|
|
353
|
|
|
def test_edit_limit_order(self): |
354
|
|
|
time.sleep(2) |
355
|
|
|
limit_order_1: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000075') |
356
|
|
|
self.checkResponse(limit_order_1) |
357
|
|
|
order_id_1: str = json.loads(limit_order_1["logs"][0]['log'])["order"]["order_id"] |
358
|
|
|
|
359
|
|
|
time.sleep(2) |
360
|
|
|
self.demex_client.edit_limit_order(order_id=order_id_1, quantity='600') |
361
|
|
|
|
362
|
|
|
time.sleep(2) |
363
|
|
|
check_order_1: dict = self.demex_client.tradehub.get_order(order_id=order_id_1) |
364
|
|
|
self.assertEqual(check_order_1["quantity"], '600') |
365
|
|
|
|
366
|
|
|
def test_edit_stop_order(self): |
367
|
|
|
pair = 'swth_eth' |
368
|
|
|
current_price = self.demex_client.tradehub.get_prices(market=pair)["last"] |
369
|
|
|
stop_price = "{:10.8f}".format(float(current_price) + 0.000001) |
370
|
|
|
|
371
|
|
|
time.sleep(2) |
372
|
|
|
limit_order_1: dict = self.demex_client.stop_limit_buy(pair=pair, quantity='400', price='0.0000091', stop_price=stop_price) |
373
|
|
|
self.checkResponse(limit_order_1) |
374
|
|
|
order_id_1: str = json.loads(limit_order_1["logs"][0]['log'])["order"]["order_id"] |
375
|
|
|
|
376
|
|
|
time.sleep(2) |
377
|
|
|
stop_price = "{:10.8f}".format(float(current_price) + 0.000002) |
378
|
|
|
self.demex_client.edit_stop_order(order_id=order_id_1, quantity='600', price='0.0000081', stop_price=stop_price) |
379
|
|
|
|
380
|
|
|
time.sleep(2) |
381
|
|
|
check_order_1: dict = self.demex_client.tradehub.get_order(order_id=order_id_1) |
382
|
|
|
self.assertEqual(check_order_1["quantity"], '600') |
383
|
|
|
|
384
|
|
|
def test_get_open_orders(self): |
385
|
|
|
pair = 'swth_btc' |
386
|
|
|
time.sleep(2) |
387
|
|
|
self.demex_client.limit_buy(pair=pair, quantity='400', price='0.0000010') |
388
|
|
|
time.sleep(2) |
389
|
|
|
self.demex_client.limit_sell(pair=pair, quantity='400', price='0.0000020') |
390
|
|
|
result: list = self.demex_client.get_open_orders() |
391
|
|
|
self.assertTrue(result) |
392
|
|
|
|
393
|
|
|
def test_get_open_orders_by_pair(self): |
394
|
|
|
pair = 'swth_btc' |
395
|
|
|
time.sleep(2) |
396
|
|
|
self.demex_client.limit_buy(pair=pair, quantity='400', price='0.0000010') |
397
|
|
|
time.sleep(2) |
398
|
|
|
self.demex_client.limit_sell(pair=pair, quantity='400', price='0.0000020') |
399
|
|
|
result: list = self.demex_client.get_open_orders_by_pair(pair=pair) |
400
|
|
|
self.assertTrue(result) |
401
|
|
|
|
402
|
|
|
def test_get_open_limit_orders(self): |
403
|
|
|
pair = 'swth_btc' |
404
|
|
|
time.sleep(2) |
405
|
|
|
self.demex_client.limit_buy(pair=pair, quantity='400', price='0.0000010') |
406
|
|
|
time.sleep(2) |
407
|
|
|
self.demex_client.limit_sell(pair=pair, quantity='400', price='0.0000020') |
408
|
|
|
result: list = self.demex_client.get_open_limit_orders() |
409
|
|
|
self.assertTrue(result) |
410
|
|
|
|
411
|
|
|
def test_get_open_stop_orders(self): |
412
|
|
|
pair = 'swth_eth' |
413
|
|
|
current_price = self.demex_client.tradehub.get_prices(market=pair)["last"] |
414
|
|
|
stop_price = "{:10.8f}".format(float(current_price) + 0.000001) |
415
|
|
|
time.sleep(2) |
416
|
|
|
self.demex_client.stop_limit_buy(pair=pair, quantity='400', price='0.0000091', stop_price=stop_price) |
417
|
|
|
result: list = self.demex_client.get_open_stop_orders() |
418
|
|
|
self.assertTrue(result) |
419
|
|
|
|