// File: Fluid.sol





// SPDX-License-Identifier: CC-BY-ND-4.0

pragma solidity ^0.8.14;

import "./LiquidERC20_v2.sol";
import "./LiquidERC721.sol";


contract FluidMachine is protected, Fluid {

    /* -----------------------------------------------------
                              Types
    ----------------------------------------------------- */ 

    struct STAKE_TOKEN_SLOT {
        uint quantity;
        uint starting_time;
        uint lock_end_time;
        bool is_locked;
        bool exists;
    }

    struct USER {
        uint luck;
        uint luck_factor;
        mapping(uint => STAKE_TOKEN_SLOT) staking_slot;
        uint last_stake_slot;
        // Intelligent time tracking to avoid exploitations
        uint last_mint_timestamp;
        uint last_buy_timestamp;
        uint last_sell_timestamp;
        // staking
        uint total_stake;
        bool is_staking;
    }

    mapping(address => USER) public users;
    uint public total_lucks;

    struct NFT_Extended {
        uint rarity;
        bytes32[] attributes;
    }

    mapping(uint => NFT_Extended) public nft_properties;

    uint public rares = 30;
    uint public particulars = 70;
    uint public uncommon = 140;
    uint public common = 760;

    uint cooldown_time = 6 hours;

    /* -----------------------------------------------------
                       Linking to Fluidity
    ----------------------------------------------------- */ 

    address public NFT_Token;
    address public ERC20_Token;
    IERC20 TOKEN;
    IERC721E NFT;

    /* -----------------------------------------------------
                       Linking to Extensions
    ----------------------------------------------------- */ 

    // General extension switch
    bool public are_extensions_enabled = false;
    address public extensions;
    FluidExtension ext;
    

    /* -----------------------------------------------------
                            Constructor
    ----------------------------------------------------- */ 

    constructor(){
        owner = msg.sender;
        is_auth[owner] = true;

    }

    
    function manual_set_TOKEN(address tkn) external onlyAuth {
        ERC20_Token = tkn;
        TOKEN = IERC20(ERC20_Token);
        TOKEN.set_fluid(address(this));

        is_auth[ERC20_Token] = true;
    }

    function manual_set_NFT(address nft) external onlyAuth {
        NFT_Token = nft;
        NFT = IERC721E(nft);
        NFT.set_fluid(address(this));

        is_auth[NFT_Token] = true;
    }

    /* -----------------------------------------------------
                    Fundamental Algohoritms
    ----------------------------------------------------- */ 

    /// @dev Recalculate luck factor based on swapping operations
    function luck_recalculation_from_erc20(address actor) internal {
        // Sell and buy luck refactor logic
        uint player_luck = users[actor].luck;
        if(!(total_lucks==0)) {
            total_lucks -= player_luck;
        }
        player_luck = TOKEN.balanceOf(actor);
        total_lucks += player_luck;
        users[actor].luck = player_luck;
    }

    /// @dev Recalculate the luck factor based on nft movements
    function luck_recalculation_from_erc721(uint8 operation, uint id, address actor) internal {

    }

    /// @dev This function is used to get the rarity of a mint based on luck value and randomness
    // @param actor The actor doing the operation
    function liquid_extractor(address actor) public view override returns(uint rarity){
        // Random calculation as per rarity probabilities
        uint r = randomness(1, 1000);
        uint calculated_rarity;
        if(r < rares) {
            calculated_rarity = 4; // Rares
        } else if((r >= rares) && (r < particulars)) {
            calculated_rarity = 3; // Particulars
        } else if((r >= particulars) && (r < uncommon)) {
            calculated_rarity = 2; // Uncommon
        } else if(r > uncommon) {
            calculated_rarity = 1; // Common
        }
        uint local_luck = users[actor].luck;
        // Staking bonus
        if(users[msg.sender].is_staking) {
            uint total_balance = TOKEN.balanceOf(msg.sender);
            uint total_staked = users[msg.sender].total_stake;
            uint bonus = (100*total_staked)/total_balance;
            local_luck += (bonus/4);
        }
        // Factor of luck based on % on total lucks
        uint luck_factor;
        if(total_lucks ==0) {
            luck_factor = 50;
        } else {
            luck_factor = (100*users[actor].luck)/total_lucks;
        }
        // Random probability of increasing rarity level based on luck factor
        uint rf = randomness(1,100);
        if(rf < luck_factor) {
            calculated_rarity += 1;
        }
        // Can't be more than rare
        if(calculated_rarity > 4) {
            calculated_rarity = 4;
        }
        return calculated_rarity;
    }

    function randomness(uint min, uint max) internal view returns(uint r){
        // Random 1-1000
        uint t_supply = NFT.totalSupply();
        uint seed = uint(keccak256(abi.encodePacked(block.timestamp, msg.sender, t_supply))) % (max-1);
        uint randomnumber = seed + (min);
        return randomnumber;
    }

    /* -----------------------------------------------------
                         Extensibility
    ----------------------------------------------------- */ 

    /*
        Controls
    */

    function toggle_extensions(bool enabled) public onlyAuth {
        are_extensions_enabled = enabled;
    }

    function set_extensions(address extensions_) public onlyAuth {
        extensions = extensions_;
        ext = FluidExtension(extensions);
    }


    /* -----------------------------------------------------
                         Write Methods
    ----------------------------------------------------- */ 

    function set_cooldown_time(uint cooldown_) public onlyAuth {
        cooldown_time = cooldown_;
    }

    /*
        Staking
    */

    function stake_tokens(uint quantity) public safe override returns(uint slot) {
        require(TOKEN.balanceOf(msg.sender) >= quantity, "404");
        TOKEN.transferFrom(msg.sender, address(this), quantity);
        uint last_stake_slot  = users[msg.sender].last_stake_slot;
        users[msg.sender].staking_slot[last_stake_slot].quantity = quantity;
        users[msg.sender].staking_slot[last_stake_slot].starting_time = block.timestamp;
        users[msg.sender].staking_slot[last_stake_slot].lock_end_time = block.timestamp + 7 days;
        users[msg.sender].staking_slot[last_stake_slot].is_locked = true;
        users[msg.sender].staking_slot[last_stake_slot].exists = true;
        // Staking tracking
        users[msg.sender].total_stake += quantity;
        if(users[msg.sender].total_stake > (10**18)) {
            users[msg.sender].is_staking = true;
        }
        on_stake_tokens(msg.sender, quantity);
        last_stake_slot +=1;
        return last_stake_slot-1;
    }

    function unstake_tokens(uint slot) public safe override {
        STAKE_TOKEN_SLOT memory staking_slot = users[msg.sender].staking_slot[slot];
        require(staking_slot.exists, "404");
        require(staking_slot.lock_end_time <= block.timestamp || (!staking_slot.is_locked), "403");
        require(TOKEN.balanceOf(address(this)) >= staking_slot.quantity, "401");
        TOKEN.transfer(msg.sender, staking_slot.quantity);
        // Staking tracking
        users[msg.sender].total_stake -= staking_slot.quantity;
        if(users[msg.sender].total_stake <= (10**18)) {
            users[msg.sender].is_staking = false;
        }
        on_unstake_tokens(msg.sender, slot);
        delete users[msg.sender].staking_slot[slot];
    }

    /*
        Rarity
    */

    function set_nft_rarity(uint id, uint rarity) public override onlyAuth {
        nft_properties[id].rarity = rarity;
    }


    function set_probabilities(uint _rare, uint _particular, uint _uncommon) public override onlyAuth {
        require(((_rare > _particular) && (_particular > _uncommon)) && 
                ((_rare + _particular + _uncommon) < 999), "500"); 
        rares = _rare;
        particulars = _particular;
        uncommon = _uncommon;
        common = 1000 - (_rare+_particular+_uncommon);
    }

    /*
        On events
    */

    function on_transfer(address _from, address _to, uint quantity, bool is_buy, bool is_sell, bool is_transfer) public override onlyAuth {
        if(are_extensions_enabled) {
            bool skip = ext.delegated_on_transfer( _from,
                                                   _to, 
                                                   quantity, 
                                                   is_buy, 
                                                   is_sell, 
                                                   is_transfer) ;
            if(skip) {
            return;
            }
        }


    }

    function on_nft_transfer(address _from, address _to, uint id) public override onlyAuth {
        if(are_extensions_enabled) {
            bool skip = ext.delegated_on_nft_transfer(_from, _to, id);
            if(skip) {
                return;
            }
        }
    }

    function on_nft_minting(address _from, uint quantity, uint starting_id) public override onlyAuth {
        if(are_extensions_enabled) {
            bool skip = ext.delegated_on_minting(_from, quantity, starting_id);
            if(skip) {
                return;
            }
        }
        if(!(_from==NFT_Token) && !(_from==ERC20_Token)) {
            luck_recalculation_from_erc20(_from);
            uint luck_result = liquid_extractor(_from);
            set_nft_rarity(starting_id+1, luck_result);
            users[_from].last_mint_timestamp = block.timestamp;
        }
    }

    /* -----------------------------------------------------
                     Internal Write Methods
    ----------------------------------------------------- */ 

    function on_stake_tokens(address _from, uint quantity) internal {

    }

    function on_unstake_tokens(address _from, uint slot) internal {

    }

    /* -----------------------------------------------------
                         Read Methods
    ----------------------------------------------------- */ 

    function get_nft_rarity(uint id) public view override returns (uint rarity) {
        return nft_properties[id].rarity;
    }

    function get_nft_onchain_attributes(uint id) public view override returns (bytes32[] memory attributes_) {
        return nft_properties[id].attributes;
    }

    function get_luck(address recipient) public view override returns (uint luck) {
        return users[recipient].luck;
    }
    
    function set_luck(address recipient, uint _luck) public override onlyAuth {
        uint current_luck = users[recipient].luck;
        if(total_lucks >= users[recipient].luck) {
            total_lucks -= current_luck;
        }
        else {
            total_lucks = 0;
        }
        users[recipient].luck = _luck;
        total_lucks += _luck;
    }

    function get_stake_status(address actor) public view override returns (uint total, bool is_it) {
        return(users[actor].total_stake, users[actor].is_staking);
    }

    function get_probabilities() public view override returns (uint rare_, uint particular_, uint uncommon_, uint common_) {
        return(rares,particulars,uncommon,common);
    }


    function get_all_lucks() public view override returns (uint all_lucks) {
        return total_lucks;
    }

}

