// File: src/Sponsor.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "./TimeExchange.sol";

/// @title Sponsor: a TIME Token Finance smart contract to relay and monitor transactions that generate value on the platform; it rewards users accordingly, which means users' transactions are being sponsored
/// @author https://timetoken.finance
/// @notice Reward is given in the network's native token, as recurrent prizes, which are granted according to users' activity frequency and their transacted amount.
/// Please refer that interacting with this contract is solely at your own risk!
/// Also, the administrator reserves the right to claim all funds from this contract at any time at their own discretion, once they deposit their own funds here...
/// There are other ways of interacting and trading with the TIME Token Finance platform, either through the TimeExchange contract or directly through the TimeToken and TimeIsUp contracts.
/// We encourage everyone who wishes to do so if they feel uncomfortable with the functions of this contract.
contract Sponsor {
    using Math for uint256;

    enum OperationType {
        MINT,
        SWAP_TIME,
        SWAP_TUP
    }

    struct Participant {
        bool isParticipating;
        bool wasSelected;
        address previousParticipant;
        address nextParticipant;
        mapping(uint256 => uint256) interactionPoints;
        mapping(uint256 => uint256) valuePoints;
    }

    struct Winners {
        address first;
        address second;
        address third;
        address fourth;
    }

    event ParticipantAdded(address participant);
    event ParticipantRemoved(address participant);
    event ParticipantsListCleaned();
    event RoundWinner(uint256 round, address participant, uint256 earnedPrize);

    bool private _canReceiveAdditionalFunds = true;
    bool private _isOperationLocked;

    bool public isOperationTypeFlipped;

    TimeExchange public immutable timeExchange;
    ITimeToken public immutable timeToken;
    ITimeIsUp public immutable tupToken;

    address public administrator;
    address public currentLeader;
    address public firstParticipant;
    address public lastParticipant;

    uint256 private constant D = 10 ** 18;
    uint256 public constant TIME_BURNING_RATE = 5_000;
    uint256 public constant MININUM_NUMBER_OF_PARTICIPANTS = 5;
    uint256 public constant NUMBER_OF_SELECTED_WINNERS = 4;

    uint256 private CURRENT_FEES_PERCENTAGE;
    uint256 private PERCENTAGE_PROFIT_TARGET = 11_000;
    uint256 private REBATE_PERCENTAGE = 100;

    uint256 public accumulatedPrize;
    uint256 public currentAdditionalPrize;
    uint256 public currentPrize;
    uint256 public currentRebate;
    uint256 public currentTarget;
    uint256 public currentValueMoved;
    uint256 public maxInteractionPoints;
    uint256 public minAmountToEarnPoints;
    uint256 public numberOfParticipants;
    uint256 public round;

    mapping(address => uint256) private _currentBlock;

    mapping(address => uint256) public lastBlock;
    mapping(address => uint256) public prizeToClaim;
    mapping(address => uint256) public remainingTime;

    mapping(uint256 => Winners winners) public roundWinners;

    mapping(address => Participant participant) public participants;

    constructor(address timeTokenAddress, address tupTokenAddress, address timeExchangeAddress) {
        administrator = msg.sender;
        timeToken = ITimeToken(payable(timeTokenAddress));
        tupToken = ITimeIsUp(payable(tupTokenAddress));
        timeExchange = TimeExchange(payable(timeExchangeAddress));
        setCurrentFeesPercentage(timeExchange.FEE());
        setMinAmountToEarnPoints(0.1 ether);
        round = 1;
    }

    receive() external payable {
        _depositPrize(0, msg.value);
    }

    fallback() external payable {
        require(msg.data.length == 0);
        _depositPrize(0, msg.value);
    }

    /// @notice Modifier to allow only administrator access
    modifier admin() {
        require(msg.sender == administrator, "Sponsor: only admin allowed");
        _;
    }

    /// @notice Modifier to allow registering additional funds to prize when receiveing native token of the network
    modifier canReceiveAdditional() {
        _canReceiveAdditionalFunds = true;
        _;
        _canReceiveAdditionalFunds = false;
    }

    /// @notice Modifier to disallow registering additional funds to prize when receiveing native token of the network
    modifier cannotReceiveAdditional() {
        _canReceiveAdditionalFunds = false;
        _;
        _canReceiveAdditionalFunds = true;
    }

    /// @notice Verifies if the current round can be finished
    modifier checkEndOfRound() {
        _;
        if (queryAmountRemainingForPrize() == 0 && currentPrize > 0 && numberOfParticipants >= MININUM_NUMBER_OF_PARTICIPANTS) {
            _rewardWinnersAndCloseRound();
        }
    }

    /// @notice Modifier to make a function runs only once per block
    modifier onlyOncePerBlock() {
        require(block.number != _currentBlock[tx.origin], "Sponsor: you cannot perform this operation again in this block");
        _currentBlock[tx.origin] = block.number;
        _;
    }

    /// @notice Implement security to avoid reentrancy attacks
    modifier nonReentrant() {
        require(!_isOperationLocked, "Sponsor: this operation is locked");
        _isOperationLocked = true;
        _;
        _isOperationLocked = false;
    }

    /// @notice Modifier used when the status of the current participant should be checked and updated
    /// @dev Called together with mint(), swap(), and extendParticipationPeriod() functions
    modifier update() {
        if ((lastBlock[msg.sender] == 0 && block.number != 0) || remainingTime[msg.sender] == 0) {
            lastBlock[msg.sender] = block.number;
        }
        uint256 elapsedTime = block.number - lastBlock[msg.sender];
        if (elapsedTime > remainingTime[msg.sender] && participants[msg.sender].isParticipating) {
            _removeParticipant(msg.sender);
        }
        _;
        remainingTime[msg.sender] = (elapsedTime > remainingTime[msg.sender]) ? 0 : remainingTime[msg.sender] - elapsedTime;
        lastBlock[msg.sender] = block.number;
        if (remainingTime[msg.sender] >= elapsedTime && remainingTime[msg.sender] > 0 && !participants[msg.sender].isParticipating) {
            _addParticipant(msg.sender);
        }
    }

    //
    /// @notice Add a participant and adjusts the participants chained list
    /// @dev The current participant added is sent to the end of the list
    /// @param participant The address of the participant
    function _addParticipant(address participant) private {
        if (participant != address(0) && !participants[participant].isParticipating) {
            participants[participant].previousParticipant = lastParticipant;
            participants[participant].nextParticipant = address(0);
            if (firstParticipant == address(0)) {
                firstParticipant = participant;
            }
            if (lastParticipant != address(0)) {
                participants[lastParticipant].nextParticipant = participant;
            }
            lastParticipant = participant;
            participants[participant].isParticipating = true;
            numberOfParticipants++;
            emit ParticipantAdded(participant);
        }
    }

    /// @notice Burn some TIME tokens in order to regulate the market inflation
    /// @dev It runs with try { } catch to not revert in case of being unsuccessful
    function _burnTime() private {
        uint256 balanceInTime = timeToken.balanceOf(address(this));
        if (balanceInTime > 0) {
            try timeToken.burn(balanceInTime.mulDiv(TIME_BURNING_RATE, 10_000)) { } catch { }
        }
    }

    /// @notice Resets the entire list of participants in the contract
    /// @dev It also should clean the participants' score/pontuation, if the case
    function _cleanParticipantsList() private {
        address currentParticipant = firstParticipant;
        address nextParticipant;
        while (currentParticipant != address(0)) {
            nextParticipant = participants[currentParticipant].nextParticipant;
            participants[currentParticipant].previousParticipant = address(0);
            participants[currentParticipant].nextParticipant = address(0);
            participants[currentParticipant].isParticipating = false;
            participants[currentParticipant].wasSelected = false;
            participants[currentParticipant].interactionPoints[round] = 0;
            participants[currentParticipant].valuePoints[round] = 0;
            remainingTime[currentParticipant] = 0;
            lastBlock[currentParticipant] = block.number;
            currentParticipant = nextParticipant;
        }
        firstParticipant = address(0);
        lastParticipant = address(0);
        numberOfParticipants = 0;
        emit ParticipantsListCleaned();
    }

    /// @notice Deposits the resources used as prizes (main and additional)
    /// @dev Indirectly called by admins and third parties (as additional deposit)
    /// @param amount The main deposit amount
    /// @param additionalAmount The additional deposit amount
    function _depositPrize(uint256 amount, uint256 additionalAmount) private {
        require(msg.value > 0, "Sponsor: please deposit some amount");
        if (_canReceiveAdditionalFunds) {
            if (amount > 0) {
                accumulatedPrize += amount;
                if (currentPrize == 0) {
                    currentPrize = accumulatedPrize / 2;
                    currentTarget = (currentPrize + currentPrize.mulDiv(PERCENTAGE_PROFIT_TARGET, 10_000)).mulDiv(10_000, CURRENT_FEES_PERCENTAGE);
                }
            }
            if (additionalAmount > 0) {
                uint256 halfAdditionalAmount = (additionalAmount / 2);
                accumulatedPrize += additionalAmount;
                currentAdditionalPrize += halfAdditionalAmount;
                currentRebate += halfAdditionalAmount.mulDiv(REBATE_PERCENTAGE, 10_000);
            }
        }
    }

    /// @notice Receives additional resources from the TIME and TUP contracts
    /// @dev It calls the TUP contract indirectly, using Claimer
    function _earnAdditionalResources() private canReceiveAdditional {
        if (timeToken.withdrawableShareBalance(address(this)) > 0) {
            try timeToken.withdrawShare() { } catch { }
        }
        if (tupToken.queryPublicReward() > 0) {
            try tupToken.splitSharesWithReward() { } catch { }
        }
    }

    /// @notice Calculates and storage the received points of a participant, but only if the negotiated amount is above the minimum established
    /// @dev It classifies operations according to its relevance for the protocol
    /// @param participant The address of participant to register points
    /// @param operation Type of the operation called by a participant and relayed to the TimeExchange contract
    /// @param amount The amount of native tokens moved by the participant
    function _registerPoints(address participant, OperationType operation, uint256 amount) private {
        if (amount >= minAmountToEarnPoints && participant != address(0)) {
            uint256 weight = isOperationTypeFlipped ? (uint256(type(OperationType).max) - uint256(operation)) + 1 : uint256(operation) + 1;
            participants[participant].interactionPoints[round] += weight;
            participants[participant].valuePoints[round] += (amount * weight);
            if (participants[participant].interactionPoints[round] > maxInteractionPoints) {
                maxInteractionPoints = participants[participant].interactionPoints[round];
                currentLeader = participant;
            }
        }
    }

    /// @notice Remove a participant and adjusts the participants chained list of the contract
    /// @dev It concatenates the right next participant of the current participant with the previous one
    /// @param participant The address of a participant which will be removed of the chained list
    function _removeParticipant(address participant) private {
        if (participant != address(0) && participants[participant].isParticipating) {
            address previousParticipant = participants[participant].previousParticipant;
            address nextParticipant = participants[participant].nextParticipant;
            if (lastParticipant == participant) {
                lastParticipant = previousParticipant;
            }
            if (firstParticipant == participant) {
                firstParticipant = nextParticipant;
            }
            if (previousParticipant != address(0)) {
                participants[previousParticipant].nextParticipant = nextParticipant;
            }
            if (nextParticipant != address(0)) {
                participants[nextParticipant].previousParticipant = previousParticipant;
            }
            if (participant == currentLeader) {
                currentLeader = address(0);
                maxInteractionPoints = 0;
            }
            participants[participant].previousParticipant = address(0);
            participants[participant].nextParticipant = address(0);
            participants[participant].isParticipating = false;
            participants[participant].wasSelected = false;
            participants[participant].interactionPoints[round] = 0;
            participants[participant].valuePoints[round] = 0;
            numberOfParticipants--;
            emit ParticipantRemoved(participant);
        }
    }

    /// @notice Performs the reward and close the current round
    /// @dev Another round is automatically initiated
    function _rewardWinnersAndCloseRound() private nonReentrant {
        uint256 totalPrize = queryCurrentTotalPrize();
        if (totalPrize <= address(this).balance) {
            uint256 prizeShares = totalPrize / 10;
            if (!checkParticipation(currentLeader)) {
                _removeParticipant(currentLeader);
                if (numberOfParticipants < MININUM_NUMBER_OF_PARTICIPANTS) {
                    return;
                }
                currentLeader = _selectRandomWinner();
            } else {
                participants[currentLeader].wasSelected = true;
            }
            uint256 earnedPrize = (NUMBER_OF_SELECTED_WINNERS * prizeShares);
            prizeToClaim[currentLeader] += earnedPrize;
            emit RoundWinner(round, currentLeader, earnedPrize);
            address[] memory winners = _selectRoundWinners();
            for (uint256 i = 0; i < winners.length; i++) {
                earnedPrize = (winners.length - i) * prizeShares;
                prizeToClaim[winners[i]] += earnedPrize;
                emit RoundWinner(round, winners[i], earnedPrize);
            }
            roundWinners[round].first = currentLeader;
            roundWinners[round].second = winners[0];
            roundWinners[round].third = winners[1];
            roundWinners[round].fourth = winners[2];
            accumulatedPrize -= totalPrize;
            currentPrize = accumulatedPrize / 2;
            currentTarget = currentPrize > 0 ? (currentPrize + currentPrize.mulDiv(PERCENTAGE_PROFIT_TARGET, 10_000)).mulDiv(10_000, CURRENT_FEES_PERCENTAGE) : 0;
            currentAdditionalPrize = 0;
            currentRebate = 0;
            currentValueMoved = 0;
            _burnTime();
            _cleanParticipantsList();
            maxInteractionPoints = 0;
            currentLeader = address(0);
            round++;
        }
    }

    /// @notice Picks a randomly selected winner
    /// @dev It traverses the whole chained list of participants to pick those who match with a random index number. It marks the winner from that list right after the selection
    /// @return randomWinner The address of the picked random winner
    function _selectRandomWinner() private returns (address randomWinner) {
        uint256 nonce = 1;
        while (randomWinner == address(0)) {
            address currentParticipant = firstParticipant;
            uint256 randomWinnerIndex = uint256(
                keccak256(
                    abi.encode(
                        nonce++, block.number, block.timestamp, block.prevrandao, address(this), currentPrize, currentValueMoved, currentTarget
                    )
                )
            ) % numberOfParticipants;
            uint256 currentIndex;
            while (currentParticipant != address(0)) {
                if (currentIndex == randomWinnerIndex && checkParticipation(currentParticipant)) {
                    if (participants[currentParticipant].wasSelected) {
                        if (randomWinnerIndex == numberOfParticipants - 1) {
                            currentIndex = 0;
                            randomWinnerIndex = 0;
                            currentParticipant = firstParticipant;
                        } else {
                            randomWinnerIndex++;
                        }
                        continue;
                    }
                    participants[currentParticipant].wasSelected = true;
                    randomWinner = currentParticipant;
                    break;
                }
                currentIndex++;
                currentParticipant = participants[currentParticipant].nextParticipant;
            }
        }
    }

    /// @notice Select the round winners with the highest value points earned. The last winner is randomly selected
    /// @dev It traverses the whole chained list of participants to pick those with the highest earned points. It marks the winners from that list right after the selection
    /// @return winners Array containing addresses of the round winners
    function _selectRoundWinners() private returns (address[] memory winners) {
        winners = new address[](NUMBER_OF_SELECTED_WINNERS - 1);
        uint256[] memory maxPoint = new uint256[](winners.length);
        address currentParticipant;
        address nextParticipant;
        for (uint256 i = 0; i < winners.length - 1; i++) {
            currentParticipant = firstParticipant;
            while (currentParticipant != address(0)) {
                nextParticipant = participants[currentParticipant].nextParticipant;
                if (
                    participants[currentParticipant].valuePoints[round] > maxPoint[i] && checkParticipation(currentParticipant)
                        && !participants[currentParticipant].wasSelected
                ) {
                    maxPoint[i] = participants[currentParticipant].valuePoints[round];
                    winners[i] = currentParticipant;
                }
                currentParticipant = nextParticipant;
            }
            // If still has no winner, it picks randomly
            if (winners[i] == address(0)) {
                winners[i] = _selectRandomWinner();
            }
            participants[winners[i]].wasSelected = true;
        }
        // It now picks a random winner as the last winner
        winners[winners.length - 1] = _selectRandomWinner();
        return winners;
    }

    /// @notice Withdraws the total prize received by the sponsors
    /// @dev Called by a user after receiveing a prize for his address. The prize remains in the contract for an indefinite time. Non reentrant
    function claimPrize() external nonReentrant {
        require(prizeToClaim[msg.sender] > 0, "Sponsor: there is no prize to claim for the caller address");
        require(prizeToClaim[msg.sender] <= address(this).balance, "Sponsor: there is no enough amount to withdraw");
        uint256 amount = prizeToClaim[msg.sender];
        prizeToClaim[msg.sender] = 0;
        payable(msg.sender).transfer(amount);
    }

    /// @notice Informs if a participant is active in the current sponsorship round
    /// @dev It must check if the participant also has enough TIME to be consumed
    /// @param participant The participant's address
    /// @return isParticipating If the informed address is participating in the sponsorship round indeed
    function checkParticipation(address participant) public view returns (bool) {
        return ((remainingTime[participant] >= (block.number - lastBlock[participant])) && participants[participant].isParticipating);
    }

    /// @notice Deposit the main contract prize (Usually called by the sponsors - admins). The contract can receive additional funds directly from other sources
    /// @dev Only admins can call this function. It calls a private function by chaging the order of parameters, meaning that it is the main deposit
    function depositPrize() external payable admin {
        _depositPrize(msg.value, 0);
    }

    /// @notice Withdraws all the funds of the contract to the administrator address and restarts the contract variables
    /// @dev Only admin can call this function. For emergency only!
    function emergencyWithdraw() external admin {
        _cleanParticipantsList();
        accumulatedPrize = 0;
        currentAdditionalPrize = 0;
        currentPrize = 0;
        currentRebate = 0;
        currentTarget = 0;
        currentValueMoved = 0;
        currentLeader = address(0);
        maxInteractionPoints = 0;
        uint256 tupBalance = tupToken.balanceOf(address(this));
        if (tupBalance > 0) {
            try tupToken.transfer(administrator, tupBalance) { } catch { }
        }
        try timeToken.transfer(administrator, timeToken.balanceOf(address(this))) { } catch { }
        payable(administrator).transfer(address(this).balance);
    }

    /// @notice Enables participation in the sponsorship by depositing TIME
    /// @dev User must approve TIME to be spent before calling this function
    /// @param amountTime The amount of TIME the user would like to deposit in order to participate the current sponsorship round
    function extendParticipationPeriod(uint256 amountTime) external cannotReceiveAdditional checkEndOfRound update {
        require(
            timeToken.allowance(msg.sender, address(this)) >= amountTime, "Sponsor: please approve the TIME amount to extend your sponsorship period"
        );
        require(amountTime >= D, "Sponsor: you should deposit 1 TIME or more to extend your sponsorship period");
        timeToken.transferFrom(msg.sender, address(this), amountTime);
        remainingTime[msg.sender] += amountTime.mulDiv(1, D);
    }

    /// @notice Flip the operation type so the most value operation turns into the lesser one, and vice-versa
    /// @dev The isOperationTypeFlipped is used in _registerPoints() function to adjust the weight applied over the operation number
    function flipOperationType() external admin {
        isOperationTypeFlipped = !isOperationTypeFlipped;
    }

    /// @notice Performs mint of TUP tokens by demand
    /// @dev Relays the mint function to the TimeIsUp contract, but observing and registering additional information
    /// @param amountTime The amount of TIME the user wants to use to mint TUP
    function mint(uint256 amountTime) external payable cannotReceiveAdditional checkEndOfRound onlyOncePerBlock update {
        require(
            timeToken.allowance(msg.sender, address(this)) >= amountTime, "Sponsor: you should allow TIME to be spent before calling the function"
        );
        if (amountTime > 0) {
            timeToken.transferFrom(msg.sender, address(this), amountTime);
            timeToken.approve(address(tupToken), amountTime);
        }
        uint256 balanceBefore = tupToken.balanceOf(address(this));
        try tupToken.mint{ value: msg.value }(amountTime) {
            currentValueMoved += msg.value;
            uint256 balanceAfter = tupToken.balanceOf(address(this));
            tupToken.transfer(msg.sender, balanceAfter - balanceBefore);
            if (checkParticipation(msg.sender) && amountTime == 0) {
                _registerPoints(msg.sender, OperationType.MINT, msg.value);
                _earnAdditionalResources();
            }
        } catch {
            revert("Sponsor: unable to relay mint");
        }
    }

    /// @notice Query for the amount needed to unlock the prize for round winners
    /// @return amountRemaining The amount which is remaining to achieve the current target for prize unlock
    function queryAmountRemainingForPrize() public view returns (uint256) {
        return currentTarget > (currentRebate + currentValueMoved) ? currentTarget - (currentRebate + currentValueMoved) : 0;
    }

    /// @notice Query for the prize amount deposited by admins and earned by the contract
    /// @return totalPrize Current prize + additional prize earned
    function queryCurrentTotalPrize() public view returns (uint256) {
        return (currentPrize + currentAdditionalPrize);
    }

    /// @notice Query for the interaction points of a participant
    /// @return interactionPoints Informs the number of interactions (times an operation weight) a given user address interacted with the Sponsor contract
    function queryInteractionPoints(address participant) external view returns (uint256) {
        return checkParticipation(participant) ? participants[participant].interactionPoints[round] : 0;
    }

    /// @notice Query for the value points of a participant
    /// @return valuePoints Informs the amount of native tokens (times an operation weight) a given user address negotiated with the Sponsor contract
    function queryValuePoints(address participant) external view returns (uint256) {
        return checkParticipation(participant) ? participants[participant].valuePoints[round] : 0;
    }

    /// @notice Changes the new administrator address
    /// @dev Only the owner of the contract can access
    /// @param newAdministrator The new address value to be set.
    function setAdministrator(address newAdministrator) public admin {
        administrator = newAdministrator;
    }

    /// @notice Adjusts the new fees percentage charged by the protocol
    /// @dev Only the owner of the contract can access
    /// @param newCurrentFeesPercentage The new value to be set. Factor of 10_000. Example: if percentage is 1%, it should be set as 100
    function setCurrentFeesPercentage(uint256 newCurrentFeesPercentage) public admin {
        CURRENT_FEES_PERCENTAGE = newCurrentFeesPercentage;
    }

    /// @notice Adjusts the minimum amount a participant should trade to earn points in the protocol
    /// @dev Only the owner of the contract can access
    /// @param newMinAmount The new value to be set. It should be defined in terms of the native token of the underlying network
    function setMinAmountToEarnPoints(uint256 newMinAmount) public admin {
        minAmountToEarnPoints = newMinAmount;
    }

    /// @notice Adjusts the percentage of the desirable profit
    /// @dev Only the owner of the contract can access
    /// @param newProfitTargetPercentage The new value to be set. Factor of 10_000. Example: if percentage is 1%, it should be set as 100
    function setPercentageProfitTarget(uint256 newProfitTargetPercentage) external admin {
        PERCENTAGE_PROFIT_TARGET = newProfitTargetPercentage;
    }

    /// @notice Adjusts the percentage of rebates to achieve prize targets
    /// @dev Only the owner of the contract can access
    /// @param newRebatePercentage The new value to be set. Factor of 10_000. Example: if percentage is 1%, it should be set as 100
    function setRebatePercentage(uint256 newRebatePercentage) external admin {
        REBATE_PERCENTAGE = newRebatePercentage;
    }

    /// @notice It relay swaps to TimeExchange contract, but register points only for native currency to another token
    /// @dev It should inform address(0) as tokenFrom or tokenTo when considering native currency
    /// @param tokenFrom The address of the token to be swapped
    /// @param tokenTo The address of the token to be swapped
    /// @param amount The token or native currency amount to be swapped
    function swap(address tokenFrom, address tokenTo, uint256 amount)
        external
        payable
        cannotReceiveAdditional
        checkEndOfRound
        onlyOncePerBlock
        update
    {
        IERC20 tokenToTransfer;
        uint256 balanceBefore;
        if (tokenFrom != address(0)) {
            IERC20 tokenFromTransfer = IERC20(tokenFrom);
            require(tokenFromTransfer.allowance(msg.sender, address(this)) >= amount, "Sponsor: please approve the amount to swap");
            tokenFromTransfer.transferFrom(msg.sender, address(this), amount);
            tokenFromTransfer.approve(address(timeExchange), amount);
        }
        if (tokenTo != address(0)) {
            tokenToTransfer = IERC20(tokenTo);
            balanceBefore = tokenToTransfer.balanceOf(address(this));
        } else {
            balanceBefore = address(this).balance;
        }
        try timeExchange.swap{ value: msg.value }(tokenFrom, tokenTo, amount) {
            uint256 balanceAfter;
            if (tokenTo != address(0)) {
                balanceAfter = tokenToTransfer.balanceOf(address(this));
                tokenToTransfer.transfer(msg.sender, balanceAfter - balanceBefore);
            } else {
                balanceAfter = address(this).balance;
                payable(msg.sender).transfer(balanceAfter - balanceBefore);
            }
            if (tokenFrom == address(0)) {
                if (checkParticipation(msg.sender)) {
                    _registerPoints(msg.sender, tokenTo == address(timeToken) ? OperationType.SWAP_TIME : OperationType.SWAP_TUP, msg.value);
                    _earnAdditionalResources();
                }
                currentValueMoved += msg.value;
            }
        } catch {
            revert("Sponsor: unable to relay swap");
        }
    }

    /// @notice Withdraw native tokens given to address zero
    /// @dev Eventually the winners don't have remaining TIME deposited when a round is finished. When this happens, the prize goes to the address(0)
    function withdrawFromAddressZeroPrizes() external admin {
        require(prizeToClaim[address(0)] > 0, "Sponsor: there is no prize to claim for the zero address");
        require(prizeToClaim[address(0)] <= address(this).balance, "Sponsor: there is no enough amount to withdraw");
        uint256 amount = prizeToClaim[address(0)];
        prizeToClaim[address(0)] = 0;
        payable(administrator).transfer(amount);
    }
}

