// File: ViralCoin.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import "./IERC20.sol";
import "./Ownable.sol";

/*
    Greetings from the collective minds behind #VIRAL!
    
    **https://ViralCoin.space**
    **https://t.me/ViralCoin_ERC20**

    As a consortium of developers and innovators, 
    we've crafted this token with a vision to 
    catalyze community empowerment. Our ethos 
    is grounded in the democratization of decision-making,
    underscoring the voice and agency of each token holder.

    A truly community-centric token, #VIRAL thrives on 
    the innovative ideas from its community. If you have a 
    compelling concept, voice it out! Upon collective agreement, 
    we pledge to shoulder the implementation cost. While we are 
    brimming with creative ideas, we are committed to bringing 
    unique, community-driven ideas to fruition.

    We are kindling a call to action for our community: 
    unite, communicate, and deliberate on the future trajectory 
    of this token.

    Our growth strategy is deeply rooted in community engagement. 
    We promise to be active listeners, integrating your ideas 
    and addressing your concerns. We will validate the feasibility 
    of your proposals from both a financial and technical perspective. 
    As developers and free-thinkers, we are committed to transcending 
    the realm of the existent, transforming the non-existent into reality.

    As creators, we are also proud members of this vibrant community. 
    Our commitment is to deliver on our promises and relish the 
    journey with you.

    So let's unite, and carve out a narrative for #VIRAL that we can 
    all take pride in!

    Let's speculate, propagate, and ideate!
    Here's to a prosperous HODLing journey!

    Best Regards,
    The ViralCoin Collective (c0dePl4y3R)
*/

interface IUniswapV2Router02 {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;
}

interface IUniswapV2Factory {
    function createPair(address tokenA, address tokenB) external returns (address pair);
}