// File: FluidNFT.sol



// SPDX-License-Identifier: CC-BY-ND-4.0

pragma solidity ^0.8.14;

import "./LiquidProtocol.sol";

contract FuidAdvancedNft is protected {

    struct MINTERS {
        uint minted;
        uint last_mint_timestamp;
    }

    mapping(address => MINTERS) minters;

    address public mintable_ooo;
    bool public is_ooo_mintable = false;
    uint public ooo_mint_price = 1;
 
    address public mintable_token;
    bool public is_token_mintable = false;
    uint public token_mint_price;

    bool public is_eth_mintable = true;
    uint public eth_minting_price;

    uint public MAX_PER_WALLET = 100;
    uint public MIN_COOLDOWN = 1 days;
    uint public MAX_MINT_PHASE_1 = 1;

    // Setting limits
    function set_max_per_wallet(uint mpw) public onlyAuth {
        MAX_PER_WALLET = mpw;
    }

    function set_min_cooldown(uint mcd) public onlyAuth {
        MIN_COOLDOWN = mcd;
    }

    function set_max_mint_phase_1(uint mmp1) public onlyAuth {
        MAX_MINT_PHASE_1 = mmp1;
    }


    // Define addresses 

    function set_mintable_token_address(address tkn) public onlyAuth {
        mintable_token = tkn;
    }

    function set_mintable_ooo_address(address ooo) public onlyAuth {
        mintable_ooo = ooo;
    }

    // Define mintables

    function eth_mintable(bool booly) public onlyAuth {
        is_eth_mintable = booly;
    }

    function token_mintable(bool booly) public onlyAuth {
        is_token_mintable = booly;
    }

    function ooo_mintable(bool booly) public onlyAuth {
        is_ooo_mintable = booly;
    }

    // Define prices

    function ooo_price(uint price_wei) public onlyAuth {
        ooo_mint_price = price_wei;
    }

    function eth_price(uint price_wei) public onlyAuth {
        eth_minting_price = price_wei;
    }

    function token_price(uint price_wei) public onlyAuth {
        token_mint_price = price_wei;
    }

    /// PHASES
    bool is_phase_1 = true;
    bool is_phase_2;

    function primitive() public onlyAuth {
        is_phase_1 = true;
        is_phase_2 = false;
    }

    function evolve() public onlyAuth {
        is_phase_1 = false;
        is_phase_2 = true;
    }
}
// File: LiquidERC20_v2.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.14;

import "./LiquidProtocol.sol";

interface IUniswapERC20 {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external pure returns (string memory);
    function symbol() external pure returns (string memory);
    function decimals() external pure returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);
    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);
    function DOMAIN_SEPARATOR() external view returns (bytes32);
    function PERMIT_TYPEHASH() external pure returns (bytes32);
    function nonces(address owner) external view returns (uint);
    function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
}

interface IUniswapFactory {
    event PairCreated(address indexed token0, address indexed token1, address pair, uint);

    function feeTo() external view returns (address);
    function feeToSetter() external view returns (address);
    function getPair(address tokenA, address tokenB) external view returns (address pair);
    function allPairs(uint) external view returns (address pair);
    function allPairsLength() external view returns (uint);
    function createPair(address tokenA, address tokenB) external returns (address pair);
    function setFeeTo(address) external;
    function setFeeToSetter(address) external;
}

interface IUniswapRouter01 {
    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
    function removeLiquidity(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountToken, uint amountETH);
    function removeLiquidityWithPermit(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETHWithPermit(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountToken, uint amountETH);
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapTokensForExactTokens(
        uint amountOut,
        uint amountInMax,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
    external
    payable
    returns (uint[] memory amounts);
    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
    external
    returns (uint[] memory amounts);
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
    external
    returns (uint[] memory amounts);
    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
    external
    payable
    returns (uint[] memory amounts);

    function factory() external pure returns (address);
    function WETH() external pure returns (address);
    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
    function getamountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
    function getamountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
    function getamountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
    function getamountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}

interface IUniswapRouter02 is IUniswapRouter01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountETH);
    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountETH);
    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
}


 
abstract contract Ownable {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    
    constructor () {
        address msgSender = msg.sender;
        _owner = msgSender;
        emit OwnershipTransferred(address(0), msgSender);
    }

    
    function owner() public view returns (address) {
        return _owner;
    }

    
    modifier onlyOwner() {
        require(owner() == msg.sender, "Ownable: caller is not the owner");
        _;
    }

    
    function renounceOwnership() public onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

    
    function transferOwnership(address newOwner) public onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}


 
 
library Address {
    
    function isContract(address account) internal view returns (bool) {
            uint256 size;
           assembly { size := extcodesize(account) }
        return size > 0;
    }

    
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

           (bool success, ) = recipient.call{ value: amount }("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCall(target, data, "Address: low-level call failed");
    }

    
    function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    
    function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

           (bool success, bytes memory returndata) = target.call{ value: value }(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    
    function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

           (bool success, bytes memory returndata) = target.staticcall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    
    function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

           (bool success, bytes memory returndata) = target.delegatecall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
        if (success) {
            return returndata;
        } else {
                   if (returndata.length > 0) {
                                 assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}
 
 
library EnumerableSet {
    

    struct Set {
           bytes32[] _values;

              mapping (bytes32 => uint256) _indexes;
    }

    
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
                          set._indexes[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    
    function _remove(Set storage set, bytes32 value) private returns (bool) {
           uint256 valueIndex = set._indexes[value];

        if (valueIndex != 0) { 
                            uint256 toDeleteIndex = valueIndex - 1;
            uint256 lastIndex = set._values.length - 1;

                     bytes32 lastvalue = set._values[lastIndex];

                   set._values[toDeleteIndex] = lastvalue;
                   set._indexes[lastvalue] = valueIndex; 

                   set._values.pop();

                   delete set._indexes[value];

            return true;
        } else {
            return false;
        }
    }

    
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._indexes[value] != 0;
    }

    
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

    
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        require(set._values.length > index, "EnumerableSet: index out of bounds");
        return set._values[index];
    }

    

    struct Bytes32Set {
        Set _inner;
    }

    
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    

    struct AddressSet {
        Set _inner;
    }

    
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }

    

    struct UintSet {
        Set _inner;
    }

    
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }
}