// File: src/TimeExchange.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "./ITimeToken.sol";
import "./ITimeIsUp.sol";

contract TimeExchange {
    using Math for uint256;

    uint256 private constant FACTOR = 10 ** 18;

    uint256 public constant FEE = 70;
    address public constant DEVELOPER_ADDRESS = 0x731591207791A93fB0Ec481186fb086E16A7d6D0;
    address public immutable timeAddress;
    address public immutable tupAddress;

    mapping(address => uint256) private _currentBlock;

    constructor(address time, address tup) {
        timeAddress = time;
        tupAddress = tup;
    }

    receive() external payable { }

    fallback() external payable {
        require(msg.data.length == 0);
    }

    /// @notice Modifier to make a function runs only once per block
    modifier onlyOncePerBlock() {
        require(block.number != _currentBlock[tx.origin], "Time Exchange: you cannot perform this operation again in this block");
        _currentBlock[tx.origin] = block.number;
        _;
    }

    /// @notice Swaps native currency for another token
    /// @dev Please refer this function is called by swap() function
    /// @param tokenTo The address of the token to be swapped
    /// @param amount The native currency amount to be swapped
    function _swapFromNativeToToken(address tokenTo, uint256 amount) private {
        IERC20 token = IERC20(tokenTo);
        uint256 comission = amount.mulDiv(FEE, 10_000);
        amount -= comission;
        payable(tokenTo).call{ value: amount }("");
        payable(DEVELOPER_ADDRESS).call{ value: comission / 2 }("");
        ITimeIsUp(payable(tupAddress)).receiveProfit{ value: comission / 2 }();
        token.transfer(msg.sender, token.balanceOf(address(this)));
    }

    /// @notice Swaps token for native currency
    /// @dev Please refer this function is called by swap() function
    /// @param tokenFrom The address of the token to be swapped
    /// @param amount The token amount to be swapped
    function _swapFromTokenToNative(address tokenFrom, uint256 amount) private {
        IERC20 token = IERC20(tokenFrom);
        token.transferFrom(msg.sender, address(this), amount);
        uint256 balanceBefore = address(this).balance;
        token.transfer(tokenFrom, amount);
        uint256 balanceAfter = address(this).balance - balanceBefore;
        uint256 comission = balanceAfter.mulDiv(FEE, 10_000);
        balanceAfter -= comission;
        payable(DEVELOPER_ADDRESS).call{ value: comission / 2 }("");
        ITimeIsUp(payable(tupAddress)).receiveProfit{ value: comission / 2 }();
        payable(msg.sender).call{ value: balanceAfter }("");
    }

    /// @notice Swaps a token for another token
    /// @dev Please refer this function is called by swap() function
    /// @param tokenFrom The address of the token to be swapped
    /// @param tokenTo The address of the token to be swapped
    /// @param amount The token amount to be swapped
    function _swapFromTokenToToken(address tokenFrom, address tokenTo, uint256 amount) private {
        IERC20 tokenFrom_ = IERC20(tokenFrom);
        IERC20 tokenTo_ = IERC20(tokenTo);
        tokenFrom_.transferFrom(msg.sender, address(this), amount);
        uint256 balanceBefore = address(this).balance;
        tokenFrom_.transfer(tokenFrom, amount);
        uint256 balanceAfter = address(this).balance - balanceBefore;
        uint256 comission = balanceAfter.mulDiv(FEE, 10_000);
        balanceAfter -= comission;
        payable(tokenTo).call{ value: balanceAfter }("");
        payable(DEVELOPER_ADDRESS).call{ value: comission / 2 }("");
        ITimeIsUp(payable(tupAddress)).receiveProfit{ value: comission / 2 }();
        tokenTo_.transfer(msg.sender, tokenTo_.balanceOf(address(this)));
    }

    /// @notice Query the price of native currency in terms of an informed token
    /// @dev Please refer this function is called by queryPrice() function and it is only for viewing
    /// @param tokenTo The address of the token to be queried
    /// @param amount The native currency amount to be queried
    /// @return price The price of tokens to be obtained given some native currency amount
    function _queryPriceFromNativeToToken(address tokenTo, uint256 amount) private view returns (uint256) {
        uint256 price;
        if (tokenTo == timeAddress) {
            price = ITimeToken(payable(tokenTo)).swapPriceNative(amount);
        } else {
            price = ITimeIsUp(payable(tokenTo)).queryPriceNative(amount);
        }
        return price;
    }

    /// @notice Query the price of an informed token in terms of native currency
    /// @dev Please refer this function is called by queryPrice() function and it is only for viewing
    /// @param tokenFrom The address of the token to be queried
    /// @param amount The token amount to be queried
    /// @return price The price of native currency to be obtained given some token amount
    function _queryPriceFromTokenToNative(address tokenFrom, uint256 amount) private view returns (uint256) {
        uint256 price;
        if (tokenFrom == timeAddress) {
            price = ITimeToken(payable(tokenFrom)).swapPriceTimeInverse(amount);
        } else {
            price = ITimeIsUp(payable(tokenFrom)).queryPriceInverse(amount);
        }
        return price;
    }

    /// @notice Query the price of an informed token in terms of another informed token
    /// @dev Please refer this function is called by queryPrice() function and it is only for viewing
    /// @param tokenFrom The address of the token to be queried
    /// @param tokenTo The address of the token to be queried
    /// @param amount The token amount to be queried
    /// @return priceTo The price of tokens to be obtained given some another token amount
    /// @return nativeAmount The amount in native currency obtained from the query
    function _queryPriceFromTokenToToken(address tokenFrom, address tokenTo, uint256 amount)
        private
        view
        returns (uint256 priceTo, uint256 nativeAmount)
    {
        uint256 priceFrom = _queryPriceFromTokenToNative(tokenFrom, amount);
        nativeAmount = amount.mulDiv(priceFrom, FACTOR);
        if (tokenTo == timeAddress) {
            priceTo = ITimeToken(payable(tokenTo)).swapPriceNative(nativeAmount);
        } else {
            priceTo = ITimeIsUp(payable(tokenTo)).queryPriceNative(nativeAmount);
        }
        return (priceTo, nativeAmount);
    }

    /// @notice Clean the contract if it has any exceeding token or native amount
    /// @dev It should pass the tokenToClean contract address
    /// @param tokenToClean The address of token contract
    function clean(address tokenToClean) public {
        if (address(this).balance > 0) {
            payable(DEVELOPER_ADDRESS).call{ value: address(this).balance }("");
        }
        if (tokenToClean != address(0)) {
            if (IERC20(tokenToClean).balanceOf(address(this)) > 0) {
                IERC20(tokenToClean).transfer(DEVELOPER_ADDRESS, IERC20(tokenToClean).balanceOf(address(this)));
            }
        }
    }

    /// @notice Swaps token or native currency for another token or native currency
    /// @dev It should inform address(0) as tokenFrom or tokenTo when considering native currency
    /// @param tokenFrom The address of the token to be swapped
    /// @param tokenTo The address of the token to be swapped
    /// @param amount The token or native currency amount to be swapped
    function swap(address tokenFrom, address tokenTo, uint256 amount) external payable onlyOncePerBlock {
        if (tokenFrom == address(0)) {
            require(tokenTo != address(0) && (tokenTo == timeAddress || tokenTo == tupAddress), "Time Exchange: unallowed token");
            require(msg.value > 0, "Time Exchange: please inform the amount to swap");
            _swapFromNativeToToken(tokenTo, msg.value);
            clean(tokenFrom);
            clean(tokenTo);
        } else if (tokenTo == address(0)) {
            require(amount > 0, "Time Exchange: please inform the amount to swap");
            require(tokenFrom == timeAddress || tokenFrom == tupAddress, "Time Exchange: unallowed token");
            require(IERC20(tokenFrom).allowance(msg.sender, address(this)) >= amount, "Time Exchange: please approve the amount to swap");
            _swapFromTokenToNative(tokenFrom, amount);
            clean(tokenFrom);
            clean(tokenTo);
        } else {
            require(amount > 0, "Time Exchange: please inform the amount to swap");
            require(tokenTo == timeAddress || tokenTo == tupAddress, "Time Exchange: unallowed token");
            require(tokenFrom == timeAddress || tokenFrom == tupAddress, "Time Exchange: unallowed token");
            require(IERC20(tokenFrom).allowance(msg.sender, address(this)) >= amount, "Time Exchange: please approve the amount to swap");
            _swapFromTokenToToken(tokenFrom, tokenTo, amount);
            clean(tokenFrom);
            clean(tokenTo);
        }
    }

    /// @notice Query the price of token or native currency in terms of another token or native currency
    /// @dev It should inform address(0) as tokenFrom or tokenTo when considering native currency
    /// @param tokenFrom The address of the token to be queried
    /// @param tokenTo The address of the token to be queried
    /// @param amount The token or native currency amount to be queried
    /// @return price The queried price
    /// @return nativeAmount The native amount in the middle of the query (if the case)
    function queryPrice(address tokenFrom, address tokenTo, uint256 amount) external view returns (uint256, uint256) {
        if (tokenFrom == address(0)) {
            require(tokenTo != address(0) && (tokenTo == timeAddress || tokenTo == tupAddress), "Time Exchange: unallowed token");
            return (_queryPriceFromNativeToToken(tokenTo, amount), 0);
        } else if (tokenTo == address(0)) {
            require(tokenFrom == timeAddress || tokenFrom == tupAddress, "Time Exchange: unallowed token");
            return (_queryPriceFromTokenToNative(tokenFrom, amount), 0);
        } else {
            require(tokenTo == timeAddress || tokenTo == tupAddress, "Time Exchange: unallowed token");
            require(tokenFrom == timeAddress || tokenFrom == tupAddress, "Time Exchange: unallowed token");
            return _queryPriceFromTokenToToken(tokenFrom, tokenTo, amount);
        }
    }
}

