· 5 years ago · May 12, 2020, 03:44 PM
1using System;
2using System.Linq;
3using ExchangeSharp;
4using System.Threading.Tasks;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6
7namespace ExchangeSharpTests
8{
9 /// <summary>
10 /// NOTE: This 'test file' should be used to assist with developing the private API calls
11 /// </summary>
12 [TestClass]
13 public sealed class PrivateAPIDevelopmentTestClass
14 {
15 #region Properties
16
17 public string Exchange { get; set; }
18 public string ApiKey { get; set; }
19 public string ApiSecret { get; set; }
20 public string Memo { get; set; }
21 public IExchangeAPI API { get; set; }
22
23 #endregion
24
25 public PrivateAPIDevelopmentTestClass()
26 {
27 // Setup the api key and exchange based upon the scenario
28 Exchange = "BitMart";
29
30 // NOTE: Do not save your API keys in the repository!!!!!!!
31 ApiKey = "9b2d84a40cd1926a8968f2018a585dd1";
32 ApiSecret = "acb360d8ff4c74488938f49fa50e3f40";
33 Memo = "Bitmart API Dev";
34 // NOTE: Do not save your API keys in the repository!!!!!!!
35
36 API = ExchangeAPI.GetExchangeAPI(Exchange);
37 API.LoadAPIKeysUnsecure(ApiKey, ApiSecret, Memo);
38
39 API.Passphrase = Memo.ToSecureString();
40 }
41
42 #region Balances/Amounts
43
44 [TestMethod]
45 [DataTestMethod]
46 public async Task GetAmountsAvailableToTradeAsyncTest()
47 {
48 try
49 {
50 var amounts = await API.GetAmountsAvailableToTradeAsync();
51 Assert.IsTrue(amounts.ContainsKey("BTC"));
52 }
53 catch (APIException apie)
54 {
55 Console.WriteLine(apie.Message);
56 }
57 catch (Exception ex)
58 {
59 Console.WriteLine(ex.Message);
60 }
61 }
62
63 [TestMethod]
64 public async Task GetAmountsAsyncTest()
65 {
66 try
67 {
68 var amounts = await API.GetAmountsAsync();
69 Assert.IsTrue(amounts.ContainsKey("BTC"));
70
71 }
72 catch (APIException apie)
73 {
74 Console.WriteLine(apie.Message);
75 }
76 catch (Exception ex)
77 {
78 Console.WriteLine(ex.Message);
79 }
80 }
81
82 #endregion
83
84 #region Trading
85
86 [TestMethod]
87 public async Task PlaceOrderAsyncTest()
88 {
89 try
90 {
91 //await API.CancelOrderAsync("21");
92
93 var result = await PlaceOrderWhichWillNotFill("BTC-ABBC", 10);
94
95 // TODO : Add tests to validate that the order is created successfully and is in the correct state (should be Pending)
96 Assert.IsTrue(result.Result == ExchangeAPIOrderResult.Pending);
97
98 // Cancel the order
99 await API.CancelOrderAsync(result.OrderId);
100
101 // TODO: Add appropriate test validation/assertions !
102 }
103 catch (APIException apie)
104 {
105 Console.WriteLine(apie.Message);
106 }
107 catch (Exception ex)
108 {
109 Console.WriteLine(ex.Message);
110 }
111 }
112
113 [TestMethod]
114 public async Task PlaceOrdersAsyncTest()
115 {
116 try
117 {
118 string globalCurrency = "BTC-ETH";
119 string marketSymbol = await API.GlobalMarketSymbolToExchangeMarketSymbolAsync(globalCurrency);
120 var ticker = await API.GetTickerAsync(globalCurrency);
121
122 int percentBelow = 10;
123
124 //BUY 1
125
126 var eor1 = new ExchangeOrderRequest
127 {
128 GlobalSymbol = globalCurrency,
129 MarketSymbol = marketSymbol,
130 Amount = 0.002m,
131 Price = decimal.Round(ticker.Ask - (ticker.Ask / 100) * percentBelow, 2),
132 IsBuy = true
133 };
134
135 //SELL 2
136
137 var eor2 = new ExchangeOrderRequest
138 {
139 GlobalSymbol = globalCurrency,
140 MarketSymbol = marketSymbol,
141 Amount = 0.002m,
142 Price = decimal.Round(ticker.Ask + (ticker.Ask / 100) * percentBelow, 2),
143 IsBuy = false
144 };
145
146
147 ExchangeOrderResult[] result = await API.PlaceOrdersAsync(new[] { eor1, eor2 });
148
149 Assert.IsTrue(result.Length == 2);
150
151 foreach (var r in result)
152 {
153 await API.CancelOrderAsync(r.OrderId);
154 }
155 }
156 catch (APIException apie)
157 {
158 Console.WriteLine(apie.Message);
159 }
160 catch (Exception ex)
161 {
162 Console.WriteLine(ex.Message);
163 }
164 }
165
166 private async Task<ExchangeOrderResult> PlaceOrderWhichWillNotFill(string globalCurrency, int percentBelow = 10, bool isBuy = true)
167 {
168 var ticker = await API.GetTickerAsync(globalCurrency);
169
170 // Retrieve the meta-data so we can use the minimum price (so we minimize trade amounts)
171 var marketSymbols = await API.GetMarketSymbolsMetadataAsync();
172 var gsData = marketSymbols.FirstOrDefault(w => w.GlobalSymbol == globalCurrency);
173
174 decimal price = isBuy ? decimal.Round(ticker.Ask - (ticker.Ask / 100) * percentBelow, 2) : decimal.Round(ticker.Ask + (ticker.Ask / 100) * percentBelow, 2);
175
176 var eor = new ExchangeOrderRequest
177 {
178 GlobalSymbol = globalCurrency,
179 MarketSymbol = API.GlobalMarketSymbolToExchangeMarketSymbolAsync(globalCurrency).Sync(),
180 Amount = 0.00015m / ticker.Ask,
181 Price = ticker.Ask,
182 IsBuy = isBuy
183 };
184
185 return await API.PlaceOrderAsync(eor);
186 }
187
188 #endregion
189
190 #region Orders
191
192 [TestMethod]
193 public async Task GetOrderDetailsAsyncTest()
194 {
195 try
196 {
197 // TODO: Either use GetCompletedOrderDetailsAsync or place an order which will not fill and retrieve those details
198 var global = "BTC-ABBC";
199 var exchangeMarketSymbol = await API.GlobalMarketSymbolToExchangeMarketSymbolAsync(global);
200 var orders = await API.GetCompletedOrderDetailsAsync(exchangeMarketSymbol);
201 var order = orders.FirstOrDefault();
202 if (order != null)
203 {
204 var orderDetails = await API.GetOrderDetailsAsync(order.OrderId);
205 var orderJson = Newtonsoft.Json.JsonConvert.SerializeObject(orderDetails, Newtonsoft.Json.Formatting.Indented);
206 Console.WriteLine(orderJson);
207
208 // TODO: Add appropriate test validation/assertions !
209 }
210 }
211 catch (APIException apie)
212 {
213 Console.WriteLine(apie.Message);
214 }
215 catch (Exception ex)
216 {
217 Console.WriteLine(ex.Message);
218 }
219 }
220
221 [TestMethod]
222 public async Task GetOpenOrderDetailsAsyncTest()
223 {
224 try
225 {
226 var order = await PlaceOrderWhichWillNotFill("BTC-EUR", 10, true);
227
228 if (order != null)
229 {
230 Assert.IsTrue(order.Result == ExchangeAPIOrderResult.Pending);
231
232 var orderDetails = await API.GetOpenOrderDetailsAsync("BTC-EUR");
233
234 if (orderDetails.Count() > 0)
235 {
236 var detail = orderDetails.FirstOrDefault();
237
238 if (detail != null)
239 {
240 Assert.IsTrue(order.OrderId == orderDetails.FirstOrDefault().OrderId);
241
242 var orderJson = Newtonsoft.Json.JsonConvert.SerializeObject(orderDetails, Newtonsoft.Json.Formatting.Indented);
243 Console.WriteLine(orderJson);
244 }
245
246 }
247 await API.CancelOrderAsync(order.OrderId);
248 }
249 }
250 catch (APIException apie)
251 {
252 Console.WriteLine(apie.Message);
253 }
254 catch (Exception ex)
255 {
256 Console.WriteLine(ex.Message);
257 }
258 }
259
260 [TestMethod]
261 public async Task GetCompletedOrderDetailsAsyncTest()
262 {
263 try
264 {
265 var orders = await API.GetCompletedOrderDetailsAsync("BTC-EUR");
266 Assert.IsTrue(orders.Count() > 0);
267 }
268 catch (APIException apie)
269 {
270 Console.WriteLine(apie.Message);
271 }
272 catch (Exception ex)
273 {
274 Console.WriteLine(ex.Message);
275 }
276 }
277
278 #endregion
279
280 #region Trade Retrieval
281
282 [TestMethod]
283 public async Task GetHistoricalTradesAsyncTest()
284 {
285 try
286 {
287 ExchangeTrade[] trades = null;
288
289 await API.GetHistoricalTradesAsync(x => { trades = x.ToArray(); return true; }, "BTC-EUR");
290
291 var tradeJson = Newtonsoft.Json.JsonConvert.SerializeObject(trades, Newtonsoft.Json.Formatting.Indented);
292 Console.WriteLine(tradeJson);
293
294 Assert.IsTrue(trades.Count() > 0);
295
296 }
297 catch (APIException apie)
298 {
299 Console.WriteLine(apie.Message);
300 }
301 catch (Exception ex)
302 {
303 Console.WriteLine(ex.Message);
304 }
305 }
306
307 [TestMethod]
308 public async Task GetRecentTradesAsyncTest()
309 {
310 try
311 {
312 var trades = await API.GetRecentTradesAsync("BTC-EUR");
313 }
314 catch (APIException apie)
315 {
316 Console.WriteLine(apie.Message);
317 }
318 catch (Exception ex)
319 {
320 Console.WriteLine(ex.Message);
321 }
322 }
323
324 #endregion
325
326 #region Deposits
327
328 [TestMethod]
329 public async Task GetDepositHistoryAsyncTest()
330 {
331 try
332 {
333 var history = await API.GetDepositHistoryAsync("BTC");
334 }
335 catch (APIException apie)
336 {
337 Console.WriteLine(apie.Message);
338 }
339 catch (Exception ex)
340 {
341 Console.WriteLine(ex.Message);
342 }
343 }
344
345 [TestMethod]
346 public async Task GetDepositAddressAsyncTest()
347 {
348 try
349 {
350 var address = await API.GetDepositAddressAsync("BTC");
351 }
352 catch (APIException apie)
353 {
354 Console.WriteLine(apie.Message);
355 }
356 catch (Exception ex)
357 {
358 Console.WriteLine(ex.Message);
359 }
360 }
361
362 #endregion
363 }
364}