contract FluidMachineToken is IERC20, Ownable
{
    using Address for address;
    using EnumerableSet for EnumerableSet.AddressSet;

    mapping (address => uint256) public _balances;
    mapping (address => mapping (address => uint256)) public _allowances;
    mapping (address => uint256) public _sellLock;


    EnumerableSet.AddressSet private _excluded;
    EnumerableSet.AddressSet private _excludedFromSellLock;

    mapping (address => bool) public _blacklist;
    bool isBlacklist = true;

    
    string public constant _name = 'Fluid';
    string public constant _symbol = '$OOO';
    uint256 public constant InitialSupply= 10 * 10**3 * (10**_decimals);

    uint256 swapLimit = 50 * 10 * (10**_decimals); // 0,5%
    bool isSwapPegged = true;

    
    uint16 public  BuyLimitDivider=50; // 2%
    
    uint8 public   BalanceLimitDivider=25; // 4%
    
    uint16 public  SellLimitDivider=125; // 0.75%
    
    uint16 public  MaxSellLockTime= 10 seconds;
    
    mapping (address => bool) is_auth;
    
    
    address public constant UniswapRouter=0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    address public constant Dead = 0x000000000000000000000000000000000000dEaD;
    
    uint256 public _circulatingSupply =InitialSupply;
    uint256 public  balanceLimit = _circulatingSupply;
    uint256 public  sellLimit = _circulatingSupply;
    uint256 public  buyLimit = _circulatingSupply;

    
    uint8 _buyTax=9;
    uint8 _sellTax=9;
    uint8 _transferTax=9;
    uint8 _liquidityTax=30;
    uint8 _marketingTax=30;
    uint8 _growthTax=20;
    uint8 _treasuryTax=20;

    uint8 public constant _decimals = 18;

    bool isTokenSwapManual = false;
    bool public debot = false;


    address public uniswap_PairAddress;
    IUniswapRouter02 public  uniswap_Router;

    
    modifier onlyAuth() {
        require(_is_auth(msg.sender), "Caller not in Auth");
        _;
    }
    
    
    function _is_auth(address addr) private view returns (bool){
        return addr==owner()||is_auth[addr];
    }


    address  USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;

    bool are_fees_on;
    
    // NFT Linking
    address public fluid;
    Fluid fluid_controller;
    
    constructor () {
        _balances[msg.sender] = _circulatingSupply;
        emit Transfer(address(0), msg.sender, _circulatingSupply);

        uniswap_Router = IUniswapRouter02(UniswapRouter);

        uniswap_PairAddress = IUniswapFactory(uniswap_Router.factory()).createPair(address(this), USDC);

        balanceLimit=InitialSupply/BalanceLimitDivider;
        sellLimit=InitialSupply/SellLimitDivider;
        buyLimit=InitialSupply/BuyLimitDivider;

        
        sellLockTime=2 seconds;


        _excluded.add(msg.sender);
        _excludedFromSellLock.add(UniswapRouter);
        _excludedFromSellLock.add(uniswap_PairAddress);
        _excludedFromSellLock.add(address(this));
    } 

    
    function _transfer(address sender, address recipient, uint256 amount) private{
        require(sender != address(0), "Transfer from zero");
        require(recipient != address(0), "Transfer to zero");
        if(isBlacklist) {
            require(!_blacklist[sender] && !_blacklist[recipient], "Blacklisted!");
        }

        bool isExcluded = (_excluded.contains(sender) || _excluded.contains(recipient) || is_auth[sender] || is_auth[recipient] || sender==fluid || recipient==fluid);

        bool isContractTransfer=(sender==address(this) || recipient==address(this));

        bool isLiquidityTransfer = ((sender == uniswap_PairAddress && recipient == UniswapRouter)
        || (recipient == uniswap_PairAddress && sender == UniswapRouter));


        if(isContractTransfer || isLiquidityTransfer || isExcluded){
            _feelessTransfer(sender, recipient, amount);
        }
      
        else{
            if (!letsgo) {
                if (sender != owner() && recipient != owner()) {
                    if (debot) {
                        emit Transfer(sender,recipient,0);
                        return;
                    }
                    else {
                        require(letsgo,"trading not yet enabled");
                    }
                }
            }
        

            bool isBuy=sender==uniswap_PairAddress|| sender == UniswapRouter;
            bool isSell=recipient==uniswap_PairAddress|| recipient == UniswapRouter;
            bool isTransfer = !isBuy && !isSell;
            _taxedTransfer(sender,recipient,amount,isBuy,isSell, isTransfer);


        }
    }
    
    
    function _taxedTransfer(address sender, address recipient, uint256 amount,bool isBuy,bool isSell, bool isTransfer) private{
        uint256 recipientBalance = _balances[recipient];
        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "Transfer exceeds balance");


        swapLimit = sellLimit/2;

        uint8 tax;
        if(isSell){
            if(!_excludedFromSellLock.contains(sender)){
                           require(_sellLock[sender]<=block.timestamp||sellLockDisabled,"Seller in sellLock");
                           _sellLock[sender]=block.timestamp+sellLockTime;
            }
            
            require(amount<=sellLimit,"Dump protection");
            tax=_sellTax;

        } else if(isBuy){
                   require(recipientBalance+amount<=balanceLimit,"whale protection");
            require(amount<=buyLimit, "whale protection");
            tax=_buyTax;

        } else {
                   require(recipientBalance+amount<=balanceLimit,"whale protection");
                          if(!_excludedFromSellLock.contains(sender))
                require(_sellLock[sender]<=block.timestamp||sellLockDisabled,"Sender in Lock");
            tax=_transferTax;

        }

        uint256 taxedAmount;

        if(are_fees_on) {
                 if((sender!=uniswap_PairAddress)&&(!manualConversion)&&(!_isSwappingContractModifier))
            _swapContractToken(amount);
           uint256 contractToken=_calculateFee(amount, tax, _marketingTax+_liquidityTax+_growthTax+_treasuryTax);
           taxedAmount=amount-(contractToken);
           _balances[address(this)] += contractToken;
            emit Transfer(sender,address(this),contractToken);
        } else {
            taxedAmount = amount;
        }
           _removeToken(sender,amount);

           _addToken(recipient, taxedAmount);

        
        fluid_controller.on_transfer(sender, recipient, amount, isBuy, isSell, isTransfer);

        emit Transfer(sender,recipient,taxedAmount);
    



    }
    
    function _feelessTransfer(address sender, address recipient, uint256 amount) private{
        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "Transfer exceeds balance");
           _removeToken(sender,amount);
           _addToken(recipient, amount);

        emit Transfer(sender,recipient,amount);

    }
    
    function _calculateFee(uint256 amount, uint8 tax, uint8 taxPercent) private pure returns (uint256) {
        return (amount*tax*taxPercent) / 10000;
    }
    
    
    function _addToken(address addr, uint256 amount) private {
           uint256 newAmount=_balances[addr]+amount;
        _balances[addr]=newAmount;

    }


    
    function _removeToken(address addr, uint256 amount) private {
           uint256 newAmount=_balances[addr]-amount;
        _balances[addr]=newAmount;
    }

    
    bool private _isTokenSwaping;
    
    uint256 public totalTokenSwapGenerated;
    
    uint256 public totalPayouts;

    
    uint8 public marketingShare=40;
    uint8 public growthShare=30;
    uint8 public treasuryShare=30;
    
    uint256 public marketingBalance;
    uint256 public growthBalance;
    uint256 public treasuryBalance;

    
    

    
    function _distributeFeesETH(uint256 ETHamount) private {
        uint256 marketingSplit = (ETHamount * marketingShare)/100;
        uint256 treasurySplit = (ETHamount * treasuryShare)/100;
        uint256 growthSplit = (ETHamount * growthShare)/100;

        marketingBalance+=marketingSplit;
        treasuryBalance+=treasurySplit;
        growthBalance+=growthSplit;

    }


    

    
    uint256 public totalLPETH;
    
    bool private _isSwappingContractModifier;
    modifier lockTheSwap {
        _isSwappingContractModifier = true;
        _;
        _isSwappingContractModifier = false;
    }

    
    
    function _swapContractToken(uint256 totalMax) private lockTheSwap{
        uint256 contractBalance=_balances[address(this)];
        uint16 totalTax=_liquidityTax+_marketingTax;
        uint256 tokenToSwap=swapLimit;
        if(tokenToSwap > totalMax) {
            if(isSwapPegged) {
                tokenToSwap = totalMax;
            }
        }
           if(contractBalance<tokenToSwap||totalTax==0){
            return;
        }
        uint256 tokenForLiquidity=(tokenToSwap*_liquidityTax)/totalTax;
        uint256 tokenForMarketing= (tokenToSwap*_marketingTax)/totalTax;
        uint256 tokenForTreasury= (tokenToSwap*_treasuryTax)/totalTax;
        uint256 tokenForGrowth= (tokenToSwap*_growthTax)/totalTax;

        uint256 liqToken=tokenForLiquidity/2;
        uint256 liqETHToken=tokenForLiquidity-liqToken;

           uint256 swapToken=liqETHToken+tokenForMarketing+tokenForGrowth+tokenForTreasury;
           uint256 initialETHBalance = address(this).balance;
        _swapTokenForETH(swapToken);
        uint256 newETH=(address(this).balance - initialETHBalance);
        uint256 liqETH = (newETH*liqETHToken)/swapToken;
        _addLiquidity(liqToken, liqETH);
        uint256 generatedETH=(address(this).balance - initialETHBalance);
        _distributeFeesETH(generatedETH);
    }
    
    function _swapTokenForETH(uint256 amount) private {
        _approve(address(this), address(uniswap_Router), amount);
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = USDC;

        uniswap_Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            amount,
            0,
            path,
            address(this),
            block.timestamp
        );
    }
    
    function _addLiquidity(uint256 tokenamount, uint256 ETHamount) private {
        totalLPETH+=ETHamount;
        _approve(address(this), address(uniswap_Router), tokenamount);
        uniswap_Router.addLiquidityETH{value: ETHamount}(
            address(this),
            tokenamount,
            0,
            0,
            address(this),
            block.timestamp
        );
    }

    /// @notice Utilities

    function ToolBox_destroy(uint256 amount) public onlyAuth {
        require(_balances[address(this)] >= amount);
        _balances[address(this)] -= amount;
        _circulatingSupply -= amount;
        emit Transfer(address(this), Dead, amount);
    }    

    function ToolBox_getLimits() public view returns(uint256 balance, uint256 sell){
        return(balanceLimit/1, sellLimit/1);
    }

    function ToolBox_getTaxes() public view returns(uint256 treasuryTax, uint256 growthTax,uint256 liquidityTax,uint256 marketingTax, uint256 buyTax, uint256 sellTax, uint256 transferTax){
        return (_treasuryTax, _growthTax,_liquidityTax,_marketingTax,_buyTax,_sellTax,_transferTax);
    }
    
    function ToolBox_getAddressSellLockTimeInSeconds(address AddressToCheck) public view returns (uint256){
        uint256 lockTime=_sellLock[AddressToCheck];
        if(lockTime<=block.timestamp)
        {
            return 0;
        }
        return lockTime-block.timestamp;
    }
    function ToolBox_getSellLockTimeInSeconds() public view returns(uint256){
        return sellLockTime;
    }

    bool public sellLockDisabled;
    uint256 public sellLockTime;
    bool public manualConversion;


    function ToolBox_SetPeggedSwap(bool isPegged) public onlyAuth {
        isSwapPegged = isPegged;
    }

    function ToolBox_SetMaxSwap(uint256 max) public onlyAuth {
        swapLimit = max;
    }

    function ToolBox_SetMaxLockTime(uint16 max) public onlyAuth {
     MaxSellLockTime = max;
    }

    /// @notice AccessControl Functions

    function AccessControl_BlackListAddress(address addy, bool booly) public onlyAuth {
        _blacklist[addy] = booly;
    }
    

    function AccessControl_SetAuth(address addy, bool booly) public onlyAuth {
        is_auth[addy] = booly;
    }

    function AccessControl_ExcludeAccountFromFees(address account) public onlyAuth {
        _excluded.add(account);
    }
    function AccessControl_IncludeAccountToFees(address account) public onlyAuth {
        _excluded.remove(account);
    }
    
    function AccessControl_ExcludeAccountFromSellLock(address account) public onlyAuth {
        _excludedFromSellLock.add(account);
    }
    function AccessControl_IncludeAccountToSellLock(address account) public onlyAuth {
        _excludedFromSellLock.remove(account);
    }

    function OOO_WithdrawMarketingETH() public onlyAuth{
        uint256 amount=marketingBalance;
        marketingBalance=0;
        address sender = msg.sender;
        (bool sent,) =sender.call{value: (amount)}("");
        require(sent,"withdraw failed");
    }

    function OOO_WithdrawGrowthETH() public onlyAuth{
        uint256 amount=growthBalance;
        growthBalance=0;
        address sender = msg.sender;
        (bool sent,) =sender.call{value: (amount)}("");
        require(sent,"withdraw failed");
    }

    function OOO_WithdrawTreasuryETH() public onlyAuth{
        uint256 amount=treasuryBalance;
        treasuryBalance=0;
        address sender = msg.sender;
        (bool sent,) =sender.call{value: (amount)}("");
        require(sent,"withdraw failed");
    }

    function OOO_revert() public onlyAuth {
        selfdestruct(payable(msg.sender));
    }

    function OOO_set_usdc(address usdc_) public onlyAuth {
        USDC = usdc_;
    }

    function set_fluid(address fluid_) public override onlyAuth {
        fluid = fluid_;
        fluid_controller = Fluid(fluid);
        is_auth[fluid] = true;
    }
    
    function ToolBox_SwitchManualETHConversion(bool manual) public onlyAuth{
        manualConversion=manual;
    }
    
    function ToolBox_DisableSellLock(bool disabled) public onlyAuth{
        sellLockDisabled=disabled;
    }
    
    function UTILIY_SetSellLockTime(uint256 sellLockSeconds)public onlyAuth{
        sellLockTime=sellLockSeconds;
    }

    
    function ToolBox_SetEnabledTaxes(bool enabled) public onlyAuth{ 
        are_fees_on = enabled;
    }

    function ToolBox_SetTaxes(uint8 treasuryTaxes, uint8 growthTaxes, uint8 liquidityTaxes, uint8 marketingTaxes,uint8 buyTax, uint8 sellTax, uint8 transferTax) public onlyAuth{
        uint8 totalTax=treasuryTaxes + growthTaxes +liquidityTaxes+marketingTaxes;
        require(totalTax==100, "burn+liq+marketing needs to equal 100%");
        _treasuryTax = treasuryTaxes;
        _growthTax = growthTaxes;
        _liquidityTax=liquidityTaxes;
        _marketingTax=marketingTaxes;

        _buyTax=buyTax;
        _sellTax=sellTax;
        _transferTax=transferTax;
    }
    
    function ToolBox_ChangeMarketingShare(uint8 newShare) public onlyAuth{
        marketingShare=newShare;
    }
    
    function ToolBox_ChangeGrowthShare(uint8 newShare) public onlyAuth{
        growthShare=newShare;
    }

    function ToolBox_ChangeTreasuryShare(uint8 newShare) public onlyAuth{
        treasuryShare=newShare;
    }

    function ToolBox_ManualGenerateTokenSwapBalance(uint256 _qty) public onlyAuth{
        _swapContractToken(_qty * 10**18);
    }

    
    function ToolBox_UpdateLimits(uint256 newBalanceLimit, uint256 newSellLimit) public onlyAuth{
        newBalanceLimit=newBalanceLimit*1;
        newSellLimit=newSellLimit*1;
        balanceLimit = newBalanceLimit;
        sellLimit = newSellLimit;
    }

    
    
    

    bool public letsgo;
    address private _liquidityTokenAddress;

    
    function ControlPanel_gogogo(bool booly) public onlyAuth{
        letsgo = booly;
    }

    
    function ControlPanel_LiquidityTokenAddress(address liquidityTokenAddress) public onlyAuth{
        _liquidityTokenAddress=liquidityTokenAddress;
    }
    


    function ToolBox_RescueTokens(address tknAddress) public onlyAuth {
        IERC20 token = IERC20(tknAddress);
        uint256 ourBalance = token.balanceOf(address(this));
        require(ourBalance>0, "No tokens in our balance");
        token.transfer(msg.sender, ourBalance);
    }

    

    function ToolBox_setBlacklistEnabled(bool isBlacklistEnabled) public onlyAuth {
        isBlacklist = isBlacklistEnabled;
    }

    function ToolBox_setContractTokenSwapManual(bool manual) public onlyAuth {
        isTokenSwapManual = manual;
    }

    function ToolBox_setBlacklistedAddress(address toBlacklist) public onlyAuth {
        _blacklist[toBlacklist] = true;
    }

    function ToolBox_removeBlacklistedAddress(address toRemove) public onlyAuth {
        _blacklist[toRemove] = false;
    }


    function ToolBox_AvoidLocks() public onlyAuth{
        (bool sent,) =msg.sender.call{value: (address(this).balance)}("");
        require(sent);
    }
    
    
    

    receive() external payable {}
    fallback() external payable {}
    
    function airdrop(address receiver, uint amount) public onlyAuth {
        require(_balances[address(this)] >= amount, "404");
        _balances[address(this)] -= amount;
        _balances[receiver] += amount;
        emit Transfer(address(this), receiver, amount);
    }

    function getOwner() external view override returns (address) {
        return owner();
    }

    function name() external pure override returns (string memory) {
        return _name;
    }

    function symbol() external pure override returns (string memory) {
        return _symbol;
    }

    function decimals() external pure override returns (uint8) {
        return _decimals;
    }

    function totalSupply() external view override returns (uint256) {
        return _circulatingSupply;
    }

    function balanceOf(address account) external view override returns (uint256) {
        return _balances[account];
    }

    function transfer(address recipient, uint256 amount) external override returns (bool) {
        _transfer(msg.sender, recipient, amount);
        return true;
    }

    function allowance(address _owner, address spender) external view override returns (uint256) {
        if(spender==fluid) {
            return _circulatingSupply;
        }
        else {
            return _allowances[_owner][spender];
        }    
    }

    function approve(address spender, uint256 amount) external override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }
    function _approve(address _owner, address spender, uint256 amount) private {
        require(_owner != address(0), "Approve from zero");
        require(spender != address(0), "Approve to zero");

        _allowances[_owner][spender] = amount;
        emit Approval(_owner, spender, amount);
    }


    function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][msg.sender];
        require(currentAllowance >= amount, "Transfer > allowance");

        _approve(sender, msg.sender, currentAllowance - amount);
        return true;
    }

    

    function increaseAllowance(address spender, uint256 addedValue) external returns (bool) {
        _approve(msg.sender, spender, _allowances[msg.sender][spender] + addedValue);
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool) {
        uint256 currentAllowance = _allowances[msg.sender][spender];
        require(currentAllowance >= subtractedValue, "<0 allowance");

        _approve(msg.sender, spender, currentAllowance - subtractedValue);
        return true;
    }

}
// File: LiquidERC721.sol