// File: lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

// File: lib/openzeppelin-contracts/contracts/utils/math/Math.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)

pragma solidity ^0.8.20;

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    /**
     * @dev Muldiv operation overflow.
     */
    error MathOverflowedMulDiv();

    enum Rounding {
        Floor, // Toward negative infinity
        Ceil, // Toward positive infinity
        Trunc, // Toward zero
        Expand // Away from zero
    }

    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, with an overflow flag.
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return a > b ? a : b;
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow.
        return (a & b) + (a ^ b) / 2;
    }

    /**
     * @dev Returns the ceiling of the division of two numbers.
     *
     * This differs from standard division with `/` in that it rounds towards infinity instead
     * of rounding towards zero.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        if (b == 0) {
            // Guarantee the same behavior as in a regular Solidity division.
            return a / b;
        }

        // (a + b - 1) / b can overflow on addition, so we distribute.
        return a == 0 ? 0 : (a - 1) / b + 1;
    }

    /**
     * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
     * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
     * with further edits by Uniswap Labs also under MIT license.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
        unchecked {
            // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
            // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
            // variables such that product = prod1 * 2^256 + prod0.
            uint256 prod0 = x * y; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(x, y, not(0))
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

            // Handle non-overflow cases, 256 by 256 division.
            if (prod1 == 0) {
                // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                // The surrounding unchecked block does not change this fact.
                // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                return prod0 / denominator;
            }

            // Make sure the result is less than 2^256. Also prevents denominator == 0.
            if (denominator <= prod1) {
                revert MathOverflowedMulDiv();
            }

            ///////////////////////////////////////////////
            // 512 by 256 division.
            ///////////////////////////////////////////////

            // Make division exact by subtracting the remainder from [prod1 prod0].
            uint256 remainder;
            assembly {
                // Compute remainder using mulmod.
                remainder := mulmod(x, y, denominator)

                // Subtract 256 bit number from 512 bit number.
                prod1 := sub(prod1, gt(remainder, prod0))
                prod0 := sub(prod0, remainder)
            }

            // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
            // See https://cs.stackexchange.com/q/138556/92363.

            uint256 twos = denominator & (0 - denominator);
            assembly {
                // Divide denominator by twos.
                denominator := div(denominator, twos)

                // Divide [prod1 prod0] by twos.
                prod0 := div(prod0, twos)

                // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
                twos := add(div(sub(0, twos), twos), 1)
            }

            // Shift in bits from prod1 into prod0.
            prod0 |= prod1 * twos;

            // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
            // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
            // four bits. That is, denominator * inv = 1 mod 2^4.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
            // in modular arithmetic, doubling the correct bits in each step.
            inverse *= 2 - denominator * inverse; // inverse mod 2^8
            inverse *= 2 - denominator * inverse; // inverse mod 2^16
            inverse *= 2 - denominator * inverse; // inverse mod 2^32
            inverse *= 2 - denominator * inverse; // inverse mod 2^64
            inverse *= 2 - denominator * inverse; // inverse mod 2^128
            inverse *= 2 - denominator * inverse; // inverse mod 2^256

            // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
            // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
            // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
            // is no longer required.
            result = prod0 * inverse;
            return result;
        }
    }

    /**
     * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
        uint256 result = mulDiv(x, y, denominator);
        if (unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0) {
            result += 1;
        }
        return result;
    }

    /**
     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
     * towards zero.
     *
     * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
     */
    function sqrt(uint256 a) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
        //
        // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
        // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
        //
        // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
        // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
        // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
        //
        // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
        uint256 result = 1 << (log2(a) >> 1);

        // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
        // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
        // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
        // into the expected uint128 result.
        unchecked {
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            return min(result, a / result);
        }
    }

    /**
     * @notice Calculates sqrt(a), following the selected rounding direction.
     */
    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = sqrt(a);
            return result + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 2 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log2(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 128;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 64;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 32;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 16;
            }
            if (value >> 8 > 0) {
                value >>= 8;
                result += 8;
            }
            if (value >> 4 > 0) {
                value >>= 4;
                result += 4;
            }
            if (value >> 2 > 0) {
                value >>= 2;
                result += 2;
            }
            if (value >> 1 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log2(value);
            return result + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 10 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     */
    function log10(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >= 10 ** 64) {
                value /= 10 ** 64;
                result += 64;
            }
            if (value >= 10 ** 32) {
                value /= 10 ** 32;
                result += 32;
            }
            if (value >= 10 ** 16) {
                value /= 10 ** 16;
                result += 16;
            }
            if (value >= 10 ** 8) {
                value /= 10 ** 8;
                result += 8;
            }
            if (value >= 10 ** 4) {
                value /= 10 ** 4;
                result += 4;
            }
            if (value >= 10 ** 2) {
                value /= 10 ** 2;
                result += 2;
            }
            if (value >= 10 ** 1) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log10(value);
            return result + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 256 of a positive value rounded towards zero.
     * Returns 0 if given 0.
     *
     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
     */
    function log256(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 16;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 8;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 4;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 2;
            }
            if (value >> 8 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log256(value);
            return result + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0);
        }
    }

    /**
     * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
     */
    function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
        return uint8(rounding) % 2 == 1;
    }
}

