{
    "Function": "compensate",
    "File": "contracts/CDSTemplate.sol",
    "Parent Contracts": [
        "contracts/interfaces/IUniversalMarket.sol",
        "contracts/interfaces/ICDSTemplate.sol",
        "contracts/InsureDAOERC20.sol",
        "node_modules/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol",
        "node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol",
        "node_modules/@openzeppelin/contracts/utils/Context.sol"
    ],
    "High-Level Calls": [
        "IVault",
        "IVault",
        "IVault",
        "IRegistry"
    ],
    "Internal Calls": [
        "require(bool)"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function compensate(uint256 _amount)\n        external\n        override\n        returns (uint256 _compensated)\n    {\n        require(registry.isListed(msg.sender));\n        \n        uint256 _available = vault.underlyingValue(address(this));\n        uint256 _crowdAttribution = crowdPool;\n        uint256 _surplusAttribution = surplusPool;\n        uint256 _attributionLoss;\n\n        if (_available >= _amount) {\n            _compensated = _amount;\n            _attributionLoss = vault.transferValue(_amount, msg.sender);\n            emit Compensated(msg.sender, _amount);\n        } else {\n            //when CDS cannot afford, pay as much as possible\n            _compensated = _available;\n            _attributionLoss = vault.transferValue(_available, msg.sender);\n            emit Compensated(msg.sender, _available);\n        }\n\n        uint256 _crowdPoolLoss = \n            (_crowdAttribution * _attributionLoss) /\n            (_crowdAttribution + _surplusAttribution);\n\n        crowdPool -= _crowdPoolLoss;\n        surplusPool -= (_attributionLoss - _crowdPoolLoss);\n    }"
}