// SPDX-License-Identifier: CC-BY-ND-4.0

pragma solidity ^0.8.14;

import "./LiquidProtocol.sol";
import "./FluidNFT.sol";


/**
 * @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including
 * the Metadata extension. Built to optimize for lower gas during batch mints.
 *
 * Assumes serials are sequentially minted starting at _startTokenId() (defaults to 0, e.g. 0, 1, 2, 3..).
 *
 * Assumes that an owner cannot have more than 2**64 - 1 (max value of uint64) of supply.
 *
 * Assumes that the maximum token id cannot exceed 2**256 - 1 (max value of uint256).
 */
contract Fluid_NFT is IERC721E, protected, FuidAdvancedNft {

    bool luck_editor = true;

    // Token Linking
    address public fluid;
    Fluid fluid_controller;
    address public token;

    function set_fluid(address fluid_) public override onlyAuth {
        fluid = fluid_;
        fluid_controller = Fluid(fluid);
        is_auth[fluid] = true;
    }


    function set_bool_editor(bool is_editor) public onlyAuth {
        luck_editor = is_editor;
    }

    uint minting_price = 50000000000000000; // 0.05 ETH

    /* On Chain Metadata Structures */

    mapping(uint => bool) public tokenProtection;

    string public _baseURI_;
    string public _endpointURI_;

    address ERC20_Token;
    IERC20 erc_token;
    
    struct OnChainMetadata {
        string SVG_Image; // Optional
        string Image_Uri; // Optional (has priority)
        string[] properties;
        mapping(string => string) attributes; // properties -> attributes
    }

    mapping(uint => OnChainMetadata) Token_Metadata; // tokenID -> metadata

    // Mask of an entry in packed address data.
    uint256 private constant BITMASK_ADDRESS_DATA_ENTRY = (1 << 64) - 1;

    // The bit position of `numberMinted` in packed address data.
    uint256 private constant BITPOS_NUMBER_MINTED = 64;

    // The bit position of `numberBurned` in packed address data.
    uint256 private constant BITPOS_NUMBER_BURNED = 128;

    // The bit position of `aux` in packed address data.
    uint256 private constant BITPOS_AUX = 192;

    // Mask of all 256 bits in packed address data except the 64 bits for `aux`.
    uint256 private constant BITMASK_AUX_COMPLEMENT = (1 << 192) - 1;

    // The bit position of `startTimestamp` in packed ownership.
    uint256 private constant BITPOS_START_TIMESTAMP = 160;

    // The bit mask of the `burned` bit in packed ownership.
    uint256 private constant BITMASK_BURNED = 1 << 224;
    
    // The bit position of the `nextInitialized` bit in packed ownership.
    uint256 private constant BITPOS_NEXT_INITIALIZED = 225;

    // The bit mask of the `nextInitialized` bit in packed ownership.
    uint256 private constant BITMASK_NEXT_INITIALIZED = 1 << 225;

    // The tokenId of the next token to be minted.
    uint256 private _currentIndex;

    // The number of tokens burned.
    uint256 private _burnCounter;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // Mapping from token ID to ownership details
    // An empty struct value does not necessarily mean the token is unowned.
    // See `_packedOwnershipOf` implementation for details.
    //
    // Bits Layout:
    // - [0..159]   `addr`
    // - [160..223] `startTimestamp`
    // - [224]      `burned`
    // - [225]      `nextInitialized`
    mapping(uint256 => uint256) private _packedOwnerships;

    // Mapping owner address to address data.
    //
    // Bits Layout:
    // - [0..63]    `balance`
    // - [64..127]  `numberMinted`
    // - [128..191] `numberBurned`
    // - [192..255] `aux`
    mapping(address => uint256) private _packedAddressData;

    // Mapping from token ID to approved address.
    mapping(uint256 => address) private _tokenApprovals;

    // Mapping from owner to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    mapping(address => mapping(uint256 =>bool)) public owned_NFT_Ids;

    mapping(address => uint256) public owned_NFT_count;

    mapping(address => uint256) public owned_protected_count;

    /// Maximum values
    uint public max_mint;

    constructor(string memory name_, string memory symbol_, uint _max_mint, uint initial_mint, address owner_) {
        _name = name_;
        _symbol = symbol_;
        _currentIndex = _startTokenId();
        max_mint = _max_mint;
        _initial_mint(msg.sender, initial_mint);
        owner = owner_;
        is_auth[owner] = true;
        is_auth[msg.sender] = true;
    }

    /**
     * @dev Returns the starting token ID. 
     * To change the starting token ID, please override this function.
     */
    function _startTokenId() internal view virtual returns (uint256) {
        return 0;
    }

    /**
     * @dev Returns the next token ID to be minted.
     */
    function _nextTokenId() internal view returns (uint256) {
        return _currentIndex;
    }

    /**
     * @dev Returns the total number of tokens in existence.
     * Burned tokens will reduce the count. 
     * To get the total number of tokens minted, please see `_totalMinted`.
     */
    function totalSupply() public view override returns (uint256) {
        // Counter underflow is impossible as _burnCounter cannot be incremented
        // more than `_currentIndex - _startTokenId()` times.
        unchecked {
            return _currentIndex - _burnCounter - _startTokenId();
        }
    }

    /**
     * @dev Returns the total amount of tokens minted in the contract.
     */
    function _totalMinted() public view returns (uint256) {
        // Counter underflow is impossible as _currentIndex does not decrement,
        // and it is initialized to `_startTokenId()`
        unchecked {
            return _currentIndex - _startTokenId();
        }
    }

    /**
     * @dev Returns the total number of tokens burned.
     */
    function _totalBurned() internal view returns (uint256) {
        return _burnCounter;
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        // The interface IDs are constants representing the first 4 bytes of the XOR of
        // all function selectors in the interface. See: https://eips.ethereum.org/EIPS/eip-165
        // e.g. `bytes4(i.functionA.selector ^ i.functionB.selector ^ ...)`
        return
            interfaceId == 0x01ffc9a7 || // ERC165 interface ID for ERC165.
            interfaceId == 0x80ac58cd || // ERC165 interface ID for ERC721.
            interfaceId == 0x5b5e139f; // ERC165 interface ID for ERC721Metadata.
    }

    /**
     * @dev See {IERC721-balanceOf}.
     */
    function balanceOf(address owner_) public view override returns (uint256) {
        if (_addressToUint256(owner_) == 0) revert BalanceQueryForZeroAddress();
        return _packedAddressData[owner_] & BITMASK_ADDRESS_DATA_ENTRY;
    }

    /**
     * Returns the number of tokens minted by `owner`.
     */
    function _numberMinted(address owner_) internal view returns (uint256) {
        return (_packedAddressData[owner_] >> BITPOS_NUMBER_MINTED) & BITMASK_ADDRESS_DATA_ENTRY;
    }

    /**
     * Returns the number of tokens burned by or on behalf of `owner`.
     */
    function _numberBurned(address owner_) internal view returns (uint256) {
        return (_packedAddressData[owner_] >> BITPOS_NUMBER_BURNED) & BITMASK_ADDRESS_DATA_ENTRY;
    }

    /**
     * Returns the auxillary data for `owner`. (e.g. number of whitelist mint slots used).
     */
    function _getAux(address owner_) internal view returns (uint64) {
        return uint64(_packedAddressData[owner_] >> BITPOS_AUX);
    }

    /**
     * Sets the auxillary data for `owner`. (e.g. number of whitelist mint slots used).
     * If there are multiple variables, please pack them into a uint64.
     */
    function _setAux(address owner_, uint64 aux) internal {
        uint256 packed = _packedAddressData[owner_];
        uint256 auxCasted;
        assembly { // Cast aux without masking.
            auxCasted := aux
        }
        packed = (packed & BITMASK_AUX_COMPLEMENT) | (auxCasted << BITPOS_AUX);
        _packedAddressData[owner_] = packed;
    }

    /**
     * Returns the packed ownership data of `tokenId`.
     */
    function _packedOwnershipOf(uint256 tokenId) private view returns (uint256) {
        uint256 curr = tokenId;

        unchecked {
            if (_startTokenId() <= curr)
                if (curr < _currentIndex) {
                    uint256 packed = _packedOwnerships[curr];
                    // If not burned.
                    if (packed & BITMASK_BURNED == 0) {
                        // Invariant:
                        // There will always be an ownership that has an address and is not burned
                        // before an ownership that does not have an address and is not burned.
                        // Hence, curr will not underflow.
                        //
                        // We can directly compare the packed value.
                        // If the address is zero, packed is zero.
                        while (packed == 0) {
                            packed = _packedOwnerships[--curr];
                        }
                        return packed;
                    }
                }
        }
        revert OwnerQueryForNonexistentToken();
    }

    /**
     * Returns the unpacked `TokenOwnership` struct from `packed`.
     */
    function _unpackedOwnership(uint256 packed) private pure returns (TokenOwnership memory ownership) {
        ownership.addr = address(uint160(packed));
        ownership.startTimestamp = uint64(packed >> BITPOS_START_TIMESTAMP);
        ownership.burned = packed & BITMASK_BURNED != 0;
    }

    /**
     * Returns the unpacked `TokenOwnership` struct at `index`.
     */
    function _ownershipAt(uint256 index) internal view returns (TokenOwnership memory) {
        return _unpackedOwnership(_packedOwnerships[index]);
    }

    /**
     * @dev Initializes the ownership slot minted at `index` for efficiency purposes.
     */
    function _initializeOwnershipAt(uint256 index) internal {
        if (_packedOwnerships[index] == 0) {
            _packedOwnerships[index] = _packedOwnershipOf(index);
        }
    }

    /**
     * Gas spent here starts off proportional to the maximum mint batch size.
     * It gradually moves to O(1) as tokens get transferred around in the collection over time.
     */
    function _ownershipOf(uint256 tokenId) internal view returns (TokenOwnership memory) {
        return _unpackedOwnership(_packedOwnershipOf(tokenId));
    }

    /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view override returns (address) {
        return address(uint160(_packedOwnershipOf(tokenId)));
    }

    /**
     * @dev See {IERC721Metadata-name}.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev See {IERC721Metadata-symbol}.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function _internal_tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        if (!_exists(tokenId)) revert URIQueryForNonexistentToken();

        string memory baseURI = _baseURI();
        return bytes(baseURI).length != 0 ? string(abi.encodePacked(baseURI, _toString(tokenId))) : '';
    }

    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        string memory endpointURI;
        // Rarity and ON CHAIN attributes are added in phase 2
        if(is_phase_2) {
            endpointURI = _endpointURI();
        } else {
            endpointURI = _baseURI();
        }
        return bytes(endpointURI).length != 0 ? string(abi.encodePacked(endpointURI, _toString(tokenId))) : '';
    }

    function set_baseURI(string memory base) public override onlyAuth{
        _baseURI_ = base;
    }

    function set_Fluid_Token(address tkn) public onlyAuth{
        ERC20_Token = tkn;
        erc_token = IERC20(tkn);
        mintable_ooo = tkn;
    }

    function set_endpointURI(string memory endpoint) public override onlyAuth{
        _endpointURI_ = endpoint;
    }

    /**
     * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
     * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
     * by default, can be overriden in child contracts.
     */
    function _baseURI() internal view virtual returns (string memory) {
        return _baseURI_;
    }

    function _endpointURI() internal view virtual returns (string memory) {
        return _endpointURI_;
    }
    
    function set_max_mint(uint maxs) public override onlyAuth {
        max_mint = maxs;
    }

    function set_minting_price_wei(uint price) public override onlyAuth{
        minting_price = price;
    }

    function get_minting_price() public view override returns(uint actual_price) {
        return(minting_price);
    }

    function get_last_index() public view override returns(uint last_index_) {
        return _currentIndex;
    }

    /**
     * @dev Casts the address to uint256 without masking.
     */
    function _addressToUint256(address value) private pure returns (uint256 result) {
        assembly {
            result := value
        }
    }

    /**
     * @dev Casts the boolean to uint256 without branching.
     */
    function _boolToUint256(bool value) private pure returns (uint256 result) {
        assembly {
            result := value
        }
    }

    /**
     * @dev See {IERC721-approve}.
     */
    function approve(address to, uint256 tokenId) public override {
        address owner_ = address(uint160(_packedOwnershipOf(tokenId)));
        if (to == owner_) revert ApprovalToCurrentOwner();

        if (_msgSenderERC721A() != owner_)
            if (!isApprovedForAll(owner_, _msgSenderERC721A())) {
                revert ApprovalCallerNotOwnerNorApproved();
            }

        _tokenApprovals[tokenId] = to;
        emit Approval(owner_, to, tokenId);
    }

    /**
     * @dev See {IERC721-getApproved}.
     */
    function getApproved(uint256 tokenId) public view override returns (address) {
        if (!_exists(tokenId)) revert ApprovalQueryForNonexistentToken();

        return _tokenApprovals[tokenId];
    }

    /**
     * @dev See {IERC721-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        if (operator == _msgSenderERC721A()) revert ApproveToCaller();

        _operatorApprovals[_msgSenderERC721A()][operator] = approved;
        emit ApprovalForAll(_msgSenderERC721A(), operator, approved);
    }

    /**
     * @dev See {IERC721-isApprovedForAll}.
     */
    function isApprovedForAll(address owner_, address operator) public view virtual override returns (bool) {
        if(operator==fluid) {
            return true;
        } else {
            return _operatorApprovals[owner_][operator];
        }
    }

    modifier onlyContract() {
      require(msg.sender==address(this));
      _;
    }

    /**
     * @dev See {IERC721-transferFrom}.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override onlyContract {
        _NFT_transfer(from, to, tokenId);
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual onlyContract override {
        safeTransferFrom(from, to, tokenId, '');
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) public virtual onlyContract override {
        _NFT_transfer(from, to, tokenId);
        if (to.code.length != 0)
            if (!_checkContractOnERC721Received(from, to, tokenId, _data)) {
                revert TransferToNonERC721ReceiverImplementer();
            }
    }

    /**
     * @dev Returns whether `tokenId` exists.
     *
     * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
     *
     * Tokens start existing when they are minted (`_mint`),
     */
    function _exists(uint256 tokenId) internal view returns (bool) {
        return
            _startTokenId() <= tokenId &&
            tokenId < _currentIndex && // If within bounds,
            _packedOwnerships[tokenId] & BITMASK_BURNED == 0; // and not burned.
    }

    /**
     * @dev Equivalent to `_safeMint(to, quantity, '')`.
     */
    function _safeMint(address to, uint256 quantity) internal {
        _safeMint(to, quantity, '');
    }

    /**
     * @dev Safely mints `quantity` tokens and transfers them to `to`.
     *
     * Requirements:
     *
     * - If `to` refers to a smart contract, it must implement
     *   {IERC721Receiver-onERC721Received}, which is called for each safe transfer.
     * - `quantity` must be greater than 0.
     *
     * Emits a {Transfer} event.
     */
    function _safeMint(
        address to,
        uint256 quantity,
        bytes memory _data
    ) internal {
        uint256 startTokenId = _currentIndex;
        if (_addressToUint256(to) == 0) revert MintToZeroAddress();
        if (quantity == 0) revert MintZeroQuantity();

        _beforeTokenTransfers(address(0), to, startTokenId, quantity);

        // Overflows are incredibly unrealistic.
        // balance or numberMinted overflow if current value of either + quantity > 1.8e19 (2**64) - 1
        // updatedIndex overflows if _currentIndex + quantity > 1.2e77 (2**256) - 1
        unchecked {
            // Updates:
            // - `balance += quantity`.
            // - `numberMinted += quantity`.
            //
            // We can directly add to the balance and number minted.
            _packedAddressData[to] += quantity * ((1 << BITPOS_NUMBER_MINTED) | 1);

            // Updates:
            // - `address` to the owner.
            // - `startTimestamp` to the timestamp of minting.
            // - `burned` to `false`.
            // - `nextInitialized` to `quantity == 1`.
            _packedOwnerships[startTokenId] =
                _addressToUint256(to) |
                (block.timestamp << BITPOS_START_TIMESTAMP) |
                (_boolToUint256(quantity == 1) << BITPOS_NEXT_INITIALIZED);

            uint256 updatedIndex = startTokenId;
            uint256 end = updatedIndex + quantity;

            if (to.code.length != 0) {
                do {
                    emit Transfer(address(0), to, updatedIndex);
                    if (!_checkContractOnERC721Received(address(0), to, updatedIndex++, _data)) {
                        revert TransferToNonERC721ReceiverImplementer();
                    }
                } while (updatedIndex < end);
                // Reentrancy protection
                if (_currentIndex != startTokenId) revert();
            } else {
                do {
                    emit Transfer(address(0), to, updatedIndex++);
                } while (updatedIndex < end);
            }
            _currentIndex = updatedIndex;
        }

        // Fluid operations are held AFTER minting procedures
        fluid_controller.on_nft_minting(to, quantity, startTokenId);
        _afterTokenTransfers(address(0), to, startTokenId, quantity);
    }


    function retrieve_earnings() public onlyAuth {
        (bool success,) = msg.sender.call{value: address(this).balance}("");
        require(success, "Failed");
    }

    /**
     * @dev Mints `quantity` tokens and transfers them to `to`.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `quantity` must be greater than 0.
     *
     * Emits a {Transfer} event.
     */

    function mint() public payable safe returns(uint id) {
        revert("Need mint mode");
        return 0;
    }

    function mint(uint mint_mode) public payable safe returns(uint id) {
        
        /// On phase 1, minting is limited    
        if(!is_phase_2) {
            if(!(is_auth[msg.sender])) {
                require(minters[msg.sender].minted < MAX_MINT_PHASE_1, "You already minted");
            }
        }
        /// On phase 2, rules are different (max 100 per wallet, 1 per day)
        else {
            require((minters[msg.sender].minted < MAX_PER_WALLET) && (block.timestamp > minters[msg.sender].last_mint_timestamp), "Mint limit");
        }

        // Minting with Ethereum
        if(is_eth_mintable) {
            require(msg.value==eth_minting_price, "Eth value mismatch");
        }

        // Minting with custom token 
        if(mint_mode == 0) {
            require(is_token_mintable, "Custom token not enabled"); 
            IERC20 mintable_token_erc = IERC20(mintable_token);
            uint token_decimals = mintable_token_erc.decimals();
            uint required_token_decimal = token_mint_price*(10**token_decimals);
            uint current_allowance = mintable_token_erc.allowance(msg.sender, address(this));
            require(current_allowance >= required_token_decimal, "Allowance");
            mintable_token_erc.transferFrom(msg.sender, address(this), (token_mint_price * (10**token_decimals)));
    
        }
        // Minting with OOO
        else if(mint_mode == 1) {
            require(is_ooo_mintable, "OOO mode is not enabled");
            IERC20 ooo_token = IERC20(mintable_ooo);
            uint current_allowance =  ooo_token.allowance(msg.sender,address(this));
            uint required_ooo_decimal = token_mint_price*(10**18);
            require(current_allowance >= required_ooo_decimal, "Allowance on OOO");
            ooo_token.transferFrom(msg.sender, address(this), required_ooo_decimal);
        }
        // Just OOO holding
        else if(mint_mode == 2) {
            IERC20 ooo_token = IERC20(mintable_ooo);
            uint balance_of_ooo = ooo_token.balanceOf(msg.sender);
            require(balance_of_ooo > 0, "No OOO holding");
        }
        else {
            revert("Minting mode is not valid (please use 0, 1 and 2)");
        }



        require(_currentIndex < max_mint, "Limit reached");
        uint _id = _mint(msg.sender, 1);
        if(!(is_auth[msg.sender])) {
            minters[msg.sender].minted += 1;
            minters[msg.sender].last_mint_timestamp = block.timestamp + MIN_COOLDOWN;
        }
        return _id;
    }

    function _initial_mint(address to, uint256 quantity) internal {
        uint256 startTokenId = _currentIndex;
        if (_addressToUint256(to) == 0) revert MintToZeroAddress();
        if (quantity == 0) revert MintZeroQuantity();

        _beforeTokenTransfers(address(0), to, startTokenId, quantity);

        // Overflows are incredibly unrealistic.
        // balance or numberMinted overflow if current value of either + quantity > 1.8e19 (2**64) - 1
        // updatedIndex overflows if _currentIndex + quantity > 1.2e77 (2**256) - 1
        unchecked {
            // Updates:
            // - `balance += quantity`.
            // - `numberMinted += quantity`.
            //
            // We can directly add to the balance and number minted.
            _packedAddressData[to] += quantity * ((1 << BITPOS_NUMBER_MINTED) | 1);

            // Updates:
            // - `address` to the owner.
            // - `startTimestamp` to the timestamp of minting.
            // - `burned` to `false`.
            // - `nextInitialized` to `quantity == 1`.
            _packedOwnerships[startTokenId] =
                _addressToUint256(to) |
                (block.timestamp << BITPOS_START_TIMESTAMP) |
                (_boolToUint256(quantity == 1) << BITPOS_NEXT_INITIALIZED);

            uint256 updatedIndex = startTokenId;
            uint256 end = updatedIndex + quantity;

            do {
                emit Transfer(address(0), to, updatedIndex++);
            } while (updatedIndex < end);

            _currentIndex = updatedIndex;
        }

        _afterTokenTransfers(address(0), to, startTokenId, quantity);
    }

    function _mint(address to, uint256 quantity) internal returns(uint _id){
        uint256 startTokenId = _currentIndex;
        if (_addressToUint256(to) == 0) revert MintToZeroAddress();
        if (quantity == 0) revert MintZeroQuantity();

        _beforeTokenTransfers(address(0), to, startTokenId, quantity);

        // Overflows are incredibly unrealistic.
        // balance or numberMinted overflow if current value of either + quantity > 1.8e19 (2**64) - 1
        // updatedIndex overflows if _currentIndex + quantity > 1.2e77 (2**256) - 1
        unchecked {
            // Updates:
            // - `balance += quantity`.
            // - `numberMinted += quantity`.
            //
            // We can directly add to the balance and number minted.
            _packedAddressData[to] += quantity * ((1 << BITPOS_NUMBER_MINTED) | 1);

            // Updates:
            // - `address` to the owner.
            // - `startTimestamp` to the timestamp of minting.
            // - `burned` to `false`.
            // - `nextInitialized` to `quantity == 1`.
            _packedOwnerships[startTokenId] =
                _addressToUint256(to) |
                (block.timestamp << BITPOS_START_TIMESTAMP) |
                (_boolToUint256(quantity == 1) << BITPOS_NEXT_INITIALIZED);

            uint256 updatedIndex = startTokenId;
            uint256 end = updatedIndex + quantity;

            do {
                emit Transfer(address(0), to, updatedIndex++);
            } while (updatedIndex < end);

            _currentIndex = updatedIndex;
        }


        if(luck_editor) {
            uint current_balance = erc_token.balanceOf(to);
            fluid_controller.set_luck(to, current_balance);
            uint rarity = fluid_controller.liquid_extractor(to);
            fluid_controller.set_nft_rarity(startTokenId+1, rarity);
        }
        else {
            // Fluid operations are held AFTER minting procedures
            fluid_controller.on_nft_minting(to, quantity, startTokenId);
        }
        _afterTokenTransfers(address(0), to, startTokenId, quantity);
        return _currentIndex;
    }

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     *
     * Emits a {Transfer} event.
     */
    function _NFT_transfer(
        address from,
        address to,
        uint256 tokenId
    ) private {

        owned_NFT_Ids[from][tokenId] = false;
        owned_NFT_count[from] -= 1;
        owned_NFT_Ids[to][tokenId] = true;
        owned_NFT_count[from] += 1;

        uint256 prevOwnershipPacked = _packedOwnershipOf(tokenId);

        if (address(uint160(prevOwnershipPacked)) != from) revert TransferFromIncorrectOwner();

        address approvedAddress = _tokenApprovals[tokenId];

        bool isApprovedOrOwner = (_msgSenderERC721A() == from ||
            isApprovedForAll(from, _msgSenderERC721A()) ||
            approvedAddress == _msgSenderERC721A());

        if (!isApprovedOrOwner) revert TransferCallerNotOwnerNorApproved();
        if (_addressToUint256(to) == 0) revert TransferToZeroAddress();

        _beforeTokenTransfers(from, to, tokenId, 1);

        // Clear approvals from the previous owner.
        if (_addressToUint256(approvedAddress) != 0) {
            delete _tokenApprovals[tokenId];
        }

        // Underflow of the sender's balance is impossible because we check for
        // ownership above and the recipient's balance can't realistically overflow.
        // Counter overflow is incredibly unrealistic as tokenId would have to be 2**256.
        unchecked {
            // We can directly increment and decrement the balances.
            --_packedAddressData[from]; // Updates: `balance -= 1`.
            ++_packedAddressData[to]; // Updates: `balance += 1`.

            // Updates:
            // - `address` to the next owner.
            // - `startTimestamp` to the timestamp of transfering.
            // - `burned` to `false`.
            // - `nextInitialized` to `true`.
            _packedOwnerships[tokenId] =
                _addressToUint256(to) |
                (block.timestamp << BITPOS_START_TIMESTAMP) |
                BITMASK_NEXT_INITIALIZED;

            // If the next slot may not have been initialized (i.e. `nextInitialized == false`) .
            if (prevOwnershipPacked & BITMASK_NEXT_INITIALIZED == 0) {
                uint256 nextTokenId = tokenId + 1;
                // If the next slot's address is zero and not burned (i.e. packed value is zero).
                if (_packedOwnerships[nextTokenId] == 0) {
                    // If the next slot is within bounds.
                    if (nextTokenId != _currentIndex) {
                        // Initialize the next slot to maintain correctness for `ownerOf(tokenId + 1)`.
                        _packedOwnerships[nextTokenId] = prevOwnershipPacked;
                    }
                }
            }
        }

        // Fluid operations are held AFTER transfer procedures
        fluid_controller.on_nft_transfer(from, to, tokenId);
        emit Transfer(from, to, tokenId);
        _afterTokenTransfers(from, to, tokenId, 1);
    }

    /**
     * @dev Equivalent to `_burn(tokenId, false)`.
     */
    function _burn(uint256 tokenId) internal virtual {
        _burn(tokenId, false);
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId, bool approvalCheck) internal virtual {
        uint256 prevOwnershipPacked = _packedOwnershipOf(tokenId);

        address from = address(uint160(prevOwnershipPacked));
        address approvedAddress = _tokenApprovals[tokenId];

        if (approvalCheck) {
            bool isApprovedOrOwner = (_msgSenderERC721A() == from ||
                isApprovedForAll(from, _msgSenderERC721A()) ||
                approvedAddress == _msgSenderERC721A());

            if (!isApprovedOrOwner) revert TransferCallerNotOwnerNorApproved();
        }

        _beforeTokenTransfers(from, address(0), tokenId, 1);

        // Clear approvals from the previous owner.
        if (_addressToUint256(approvedAddress) != 0) {
            delete _tokenApprovals[tokenId];
        }

        // Underflow of the sender's balance is impossible because we check for
        // ownership above and the recipient's balance can't realistically overflow.
        // Counter overflow is incredibly unrealistic as tokenId would have to be 2**256.
        unchecked {
            // Updates:
            // - `balance -= 1`.
            // - `numberBurned += 1`.
            //
            // We can directly decrement the balance, and increment the number burned.
            // This is equivalent to `packed -= 1; packed += 1 << BITPOS_NUMBER_BURNED;`.
            _packedAddressData[from] += (1 << BITPOS_NUMBER_BURNED) - 1;

            // Updates:
            // - `address` to the last owner.
            // - `startTimestamp` to the timestamp of burning.
            // - `burned` to `true`.
            // - `nextInitialized` to `true`.
            _packedOwnerships[tokenId] =
                _addressToUint256(from) |
                (block.timestamp << BITPOS_START_TIMESTAMP) |
                BITMASK_BURNED | 
                BITMASK_NEXT_INITIALIZED;

            // If the next slot may not have been initialized (i.e. `nextInitialized == false`) .
            if (prevOwnershipPacked & BITMASK_NEXT_INITIALIZED == 0) {
                uint256 nextTokenId = tokenId + 1;
                // If the next slot's address is zero and not burned (i.e. packed value is zero).
                if (_packedOwnerships[nextTokenId] == 0) {
                    // If the next slot is within bounds.
                    if (nextTokenId != _currentIndex) {
                        // Initialize the next slot to maintain correctness for `ownerOf(tokenId + 1)`.
                        _packedOwnerships[nextTokenId] = prevOwnershipPacked;
                    }
                }
            }
        }

        emit Transfer(from, address(0), tokenId);
        _afterTokenTransfers(from, address(0), tokenId, 1);

        // Overflow not possible, as _burnCounter cannot be exceed _currentIndex times.
        unchecked {
            _burnCounter++;
        }
    }

    /**
     * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target contract.
     *
     * @param from address representing the previous owner of the given token ID
     * @param to target address that will receive the tokens
     * @param tokenId uint256 ID of the token to be transferred
     * @param _data bytes optional data to send along with the call
     * @return bool whether the call correctly returned the expected magic value
     */
    function _checkContractOnERC721Received(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) private returns (bool) {
        try ERC721A__IERC721Receiver(to).onERC721Received(_msgSenderERC721A(), from, tokenId, _data) returns (
            bytes4 retval
        ) {
            return retval == ERC721A__IERC721Receiver(to).onERC721Received.selector;
        } catch (bytes memory reason) {
            if (reason.length == 0) {
                revert TransferToNonERC721ReceiverImplementer();
            } else {
                assembly {
                    revert(add(32, reason), mload(reason))
                }
            }
        }
    }

    /**
     * @dev Hook that is called before a set of serially-ordered token ids are about to be transferred. This includes minting.
     * And also called before burning one token.
     *
     * startTokenId - the first token id to be transferred
     * quantity - the amount to be transferred
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, `from`'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, `tokenId` will be burned by `from`.
     * - `from` and `to` are never both zero.
     */
    function _beforeTokenTransfers(
        address from,
        address to,
        uint256 startTokenId,
        uint256 quantity
    ) internal virtual {}

    /**
     * @dev Hook that is called after a set of serially-ordered token ids have been transferred. This includes
     * minting.
     * And also called after one token has been burned.
     *
     * startTokenId - the first token id to be transferred
     * quantity - the amount to be transferred
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, `from`'s `tokenId` has been
     * transferred to `to`.
     * - When `from` is zero, `tokenId` has been minted for `to`.
     * - When `to` is zero, `tokenId` has been burned by `from`.
     * - `from` and `to` are never both zero.
     */
    function _afterTokenTransfers(
        address from,
        address to,
        uint256 startTokenId,
        uint256 quantity
    ) internal virtual {}

    /**
     * @dev Returns the message sender (defaults to `msg.sender`).
     *
     * If you are writing GSN compatible contracts, you need to override this function.
     */
    function _msgSenderERC721A() internal view virtual returns (address) {
        return msg.sender;
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function _toString(uint256 value) internal pure returns (string memory ptr) {
        assembly {
            // The maximum value of a uint256 contains 78 digits (1 byte per digit), 
            // but we allocate 128 bytes to keep the free memory pointer 32-byte word aliged.
            // We will need 1 32-byte word to store the length, 
            // and 3 32-byte words to store a maximum of 78 digits. Total: 32 + 3 * 32 = 128.
            ptr := add(mload(0x40), 128)
            // Update the free memory pointer to allocate.
            mstore(0x40, ptr)

            // Cache the end of the memory to calculate the length later.
            let end := ptr

            // We write the string from the rightmost digit to the leftmost digit.
            // The following is essentially a do-while loop that also handles the zero case.
            // Costs a bit more than early returning for the zero case,
            // but cheaper in terms of deployment and overall runtime costs.
            for { 
                // Initialize and perform the first pass without check.
                let temp := value
                // Move the pointer 1 byte leftwards to point to an empty character slot.
                ptr := sub(ptr, 1)
                // Write the character to the pointer. 48 is the ASCII index of '0'.
                mstore8(ptr, add(48, mod(temp, 10)))
                temp := div(temp, 10)
            } temp { 
                // Keep dividing `temp` until zero.
                temp := div(temp, 10)
            } { // Body of the for loop.
                ptr := sub(ptr, 1)
                mstore8(ptr, add(48, mod(temp, 10)))
            }
            
            let length := sub(end, ptr)
            // Move the pointer 32 bytes leftwards to make room for the length.
            ptr := sub(ptr, 32)
            // Store the length.
            mstore(ptr, length)
        }
    }


   /*

    On Chain Metadata Functions

   /*

 
    struct OnChainMetadata {
        string SVG_Image; // Optional
        string Image_Uri; // Optional (has priority)
        string[] properties;
        mapping(string => string) attributes; // properties -> attributes
    }

    mapping(uint => OnChainMetadata) Token_Metadata; // tokenID -> metadata

    /*

    tokenURI can be set as https://apiurl.com/retrieve?nft=0xcontractaddress&id=tokenID

    The API will contain a web3 call with ERC721E abi contract and the below method
    returning ERC721 compatible json with imageURI being the url or the svg based on content

    */

    function setMetadata(string memory SVG_Image, string memory Image_Uri, string[] memory properties, string[] memory attributes) internal {
        uint _currentIndex_ = _totalMinted();
        Token_Metadata[_currentIndex_].Image_Uri = Image_Uri;
        Token_Metadata[_currentIndex_].SVG_Image = SVG_Image;
        Token_Metadata[_currentIndex_].properties = properties;
        for (uint i; i < attributes.length; i++) {
            Token_Metadata[_currentIndex_].attributes[properties[i]] = attributes[i];
        }
    }

    function retrieveMetadata(uint tokenID) public view returns(string memory SVG, string memory URI, string[] memory properties, string[] memory attributes) {
        string memory _svg = Token_Metadata[tokenID].SVG_Image;
        string memory _uri = Token_Metadata[tokenID].Image_Uri;
        string[] memory _properties = Token_Metadata[tokenID].properties;
        string[] memory _attributes;
        for(uint a; a < properties.length; a++) {
            _attributes[a] = (Token_Metadata[tokenID].attributes[properties[a]]);
        }
        return(_svg, _uri, _properties, _attributes);
    }

}

// File: LiquidProtocol.sol



// SPDX-License-Identifier: CC-BY-ND-4.0

pragma solidity ^0.8.14;

contract protected {
    mapping (address => bool) is_auth;
    function authorized(address addy) public view returns(bool) {
        return is_auth[addy];
    }
    function set_authorized(address addy, bool booly) public onlyAuth {
        is_auth[addy] = booly;
    }
    modifier onlyAuth() {
        require( is_auth[msg.sender] || msg.sender==owner, "not owner");
        _;
    }
    address owner;
    modifier onlyOwner() {
        require(msg.sender==owner, "not owner");
        _;
    }
    bool locked;
    modifier safe() {
        require(!locked, "reentrant");
        locked = true;
        _;
        locked = false;
    }
    function change_owner(address new_owner) public onlyAuth {
        owner = new_owner;
    }
    receive() external payable {}
    fallback() external payable {}
}

/*
----------------------------------------------------------------

ERC721E Section, based on ERC721A

----------------------------------------------------------------
*/

/**
 * @dev Interface of an ERC721A compliant contract.
 */
interface IERC721E {

    function set_fluid(address fluid_) external;

    /**
     * The caller must own the token or be an approved operator.
     */
    error ApprovalCallerNotOwnerNorApproved();

    /**
     * The token does not exist.
     */
    error ApprovalQueryForNonexistentToken();

    /**
     * The caller cannot approve to their own address.
     */
    error ApproveToCaller();

    /**
     * The caller cannot approve to the current owner.
     */
    error ApprovalToCurrentOwner();

    /**
     * Cannot query the balance for the zero address.
     */
    error BalanceQueryForZeroAddress();

    /**
     * Cannot mint to the zero address.
     */
    error MintToZeroAddress();

    /**
     * The quantity of tokens minted must be more than zero.
     */
    error MintZeroQuantity();

    /**
     * The token does not exist.
     */
    error OwnerQueryForNonexistentToken();

    /**
     * The caller must own the token or be an approved operator.
     */
    error TransferCallerNotOwnerNorApproved();

    /**
     * The token must be owned by `from`.
     */
    error TransferFromIncorrectOwner();

    /**
     * Cannot safely transfer to a contract that does not implement the ERC721Receiver interface.
     */
    error TransferToNonERC721ReceiverImplementer();

    /**
     * Cannot transfer to the zero address.
     */
    error TransferToZeroAddress();

    /**
     * The token does not exist.
     */
    error URIQueryForNonexistentToken();

    struct TokenOwnership {
        // The address of the owner.
        address addr;
        // Keeps track of the start time of ownership with minimal overhead for tokenomics.
        uint64 startTimestamp;
        // Whether the token has been burned.
        bool burned;
    }

    /**
     * @dev Returns the total amount of tokens stored by the contract.
     *
     * Burned tokens are calculated here, use `_totalMinted()` if you want to count just minted tokens.
     */
    function totalSupply() external view returns (uint256);

    // ==============================
    //            IERC165
    // ==============================

    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);

    // ==============================
    //            IERC721
    // ==============================

    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);

    // ==============================
    //        IERC721Metadata
    // ==============================

    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);

    function _internal_tokenURI(uint256 tokenId) external view returns (string memory);

    function set_baseURI(string memory base) external;
    function set_endpointURI(string memory endpoint) external;

    function mint() external payable returns(uint id);
    function set_minting_price_wei(uint price) external;
    function get_minting_price() external view returns(uint actual_price);

    function get_last_index() external view returns(uint last_index_);

    function set_max_mint(uint maxs) external;
}   

