-
Notifications
You must be signed in to change notification settings - Fork 72
/
main.cc
258 lines (195 loc) · 9.37 KB
/
main.cc
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
#include <charconv>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <config.hpp>
#include <utils.hpp>
#include <transaction.hpp>
#include <bot.hpp>
// websocketpp includes
#define ASIO_STANDALONE
#ifdef WS_TLS
#include <websocketpp/config/asio_client.hpp>
using AsioClientConfig = websocketpp::config::asio_tls_client;
#else
#include <websocketpp/config/asio_no_tls_client.hpp>
using AsioClientConfig = websocketpp::config::asio_client;
#endif
#include <websocketpp/client.hpp>
struct CustomWSConfig : public AsioClientConfig {
static const std::size_t connection_read_buffer_size = 1024;
static const bool enable_multithreading = false;
};
// Global variables, do not do that at home kids
Utils::Byte privateKey[32];
Transaction tx;
char pregenTxs[Config::TransactionPreGen::ArraySize][Config::Size::BloXrouteTransactionMessageString];
websocketpp::client<CustomWSConfig> wsClient;
websocketpp::connection_hdl wsConnectionHdl;
websocketpp::client<CustomWSConfig>::timer_ptr wsTimer;
// Forward declare functions
#ifdef WS_TLS
websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> onTLSInit(websocketpp::connection_hdl);
#endif
void onOpen(websocketpp::connection_hdl connectionHdl);
void onMessage(websocketpp::connection_hdl connectionHdl, websocketpp::client<CustomWSConfig>::message_ptr message);
void onClose(websocketpp::connection_hdl connectionHdl);
void sendPing(__attribute__((unused)) websocketpp::lib::error_code const &errorCode);
void setTimer();
int main () {
// Convert private key to buffer
Utils::hexStringToBuffer(Config::Transaction::PrivateKey, privateKey);
// Generate transaction data
char data[TransactionDataBuilder::DataLength + 1];
TransactionDataBuilder::buildData(
Config::Transaction::SwapExactETHForTokens::AmountOutMin,
Config::Transaction::SwapExactETHForTokens::TokenAddress,
Config::Transaction::SwapExactETHForTokens::ReceiverAddress,
data
);
// Print debug info
uint64_t gasLimit, value;
std::from_chars(Config::Transaction::GasLimit, Config::Transaction::GasLimit + strlen(Config::Transaction::GasLimit), gasLimit, 16);
std::from_chars(Config::Transaction::Value, Config::Transaction::Value + strlen(Config::Transaction::Value), value, 16);
printf("Transaction fields:\n");
printf("Nonce: %s\n", Config::Transaction::Nonce);
printf("Gas price: to be determined\n");
printf("Gas limit: %" PRIu64 "\n", gasLimit);
printf("To: 0x%s\n", Config::Transaction::To);
printf("Value: %" PRIu64 " wei\n", value);
printf("Data: 0x%s\n", data);
printf("\nListener filters:\n");
printf("Maximum gas price: %s wei\n", Config::BloXroute::Filters::MaxGasPrice);
printf("Minimum value: %s wei\n", Config::BloXroute::Filters::MinValue);
// Set transaction fields
tx.setField(Transaction::Field::Nonce, Config::Transaction::Nonce);
tx.setField(Transaction::Field::GasLimit, Config::Transaction::GasLimit);
tx.setField(Transaction::Field::To, Config::Transaction::To);
tx.setField(Transaction::Field::Value, Config::Transaction::Value);
tx.setField(Transaction::Field::Data, data);
// Pregenerate transactions
printf("\nPregenerating transactions...\n");
Utils::Byte transactionBuffer[Config::Size::TransactionRawBuffer];
char transactionString[Config::Size::TransactionRawBuffer * 2];
for(
std::size_t gasPrice = Config::TransactionPreGen::GasPriceGweiFrom * Config::TransactionPreGen::GasPriceGweiDecimals;
gasPrice <= Config::TransactionPreGen::GasPriceGweiTo * Config::TransactionPreGen::GasPriceGweiDecimals;
gasPrice++
) {
Utils::Byte gasPriceBuffer[8];
std::size_t gasPriceBufferSize = Utils::intToBuffer(
gasPrice * (1000000000 / Config::TransactionPreGen::GasPriceGweiDecimals),
gasPriceBuffer
);
tx.setField(Transaction::Field::GasPrice, gasPriceBuffer, gasPriceBufferSize);
std::size_t transactionBufferSize = tx.sign(privateKey, transactionBuffer);
Utils::bufferToHexString(transactionBuffer, transactionBufferSize, transactionString, true);
BloXrouteMessageBuilder::buildTransaction(
transactionString,
pregenTxs[gasPrice - Config::TransactionPreGen::GasPriceGweiFrom * Config::TransactionPreGen::GasPriceGweiDecimals]
);
}
printf(
"Successfully pregenerated transactions with gas price from %" PRIu64 " to %" PRIu64 " gwei (%zu in total)\n",
Config::TransactionPreGen::GasPriceGweiFrom,
Config::TransactionPreGen::GasPriceGweiTo,
Config::TransactionPreGen::ArraySize
);
// Connect to BloXroute Cloud API
printf("\nConnecting to %s...\n", Config::BloXroute::Connection::Address);
wsClient.init_asio();
wsClient.clear_access_channels(websocketpp::log::alevel::all);
wsClient.clear_error_channels(websocketpp::log::elevel::all);
#ifdef WS_TLS
wsClient.set_tls_init_handler(onTLSInit);
#endif
wsClient.set_open_handler(onOpen);
wsClient.set_message_handler(onMessage);
wsClient.set_close_handler(onClose);
websocketpp::lib::error_code errorCode;
websocketpp::client<CustomWSConfig>::connection_ptr wsConnection = wsClient.get_connection(Config::BloXroute::Connection::Address, errorCode);
if(errorCode) exit(1);
wsConnection->append_header("Authorization", Config::BloXroute::Connection::AuthToken);
wsClient.connect(wsConnection);
wsClient.run();
}
#ifdef WS_TLS
websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> onTLSInit(websocketpp::connection_hdl) {
websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context =
websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
context->set_options(
asio::ssl::context::default_workarounds
| asio::ssl::context::no_sslv2
| asio::ssl::context::no_sslv3
| asio::ssl::context::single_dh_use
);
context->set_verify_mode(asio::ssl::verify_none);
return context;
}
#endif
void onOpen(websocketpp::connection_hdl connectionHdl) {
wsConnectionHdl = connectionHdl;
char message[256];
BloXrouteMessageBuilder::buildSubscribe(Config::BloXroute::Filters::MinValue, Config::BloXroute::Filters::MaxGasPrice, message);
wsClient.send(connectionHdl, message, websocketpp::frame::opcode::text);
printf("Sent subscribe message\n");
printf("Listening on Cloud API...\n");
// Ping connection every 30 seconds
setTimer();
}
void onMessage(websocketpp::connection_hdl connectionHdl, websocketpp::client<CustomWSConfig>::message_ptr message) {
char *messageStr = (char*) message->get_payload().c_str();
if(!BloXrouteMessageParser::validateTransaction(messageStr, Config::BloXroute::Filters::TokenAddress)) {
printf("\nReceived message: %s\n", messageStr);
return;
}
// Get gas price from transaction
char gasPriceStr[Config::Size::TransactionQuantityBuffer * 2 + 1];
std::size_t gasPriceStrLength = BloXrouteMessageParser::extractGasPrice(messageStr, gasPriceStr);
if(gasPriceStrLength <= 16) {
uint64_t gasPrice = 0;
std::from_chars(gasPriceStr, gasPriceStr + gasPriceStrLength, gasPrice, 16);
if(gasPrice % (1000000000 / Config::TransactionPreGen::GasPriceGweiDecimals) == 0) {
gasPrice /= (1000000000 / Config::TransactionPreGen::GasPriceGweiDecimals);
if(
gasPrice >= Config::TransactionPreGen::GasPriceGweiFrom * Config::TransactionPreGen::GasPriceGweiDecimals
&& gasPrice <= Config::TransactionPreGen::GasPriceGweiTo * Config::TransactionPreGen::GasPriceGweiDecimals
) {
wsClient.send(connectionHdl, pregenTxs[gasPrice - Config::TransactionPreGen::GasPriceGweiFrom * Config::TransactionPreGen::GasPriceGweiDecimals], websocketpp::frame::opcode::text);
printf("\nReceived message: %s\n", messageStr);
printf("Sent pregenerated transaction: %s\n", pregenTxs[gasPrice - Config::TransactionPreGen::GasPriceGweiFrom * Config::TransactionPreGen::GasPriceGweiDecimals]);
printf("\nClosing connection...\n");
wsClient.close(connectionHdl, websocketpp::close::status::normal, "Connection closed by client");
return;
}
}
tx.setField(Transaction::Field::GasPrice, gasPriceStr);
Utils::Byte transactionBuffer[Config::Size::TransactionRawBuffer];
std::size_t transactionBufferSize = tx.sign(privateKey, transactionBuffer);
char transactionString[Config::Size::TransactionRawBuffer * 2];
Utils::bufferToHexString(transactionBuffer, transactionBufferSize, transactionString, true);
char message[Config::Size::BloXrouteTransactionMessageString];
BloXrouteMessageBuilder::buildTransaction(transactionString, message);
wsClient.send(connectionHdl, message, websocketpp::frame::opcode::text);
printf("\nReceived message: %s\n", messageStr);
printf("Sent transaction: %s\n", message);
printf("\nClosing connection...\n");
wsClient.close(connectionHdl, websocketpp::close::status::normal, "Connection closed by client.");
}
}
void onClose(websocketpp::connection_hdl connectionHdl) {
wsTimer->cancel();
websocketpp::client<CustomWSConfig>::connection_ptr connection = wsClient.get_con_from_hdl(connectionHdl);
printf(
"Connection closed, code: %s, reason: %s\n",
websocketpp::close::status::get_string(connection->get_remote_close_code()).c_str(),
connection->get_remote_close_reason().c_str()
);
}
void sendPing(websocketpp::lib::error_code const &errorCode) {
if(errorCode) return;
wsClient.ping(wsConnectionHdl, "");
setTimer();
}
void setTimer() {
wsTimer = wsClient.set_timer(30000, sendPing);
}