1
+ #include < fstream>
2
+ #include " ../include/Binance_Client.h"
3
+
4
+ #include < thread>
5
+ #include < chrono>
6
+ #include < iostream>
7
+
8
+
9
+ bool auth_mode = 1 ;
10
+ std::string __KEYS_PATH = " keys_conf.json" ;
11
+
12
+ struct SomeFunctor
13
+ {
14
+ Json::CharReaderBuilder charbuilder;
15
+ Json::CharReader* charreader;
16
+ std::string parse_errors;
17
+ std::string msg_buffer;
18
+ Json::Value stream_msg;
19
+
20
+ SomeFunctor ()
21
+ : msg_buffer{ " " }, parse_errors{ }, charreader{ charbuilder.newCharReader () }
22
+ {}
23
+
24
+
25
+ SomeFunctor operator ()(const std::string& response)
26
+ {
27
+ this ->stream_msg .clear ();
28
+ this ->parse_errors .clear ();
29
+
30
+ this ->charreader ->parse (response.c_str (),
31
+ response.c_str () + response.size (),
32
+ &this ->stream_msg ,
33
+ &parse_errors);
34
+
35
+ std::cout << this ->stream_msg ;
36
+
37
+ return *this ;
38
+ }
39
+ };
40
+
41
+ Json::Value load_keys (std::string conf_filename) // return return type
42
+ {
43
+ Json::Value keys_json;
44
+ std::ifstream file_handle (__KEYS_PATH);
45
+ if (!file_handle.is_open ())
46
+ {
47
+ std::cout << " failed to load keys json" << std::endl;
48
+ exit (1 );
49
+ };
50
+
51
+ file_handle >> keys_json;
52
+
53
+ return keys_json;
54
+ }
55
+
56
+ int main ()
57
+ {
58
+ try
59
+ {
60
+ // ========================================================== Client Tests
61
+
62
+ std::cout << " \n\n\n Starting Client tests...\n\n " ;
63
+
64
+ Json::Value keys;
65
+
66
+ if (auth_mode) keys = load_keys (__KEYS_PATH);
67
+
68
+ SpotClient my_client{ keys[" key" ].asString (), keys[" secret" ].asString () }; // private
69
+ SpotClient::MarginAccount margin_acc{ my_client };
70
+
71
+ FuturesClientUSDT my_f_client{}; // public
72
+
73
+ try
74
+ {
75
+ std::cout << " \n attemping private struct on public client\n " ;
76
+ FuturesClientUSDT::FuturesWallet my_f_wallet{ my_f_client };
77
+ }
78
+ catch (ClientException& e)
79
+ {
80
+ std::cout << " \n result of attempt: " << e.what ();
81
+ }
82
+
83
+
84
+ // ========================================================== WS Tests
85
+
86
+ std::cout << " \n\n\n Starting WS tests...\n\n " ;
87
+
88
+ std::string str_buf{};
89
+ SomeFunctor ws_stream_read{};
90
+
91
+ std::string cust_stream = " btcusdt@aggTrade/ethusdt@aggTrade" ;
92
+ my_client.set_refresh_key_interval (1 );
93
+ std::cout << " \n starting custom stream + ping\n " ;
94
+ std::thread t1 (&SpotClient::custom_stream<SomeFunctor>, std::ref (my_client), cust_stream, std::ref (str_buf), std::ref (ws_stream_read), 1 );
95
+ Sleep (2000 );
96
+ std::cout << " \n is stream open? " << my_client.is_stream_open (cust_stream);
97
+ std::cout << " \n going to sleep for 3 seconds" ;
98
+ Sleep (3000 );
99
+ std::cout << " \n terminating custom stream" ;
100
+ my_client.close_stream (cust_stream);
101
+ std::cout << " \n custom stream was terminated" ;
102
+
103
+ std::cout << " \n starting aggTrade stream\n " ;
104
+ std::thread t2 (&SpotClient::stream_aggTrade<SomeFunctor>, std::ref (my_client), " btcusdt" , std::ref (str_buf), std::ref (ws_stream_read));
105
+ std::cout << " \n going to sleep for 2 seconds" ;
106
+ Sleep (2000 );
107
+ std::cout << " \n terminating aggTrade stream" ;
108
+ my_client.close_stream (" btcusdt@aggTrade" );
109
+ std::cout << " \n aggTrade stream was terminated" ;
110
+
111
+ t1.join ();
112
+ t2.join ();
113
+ Sleep (2000 );
114
+
115
+ // ========================================================== REST Tests
116
+
117
+ std::cout << " \n\n\n Starting REST tests...\n\n " ;
118
+
119
+ Params temp_params{};
120
+ temp_params.set_param <std::string>(" symbol" , " BTCUSDT" );
121
+ Params order_params{ Params{} };
122
+ order_params = Params{};
123
+
124
+ std::cout << " \n fetching all orders spotclient:\n " ;
125
+ std::cout << my_client.all_orders (&temp_params) << " \n " ;
126
+
127
+ std::cout << " \n fetching all orders futuresclient:\n " ;
128
+ std::cout << my_f_client.all_orders (&temp_params) << " \n " ;
129
+
130
+ std::cout << " \n MarginAccount default not isolated listen_key: " << margin_acc.margin_get_listen_key () << " \n " ;
131
+ std::cout << " \n MarginAccount isolated listen_key: " << margin_acc.margin_isolated_get_listen_key (" btcusdt" ) << " \n " ;
132
+
133
+
134
+ std::cout << " \t esting custom request with params: \n " << my_client.custom_get_req (" https://api.binance.com" , " /api/v3/time" , &order_params, 0 );
135
+
136
+
137
+ // ========================================================== Exceptions
138
+
139
+ std::cout << " \n\n\n Starting Exceptions tests...\n\n " ;
140
+
141
+ try
142
+ {
143
+ BadRequestREST e{};
144
+ throw (e);
145
+ }
146
+ catch (std::exception& e)
147
+ {
148
+ std::cout << e.what () << " \n " ;
149
+ std::cout << " \n BadRequestREST was caught\n " ;
150
+ }
151
+
152
+ try
153
+ {
154
+ BadSetupHeadersREST e{};
155
+ throw (e);
156
+ }
157
+ catch (ClientException& e)
158
+ {
159
+ std::cout << e.what () << " \n " ;
160
+ std::cout << " \n BadSetupHeadersREST was caught\n " ;
161
+ }
162
+
163
+ try
164
+ {
165
+ CustomException e (" custom exception" );
166
+ throw (e);
167
+ }
168
+ catch (ClientException& e)
169
+ {
170
+ std::cout << e.what () << " \n " ;
171
+ std::cout << " \n CustomException was caught\n " ;
172
+ }
173
+
174
+ }
175
+ catch (ClientException& e)
176
+ {
177
+ std::cout << " \n an exception has occurred: " << e.what ();
178
+ }
179
+ std::cout << " finished successfully" ;
180
+ return 0 ;
181
+ }
0 commit comments