{
    "Function": "slitherConstructorConstantVariables",
    "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": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract CDSTemplate is InsureDAOERC20, ICDSTemplate, IUniversalMarket {\n    /**\n     * EVENTS\n     */\n    event Deposit(address indexed depositor, uint256 amount, uint256 mint);\n    event Fund(address indexed depositor, uint256 amount, uint256 attribution);\n    event Defund(\n        address indexed depositor,\n        uint256 amount,\n        uint256 attribution\n    );\n\n    event WithdrawRequested(\n        address indexed withdrawer,\n        uint256 amount,\n        uint256 time\n    );\n    event Withdraw(address indexed withdrawer, uint256 amount, uint256 retVal);\n    event Compensated(address indexed index, uint256 amount);\n    event Paused(bool paused);\n    event MetadataChanged(string metadata);\n\n    /**\n     * Storage\n     */\n    /// @notice Market setting\n    bool public initialized;\n    bool public paused;\n    string public metadata;\n\n    /// @notice External contract call addresses\n    IParameters public parameters;\n    IRegistry public registry;\n    IVault public vault;\n    uint256 public surplusPool;\n    uint256 public crowdPool;\n    uint256 public constant MAGIC_SCALE_1E6 = 1e6; //internal multiplication scale 1e6 to reduce decimal truncation\n\n    ///@notice user status management\n    struct Withdrawal {\n        uint256 timestamp;\n        uint256 amount;\n    }\n    mapping(address => Withdrawal) public withdrawalReq;\n\n    /**\n     * @notice Throws if called by any account other than the owner.\n     */\n    modifier onlyOwner() {\n        require(\n            msg.sender == parameters.getOwner(),\n            \"ERROR: ONLY_OWNER\"\n        );\n        _;\n    }\n\n    constructor() {\n        initialized = true;\n    }\n\n    /**\n     * Initialize interaction\n     */\n\n    /**\n     * @notice Initialize market\n     * This function registers market conditions.\n     * references[0] = underlying token address\n     * references[1] = registry\n     * references[2] = parameter\n     * @param _metaData arbitrary string to store market information\n     * @param _conditions array of conditions\n     * @param _references array of references\n     */\n    function initialize(\n        string calldata _metaData,\n        uint256[] calldata _conditions,\n        address[] calldata _references\n    ) external override{\n        require(\n            initialized == false &&\n                bytes(_metaData).length > 0 &&\n                _references[0] != address(0) &&\n                _references[1] != address(0) &&\n                _references[2] != address(0),\n            \"ERROR: INITIALIZATION_BAD_CONDITIONS\"\n        );\n\n        initialized = true;\n\n        string memory _name = \"InsureDAO-CDS\";\n        string memory _symbol = \"iCDS\";\n        uint8 _decimals = IERC20Metadata(_references[0]).decimals();\n\n        initializeToken(_name, _symbol, _decimals);\n\n        parameters = IParameters(_references[2]);\n        vault = IVault(parameters.getVault(_references[0]));\n        registry = IRegistry(_references[1]);\n\n        metadata = _metaData;\n    }\n\n    /**\n     * Pool initeractions\n     */\n\n    /**\n     * @notice A liquidity provider supplies collatral to the pool and receives iTokens\n     * @param _amount amount of token to deposit\n     */\n    function deposit(uint256 _amount) external returns (uint256 _mintAmount) {\n        require(paused == false, \"ERROR: PAUSED\");\n        require(_amount > 0, \"ERROR: DEPOSIT_ZERO\");\n\n        //deposit and pay fees\n        uint256 _liquidity = vault.attributionValue(crowdPool); //get USDC balance with crowdPool's attribution\n        uint256 _supply = totalSupply();\n\n        crowdPool += vault.addValue(_amount, msg.sender, address(this)); //increase attribution\n        \n        if (_supply > 0 && _liquidity > 0) {\n            _mintAmount = (_amount * _supply) / _liquidity;\n        } else if (_supply > 0 && _liquidity == 0) {\n            //when vault lose all underwritten asset = \n            _mintAmount = _amount * _supply; //dilute LP token value af. Start CDS again.\n        } else {\n            //when _supply == 0,\n            _mintAmount = _amount;\n        }\n\n        emit Deposit(msg.sender, _amount, _mintAmount);\n\n        //mint iToken\n        _mint(msg.sender, _mintAmount);\n    }\n\n    /**\n     * @notice A liquidity provider supplies collatral to the pool and receives iTokens\n     * @param _amount amount of token to deposit\n     */\n    function fund(uint256 _amount) external {\n        require(paused == false, \"ERROR: PAUSED\");\n\n        //deposit and pay fees\n        uint256 _attribution = vault.addValue(\n            _amount,\n            msg.sender,\n            address(this)\n        );\n\n        surplusPool += _attribution;\n\n        emit Fund(msg.sender, _amount, _attribution);\n    }\n\n    function defund(uint256 _amount) external override onlyOwner {\n        require(paused == false, \"ERROR: PAUSED\");\n\n        uint256 _attribution = vault.withdrawValue(_amount, msg.sender);\n        surplusPool -= _attribution;\n\n        emit Defund(msg.sender, _amount, _attribution);\n    }\n\n    /**\n     * @notice A liquidity provider request withdrawal of collateral\n     * @param _amount amount of iToken to burn\n     */\n    function requestWithdraw(uint256 _amount) external {\n        uint256 _balance = balanceOf(msg.sender);\n        require(_balance >= _amount, \"ERROR: REQUEST_EXCEED_BALANCE\");\n        require(_amount > 0, \"ERROR: REQUEST_ZERO\");\n        withdrawalReq[msg.sender].timestamp = block.timestamp;\n        withdrawalReq[msg.sender].amount = _amount;\n        emit WithdrawRequested(msg.sender, _amount, block.timestamp);\n    }\n\n    /**\n     * @notice A liquidity provider burns iToken and receives collatral from the pool\n     * @param _amount amount of iToken to burn\n     * @return _retVal the amount underlying token returned\n     */\n    function withdraw(uint256 _amount) external returns (uint256 _retVal) {\n        Withdrawal memory request = withdrawalReq[msg.sender];\n\n        require(paused == false, \"ERROR: PAUSED\");\n        require(\n            request.timestamp +\n                parameters.getLockup(msg.sender) <\n                block.timestamp,\n            \"ERROR: WITHDRAWAL_QUEUE\"\n        );\n        require(\n            request.timestamp +\n                parameters.getLockup(msg.sender) +\n                parameters.getWithdrawable(msg.sender) >\n                block.timestamp,\n            \"ERROR: WITHDRAWAL_NO_ACTIVE_REQUEST\"\n        );\n        require(\n            request.amount >= _amount,\n            \"ERROR: WITHDRAWAL_EXCEEDED_REQUEST\"\n        );\n        require(_amount > 0, \"ERROR: WITHDRAWAL_ZERO\");\n\n        //Calculate underlying value\n        _retVal = (vault.attributionValue(crowdPool) * _amount) / totalSupply();\n\n\n        //reduce requested amount\n        request.amount -= _amount;\n\n        //Burn iToken\n        _burn(msg.sender, _amount);\n\n        //Withdraw liquidity\n        crowdPool -= vault.withdrawValue(_retVal, msg.sender);\n        emit Withdraw(msg.sender, _amount, _retVal);\n    }\n\n    /**\n     * Insurance interactions\n     */\n\n    /**\n     * @notice Compensate the shortage if an index is insolvent\n     * @param _amount amount of underlier token to compensate shortage within index\n     */\n    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    }\n\n    /**\n     * Utilities\n     */\n\n    /**\n     * @notice total Liquidity of the pool (how much can the pool sell cover)\n     * @return _balance available liquidity of this pool\n     */\n    function totalLiquidity() public view returns (uint256 _balance) {\n        return vault.underlyingValue(address(this));\n    }\n\n    /**\n     * @notice Get the exchange rate of LP token against underlying asset(scaled by MAGIC_SCALE_1E6, if MAGIC_SCALE_1E6, the value of iToken vs underlier is 1:1)\n     * @return The value against the underlying token balance.\n     */\n    function rate() external view returns (uint256) {\n        if (totalSupply() > 0) {\n            return\n                (vault.attributionValue(crowdPool) * MAGIC_SCALE_1E6) /\n                totalSupply();\n        } else {\n            return 0;\n        }\n    }\n\n    /**\n     * @notice Get the underlying balance of the `owner`\n     * @param _owner the target address to look up value\n     * @return The balance of underlying token for the specified address\n     */\n    function valueOfUnderlying(address _owner) public view returns (uint256) {\n        uint256 _balance = balanceOf(_owner);\n        if (_balance == 0) {\n            return 0;\n        } else {\n            return\n                _balance * vault.attributionValue(crowdPool) / totalSupply();\n        }\n    }\n\n    /**\n     * Admin functions\n     */\n\n    /**\n     * @notice Change metadata string\n     * @param _metadata new metadata string\n     */\n    function changeMetadata(string calldata _metadata)\n        external\n        override\n        onlyOwner\n    {\n        metadata = _metadata;\n        emit MetadataChanged(_metadata);\n    }\n\n    /**\n     * @notice Used for changing settlementFeeRecipient\n     * @param _state true to set paused and vice versa\n     */\n    function setPaused(bool _state) external override onlyOwner {\n        if (paused != _state) {\n            paused = _state;\n            emit Paused(_state);\n        }\n    }\n\n    /**\n     * Internal functions\n     */\n\n    /**\n     * @notice Internal function to offset withdraw request and latest balance\n     * @param from the account who send\n     * @param to a\n     * @param amount the amount of token to offset\n     */\n    function _beforeTokenTransfer(\n        address from,\n        address to,\n        uint256 amount\n    ) internal virtual override {\n        super._beforeTokenTransfer(from, to, amount);\n\n        if (from != address(0)) {\n            uint256 _after = balanceOf(from) - amount;\n            if (_after < withdrawalReq[from].amount) {\n                withdrawalReq[from].amount = _after;\n            }\n        }\n    }\n}"
}