{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/contracts/RewardsDistributor.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract RewardsDistributor {\n\n    event CheckpointToken(\n        uint time,\n        uint tokens\n    );\n\n    event Claimed(\n        uint tokenId,\n        uint amount,\n        uint claim_epoch,\n        uint max_epoch\n    );\n\n    uint constant WEEK = 7 * 86400;\n\n    uint public start_time;\n    uint public time_cursor;\n    mapping(uint => uint) public time_cursor_of;\n    mapping(uint => uint) public user_epoch_of;\n\n    uint public last_token_time;\n    uint[1000000000000000] public tokens_per_week;\n\n    address public voting_escrow;\n    address public token;\n    uint public token_last_balance;\n\n    uint[1000000000000000] public ve_supply;\n\n    address public depositor;\n\n    constructor(address _voting_escrow) {\n        uint _t = block.timestamp / WEEK * WEEK;\n        start_time = _t;\n        last_token_time = _t;\n        time_cursor = _t;\n        address _token = IVotingEscrow(_voting_escrow).token();\n        token = _token;\n        voting_escrow = _voting_escrow;\n        depositor = msg.sender;\n        IERC20(_token).approve(_voting_escrow, type(uint).max);\n    }\n\n    function timestamp() external view returns (uint) {\n        return block.timestamp / WEEK * WEEK;\n    }\n\n    function _checkpoint_token() internal {\n        uint token_balance = IERC20(token).balanceOf(address(this));\n        uint to_distribute = token_balance - token_last_balance;\n        token_last_balance = token_balance;\n\n        uint t = last_token_time;\n        uint since_last = block.timestamp - t;\n        last_token_time = block.timestamp;\n        uint this_week = t / WEEK * WEEK;\n        uint next_week = 0;\n\n        for (uint i = 0; i < 20; i++) {\n            next_week = this_week + WEEK;\n            if (block.timestamp < next_week) {\n                if (since_last == 0 && block.timestamp == t) {\n                    tokens_per_week[this_week] += to_distribute;\n                } else {\n                    tokens_per_week[this_week] += to_distribute * (block.timestamp - t) / since_last;\n                }\n                break;\n            } else {\n                if (since_last == 0 && next_week == t) {\n                    tokens_per_week[this_week] += to_distribute;\n                } else {\n                    tokens_per_week[this_week] += to_distribute * (next_week - t) / since_last;\n                }\n            }\n            t = next_week;\n            this_week = next_week;\n        }\n        emit CheckpointToken(block.timestamp, to_distribute);\n    }\n\n    function checkpoint_token() external {\n        assert(msg.sender == depositor);\n        _checkpoint_token();\n    }\n\n    function _find_timestamp_epoch(address ve, uint _timestamp) internal view returns (uint) {\n        uint _min = 0;\n        uint _max = IVotingEscrow(ve).epoch();\n        for (uint i = 0; i < 128; i++) {\n            if (_min >= _max) break;\n            uint _mid = (_min + _max + 2) / 2;\n            IVotingEscrow.Point memory pt = IVotingEscrow(ve).point_history(_mid);\n            if (pt.ts <= _timestamp) {\n                _min = _mid;\n            } else {\n                _max = _mid - 1;\n            }\n        }\n        return _min;\n    }\n\n    function _find_timestamp_user_epoch(address ve, uint tokenId, uint _timestamp, uint max_user_epoch) internal view returns (uint) {\n        uint _min = 0;\n        uint _max = max_user_epoch;\n        for (uint i = 0; i < 128; i++) {\n            if (_min >= _max) break;\n            uint _mid = (_min + _max + 2) / 2;\n            IVotingEscrow.Point memory pt = IVotingEscrow(ve).user_point_history(tokenId, _mid);\n            if (pt.ts <= _timestamp) {\n                _min = _mid;\n            } else {\n                _max = _mid -1;\n            }\n        }\n        return _min;\n    }\n\n    function ve_for_at(uint _tokenId, uint _timestamp) external view returns (uint) {\n        address ve = voting_escrow;\n        uint max_user_epoch = IVotingEscrow(ve).user_point_epoch(_tokenId);\n        uint epoch = _find_timestamp_user_epoch(ve, _tokenId, _timestamp, max_user_epoch);\n        IVotingEscrow.Point memory pt = IVotingEscrow(ve).user_point_history(_tokenId, epoch);\n        return Math.max(uint(int256(pt.bias - pt.slope * (int128(int256(_timestamp - pt.ts))))), 0);\n    }\n\n    function _checkpoint_total_supply() internal {\n        address ve = voting_escrow;\n        uint t = time_cursor;\n        uint rounded_timestamp = block.timestamp / WEEK * WEEK;\n        IVotingEscrow(ve).checkpoint();\n\n        for (uint i = 0; i < 20; i++) {\n            if (t > rounded_timestamp) {\n                break;\n            } else {\n                uint epoch = _find_timestamp_epoch(ve, t);\n                IVotingEscrow.Point memory pt = IVotingEscrow(ve).point_history(epoch);\n                int128 dt = 0;\n                if (t > pt.ts) {\n                    dt = int128(int256(t - pt.ts));\n                }\n                ve_supply[t] = Math.max(uint(int256(pt.bias - pt.slope * dt)), 0);\n            }\n            t += WEEK;\n        }\n        time_cursor = t;\n    }\n\n    function checkpoint_total_supply() external {\n        _checkpoint_total_supply();\n    }\n\n    function _claim(uint _tokenId, address ve, uint _last_token_time) internal returns (uint) {\n        uint user_epoch = 0;\n        uint to_distribute = 0;\n\n        uint max_user_epoch = IVotingEscrow(ve).user_point_epoch(_tokenId);\n        uint _start_time = start_time;\n\n        if (max_user_epoch == 0) return 0;\n\n        uint week_cursor = time_cursor_of[_tokenId];\n        if (week_cursor == 0) {\n            user_epoch = _find_timestamp_user_epoch(ve, _tokenId, _start_time, max_user_epoch);\n        } else {\n            user_epoch = user_epoch_of[_tokenId];\n        }\n\n        if (user_epoch == 0) user_epoch = 1;\n\n        IVotingEscrow.Point memory user_point = IVotingEscrow(ve).user_point_history(_tokenId, user_epoch);\n\n        if (week_cursor == 0) week_cursor = (user_point.ts + WEEK - 1) / WEEK * WEEK;\n        if (week_cursor >= last_token_time) return 0;\n        if (week_cursor < _start_time) week_cursor = _start_time;\n\n        IVotingEscrow.Point memory old_user_point;\n\n        for (uint i = 0; i < 50; i++) {\n            if (week_cursor >= _last_token_time) break;\n\n            if (week_cursor >= user_point.ts && user_epoch <= max_user_epoch) {\n                user_epoch += 1;\n                old_user_point = user_point;\n                if (user_epoch > max_user_epoch) {\n                    user_point = IVotingEscrow.Point(0,0,0,0);\n                } else {\n                    user_point = IVotingEscrow(ve).user_point_history(_tokenId, user_epoch);\n                }\n            } else {\n                int128 dt = int128(int256(week_cursor - old_user_point.ts));\n                uint balance_of = Math.max(uint(int256(old_user_point.bias - dt * old_user_point.slope)), 0);\n                if (balance_of == 0 && user_epoch > max_user_epoch) break;\n                if (balance_of != 0) {\n                    to_distribute += balance_of * tokens_per_week[week_cursor] / ve_supply[week_cursor];\n                }\n                week_cursor += WEEK;\n            }\n        }\n\n        user_epoch = Math.min(max_user_epoch, user_epoch - 1);\n        user_epoch_of[_tokenId] = user_epoch;\n        time_cursor_of[_tokenId] = week_cursor;\n\n        emit Claimed(_tokenId, to_distribute, user_epoch, max_user_epoch);\n\n        return to_distribute;\n    }\n\n    function _claimable(uint _tokenId, address ve, uint _last_token_time) internal view returns (uint) {\n        uint user_epoch = 0;\n        uint to_distribute = 0;\n\n        uint max_user_epoch = IVotingEscrow(ve).user_point_epoch(_tokenId);\n        uint _start_time = start_time;\n\n        if (max_user_epoch == 0) return 0;\n\n        uint week_cursor = time_cursor_of[_tokenId];\n        if (week_cursor == 0) {\n            user_epoch = _find_timestamp_user_epoch(ve, _tokenId, _start_time, max_user_epoch);\n        } else {\n            user_epoch = user_epoch_of[_tokenId];\n        }\n\n        if (user_epoch == 0) user_epoch = 1;\n\n        IVotingEscrow.Point memory user_point = IVotingEscrow(ve).user_point_history(_tokenId, user_epoch);\n\n        if (week_cursor == 0) week_cursor = (user_point.ts + WEEK - 1) / WEEK * WEEK;\n        if (week_cursor >= last_token_time) return 0;\n        if (week_cursor < _start_time) week_cursor = _start_time;\n\n        IVotingEscrow.Point memory old_user_point;\n\n        for (uint i = 0; i < 50; i++) {\n            if (week_cursor >= _last_token_time) break;\n\n            if (week_cursor >= user_point.ts && user_epoch <= max_user_epoch) {\n                user_epoch += 1;\n                old_user_point = user_point;\n                if (user_epoch > max_user_epoch) {\n                    user_point = IVotingEscrow.Point(0,0,0,0);\n                } else {\n                    user_point = IVotingEscrow(ve).user_point_history(_tokenId, user_epoch);\n                }\n            } else {\n                int128 dt = int128(int256(week_cursor - old_user_point.ts));\n                uint balance_of = Math.max(uint(int256(old_user_point.bias - dt * old_user_point.slope)), 0);\n                if (balance_of == 0 && user_epoch > max_user_epoch) break;\n                if (balance_of != 0) {\n                    to_distribute += balance_of * tokens_per_week[week_cursor] / ve_supply[week_cursor];\n                }\n                week_cursor += WEEK;\n            }\n        }\n\n        return to_distribute;\n    }\n\n    function claimable(uint _tokenId) external view returns (uint) {\n        uint _last_token_time = last_token_time / WEEK * WEEK;\n        return _claimable(_tokenId, voting_escrow, _last_token_time);\n    }\n\n    function claim(uint _tokenId) external returns (uint) {\n        if (block.timestamp >= time_cursor) _checkpoint_total_supply();\n        uint _last_token_time = last_token_time;\n        _last_token_time = _last_token_time / WEEK * WEEK;\n        uint amount = _claim(_tokenId, voting_escrow, _last_token_time);\n        if (amount != 0) {\n            IVotingEscrow(voting_escrow).deposit_for(_tokenId, amount);\n            token_last_balance -= amount;\n        }\n        return amount;\n    }\n\n    function claim_many(uint[] memory _tokenIds) external returns (bool) {\n        if (block.timestamp >= time_cursor) _checkpoint_total_supply();\n        uint _last_token_time = last_token_time;\n        _last_token_time = _last_token_time / WEEK * WEEK;\n        address _voting_escrow = voting_escrow;\n        uint total = 0;\n\n        for (uint i = 0; i < _tokenIds.length; i++) {\n            uint _tokenId = _tokenIds[i];\n            if (_tokenId == 0) break;\n            uint amount = _claim(_tokenId, _voting_escrow, _last_token_time);\n            if (amount != 0) {\n                IVotingEscrow(_voting_escrow).deposit_for(_tokenId, amount);\n                total += amount;\n            }\n        }\n        if (total != 0) {\n            token_last_balance -= total;\n        }\n\n        return true;\n    }\n\n    // Once off event on contract initialize\n    function setDepositor(address _depositor) external {\n        require(msg.sender == depositor);\n        depositor = _depositor;\n    }\n}"
}