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