-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinanceHelper.d
258 lines (224 loc) · 6.32 KB
/
BinanceHelper.d
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
module Client.ClientHelper.BinanceHelper;
public import Utility.Config;
public import Client.ClientHelper.ClientHelper;
public import std.container : DList;
import std.digest.hmac;
import std.datetime ;
import std.digest.sha;
import vibe.http.client;
import std.stdio;
import vibe.stream.operations : readAllUTF8;
import vibe.http.websockets;
import vibe.core.core : sleep;
class SocketData
{
this (WebSocket socket)
{
this.socket = socket;
}
int maxTime = 20;
int curTime = 20;
WebSocket socket;
}
class BinanceHelper
{
KeySecretStruct keySecret;
bool PublicCall( string url, ref Json result )
{
string fullUrl = "https://api.binance.com/api/v1/" ~ url;
try
{
requestHTTP(fullUrl,
(scope req) {
req.method = HTTPMethod.GET;
},
(scope res) {
result = parseJsonString(res.bodyReader.readAllUTF8());
}
);
}
catch ( std.json.JSONException e )
{
writeln("Exception was caught while making the public binance call: ", fullUrl);
return false;
}
scope(failure)
{
writeln("Exception was caught while public data: ", url);
return false;
}
return true;
}
bool PrivateCall( string paramUrl, string quaryStr, ref Json result, HTTPMethod method)
{
import std.conv : to;
import std.digest : toHexString;
auto systime = Clock.currTime(UTC());
string time = systime.toUnixTime.to!string ~ systime.fracSec.msecs.to!string;
string url = "https://api.binance.com/api/v3/" ~ paramUrl ~ "?";
try
{
char[] quary = quaryStr.dup;
quary ~= ("×tamp=" ~ time ~ "&recvWindow=10000").dup ;
auto hmac = HMAC!SHA256(keySecret.secret.representation);
hmac.put(quary.representation);
auto generatedHmac = hmac.finish();
string generatedHmacStr = std.digest.toHexString(generatedHmac);
quary ~= "&signature=";
quary ~= generatedHmacStr.dup;
url ~= quary;
requestHTTP(url.dup,
(scope req) {
req.method = method;
req.headers["X-MBX-APIKEY"] = keySecret.key;
},
(scope res) {
result = parseJsonString(res.bodyReader.readAllUTF8());
}
);
}
catch ( std.json.JSONException e )
{
writeln("Exception was caught while making the binance private call: ", url);
return false;
}
scope(failure)
{
writeln("Exception was caught while binance private data: ", url);
return false;
}
return true;
}
bool KeepSocketAlive( string name, string txName, string streamName )
{
string uniqStreamName = name ~ txName ~ "@" ~ streamName;
auto socketPair = (uniqStreamName in sockets);
if ( socketPair )
{
socketPair.curTime = socketPair.maxTime;
return true;
}
else
{
writeln(" Keeping socket: ", uniqStreamName, " failed because socket didn't existed ");
return false;
}
}
bool LaunchSocket(CallBack)( string name, string txName, string streamName, CallBack callBackfunction )
{
import vibe.core.sync;
import vibe.core.concurrency;
string uniqStreamName = name ~ txName ~ "@" ~ streamName;
auto socketPair = (uniqStreamName in sockets);
if ( socketPair )
{
writeln(" Launching socket: ", uniqStreamName, " failed because socket already existed ");
return false;
}
auto socketData = InitSocket( name, txName, streamName );
if ( socketData )
{
auto result = vibe.core.concurrency.async( &CallBackLoop!(CallBack), name, txName, streamName, callBackfunction );
while ( socketData.curTime-- )
{
sleep(1.seconds);
if ( result.ready() )
{
CloseSocket( name, txName, streamName);
return false;
}
}
}
CloseSocket( name, txName, streamName);
return true;
}
bool CloseSocket( string name, string txName, string streamName )
{
string uniqStreamName = name ~ txName ~ "@" ~ streamName;
auto socket = GetSocket( name, txName, streamName);
if ( !socket )
{
writeln( " Socket which is to be removed does not exists ");
return false;
}
auto returnVal = sockets.remove(uniqStreamName);
socket.close();
return returnVal;
}
WebSocket GetSocket( string name, string txName, string streamName )
{
string uniqStreamName = name ~ txName ~ "@" ~ streamName;
return GetSocket(uniqStreamName);
}
WebSocket GetSocket( string uniqueName )
{
SocketData* socketPair = (uniqueName in sockets);
if ( socketPair )
return socketPair.socket;
return null;
}
private:
SocketData InitSocket( string name, string txName, string streamName )
{
import std.uni : toLower;
string uniqStreamName = name ~ txName ~ "@" ~ streamName;
if ( uniqStreamName in sockets )
{
writeln( "Socket with unique name: ", uniqStreamName, " was already existed will return" );
return null;
}
auto ws_url = URL("wss://stream.binance.com:9443/ws/" ~ uniqStreamName);
auto ws = connectWebSocket(ws_url);
if ( !ws.connected )
return null;
SocketData socketData = new SocketData(ws);
sockets[uniqStreamName] = socketData;
return socketData;
}
short CallBackLoop(CallBack)( string name, string txName, string streamName, CallBack callBackfunction )
{
string uniqStreamName = name ~ txName ~ "@" ~ streamName;
auto socket = GetSocket(name, txName, streamName);
if ( !socket )
{
writeln(" Please be sure socket is initiliazed" );
return -1;
}
while (socket && socket.waitForData())
{
try
{
Json result = parseJsonString(socket.receiveText);
callBackfunction(result);
}
catch ( std.json.JSONException e )
{
writeln("Exception was caught while making the binance socket call: ", e);
continue;
}
}
CloseSocket(name, txName, streamName);
writeln( "Socket will be closed reason was: ", socket.closeReason );
return socket.closeCode;
}
SocketData[string] sockets;
}
unittest
{
import vibe.core.sync;
import vibe.core.concurrency;
writeln( "***** BinanceHelper Tests *****" );
auto helper = new BinanceHelper();
void testFoo( Json json )
{
//writeln(json);
}
// This is a basic test for blocking call for LaunchSocket
// assert (helper.LaunchSocket!((typeof(&testFoo)))("iota", "btc", "aggTrade", &testFoo ));
// I expect LaunchSocket to be called with async normally
// auto result = vibe.core.concurrency.async( &helper.LaunchSocket!((typeof(&testFoo))),"iota", "btc", "aggTrade", &testFoo );
// sleep(2.seconds);
// helper.CloseSocket("iota", "btc", "aggTrade");
//Premature close result should be close
// assert( !result.ready() );
}