{
    "Function": "claimRewards",
    "File": "contracts/ExtraRewardStashV2.sol",
    "Parent Contracts": [],
    "High-Level Calls": [
        "IStaker",
        "SafeERC20",
        "IERC20",
        "SafeERC20",
        "IRewardFactory",
        "IRewardFactory",
        "IRewardFactory",
        "IERC20",
        "IDeposit",
        "IRewardFactory",
        "IDeposit",
        "IStaker",
        "IDeposit"
    ],
    "Internal Calls": [
        "checkForNewRewardTokens",
        "require(bool,string)"
    ],
    "Library Calls": [
        "safeTransfer",
        "safeTransfer"
    ],
    "Low-Level Calls": [],
    "Code": "function claimRewards() external returns (bool) {\n        require(msg.sender == operator, \"!authorized\");\n\n        //this is updateable in v2 gauges now so must check each time.\n        checkForNewRewardTokens();\n\n        uint256 length = tokenCount;\n        if (length > 0) {\n            //get previous balances of all tokens\n            uint256[] memory balances = new uint256[](length);\n            for (uint256 i = 0; i < length; i++) {\n                balances[i] = IERC20(tokenInfo[i].token).balanceOf(staker);\n            }\n            //claim rewards on gauge for staker\n            //booster will call for future proofing (cant assume anyone will always be able to call)\n            IDeposit(operator).claimRewards(pid, gauge);\n\n            for (uint256 i = 0; i < length; i++) {\n                address token = tokenInfo[i].token;\n                uint256 newbalance = IERC20(token).balanceOf(staker);\n                //stash if balance increased\n                if (newbalance > balances[i]) {\n                    IStaker(staker).withdraw(token);\n                    tokenInfo[i].lastActiveTime = block.timestamp;\n\n                    //make sure this pool is in active list,\n                    IRewardFactory(rewardFactory).addActiveReward(token, pid);\n\n                    //check if other stashes are also active, and if so, send to arbitrator\n                    //do this here because processStash will have tokens from the arbitrator\n                    uint256 activeCount = IRewardFactory(rewardFactory).activeRewardCount(token);\n                    if (activeCount > 1) {\n                        //send to arbitrator\n                        address arb = IDeposit(operator).rewardArbitrator();\n                        if (arb != address(0)) {\n                            IERC20(token).safeTransfer(arb, newbalance);\n                        }\n                    }\n                } else {\n                    //check if this reward has been inactive too long\n                    if (block.timestamp > tokenInfo[i].lastActiveTime + WEEK) {\n                        //set as inactive\n                        IRewardFactory(rewardFactory).removeActiveReward(token, pid);\n                    } else {\n                        //edge case around reward ending periods\n                        if (newbalance > 0) {\n                            // - recently active pool\n                            // - rewards claimed to staker contract via a deposit/withdraw(or someone manually calling on the gauge)\n                            // - rewards ended before the deposit, thus deposit took the last available tokens\n                            // - thus claimRewards doesnt see any new rewards, but there are rewards on the staker contract\n                            // - i think its safe to assume claim will be called within the timeframe, or else these rewards\n                            //     will be unretrievable until some pool starts rewards again\n\n                            //claim the tokens\n                            IStaker(staker).withdraw(token);\n\n                            uint256 activeCount = IRewardFactory(rewardFactory).activeRewardCount(\n                                token\n                            );\n                            if (activeCount > 1) {\n                                //send to arbitrator\n                                address arb = IDeposit(operator).rewardArbitrator();\n                                if (arb != address(0)) {\n                                    IERC20(token).safeTransfer(arb, newbalance);\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n        }\n        return true;\n    }"
}