//Lego Inu ($LEGO)
/*
🔸♦️🔷LEGO INU🔷♦️🔸

Telegram: https://t.me/LegoInu1
Twitter: https://twitter.com/lego_inu
Reddit: https://www.reddit.com/r/LegoInu
Youtube: https://www.youtube.com/channel/UC4iw7KAtpNC-Zv5xfEzJP2g
Github: https://github.com/LegoInu/Lego
Website: https://legoinu.com

...................;LGGGGGGGGGGGGGGGGGGGGGGGG00Cf:..................
.................i@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8,................
.................8@@@0LLCCCCCCCCCCCCCCCCGGGGGGC@@@@f................
.................8@@@L,:;iiiiiiiiiiiiiii111111i8@@@f................
.................8@@@L,:;iiiiiiiiiiiiiii111111i8@@@f................
.................8@@@L,,;iiiiiiiiiiiiii;111111i8@@@f................
.........;C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8f,........
......:0@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@C......
.....0@@@@@Ci,:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ii1111i1L8@@@@@f....
...,@@@@@;::;iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii1111111C@@@@C...
...G@@@0,,:tLLCGCLLf1i;;iiiiiiiiiiiiiiiiiiiii;;itLLCGGCLft11t@@@@t..
.:t@@@8LC0@@@@@@@@@@@@@@@@@@GLf1i;;;i1fLG@@@@@@@@@@@@@@@@@0LC8@@@0..
Gf.:f8@@@@@@@8800G008@@@@@@@0LtfC8@0LfC0@@@@@80GCCCCCG08@@@@@@8t,.CL
0fG@@@@@8CLLtttttf,1ttttttL8@@@@@@@@@@@@0fttttttf,ttttttfLLG@@@@@fC0
L8@@@@@0LLftttttf;;11tttttttt@@@@@@@@@@ftttttttt;;11ttttttLLC@@@@@80
.0@@@@@LLftttttt;;;1iitttttttG@@@@@@@@8tttttttt;;;iiitttttfLL0@@@@8:
..t@@@8LLftttt1i1f:tf1;fttttt0@@@@@@@@@tttttt1i1t:tt1;ftttfLL0@@@8..
..;@@@@LLftttt.iff;ff1:;ttttt@@@@C;8@@@Ltttt1,1ff:ff1:;tttfLL0@@@8..
..;@@@@CLLtttf;ii;;i;1:fttttG@@@@iit@@@8ttttf:ii;:i;i:ftttLLC@@@@8..
..;@@@@@LLftttt1ii;;1ifttttC@@@@1iiiL@@@8ttttt1ii;;1ittttfLL@@@@@8..
..;@@@@@@CLLttttf:;;tttttt0@@@@tiiii;C@@@@Lttttf;;;tttttLLC@@@@@@8..
..;@@@@@@@@GLLfttttftttf8@@@@8iiiiiiiif@@@@@GtttttfttfLL0@@@@@@@@@..
..;@@@@G@@@@@@@88000@@@@@@@@fiiiiiiiiiiiG@@@@@@@@@@@@@@@@@@@0C@@@8..
..;@@@@;,t8@@@@@@@@@@@@@@01iiiiiiiiiiiiii;t0@@@@@@@@@@@@@8L11C@@@8..
..;@@@@;:,;i;itfLCCLfti;iiiiiiiiiiiiiiiiiiiii;;1fLLLft1111111C@@@@..
..;@@@@;:,;iiiiiiiiiiiiiii;iiiiiiiiiiiiiiiiiiiiiiiiiii1111111C@@@@..
..;@@@@;:,;iiiiiiiiiiiiit8081iiiiiiiiii;G00G;iiiiiiiii1111111C@@@8..
..;@@@@;:,;iiiiiiiiiiiii;C0008Cti;;i1L00000iiiiiiiiiii1111111C@@@8..
...@@@@f::;iiiiiiiiiiiiiiiiC80000000000001iiiiiiiiiiii11111118@@@C..
...t@@@@t::;iiiiiiiiiiiiiiiii;ifLGGCLt;iiiiiiiiiiiiii1111111G@@@@:..
....f@@@@8i,:;iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii1111111C@@@@@;...
.....:8@@@@@@80GGG000GGGGGGGGGGGGGGGGGGGGGGGGGG00G00000@@@@@@@G.....
.......,C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@f.......
...........,1fLCC@@@@00088888888888888888888888@@@0CCCLti...........
.................8@@@;:;1iiiiiiiiiiiiii1111111L@@@L.................
.................t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;.................
...................tG00000000000000000000000000C1...................


🔸♦️🔷LEGO INU🔷♦️🔸

ONLY THE BEST IS GOOD ENOUGH. LET'S BUILD THE BEST COMMUNITY OF ALL, BRICK BY BRICK.

⏰⏰Lanuch time: Thursday 15/07/2021 @ 20:00 GMT

💁🏻‍♂️LEGO INU is a reflective, NFT enriched and Lottery oriented ERC20 community-driven meme token with the vision of maximizing profit for LEGO holders employing many of its unique features.

🧮 LEGO.NOMICS:
2,500,000,000,000 Total LEGOs 
7,500,000,000 initial buy limit 
1 Min Cooldown 

💠 LEGO.TRUST (https://legoinu.com/):
Liquidity Locked on Unicrypt
Ownership Renounced after everything works as intended

💲 LEGO.FEES:
5% LEGO Redistribution among Holders
15% Tax as following:
       > 50% for Development
       > 40% for Marketing and Buybacks
       > 10% for LEGO.LOTTO to Reward Top Holders

🎟 LEGO.LOTTO (https://legoinu.com/):
This is the most exciting part of our ecosystem. LEGO architect has designed a lottery system to incentivize community to hold and flaunt their holdings. 10% of all collected fees will be distributed to 3 lucky winners on every lottery draw. LEGO.LOTTO (https://legoinu.com/) has the following criteria:
        1) You need to be among top 20 holders until the deadline of that lottery draw.
        2) The amount of LEGO with which you are a top 20 holder, should have been bought by the same address and not transferred from another address. You got no place in LEGO.LOTTO you filthy multiple-address-smart-contract buyer.
        3) If you were chosen as one of lucky winners, you must maintain the same amount of LEGO holdings until the next lottery draw. Otherwise, your spot will be given to the next person who has held until the next lottery draw (even though he was not among the 3 winners).
        4) The first lottery draw will happen at 5 hours post launch, following by daily lottery draw at the same time of the day.
        5) At the end of each deadline, a random number will be assigned to each of top 20 holders and before the draw, a google sheet containing all eligible entries will be shared in our telegram channel. 
        6) The LEGO.POT for every draw is shown on the front page of the website live.

🖼 LEGO.NFT:
This is the next area in which LEGO shines. With LEGO.NFT, you can mint your own LEGO themed NFT, every of which are unique and not mintable by anyone once deployed by LEGO stakers. All minted LEGO.NFTs will be tradable right away in NFT marketplaces such as OpenSea and Rarible. This has never done before and will be a game changer once live.
In order to be able to mint a LEGO.NFT, you should stake and burn a certain amount of LEGOs which will be decided before it is fully launched. Keep in mind that the burning requirement make LEGOs even more deflationary and valuable.

*/
//5% Deflationary yes
//Bot Protect yes

// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.4;

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }
}

interface IERC20 {
    function totalSupply() external view returns (uint256);

    function balanceOf(address account) external view returns (uint256);

    function transfer(address recipient, uint256 amount)
        external
        returns (bool);

    function allowance(address owner, address spender)
        external
        view
        returns (uint256);

    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );
}

library SafeMath {
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }

    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }

    function sub(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;
        return c;
    }

    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    function div(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        return c;
    }
}

contract Ownable is Context {
    address private _owner;
    address private _previousOwner;
    event OwnershipTransferred(
        address indexed previousOwner,
        address indexed newOwner
    );

    constructor() {
        address msgSender = _msgSender();
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    function owner() public view returns (address) {
        return _owner;
    }

    modifier onlyOwner() {
        require(_owner == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }
}

interface IUniswapV2Factory {
    function createPair(address tokenA, address tokenB)
        external
        returns (address pair);
}

interface IUniswapV2Router02 {
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;

    function factory() external pure returns (address);

    function WETH() external pure returns (address);

    function addLiquidityETH(
        address token,
        uint256 amountTokenDesired,
        uint256 amountTokenMin,
        uint256 amountETHMin,
        address to,
        uint256 deadline
    )
        external
        payable
        returns (
            uint256 amountToken,
            uint256 amountETH,
            uint256 liquidity
        );
}

contract LegoInu is Context, IERC20, Ownable {
    using SafeMath for uint256;

    string private constant _name = "Lego Inu";
    string private constant _symbol = "\xf0\x9f\x94\xb7LEGO\xf0\x9f\x94\xb6";
    uint8 private constant _decimals = 8;

    // RFI
    mapping(address => uint256) private _rOwned;
    mapping(address => uint256) private _tOwned;
    mapping(address => mapping(address => uint256)) private _allowances;
    mapping(address => bool) private _isExcludedFromFee;
    uint256 private constant MAX = ~uint256(0);
    uint256 private constant _tTotal = 2500000000000 * 10**8;
    uint256 private _rTotal = (MAX - (MAX % _tTotal));
    uint256 private _tFeeTotal;
    uint256 private _taxFee = 5;
    uint256 private _teamFee = 15;

    // Bot detection
    mapping(address => bool) private bots;
    mapping(address => uint256) private cooldown;
    address payable private _devWallet;
    address payable private _marketingBuyback;
    address payable private _lotteryWallet;
    IUniswapV2Router02 private uniswapV2Router;
    address private uniswapV2Pair;
    bool private tradingOpen;
    bool private inSwap = false;
    bool private swapEnabled = false;
    bool private cooldownEnabled = false;
    uint256 private _maxTxAmount = _tTotal;

    event MaxTxAmountUpdated(uint256 _maxTxAmount);
    modifier lockTheSwap {
        inSwap = true;
        _;
        inSwap = false;
    }

    constructor(address payable devWallet, address payable marketingBuyback, address payable lotteryWallet) {
        _devWallet = devWallet;
        _marketingBuyback = marketingBuyback;
        _lotteryWallet = lotteryWallet;
        _rOwned[_msgSender()] = _rTotal;
        _isExcludedFromFee[owner()] = true;
        _isExcludedFromFee[address(this)] = true;
        _isExcludedFromFee[_devWallet] = true;
        _isExcludedFromFee[_marketingBuyback] = true;
        _isExcludedFromFee[_lotteryWallet] = true;
        emit Transfer(address(0), _msgSender(), _tTotal);
    }

    function name() public pure returns (string memory) {
        return _name;
    }

    function symbol() public pure returns (string memory) {
        return _symbol;
    }

    function decimals() public pure returns (uint8) {
        return _decimals;
    }

    function totalSupply() public pure override returns (uint256) {
        return _tTotal;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return tokenFromReflection(_rOwned[account]);
    }

    function transfer(address recipient, uint256 amount)
        public
        override
        returns (bool)
    {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    function allowance(address owner, address spender)
        public
        view
        override
        returns (uint256)
    {
        return _allowances[owner][spender];
    }

    function approve(address spender, uint256 amount)
        public
        override
        returns (bool)
    {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(
            sender,
            _msgSender(),
            _allowances[sender][_msgSender()].sub(
                amount,
                "ERC20: transfer amount exceeds allowance"
            )
        );
        return true;
    }

    function setCooldownEnabled(bool onoff) external onlyOwner() {
        cooldownEnabled = onoff;
    }

    function tokenFromReflection(uint256 rAmount)
        private
        view
        returns (uint256)
    {
        require(
            rAmount <= _rTotal,
            "Amount must be less than total reflections"
        );
        uint256 currentRate = _getRate();
        return rAmount.div(currentRate);
    }

    function removeAllFee() private {
        if (_taxFee == 0 && _teamFee == 0) return;
        _taxFee = 0;
        _teamFee = 0;
    }

    function restoreAllFee() private {
        _taxFee = 5;
        _teamFee = 15;
    }

    function _approve(
        address owner,
        address spender,
        uint256 amount
    ) private {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");
        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    function _transfer(
        address from,
        address to,
        uint256 amount
    ) private {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");
        require(amount > 0, "Transfer amount must be greater than zero");

        if (from != owner() && to != owner()) {
            if (cooldownEnabled) {
                if (
                    from != address(this) &&
                    to != address(this) &&
                    from != address(uniswapV2Router) &&
                    to != address(uniswapV2Router)
                ) {
                    require(
                        _msgSender() == address(uniswapV2Router) ||
                            _msgSender() == uniswapV2Pair,
                        "ERR: Uniswap only"
                    );
                }
            }
            require(amount <= _maxTxAmount);
            require(!bots[from] && !bots[to]);
            if (
                from == uniswapV2Pair &&
                to != address(uniswapV2Router) &&
                !_isExcludedFromFee[to] &&
                cooldownEnabled
            ) {
                require(cooldown[to] < block.timestamp);
                cooldown[to] = block.timestamp + (60 seconds);
            }
            uint256 contractTokenBalance = balanceOf(address(this));
            if (!inSwap && from != uniswapV2Pair && swapEnabled) {
                swapTokensForEth(contractTokenBalance);
                uint256 contractETHBalance = address(this).balance;
                if (contractETHBalance > 0) {
                    sendETHToFee(address(this).balance);
                }
            }
        }
        bool takeFee = true;

        if (_isExcludedFromFee[from] || _isExcludedFromFee[to]) {
            takeFee = false;
        }

        _tokenTransfer(from, to, amount, takeFee);
    }

    function swapTokensForEth(uint256 tokenAmount) private lockTheSwap {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapV2Router.WETH();
        _approve(address(this), address(uniswapV2Router), tokenAmount);
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0,
            path,
            address(this),
            block.timestamp
        );
    }


    function sendETHToFee(uint256 amount) private {
        _devWallet.transfer(amount.mul(5).div(10));
        _marketingBuyback.transfer(amount.mul(4).div(10));
        _lotteryWallet.transfer(amount.mul(1).div(10));
    }

    function openTrading() external onlyOwner() {
        require(!tradingOpen, "trading is already open");
        IUniswapV2Router02 _uniswapV2Router =
            IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
        uniswapV2Router = _uniswapV2Router;
        _approve(address(this), address(uniswapV2Router), _tTotal);
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());
        uniswapV2Router.addLiquidityETH{value: address(this).balance}(
            address(this),
            balanceOf(address(this)),
            0,
            0,
            owner(),
            block.timestamp
        );
        swapEnabled = true;
        cooldownEnabled = true;
        _maxTxAmount = 7500000000 * 10**8;
        tradingOpen = true;
        IERC20(uniswapV2Pair).approve(
            address(uniswapV2Router),
            type(uint256).max
        );
    }

    function manualswap() external {
        require(_msgSender() == _devWallet);
        uint256 contractBalance = balanceOf(address(this));
        swapTokensForEth(contractBalance);
    }

    function manualsend() external {
        require(_msgSender() == _devWallet);
        uint256 contractETHBalance = address(this).balance;
        sendETHToFee(contractETHBalance);
    }

    function setBots(address[] memory bots_) public onlyOwner {
        for (uint256 i = 0; i < bots_.length; i++) {
            bots[bots_[i]] = true;
        }
    }

    function delBot(address notbot) public onlyOwner {
        bots[notbot] = false;
    }

    function _tokenTransfer(
        address sender,
        address recipient,
        uint256 amount,
        bool takeFee
    ) private {
        if (!takeFee) removeAllFee();
        _transferStandard(sender, recipient, amount);
        if (!takeFee) restoreAllFee();
    }

    function _transferStandard(
        address sender,
        address recipient,
        uint256 tAmount
    ) private {
        (
            uint256 rAmount,
            uint256 rTransferAmount,
            uint256 rFee,
            uint256 tTransferAmount,
            uint256 tFee,
            uint256 tTeam
        ) = _getValues(tAmount);
        _rOwned[sender] = _rOwned[sender].sub(rAmount);
        _rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
        _takeTeam(tTeam);
        _reflectFee(rFee, tFee);
        emit Transfer(sender, recipient, tTransferAmount);
    }

    function _takeTeam(uint256 tTeam) private {
        uint256 currentRate = _getRate();
        uint256 rTeam = tTeam.mul(currentRate);
        _rOwned[address(this)] = _rOwned[address(this)].add(rTeam);
    }

    function _reflectFee(uint256 rFee, uint256 tFee) private {
        _rTotal = _rTotal.sub(rFee);
        _tFeeTotal = _tFeeTotal.add(tFee);
    }

    receive() external payable {}

    function _getValues(uint256 tAmount)
        private
        view
        returns (
            uint256,
            uint256,
            uint256,
            uint256,
            uint256,
            uint256
        )
    {
        (uint256 tTransferAmount, uint256 tFee, uint256 tTeam) =
            _getTValues(tAmount, _taxFee, 15);
        uint256 currentRate = _getRate();
        (uint256 rAmount, uint256 rTransferAmount, uint256 rFee) =
            _getRValues(tAmount, tFee, tTeam, currentRate);
        return (rAmount, rTransferAmount, rFee, tTransferAmount, tFee, tTeam);
    }

    function _getTValues(
        uint256 tAmount,
        uint256 taxFee,
        uint256 TeamFee
    )
        private
        pure
        returns (
            uint256,
            uint256,
            uint256
        )
    {
        uint256 tFee = tAmount.mul(taxFee).div(100);
        uint256 tTeam = tAmount.mul(TeamFee).div(100);
        uint256 tTransferAmount = tAmount.sub(tFee).sub(tTeam);
        return (tTransferAmount, tFee, tTeam);
    }

    function _getRValues(
        uint256 tAmount,
        uint256 tFee,
        uint256 tTeam,
        uint256 currentRate
    )
        private
        pure
        returns (
            uint256,
            uint256,
            uint256
        )
    {
        uint256 rAmount = tAmount.mul(currentRate);
        uint256 rFee = tFee.mul(currentRate);
        uint256 rTeam = tTeam.mul(currentRate);
        uint256 rTransferAmount = rAmount.sub(rFee).sub(rTeam);
        return (rAmount, rTransferAmount, rFee);
    }

    function _getRate() private view returns (uint256) {
        (uint256 rSupply, uint256 tSupply) = _getCurrentSupply();
        return rSupply.div(tSupply);
    }

    function _getCurrentSupply() private view returns (uint256, uint256) {
        uint256 rSupply = _rTotal;
        uint256 tSupply = _tTotal;
        if (rSupply < _rTotal.div(_tTotal)) return (_rTotal, _tTotal);
        return (rSupply, tSupply);
    }

    function setMaxTxPercent(uint256 maxTxPercent) external onlyOwner() {
        require(maxTxPercent > 0, "Amount must be greater than 0");
        _maxTxAmount = _tTotal.mul(maxTxPercent).div(10**2);
        emit MaxTxAmountUpdated(_maxTxAmount);
    }
}