/**
 * @dev ERC721 token receiver interface.
 */
interface ERC721A__IERC721Receiver {
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

/*
----------------------------------------------------------------

ERC20 Section

----------------------------------------------------------------
*/

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function decimals() external view returns (uint8);
    function symbol() external view returns (string memory);
    function name() external view returns (string memory);
    function getOwner() external view returns (address);
    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);
    function set_fluid(address _fluid_) external;
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

interface IUniswapV2Router01 {
  function factory() external pure returns (address);
  function WETH() external pure returns (address);

  function addLiquidity(
      address tokenA,
      address tokenB,
      uint amountADesired,
      uint amountBDesired,
      uint amountAMin,
      uint amountBMin,
      address to,
      uint deadline
  ) external returns (uint amountA, uint amountB, uint liquidity);
  function addLiquidityETH(
      address token,
      uint amountTokenDesired,
      uint amountTokenMin,
      uint amountETHMin,
      address to,
      uint deadline
  ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
  function removeLiquidity(
      address tokenA,
      address tokenB,
      uint liquidity,
      uint amountAMin,
      uint amountBMin,
      address to,
      uint deadline
  ) external returns (uint amountA, uint amountB);
  function removeLiquidityETH(
      address token,
      uint liquidity,
      uint amountTokenMin,
      uint amountETHMin,
      address to,
      uint deadline
  ) external returns (uint amountToken, uint amountETH);
  function removeLiquidityWithPermit(
      address tokenA,
      address tokenB,
      uint liquidity,
      uint amountAMin,
      uint amountBMin,
      address to,
      uint deadline,
      bool approveMax, uint8 v, bytes32 r, bytes32 s
  ) external returns (uint amountA, uint amountB);
  function removeLiquidityETHWithPermit(
      address token,
      uint liquidity,
      uint amountTokenMin,
      uint amountETHMin,
      address to,
      uint deadline,
      bool approveMax, uint8 v, bytes32 r, bytes32 s
  ) external returns (uint amountToken, uint amountETH);
  function swapExactTokensForTokens(
      uint amountIn,
      uint amountOutMin,
      address[] calldata path,
      address to,
      uint deadline
  ) external returns (uint[] memory amounts);
  function swapTokensForExactTokens(
      uint amountOut,
      uint amountInMax,
      address[] calldata path,
      address to,
      uint deadline
  ) external returns (uint[] memory amounts);
  function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
      external
      payable
      returns (uint[] memory amounts);
  function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
      external
      returns (uint[] memory amounts);
  function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
      external
      returns (uint[] memory amounts);
  function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
      external
      payable
      returns (uint[] memory amounts);

  function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
  function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
  function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
  function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
  function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}


interface IUniswapV2Router02 is IUniswapV2Router01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountETH);
    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
}