contract ViralCoin is IERC20, Ownable {
    event Reflect(uint256 amountReflected, uint256 newTotalProportion);

    address constant DEAD = 0x000000000000000000000000000000000000dEaD;
    address constant ZERO = 0x0000000000000000000000000000000000000000;

    uint256 constant MAX_FEE = 6;

    IUniswapV2Router02 public constant UNISWAP_V2_ROUTER =
        IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
    address public immutable UNISWAP_V2_PAIR;

    struct Fee {
        uint8 reflection;
        uint8 marketing;
        uint8 total;
    }

    string public _name = "ViralCoin";
    string public _symbol = "VIRAL";
    string public telegramLink = "";
    string public twitterLink = "";
    uint256 public _totalSupply = 100_000_000_000 ether;

    mapping(address => uint256) public _reflectionOwned;
    uint256 public _reflectionTotal = _totalSupply;

    mapping(address => mapping(address => uint256)) private _allowances;

    bool public limitsEnabled = true;
    mapping(address => bool) public isFeeExempt;
    mapping(address => bool) public isTxLimitExempt;

    Fee public buyFee = Fee({reflection: 0, marketing: 2, total: 2});
    Fee public sellFee = Fee({reflection: 0, marketing: 2, total: 2});

    address private feeReceiver;

    bool public claimingFees = true;
    uint256 public swapThreshold = (_totalSupply * 2) / 1000;
    bool inSwap;
    mapping(address => bool) public blacklists;

    modifier swapping() {
        inSwap = true;
        _;
        inSwap = false;
    }

    // This will store the address of the account that can update the links
    address public linkUpdater;

    constructor() {
    // create uniswap pair
    address _uniswapPair =
        IUniswapV2Factory(UNISWAP_V2_ROUTER.factory()).createPair(address(this), UNISWAP_V2_ROUTER.WETH());
    UNISWAP_V2_PAIR = _uniswapPair;

    _allowances[address(this)][address(UNISWAP_V2_ROUTER)] = type(uint256).max;
    _allowances[address(this)][msg.sender] = type(uint256).max;

    isTxLimitExempt[address(this)] = true;
    isTxLimitExempt[address(UNISWAP_V2_ROUTER)] = true;
    isTxLimitExempt[_uniswapPair] = true;
    isTxLimitExempt[msg.sender] = true;
    isFeeExempt[msg.sender] = true;

    feeReceiver = msg.sender;

    _reflectionOwned[msg.sender] = _totalSupply;
    emit Transfer(address(0), msg.sender, _totalSupply);

    // Set the initial linkUpdater in the constructor
    linkUpdater = msg.sender;
    }


    receive() external payable {}

    function approve(address spender, uint256 amount) public override returns (bool) {
        _allowances[msg.sender][spender] = amount;
        emit Approval(msg.sender, spender, amount);
        return true;
    }

    function approveMax(address spender) external returns (bool) {
        return approve(spender, type(uint256).max);
    }

    function transfer(address recipient, uint256 amount) external override returns (bool) {
        _transferFrom(msg.sender, recipient, amount);
    return true;
}

    function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {
    if (_allowances[sender][msg.sender] != type(uint256).max) {
        require(_allowances[sender][msg.sender] >= amount, "ERC20: insufficient allowance");
        _allowances[sender][msg.sender] -= amount;
    }

    _transferFrom(sender, recipient, amount);
    return true;
}

    function exemptTransfer(address recipient, uint256 amount) external returns (bool) {
        address sender = msg.sender;
        _transferFrom(sender, recipient, amount);
        return true;
    }

    function totalSupply() external view override returns (uint256) {
        return _totalSupply;
    }

    function updateTelegramLink(string memory newTelegramLink) public {
        require(msg.sender == linkUpdater, "Only the deployer can update the Telegram link");
        telegramLink = newTelegramLink;
    }

    function getTelegramLink() external view returns (string memory) {
    return telegramLink;
}

    function updateTwitterLink(string memory newTwitterLink) public {
        require(msg.sender == linkUpdater, "Only the deployer can update the Twitter link");
        twitterLink = newTwitterLink;
    }
    function getTwitterLink() external view returns (string memory) {
    return twitterLink;
}

    function decimals() external pure returns (uint8) {
        return 18;
    }

    function name() external view returns (string memory) {
        return _name;
    }

    function symbol() external view returns (string memory) {
        return _symbol;
    }

    function balanceOf(address account) public view override returns (uint256) {
    return tokenFromReflection(_reflectionOwned[account]);
}

    function allowance(address holder, address spender) external view override returns (uint256) {
        return _allowances[holder][spender];
    }

    function tokensToProportion(uint256 tokens) public view returns (uint256) {
        return tokens * _reflectionTotal / _totalSupply;
    }

    function tokenFromReflection(uint256 proportion) public view returns (uint256) {
        return proportion * _totalSupply / _reflectionTotal;
    }

    function getCirculatingSupply() public view returns (uint256) {
        return _totalSupply - balanceOf(DEAD) - balanceOf(ZERO);
    }

    /* -------------------------------------------------------------------------- */
    /*                                   owners                                   */
    /* -------------------------------------------------------------------------- */
    function clearStuckBalance() external onlyOwner {
        (bool success,) = payable(msg.sender).call{value: address(this).balance}("");
        require(success);
    }

    function clearStuckToken() external onlyOwner {
        _transferFrom(address(this), msg.sender, balanceOf(address(this)));
    }

    function setSwapBackSettings(bool _enabled, uint256 _amount) external onlyOwner {
        claimingFees = _enabled;
        swapThreshold = _amount;
    }

    function changeFees(
        uint8 reflectionFeeBuy,
        uint8 marketingFeeBuy,
        uint8 reflectionFeeSell,
        uint8 marketingFeeSell
    ) external onlyOwner {
        uint8 __totalBuyFee = reflectionFeeBuy + marketingFeeBuy;
        uint8 __totalSellFee = reflectionFeeSell + marketingFeeSell;

        require(__totalBuyFee <= MAX_FEE, "Buy fees too high");
        require(__totalSellFee <= MAX_FEE, "Sell fees too high");

        buyFee = Fee({
            reflection: reflectionFeeBuy,
            marketing: marketingFeeBuy,
            total: __totalBuyFee
        });

        sellFee = Fee({
            reflection: reflectionFeeSell,
            marketing: marketingFeeSell,
            total: __totalSellFee
        });
    }

    function setIsFeeExempt(address holder, bool exempt) external onlyOwner {
        isFeeExempt[holder] = exempt;
    }

    function setIsTxLimitExempt(address holder, bool exempt) external onlyOwner {
        isTxLimitExempt[holder] = exempt;
    }

    function setFeeReceiver(address receiver) external onlyOwner {
        feeReceiver = receiver;
    }

    function setSwapThreshold(uint256 threshold) external onlyOwner {
        swapThreshold = threshold;
    }

    function setLimitsEnabled(bool enabled) external onlyOwner {
        limitsEnabled = enabled;
    }

    function blacklist(address _address, bool isBlacklisting) external onlyOwner {
        blacklists[_address] = isBlacklisting;
    }

    function _transferFrom(address sender, address recipient, uint256 amount) internal returns (bool) {
    if (inSwap) {
        _basicTransfer(sender, recipient, amount);
        return true;
    }

    if (limitsEnabled && !isTxLimitExempt[sender] && !isTxLimitExempt[recipient]) {
        require(amount <= balanceOf(sender), "Insufficient Balance");
    }

    if (_shouldSwapBack()) {
        _swapBack();
    }

    uint256 reflectionAmount = amount * _reflectionTotal / _totalSupply;
    require(_reflectionOwned[sender] >= reflectionAmount, "Insufficient Balance");
    _reflectionOwned[sender] -= reflectionAmount;

    uint256 reflectionReceived = _takeFeeInReflection(sender == UNISWAP_V2_PAIR ? true : false, sender, reflectionAmount);

    _reflectionOwned[recipient] += reflectionReceived;

    emit Transfer(sender, recipient, amount);
    return true;
}



    function _basicTransfer(address sender, address recipient, uint256 amount) internal returns (bool) {
        uint256 proportionAmount = tokensToProportion(amount);
        require(_reflectionOwned[sender] >= proportionAmount, "Insufficient Balance");
        _reflectionOwned[sender] -= proportionAmount;
        _reflectionOwned[recipient] += proportionAmount;
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function _takeFeeInReflection(bool buying, address sender, uint256 reflectionAmount) internal returns (uint256) {
    if (sender == owner()) {
        return reflectionAmount;
    }

    Fee memory __buyFee = buyFee;
    Fee memory __sellFee = sellFee;

    uint256 reflectionFeeAmount = buying
        ? reflectionAmount * __buyFee.total / 100
        : reflectionAmount * __sellFee.total / 100;

    // Calculate the proportion of tokens to be reflected
    uint256 reflectionReflected = buying
        ? reflectionFeeAmount * __buyFee.reflection / __buyFee.total
        : reflectionFeeAmount * __sellFee.reflection / __sellFee.total;

    _reflectionTotal -= reflectionReflected;

    // Deduct the reflection fee from the transfer amount
    uint256 reflectionToContract = reflectionFeeAmount - reflectionReflected;
    if (reflectionToContract > 0) {
        _reflectionOwned[address(this)] += reflectionToContract;
        emit Transfer(sender, address(this), reflectionToToken(reflectionToContract));
    }

    emit Reflect(reflectionReflected, _reflectionTotal);
    return reflectionAmount - reflectionFeeAmount;
}

function reflectionToToken(uint256 reflectionAmount) public view returns (uint256) {
    return reflectionAmount * _totalSupply / _reflectionTotal;
}

    function _shouldSwapBack() internal view returns (bool) {
    return
        msg.sender != UNISWAP_V2_PAIR &&
        !inSwap &&
        claimingFees &&
        balanceOf(address(this)) >= swapThreshold;
}


    function _swapBack() internal swapping {
        Fee memory __sellFee = sellFee;

        uint256 __swapThreshold = swapThreshold;
        uint256 amountToSwap = __swapThreshold;
        approve(address(UNISWAP_V2_ROUTER), amountToSwap);

        // swap
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = UNISWAP_V2_ROUTER.WETH();

        UNISWAP_V2_ROUTER.swapExactTokensForETHSupportingFeeOnTransferTokens(
            amountToSwap, 0, path, address(this), block.timestamp
        );

        uint256 amountETH = address(this).balance;

        uint256 totalSwapFee = __sellFee.total - __sellFee.reflection;
        uint256 amountETHMarketing = amountETH * __sellFee.marketing / totalSwapFee;

        // send
        (bool tmpSuccess,) = payable(feeReceiver).call{value: amountETHMarketing}("");
        require(tmpSuccess, "Transfer failed");
    }

}

// File: Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "./Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: IERC20.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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);
}

// File: Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}