1
|
|
|
import random |
2
|
|
|
from tradehub.demex_client import DemexClient |
3
|
|
|
from tests import APITestCase, TRADING_TESTNET_WALLET_MNEMONIC |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class TestTradeHubDemexClient(APITestCase): |
7
|
|
|
|
8
|
|
|
def setUp(self) -> None: |
9
|
|
|
self.demex_client = DemexClient(mnemonic=TRADING_TESTNET_WALLET_MNEMONIC, network='testnet') |
10
|
|
|
|
11
|
|
|
def test_limit_buy(self): |
12
|
|
|
expect: dict = { |
13
|
|
|
'height': str, |
14
|
|
|
'txhash': str, |
15
|
|
|
'raw_log': str, |
16
|
|
|
'logs': [{ |
17
|
|
|
'msg_index': int, |
18
|
|
|
'log': str, |
19
|
|
|
'events': [{ |
20
|
|
|
'type': str, |
21
|
|
|
'attributes': [{ |
22
|
|
|
'key': str, |
23
|
|
|
'value': str |
24
|
|
|
}, { |
25
|
|
|
'key': str, |
26
|
|
|
'value': str |
27
|
|
|
}] |
28
|
|
|
}] |
29
|
|
|
}], |
30
|
|
|
'gas_wanted': str, |
31
|
|
|
'gas_used': str |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
result: dict = self.demex_client.limit_buy(pair='swth_eth', quantity='400', price='0.0000091') |
35
|
|
|
self.assertDictStructure(expect, result) |
36
|
|
|
|
37
|
|
|
def test_limit_sell(self): |
38
|
|
|
expect: dict = { |
39
|
|
|
'height': str, |
40
|
|
|
'txhash': str, |
41
|
|
|
'raw_log': str, |
42
|
|
|
'logs': [{ |
43
|
|
|
'msg_index': int, |
44
|
|
|
'log': str, |
45
|
|
|
'events': [{ |
46
|
|
|
'type': str, |
47
|
|
|
'attributes': [{ |
48
|
|
|
'key': str, |
49
|
|
|
'value': str |
50
|
|
|
}, { |
51
|
|
|
'key': str, |
52
|
|
|
'value': str |
53
|
|
|
}] |
54
|
|
|
}] |
55
|
|
|
}], |
56
|
|
|
'gas_wanted': str, |
57
|
|
|
'gas_used': str |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
result: dict = self.demex_client.limit_sell(pair='swth_eth', quantity='400', price='0.0000227') |
61
|
|
|
self.assertDictStructure(expect, result) |
62
|
|
|
|
63
|
|
|
def test_market_buy(self): |
64
|
|
|
expect: dict = { |
65
|
|
|
'height': str, |
66
|
|
|
'txhash': str, |
67
|
|
|
'raw_log': str, |
68
|
|
|
'logs': [{ |
69
|
|
|
'msg_index': int, |
70
|
|
|
'log': str, |
71
|
|
|
'events': [{ |
72
|
|
|
'type': str, |
73
|
|
|
'attributes': [{ |
74
|
|
|
'key': str, |
75
|
|
|
'value': str |
76
|
|
|
}, { |
77
|
|
|
'key': str, |
78
|
|
|
'value': str |
79
|
|
|
}] |
80
|
|
|
}] |
81
|
|
|
}], |
82
|
|
|
'gas_wanted': str, |
83
|
|
|
'gas_used': str |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
result: dict = self.demex_client.market_buy(pair='swth_eth', quantity='400') |
87
|
|
|
self.assertDictStructure(expect, result) |
88
|
|
|
|
89
|
|
|
def test_market_sell(self): |
90
|
|
|
expect: dict = { |
91
|
|
|
'height': str, |
92
|
|
|
'txhash': str, |
93
|
|
|
'raw_log': str, |
94
|
|
|
'logs': [{ |
95
|
|
|
'msg_index': int, |
96
|
|
|
'log': str, |
97
|
|
|
'events': [{ |
98
|
|
|
'type': str, |
99
|
|
|
'attributes': [{ |
100
|
|
|
'key': str, |
101
|
|
|
'value': str |
102
|
|
|
}, { |
103
|
|
|
'key': str, |
104
|
|
|
'value': str |
105
|
|
|
}] |
106
|
|
|
}] |
107
|
|
|
}], |
108
|
|
|
'gas_wanted': str, |
109
|
|
|
'gas_used': str |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
result: dict = self.demex_client.market_sell(pair='swth_eth', quantity='400') |
113
|
|
|
self.assertDictStructure(expect, result) |
114
|
|
|
|
115
|
|
View Code Duplication |
def test_stop_limit_buy(self): |
|
|
|
|
116
|
|
|
expect: dict = { |
117
|
|
|
'height': str, |
118
|
|
|
'txhash': str, |
119
|
|
|
'raw_log': str, |
120
|
|
|
'logs': [{ |
121
|
|
|
'msg_index': int, |
122
|
|
|
'log': str, |
123
|
|
|
'events': [{ |
124
|
|
|
'type': str, |
125
|
|
|
'attributes': [{ |
126
|
|
|
'key': str, |
127
|
|
|
'value': str |
128
|
|
|
}, { |
129
|
|
|
'key': str, |
130
|
|
|
'value': str |
131
|
|
|
}] |
132
|
|
|
}] |
133
|
|
|
}], |
134
|
|
|
'gas_wanted': str, |
135
|
|
|
'gas_used': str |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
pair = 'swth_eth' |
139
|
|
|
current_price = self.demex_client.tradehub.get_prices(market=pair)["last"] |
140
|
|
|
stop_price = "{:10.8f}".format(float(current_price) + 0.000001) |
141
|
|
|
|
142
|
|
|
result: dict = self.demex_client.stop_limit_buy(pair=pair, quantity='400', price='0.0000091', stop_price=stop_price) |
143
|
|
|
self.assertDictStructure(expect, result) |
144
|
|
|
|
145
|
|
View Code Duplication |
def test_stop_limit_sell(self): |
|
|
|
|
146
|
|
|
expect: dict = { |
147
|
|
|
'height': str, |
148
|
|
|
'txhash': str, |
149
|
|
|
'raw_log': str, |
150
|
|
|
'logs': [{ |
151
|
|
|
'msg_index': int, |
152
|
|
|
'log': str, |
153
|
|
|
'events': [{ |
154
|
|
|
'type': str, |
155
|
|
|
'attributes': [{ |
156
|
|
|
'key': str, |
157
|
|
|
'value': str |
158
|
|
|
}, { |
159
|
|
|
'key': str, |
160
|
|
|
'value': str |
161
|
|
|
}] |
162
|
|
|
}] |
163
|
|
|
}], |
164
|
|
|
'gas_wanted': str, |
165
|
|
|
'gas_used': str |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
pair = 'swth_eth' |
169
|
|
|
current_price = self.demex_client.tradehub.get_prices(market=pair)["last"] |
170
|
|
|
stop_price = "{:10.8f}".format(float(current_price) - 0.000001) |
171
|
|
|
|
172
|
|
|
result: dict = self.demex_client.stop_limit_sell(pair=pair, quantity='400', price='0.0000227', stop_price=stop_price) |
173
|
|
|
self.assertDictStructure(expect, result) |
174
|
|
|
|
175
|
|
View Code Duplication |
def test_stop_market_buy(self): |
|
|
|
|
176
|
|
|
expect: dict = { |
177
|
|
|
'height': str, |
178
|
|
|
'txhash': str, |
179
|
|
|
'raw_log': str, |
180
|
|
|
'logs': [{ |
181
|
|
|
'msg_index': int, |
182
|
|
|
'log': str, |
183
|
|
|
'events': [{ |
184
|
|
|
'type': str, |
185
|
|
|
'attributes': [{ |
186
|
|
|
'key': str, |
187
|
|
|
'value': str |
188
|
|
|
}, { |
189
|
|
|
'key': str, |
190
|
|
|
'value': str |
191
|
|
|
}] |
192
|
|
|
}] |
193
|
|
|
}], |
194
|
|
|
'gas_wanted': str, |
195
|
|
|
'gas_used': str |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
pair = 'swth_eth' |
199
|
|
|
current_price = self.demex_client.tradehub.get_prices(market=pair)["last"] |
200
|
|
|
stop_price = "{:10.8f}".format(float(current_price) + 0.000001) |
201
|
|
|
|
202
|
|
|
result: dict = self.demex_client.stop_market_buy(pair=pair, quantity='400', stop_price=stop_price) |
203
|
|
|
self.assertDictStructure(expect, result) |
204
|
|
|
|
205
|
|
View Code Duplication |
def test_stop_market_sell(self): |
|
|
|
|
206
|
|
|
expect: dict = { |
207
|
|
|
'height': str, |
208
|
|
|
'txhash': str, |
209
|
|
|
'raw_log': str, |
210
|
|
|
'logs': [{ |
211
|
|
|
'msg_index': int, |
212
|
|
|
'log': str, |
213
|
|
|
'events': [{ |
214
|
|
|
'type': str, |
215
|
|
|
'attributes': [{ |
216
|
|
|
'key': str, |
217
|
|
|
'value': str |
218
|
|
|
}, { |
219
|
|
|
'key': str, |
220
|
|
|
'value': str |
221
|
|
|
}] |
222
|
|
|
}] |
223
|
|
|
}], |
224
|
|
|
'gas_wanted': str, |
225
|
|
|
'gas_used': str |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
pair = 'swth_eth' |
229
|
|
|
current_price = self.demex_client.tradehub.get_prices(market=pair)["last"] |
230
|
|
|
stop_price = "{:10.8f}".format(float(current_price) - 0.000001) |
231
|
|
|
|
232
|
|
|
result: dict = self.demex_client.stop_market_sell(pair=pair, quantity='400', stop_price=stop_price) |
233
|
|
|
self.assertDictStructure(expect, result) |
234
|
|
|
|
235
|
|
|
def test_cancel_order(self): |
236
|
|
|
expect: dict = { |
237
|
|
|
'height': str, |
238
|
|
|
'txhash': str, |
239
|
|
|
'raw_log': str, |
240
|
|
|
'logs': [{ |
241
|
|
|
'msg_index': int, |
242
|
|
|
'log': str, |
243
|
|
|
'events': [{ |
244
|
|
|
'type': str, |
245
|
|
|
'attributes': [{ |
246
|
|
|
'key': str, |
247
|
|
|
'value': str |
248
|
|
|
}] |
249
|
|
|
}] |
250
|
|
|
}], |
251
|
|
|
'gas_wanted': str, |
252
|
|
|
'gas_used': str |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
order_id = '' |
256
|
|
|
open_orders = self.demex_client.get_open_limit_orders() |
257
|
|
|
open_orders_list = list(open_orders.keys()) |
258
|
|
|
while order_id in ['', '232570627D12F22489E4AC466DEEEFD56501645BB1F716998F688EB4EE95C081', 'F431BA8B17784DF8C2BAB72F704D703B32A52E96F261C105747EF0F3E872D27F']: |
259
|
|
|
order_id = open_orders_list[random.randint(a=0, b=len(open_orders_list)-1)] |
260
|
|
|
|
261
|
|
|
result: dict = self.demex_client.cancel_order(order_id=order_id) |
262
|
|
|
self.assertDictStructure(expect, result) |
263
|
|
|
|
264
|
|
|
def test_cancel_orders(self): |
265
|
|
|
expect: dict = { |
266
|
|
|
'height': str, |
267
|
|
|
'txhash': str, |
268
|
|
|
'raw_log': str, |
269
|
|
|
'logs': [{ |
270
|
|
|
'msg_index': int, |
271
|
|
|
'log': str, |
272
|
|
|
'events': [{ |
273
|
|
|
'type': str, |
274
|
|
|
'attributes': [{ |
275
|
|
|
'key': str, |
276
|
|
|
'value': str |
277
|
|
|
}] |
278
|
|
|
}] |
279
|
|
|
}], |
280
|
|
|
'gas_wanted': str, |
281
|
|
|
'gas_used': str |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
order_id_1 = '' |
285
|
|
|
order_id_2 = '' |
286
|
|
|
open_orders = self.demex_client.get_open_limit_orders() |
287
|
|
|
open_orders_list = list(open_orders.keys()) |
288
|
|
|
while order_id_1 in ['', '232570627D12F22489E4AC466DEEEFD56501645BB1F716998F688EB4EE95C081', 'F431BA8B17784DF8C2BAB72F704D703B32A52E96F261C105747EF0F3E872D27F']: |
289
|
|
|
order_id_1 = open_orders_list[random.randint(a=0, b=len(open_orders_list)-1)] |
290
|
|
|
|
291
|
|
|
while order_id_2 in [order_id_1, '', '232570627D12F22489E4AC466DEEEFD56501645BB1F716998F688EB4EE95C081', 'F431BA8B17784DF8C2BAB72F704D703B32A52E96F261C105747EF0F3E872D27F']: |
292
|
|
|
order_id_2 = open_orders_list[random.randint(a=0, b=len(open_orders_list)-1)] |
293
|
|
|
|
294
|
|
|
order_ids = [order_id_1, order_id_2] |
295
|
|
|
|
296
|
|
|
result: dict = self.demex_client.cancel_orders(order_ids=order_ids) |
297
|
|
|
self.assertDictStructure(expect, result) |
298
|
|
|
|
299
|
|
|
# def test_get_open_limit_orders(self): |
300
|
|
|
# self.demex_client.demex_client.get_open_limit_orders() |
301
|
|
|
|