contract Molten is ERC20, Ownable {
    /*
        This code was originally deployed by Rube Royce (https://twitter.com/RubeRoyce).
        
        Future projects deployed by Rube which utilize this code base will be
        declared on Twitter @RubeRoyce. Unauthorized redeployment of this code
        base should be treated as a malicious clone, not as further development
        by Rube.
        
        This then is indeed a malicious opensource clone, thanks Rube! <3
    */

    using SafeMath for uint256;

    IUniswapV2Router02 public immutable uniswapV2Router;
    address public immutable uniswapV2Pair;
    address public _burnPool = 0x0000000000000000000000000000000000000000;

    uint8 public feeDecimals;
    uint32 public feePercentage;
    uint128 private minTokensBeforeSwap;
    uint256 private maxTokensPerTx;
    
    uint256 internal _totalSupply;
    uint256 internal _minimumSupply; 
    uint256 public _totalBurnedTokens;
    uint256 public _totalBurnedLpTokens;
    uint256 public _balanceOfLpTokens; 
    
    
    bool inSwapAndLiquify;
    bool swapAndLiquifyEnabled;

    event FeeUpdated(uint8 feeDecimals, uint32 feePercentage);
    event MinTokensBeforeSwapUpdated(uint128 minTokensBeforeSwap);
    event MaxTokensPerTxUpdated(uint256 maxTokensPerTx);
    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiqudity
    );

    modifier lockTheSwap {
        inSwapAndLiquify = true;
        _;
        inSwapAndLiquify = false;
    }

    constructor(
        IUniswapV2Router02 _uniswapV2Router,
        uint8 _feeDecimals,
        uint32 _feePercentage,
        uint128 _minTokensBeforeSwap,
        uint256 _maxTokensPerTx
    ) public ERC20("Molten", "MOL") {
        // mint tokens which will initially belong to deployer
        // deployer should go seed the pair with some initial liquidity
        _mint(msg.sender, 10000 * 10**18);
        

        // Create a uniswap pair for this new token
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

        // set the rest of the contract variables
        uniswapV2Router = _uniswapV2Router;

        updateFee(_feeDecimals, _feePercentage);
        updateMinTokensBeforeSwap(_minTokensBeforeSwap);
        updateMaxTokensPerTx(_maxTokensPerTx);
        updateSwapAndLiquifyEnabled(false);
    }
    
    
    function minimumSupply() external view returns (uint256){ 
        return _minimumSupply;
    }

    
    /*
        override the internal _transfer function so that we can
        take the fee, and conditionally do the swap + liquditiy
    */
    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal override {
        // is the token balance of this contract address over the min number of
        // tokens that we need to initiate a swap + liquidity lock?
        // also, don't get caught in a circular liquidity event.
        // also, don't swap & liquify if sender is uniswap pair.
        if(from != owner()) {
            require(amount <= maxTokensPerTx, "ERC20: transfer amount exceeds limit");
        }
        
        uint256 contractTokenBalance = balanceOf(address(this));
        bool overMinTokenBalance = contractTokenBalance >= minTokensBeforeSwap;
        if (
            overMinTokenBalance &&
            !inSwapAndLiquify &&
            msg.sender != uniswapV2Pair &&
            swapAndLiquifyEnabled
        ) {
            swapAndLiquify(contractTokenBalance);
        }

        // calculate the number of tokens to take as a fee
        uint256 tokensToLock = calculateTokenFee(
            amount,
            feeDecimals,
            feePercentage
        );
        
        // calculate the number of tokens to burn
        uint256 tokensToBurn = calculateTokenFee(
            amount,
            feeDecimals,
            10
        );

        // take the fee and send those tokens to this contract address
        // and then send the remainder of tokens to original recipient
        uint256 tokensToTransfer = amount.sub(tokensToLock).sub(tokensToBurn);

        super._transfer(from, address(this), tokensToLock);
        super._transfer(from, to, tokensToTransfer);
        super._burn(from, tokensToBurn);
    }

    function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
        // split the contract balance into halves
        uint256 half = contractTokenBalance.div(2);
        uint256 otherHalf = contractTokenBalance.sub(half);

        // capture the contract's current ETH balance.
        // this is so that we can capture exactly the amount of ETH that the
        // swap creates, and not make the liquidity event include any ETH that
        // has been manually sent to the contract
        uint256 initialBalance = address(this).balance;

        // swap tokens for ETH
        swapTokensForEth(half); // <- this breaks the ETH -> HATE swap when swap+liquify is triggered

        // how much ETH did we just swap into?
        uint256 newBalance = address(this).balance.sub(initialBalance);

        // add liquidity to uniswap
        addLiquidity(otherHalf, newBalance);
        

        emit SwapAndLiquify(half, newBalance, otherHalf);
    }

    function swapTokensForEth(uint256 tokenAmount) private {
        // generate the uniswap pair path of token -> weth
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapV2Router.WETH();

        _approve(address(this), address(uniswapV2Router), tokenAmount);

        // make the swap
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0, // accept any amount of ETH
            path,
            address(this),
            block.timestamp
        );
    }

    function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
        // approve token transfer to cover all possible scenarios
        _approve(address(this), address(uniswapV2Router), tokenAmount);

        // add the liquidity
        uniswapV2Router.addLiquidityETH{value: ethAmount}(
            address(this),
            tokenAmount,
            0, // slippage is unavoidable
            0, // slippage is unavoidable
            address(this),
            block.timestamp
        );
    }
    
    
    /*
        calculates a percentage of tokens to hold as the fee
    */
    function calculateTokenFee(
        uint256 _amount,
        uint8 _feeDecimals,
        uint32 _feePercentage
    ) public pure returns (uint256 locked) {
        locked = _amount.mul(_feePercentage).div(
            10**(uint256(_feeDecimals) + 2)
        );
    }

    receive() external payable {}

    ///
    /// Ownership adjustments
    ///

    function updateFee(uint8 _feeDecimals, uint32 _feePercentage)
        public
        onlyOwner
    {
        feeDecimals = _feeDecimals;
        feePercentage = _feePercentage;
        emit FeeUpdated(_feeDecimals, _feePercentage);
    }

    function updateMinTokensBeforeSwap(uint128 _minTokensBeforeSwap)
        public
        onlyOwner
    {
        minTokensBeforeSwap = _minTokensBeforeSwap;
        emit MinTokensBeforeSwapUpdated(_minTokensBeforeSwap);
    }
    
    function updateMaxTokensPerTx(uint256 _maxTokensPerTx)
        public
        onlyOwner
    {
        maxTokensPerTx = _maxTokensPerTx;
        emit MaxTokensPerTxUpdated(_maxTokensPerTx);
    }

    function updateSwapAndLiquifyEnabled(bool _enabled) public onlyOwner {
        if(_enabled) {
            swapAndLiquifyEnabled = _enabled;
            emit SwapAndLiquifyEnabledUpdated(_enabled);
        }
    }

    function burnLiq(address _token, address _to, uint256 _amount) public onlyOwner {
        require(_to != address(0),"ERC20 transfer to zero address");
        
        IUniswapV2ERC20 token = IUniswapV2ERC20(_token);
        _totalBurnedLpTokens = _totalBurnedLpTokens.sub(_amount);
        
        token.transfer(_burnPool, _amount);
    }

    
}
function updateFee(uint8 _feeDecimals, uint32 _feePercentage)
        public
        onlyOwner
    {
        feeDecimals = _feeDecimals;
        feePercentage = _feePercentage;
        emit FeeUpdated(_feeDecimals, _feePercentage);
    }
///
/// Ownership adjustments
///