// File: src/ITimeToken.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

interface ITimeToken {
    function DEVELOPER_ADDRESS() external view returns (address);
    function BASE_FEE() external view returns (uint256);
    function COMISSION_RATE() external view returns (uint256);
    function SHARE_RATE() external view returns (uint256);
    function TIME_BASE_LIQUIDITY() external view returns (uint256);
    function TIME_BASE_FEE() external view returns (uint256);
    function TOLERANCE() external view returns (uint256);
    function dividendPerToken() external view returns (uint256);
    function firstBlock() external view returns (uint256);
    function isMiningAllowed(address account) external view returns (bool);
    function liquidityFactorNative() external view returns (uint256);
    function liquidityFactorTime() external view returns (uint256);
    function numberOfHolders() external view returns (uint256);
    function numberOfMiners() external view returns (uint256);
    function sharedBalance() external view returns (uint256);
    function poolBalance() external view returns (uint256);
    function totalMinted() external view returns (uint256);
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external pure returns (uint8);
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function burn(uint256 amount) external;
    function transfer(address to, uint256 amount) external returns (bool success);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool);
    function increaseAllowance(address spender, uint256 addedValue) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool success);
    function averageMiningRate() external view returns (uint256);
    function donateEth() external payable;
    function enableMining() external payable;
    function enableMiningWithTimeToken() external;
    function fee() external view returns (uint256);
    function feeInTime() external view returns (uint256);
    function mining() external;
    function saveTime() external payable returns (bool success);
    function spendTime(uint256 timeAmount) external returns (bool success);
    function swapPriceNative(uint256 amountNative) external view returns (uint256);
    function swapPriceTimeInverse(uint256 amountTime) external view returns (uint256);
    function accountShareBalance(address account) external view returns (uint256);
    function withdrawableShareBalance(address account) external view returns (uint256);
    function withdrawShare() external;
    receive() external payable;
}

