Uniswap Sniper Bot  1.0
bot.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utils.hpp>
4 
18  inline constexpr char const * DataBoilerplate { "7ff36ab5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000000000" };
19 
24  inline constexpr std::size_t DataLength { std::char_traits<char>::length(DataBoilerplate) };
25 
36  inline std::size_t buildData(const char *amountOutMin, const char *targetTokenAddress, const char *receiverAddress, char *output) {
37  std::size_t amountOutMinLength = strlen(amountOutMin);
38 
39  memcpy(output, DataBoilerplate, DataLength);
40  memcpy(output + 8 + 64 - amountOutMinLength, amountOutMin, amountOutMinLength);
41  memcpy(output + 8 + 3 * 64 - 40, receiverAddress, 40);
42  memcpy(output + 8 + 7 * 64 - 40, targetTokenAddress, 40);
43 
44  output[DataLength] = '\0';
45 
46  return DataLength;
47  }
48 }
49 
57  inline constexpr std::size_t MethodPosition = 37;
58 
62  inline constexpr std::size_t TokenPosition = 179;
63 
67  inline constexpr std::size_t GasPricePosition = 555;
68 
76  inline bool validateTransaction(const char *message, const char *targetTokenAddress) {
77  return
78  memcmp(message + MethodPosition, "subscribe", 9) == 0
79  && memcmp(message + TokenPosition, targetTokenAddress, 40) == 0;
80  }
81 
89  inline std::size_t extractGasPrice(const char *message, char *output) {
90  const char *gasPriceStart = message + GasPricePosition;
91  const char *gasPriceEnd = strchr(gasPriceStart, '\"');
92  std::size_t gasPriceLength = gasPriceEnd - gasPriceStart;
93 
94  memcpy(output, gasPriceStart, gasPriceLength);
95  output[gasPriceLength] = '\0';
96 
97  return gasPriceLength;
98  }
99 }
100 
113  inline std::size_t buildSubscribe(const char *minimumLiquidityETH, const char *maximumGasPrice, char *output) {
114  strcpy(output, "{\"method\":\"subscribe\",\"params\":[\"newTxs\",{\"include\":[\"tx_contents.input\",\"tx_contents.gas_price\"],\"filters\":\"method_id = f305d719 and to = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D and value >= ");
115  strcat(output, minimumLiquidityETH);
116  strcat(output, " and gas_price <= ");
117  strcat(output, maximumGasPrice);
118  strcat(output, "\"}]}");
119 
120  return strlen(output);
121  }
122 
130  inline std::size_t buildTransaction(const char *rawTransaction, char *output) {
131  strcpy(output, "{\"method\":\"blxr_tx\",\"params\":{\"transaction\":\"");
132  strcat(output, rawTransaction);
133  strcat(output, "\"}}");
134 
135  return strlen(output);
136  }
137 }
Utilities to build BloXroute messages.
Definition: bot.hpp:104
std::size_t buildSubscribe(const char *minimumLiquidityETH, const char *maximumGasPrice, char *output)
Builds subscribe message.
Definition: bot.hpp:113
std::size_t buildTransaction(const char *rawTransaction, char *output)
Builds transaction message.
Definition: bot.hpp:130
Utilities to parse incoming BloXroute messages.
Definition: bot.hpp:53
constexpr std::size_t TokenPosition
Position of the token address in the message string.
Definition: bot.hpp:62
std::size_t extractGasPrice(const char *message, char *output)
Extract gas price hex value from the message.
Definition: bot.hpp:89
constexpr std::size_t GasPricePosition
Position of the gas price hex value in the message string.
Definition: bot.hpp:67
bool validateTransaction(const char *message, const char *targetTokenAddress)
Check if message is of "subscribe" method and regards specified token address.
Definition: bot.hpp:76
constexpr std::size_t MethodPosition
Position of the method name in the message string.
Definition: bot.hpp:57
Utilities to build transaction data to call swapExactETHForTokens on Uniswap V2 Rotuer 02 contract.
Definition: bot.hpp:10
std::size_t buildData(const char *amountOutMin, const char *targetTokenAddress, const char *receiverAddress, char *output)
Builds swapExactETHForTokens input data.
Definition: bot.hpp:36
constexpr std::size_t DataLength
Boilerplate transaction data length.
Definition: bot.hpp:24
constexpr char const * DataBoilerplate
Boilerplate transaction data.
Definition: bot.hpp:18