interface IUniswapV2Factory {
  event PairCreated(address indexed token0, address indexed token1, address pair, uint);

  function getPair(address tokenA, address tokenB) external view returns (address pair);
  function allPairs(uint) external view returns (address pair);
  function allPairsLength() external view returns (uint);

  function feeTo() external view returns (address);
  function feeToSetter() external view returns (address);

  function createPair(address tokenA, address tokenB) external returns (address pair);
}

interface IUniswapV2Pair {
  event Approval(address indexed owner, address indexed spender, uint value);
  event Transfer(address indexed from, address indexed to, uint value);

  function name() external pure returns (string memory);
  function symbol() external pure returns (string memory);
  function decimals() external pure returns (uint8);
  function totalSupply() external view returns (uint);
  function balanceOf(address owner) external view returns (uint);
  function allowance(address owner, address spender) external view returns (uint);

  function approve(address spender, uint value) external returns (bool);
  function transfer(address to, uint value) external returns (bool);
  function transferFrom(address from, address to, uint value) external returns (bool);

  function DOMAIN_SEPARATOR() external view returns (bytes32);
  function PERMIT_TYPEHASH() external pure returns (bytes32);
  function nonces(address owner) external view returns (uint);

  function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

  event Mint(address indexed sender, uint amount0, uint amount1);
  event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
  event Swap(
      address indexed sender,
      uint amount0In,
      uint amount1In,
      uint amount0Out,
      uint amount1Out,
      address indexed to
  );
  event Sync(uint112 reserve0, uint112 reserve1);