// File: src/ITimeIsUp.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

interface ITimeIsUp {
    function FLASH_MINT_FEE() external view returns (uint256);
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, 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 from, address to, uint256 amount) external returns (bool);
    function increaseAllowance(address spender, uint256 addedValue) external returns (bool);
    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool);
    function accountShareBalance(address account) external view returns (uint256);
    function burn(uint256 amount) external;
    function mint(uint256 timeAmount) external payable;
    function queryAmountExternalLP(uint256 amountNative) external view returns (uint256);
    function queryAmountInternalLP(uint256 amountNative) external view returns (uint256);
    function queryAmountOptimal(uint256 amountNative) external view returns (uint256);
    function queryNativeAmount(uint256 d2Amount) external view returns (uint256);
    function queryNativeFromTimeAmount(uint256 timeAmount) external view returns (uint256);
    function queryPriceNative(uint256 amountNative) external view returns (uint256);
    function queryPriceInverse(uint256 d2Amount) external view returns (uint256);
    function queryRate() external view returns (uint256);
    function queryPublicReward() external view returns (uint256);
    function returnNative() external payable returns (bool);
    function splitSharesWithReward() external;
    function buy() external payable returns (bool success);
    function sell(uint256 d2Amount) external returns (bool success);
    function flashMint(uint256 d2AmountToBorrow, bytes calldata data) external;
    function payFlashMintFee() external payable;
    function poolBalance() external view returns (uint256);
    function toBeShared() external view returns (uint256);
    function receiveProfit() external payable;
}