-
Notifications
You must be signed in to change notification settings - Fork 2
/
HitBtcSocketClient.cs
309 lines (293 loc) · 14.6 KB
/
HitBtcSocketClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Logging;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Sockets;
using HitBtc.Net.Enums;
using HitBtc.Net.Interfaces;
using HitBtc.Net.Objects.Socket;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace HitBtc.Net
{
public class HitBtcSocketClient : SocketClient, IHitBtcSocketClient
{
private const string MarketDataUrlSuffix = "public";
private static HitBtcSocketClientOptions defaultOptions = new HitBtcSocketClientOptions();
private Dictionary<string, HitBtcSocketEvent> mappedResponses;
#region Events
public event Action<HitBtcSocketOrderBookEvent> OnOrderBookUpdate;
public event Action<HitBtcSocketOrderBookEvent> OnOrderBookSnapshot;
public event Action<HitBtcSocketTickerEvent> OnTickerUpdate;
public event Action<HitBtcSocketCandlesEvent> OnCandlesUpdate;
public event Action<HitBtcSocketCandlesEvent> OnCandlesSnapshot;
public event Action<HitBtcSocketTradesEvent> OnTradesSnapshot;
public event Action<HitBtcSocketTradesEvent> OnTradesUpdate;
public event Action<HitBtcSocketActiveOrdersReportEvent> OnActiveOrdersSnapshot;
public event Action<HitBtcSocketOrderReportEvent> OnOrderUpdate;
public event Action<HitBtcSocketMarginActiveOrdersReportEvent> OnMarginActiveOrdersSnapshot;
public event Action<HitBtcSocketMarginOrderReportEvent> OnMarginOrderUpdate;
public event Action<HitBtcSocketMarginAccountReportEvent> OnMarginAccountUpdate;
public event Action<HitBtcSocketMarginAccountsEvent> OnMarginAccountsSnapshot;
public event Action<HitBtcSocketAccountTransactionEvent> OnAccountTransactionUpdate;
public event Action<string> OnError;
#endregion
public HitBtcSocketClient() : this(nameof(HitBtcSocketClient), defaultOptions, null)
{
}
public HitBtcSocketClient(string key, string secret, string socketClientName = nameof(HitBtcSocketClient))
: this
(
socketClientName,
defaultOptions,
new HitBtcAuthenticationProvider(new ApiCredentials(key, secret))
)
{
}
public HitBtcSocketClient(string clientName, HitBtcSocketClientOptions exchangeOptions, HitBtcAuthenticationProvider authenticationProvider) : base(clientName, exchangeOptions, authenticationProvider)
{
mappedResponses = Objects.Socket.Helpers.SocketMapping.HitBtcSocketResponsesAsStringEnumMappedDict();
}
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookAsync(string symbol, Action<DataEvent<HitBtcSocketOrderBookEvent>> dataHandler)
{
return await SubscribeAsync<HitBtcSocketOrderBookEvent>(BaseAddress+"public", new HitBtcSubscribeToOrderBookRequest(symbol), null, false, dataHandler);
}
public async Task<CallResult<UpdateSubscription>> SubscribeAsync<TParams>(HitBtcSocketSubscribeBaseRequest<TParams> subscribeRequest)
{
var dataHandler = new Action<DataEvent<string>>(data =>
{
var token = JToken.Parse(data.Data);
var method = (string)token["method"];
if (method is null)
{
ErrorHandler(token["error"].ToString());
return;
}
HitBtcSocketEvent responseType;
if (!mappedResponses.TryGetValue(method, out responseType))
{
ErrorHandler($"Unknown response method [{method}] update catched at data {data}");
return;
}
switch (responseType)
{
case HitBtcSocketEvent.OrderBookUpdated:
{
var result = Deserialize<HitBtcSocketOrderBookEvent>(token, false);
if (result.Success)
{
OnOrderBookUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.OrderbookFullSnapshot:
{
var result = Deserialize<HitBtcSocketOrderBookEvent>(token, false);
if (result.Success)
{
OnOrderBookSnapshot?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.TradesSnapshot:
{
var result = Deserialize<HitBtcSocketTradesEvent>(token, false);
if (result.Success)
{
OnTradesSnapshot?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.TradesUpdated:
{
var result = Deserialize<HitBtcSocketTradesEvent>(token, false);
if (result.Success)
{
OnTradesUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.TickerUpdated:
{
var result = Deserialize<HitBtcSocketTickerEvent>(token, false);
if (result.Success)
{
OnTickerUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.CandlesSnapshot:
{
var result = Deserialize<HitBtcSocketCandlesEvent>(token, false);
if (result.Success)
{
OnCandlesSnapshot?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.CandlesUpdated:
{
var result = Deserialize<HitBtcSocketCandlesEvent>(token, false);
if (result.Success)
{
OnCandlesUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.ActiveOrdersSnapshot:
{
var result = Deserialize<HitBtcSocketActiveOrdersReportEvent>(token, false);
if (result.Success)
{
OnActiveOrdersSnapshot?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.OrderUpdated:
{
var result = Deserialize<HitBtcSocketOrderReportEvent>(token, false);
if (result.Success)
{
OnOrderUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.MarginOrdersSnapshot:
{
var result = Deserialize<HitBtcSocketMarginActiveOrdersReportEvent>(token, false);
if (result.Success)
{
OnMarginActiveOrdersSnapshot?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.MarginOrderUpdated:
{
var result = Deserialize<HitBtcSocketMarginOrderReportEvent>(token, false);
if (result.Success)
{
OnMarginOrderUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.MarginAccountsSnapshot:
{
var result = Deserialize<HitBtcSocketMarginAccountsEvent>(token, false);
if (result.Success)
{
OnMarginAccountsSnapshot?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.MarginAccountUpdated:
{
var result = Deserialize<HitBtcSocketMarginAccountReportEvent>(token, false);
if (result.Success)
{
OnMarginAccountUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
case HitBtcSocketEvent.TransactionUpdated:
{
var result = Deserialize<HitBtcSocketAccountTransactionEvent>(token, false);
if (result.Success)
{
OnAccountTransactionUpdate?.Invoke(result.Data);
}
else
log.Write(LogLevel.Warning, "Couldn't deserialize data received from stream: " + result.Error);
break;
}
default:
ErrorHandler($"Catched unknown method update: {data}");
break;
}
});
return await SubscribeAsync(BaseAddress + subscribeRequest.EndpointSuffix, subscribeRequest, null, subscribeRequest.EndpointSuffix != MarketDataUrlSuffix, dataHandler);
}
protected override async Task<CallResult<bool>> AuthenticateSocketAsync(SocketConnection s)
{
bool isSuccess = false;
ServerError serverError = null;
var authRequest = new HitBtcSocketLogin(
new LoginParams()
{
Algorithm = "BASIC",
Key = authProvider.Credentials.Key?.GetString(),
SecretKey = authProvider.Credentials.Secret?.GetString(),
});
await s.SendAndWaitAsync(authRequest, TimeSpan.FromSeconds(1), f =>
{
if (String.IsNullOrEmpty(f.ToString()))
{
isSuccess = false;
serverError = new ServerError("Auth request was not succesful");
}
else
{
isSuccess = true;
}
return true;
});
return new CallResult<bool>(isSuccess, null);
}
protected override bool HandleQueryResponse<T>(SocketConnection s, object request, JToken data, out CallResult<T> callResult)
{
throw new NotImplementedException();
}
protected override bool HandleSubscriptionResponse(SocketConnection s, SocketSubscription subscription, object request, JToken message, out CallResult<object> callResult)
{
var error = message["error"];
callResult = new CallResult<object>(request,error==null?null:new ServerError(error["message"].ToString(),error));
return message["result"] != null && (bool)message["result"] == true;
}
protected override bool MessageMatchesHandler(JToken message, object request)
{
return true;
}
protected override bool MessageMatchesHandler(JToken message, string identifier)
{
throw new NotImplementedException();
}
protected override Task<bool> UnsubscribeAsync(SocketConnection connection, SocketSubscription s)
{
throw new NotImplementedException();
}
private void ErrorHandler(string errorMessage)
{
OnError?.Invoke(errorMessage);
log.Write(LogLevel.Warning, errorMessage);
}
}
}