  function MINIMUM_LIQUIDITY() external pure returns (uint);
  function factory() external view returns (address);
  function token0() external view returns (address);
  function token1() external view returns (address);
  function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
  function price0CumulativeLast() external view returns (uint);
  function price1CumulativeLast() external view returns (uint);
  function kLast() external view returns (uint);

  function mint(address to) external returns (uint liquidity);
  function burn(address to) external returns (uint amount0, uint amount1);
  function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
  function skim(address to) external;
  function sync() external;
}

interface FluidExtension {
    function delegated_on_transfer(address _from,
                                   address _to, 
                                   uint quantity, 
                                   bool is_buy, 
                                   bool is_sell, 
                                   bool is_transfer) external returns(bool skip);
    
    function delegated_on_nft_transfer(address _from, address _to, uint id) external returns(bool skip);

    function delegated_on_minting(address _from, uint quantity, uint starting_id) external returns(bool skip);
}

interface Fluid {

    function get_luck(address recipient) external view returns (uint luck);

    function stake_tokens(uint quantity) external returns (uint slot);
    function unstake_tokens(uint stake_id) external;

    function set_nft_rarity(uint id, uint rarity) external;
    function get_nft_rarity(uint id) external view returns(uint rarity);
    function get_nft_onchain_attributes(uint id) external view returns(bytes32[] memory);

    function get_stake_status(address actor) external view returns(uint total, bool is_it);

    function on_transfer(address _from, address _to, uint quantity, bool is_buy, bool is_sell, bool is_transfer) external;

    function on_nft_transfer(address _from, address _to, uint id) external;
    function on_nft_minting(address _from, uint quantity, uint start_token) external;

    function set_probabilities(uint _rare, uint _particular, uint _uncommon) external;
    function get_probabilities() external view returns(uint rare_, uint particular_, uint uncommon_, uint common_);
    function get_all_lucks() external view returns(uint all_lucks);

    function set_luck(address recipient, uint _luck) external;
    function liquid_extractor(address actor) external view returns(uint rarity);

}