{
    "Function": "calcReward",
    "File": "contracts/managers/SherDistributionManager.sol",
    "Parent Contracts": [
        "contracts/managers/Manager.sol",
        "node_modules/@openzeppelin/contracts/security/Pausable.sol",
        "node_modules/@openzeppelin/contracts/access/Ownable.sol",
        "node_modules/@openzeppelin/contracts/utils/Context.sol",
        "contracts/interfaces/managers/ISherDistributionManager.sol",
        "contracts/interfaces/managers/IManager.sol"
    ],
    "High-Level Calls": [
        "Math"
    ],
    "Internal Calls": [],
    "Library Calls": [
        "max"
    ],
    "Low-Level Calls": [],
    "Code": "function calcReward(\n    uint256 _tvl,\n    uint256 _amount,\n    uint256 _period\n  ) public view override returns (uint256 _sher) {\n    if (_amount == 0) return 0;\n\n    // Figures out how much of this stake should receive max rewards\n    // _tvl is the pre-stake TVL (call it $80M) and maxRewardsEndTVL could be $100M\n    // If maxRewardsEndTVL is bigger than the pre-stake TVL, then some or all of the stake could receive max rewards\n    // In this case, the amount of the stake to receive max rewards is maxRewardsEndTVL - _tvl\n    // Otherwise, the pre-stake TVL could be bigger than the maxRewardsEndTVL, in which case 0 max rewards are available\n    uint256 maxRewardsAvailable = maxRewardsEndTVL > _tvl ? maxRewardsEndTVL - _tvl : 0;\n\n    // Same logic as above for the TVL at which all SHER rewards end\n    // If the pre-stake TVL is lower than the zeroRewardsStartTVL, then SHER rewards are still available to all or part of the stake\n    // The starting point of the slopeRewards is calculated using max(maxRewardsEndTVL, tvl).\n    // The starting point is either the beginning of the slope --> maxRewardsEndTVL\n    // Or it's the current amount of TVL in case the point on the curve is already on the slope.\n    uint256 slopeRewardsAvailable = zeroRewardsStartTVL > _tvl\n      ? zeroRewardsStartTVL - Math.max(maxRewardsEndTVL, _tvl)\n      : 0;\n\n    // If there are some max rewards available...\n    if (maxRewardsAvailable != 0) {\n      // And if the entire stake is still within the maxRewardsAvailable amount\n      if (_amount <= maxRewardsAvailable) {\n        // Then the entire stake amount should accrue max SHER rewards\n        return (_amount * maxRewardsRate * _period) / DECIMALS;\n      } else {\n        // Otherwise, the stake takes all the maxRewardsAvailable left and the calc continues\n        // We add the maxRewardsAvailable amount to the TVL (now _tvl should be equal to maxRewardsEndTVL)\n        _tvl += maxRewardsAvailable;\n        // We subtract the amount of the stake that received max rewards\n        _amount -= maxRewardsAvailable;\n\n        // We accrue the max rewards available at the max rewards rate for the stake period to the SHER balance\n        // This could be: $20M of maxRewardsAvailable which gets paid .01 SHER per second (max rate) for 3 months worth of seconds\n        // Calculation continues after this\n        _sher = (maxRewardsAvailable * maxRewardsRate * _period) / DECIMALS;\n      }\n    }\n\n    // If there are SHER rewards still available (we didn't surpass zeroRewardsStartTVL)...\n    if (slopeRewardsAvailable != 0) {\n      // If the amount left is greater than the slope rewards available, we take all the remaining slope rewards\n      if (_amount > slopeRewardsAvailable) _amount = slopeRewardsAvailable;\n\n      // We take the average position on the slope that the stake amount occupies\n      // This excludes any stake amount <= maxRewardsEndTVL or >= zeroRewardsStartTVL_\n      // e.g. if tvl = 100m (and maxRewardsEndTVL is $100M), 50m is deposited, point at 125m is taken\n      uint256 position = _tvl + (_amount / 2);\n\n      // Calc SHER rewards based on position on the curve\n      // (zeroRewardsStartTVL - position) divided by (zeroRewardsStartTVL - maxRewardsEndTVL) gives the % of max rewards the amount should get\n      // Multiply this percentage by maxRewardsRate to get the rate at which this position should accrue SHER\n      // Multiply by the _amount to get the full SHER amount earned per second\n      // Multiply by the _period to get the total SHER amount owed to this position\n      _sher +=\n        (((zeroRewardsStartTVL - position) * _amount * maxRewardsRate * _period) /\n          (zeroRewardsStartTVL - maxRewardsEndTVL)) /\n        DECIMALS;\n    }\n  }"
}