{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/Vault.sol",
    "Parent Contracts": [
        "contracts/interfaces/IVault.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract Vault is IVault {\n    using SafeERC20 for IERC20;\n\n    /**\n     * Storage\n     */\n\n    address public override token;\n    IController public controller;\n    IRegistry public registry;\n    IOwnership public ownership;\n\n    mapping(address => uint256) public override debts;\n    mapping(address => uint256) public attributions;\n    uint256 public totalAttributions;\n\n    address public keeper; //keeper can operate utilize(), if address zero, anyone can operate.\n    uint256 public balance; //balance of underlying token\n    uint256 public totalDebt; //total debt balance. 1debt:1token\n\n    uint256 public constant MAGIC_SCALE_1E6 = 1e6; //internal multiplication scale 1e6 to reduce decimal truncation\n\n\n\n    event ControllerSet(address controller);\n\n    modifier onlyOwner() {\n        require(\n            ownership.owner() == msg.sender,\n            \"Restricted: caller is not allowed to operate\"\n        );\n        _;\n    }\n\n    modifier onlyMarket() {\n        require(\n            IRegistry(registry).isListed(msg.sender),\n            \"ERROR_ONLY_MARKET\"\n        );\n        _;\n    }\n\n    constructor(\n        address _token,\n        address _registry,\n        address _controller,\n        address _ownership\n    ) {\n        require(_token != address(0));\n        require(_registry != address(0));\n        require(_ownership != address(0));\n        //controller can be zero\n\n        token = _token;\n        registry = IRegistry(_registry);\n        controller = IController(_controller);\n        ownership = IOwnership(_ownership);\n    }\n\n    /**\n     * Vault Functions\n     */\n\n    /**\n     * @notice A market contract can deposit collateral and get attribution point in return\n     * @param  _amount amount of tokens to deposit\n     * @param _from sender's address\n     * @param _beneficiaries beneficiary's address array\n     * @param _shares funds share within beneficiaries (100% = 1e6)\n     * @return _allocations attribution amount generated from the transaction\n     */\n    function addValueBatch(\n        uint256 _amount,\n        address _from,\n        address[2] memory _beneficiaries,\n        uint256[2] memory _shares\n    ) external override onlyMarket returns (uint256[2] memory _allocations) {\n        \n        require(_shares[0] + _shares[1] == 1000000, \"ERROR_INCORRECT_SHARE\");\n\n        uint256 _attributions;\n        if (totalAttributions == 0) {\n            _attributions = _amount;\n        } else {\n            uint256 _pool = valueAll();\n            _attributions = (_amount * totalAttributions) / _pool;\n        }\n        IERC20(token).safeTransferFrom(_from, address(this), _amount);\n\n        balance += _amount;\n        totalAttributions += _attributions;\n        for (uint128 i = 0; i < 2; i++) {\n            uint256 _allocation = (_shares[i] * _attributions) / MAGIC_SCALE_1E6;\n            attributions[_beneficiaries[i]] += _allocation;\n            _allocations[i] = _allocation;\n        }\n    }\n\n    /**\n     * @notice A market contract can deposit collateral and get attribution point in return\n     * @param  _amount amount of tokens to deposit\n     * @param _from sender's address\n     * @param _beneficiary beneficiary's address\n     * @return _attributions attribution amount generated from the transaction\n     */\n\n    function addValue(\n        uint256 _amount,\n        address _from,\n        address _beneficiary\n    ) external override onlyMarket returns (uint256 _attributions) {\n\n        if (totalAttributions == 0) {\n            _attributions = _amount;\n        } else {\n            uint256 _pool = valueAll();\n            _attributions = (_amount * totalAttributions) / _pool;\n        }\n        IERC20(token).safeTransferFrom(_from, address(this), _amount);\n        balance += _amount;\n        totalAttributions += _attributions;\n        attributions[_beneficiary] += _attributions;\n    }\n\n    /**\n     * @notice an address that has balance in the vault can withdraw underlying value\n     * @param _amount amount of tokens to withdraw\n     * @param _to address to get underlying tokens\n     * @return _attributions amount of attributions burnet\n     */\n    function withdrawValue(uint256 _amount, address _to)\n        external\n        override\n        returns (uint256 _attributions)\n    {\n        require(\n            attributions[msg.sender] > 0 &&\n                underlyingValue(msg.sender) >= _amount,\n            \"ERROR_WITHDRAW-VALUE_BADCONDITOONS\"\n        );\n        _attributions = (totalAttributions * _amount) / valueAll();\n\n        attributions[msg.sender] -= _attributions;\n        totalAttributions -= _attributions;\n\n        if (available() < _amount) {\n            //when USDC in this contract isn't enough\n            uint256 _shortage = _amount - available();\n            _unutilize(_shortage);\n\n            assert(available() >= _amount);\n        }\n\n        balance -= _amount;\n        IERC20(token).safeTransfer(_to, _amount);\n    }\n\n    /**\n     * @notice an address that has balance in the vault can transfer underlying value\n     * @param _amount sender of value\n     * @param _destination reciepient of value\n     */\n\n    function transferValue(uint256 _amount, address _destination)\n        external\n        override\n        returns (uint256 _attributions)\n    {\n        require(\n            attributions[msg.sender] > 0 &&\n                underlyingValue(msg.sender) >= _amount,\n            \"ERROR_TRANSFER-VALUE_BADCONDITOONS\"\n        );\n        _attributions = (_amount * totalAttributions) / valueAll();\n        attributions[msg.sender] -= _attributions;\n        attributions[_destination] += _attributions;\n    }\n\n    /**\n     * @notice a registered contract can borrow balance from the vault\n     * @param _amount borrow amount\n     * @param _to borrower's address\n     */\n    function borrowValue(uint256 _amount, address _to) external onlyMarket override {\n        debts[msg.sender] += _amount;\n        totalDebt += _amount;\n\n        IERC20(token).safeTransfer(_to, _amount);\n    }\n\n    /**\n     * @notice an address that has balance in the vault can offset an address's debt\n     * @param _amount debt amount to offset\n     * @param _target borrower's address\n     */\n\n    function offsetDebt(uint256 _amount, address _target)\n        external\n        override\n        returns (uint256 _attributions)\n    {\n        require(\n            attributions[msg.sender] > 0 &&\n                underlyingValue(msg.sender) >= _amount,\n            \"ERROR_REPAY_DEBT_BADCONDITOONS\"\n        );\n        _attributions = (_amount * totalAttributions) / valueAll();\n        attributions[msg.sender] -= _attributions;\n        totalAttributions -= _attributions;\n        balance -= _amount;\n        debts[_target] -= _amount;\n        totalDebt -= _amount;\n    }\n\n    /**\n     * @notice a registerd market can transfer their debt to system debt\n     * @param _amount debt amount to transfer\n     * @dev will be called when CDS could not afford when resume the market.\n     */\n    function transferDebt(uint256 _amount) external onlyMarket override {\n\n        if(_amount != 0){\n            debts[msg.sender] -= _amount;\n            debts[address(0)] += _amount;\n        }\n    }\n\n    /**\n     * @notice anyone can repay the system debt by sending tokens to this contract\n     * @param _amount debt amount to repay\n     * @param _target borrower's address\n     */\n    function repayDebt(uint256 _amount, address _target) external override {\n        uint256 _debt = debts[_target];\n        if (_debt >= _amount) {\n            debts[_target] -= _amount;\n            totalDebt -= _amount;\n            IERC20(token).safeTransferFrom(msg.sender, address(this), _amount);\n        } else {\n            debts[_target] = 0;\n            totalDebt -= _debt;\n            IERC20(token).safeTransferFrom(msg.sender, address(this), _debt);\n        }\n    }\n\n    /**\n     * @notice an address that has balance in the vault can withdraw value denominated in attribution\n     * @param _attribution amount of attribution to burn\n     * @param _to beneficiary's address\n     * @return _retVal number of tokens withdrawn from the transaction\n     */\n    function withdrawAttribution(uint256 _attribution, address _to)\n        external\n        override\n        returns (uint256 _retVal)\n    {\n        _retVal = _withdrawAttribution(_attribution, _to);\n    }\n\n    /**\n     * @notice an address that has balance in the vault can withdraw all value\n     * @param _to beneficiary's address\n     * @return _retVal number of tokens withdrawn from the transaction\n     */\n    function withdrawAllAttribution(address _to)\n        external\n        override\n        returns (uint256 _retVal)\n    {\n        _retVal = _withdrawAttribution(attributions[msg.sender], _to);\n    }\n\n    /**\n     * @notice an address that has balance in the vault can withdraw all value\n     * @param _attribution amount of attribution to burn\n     * @param _to beneficiary's address\n     * @return _retVal number of tokens withdrawn from the transaction\n     */\n    function _withdrawAttribution(uint256 _attribution, address _to)\n        internal\n        returns (uint256 _retVal)\n    {\n        require(\n            attributions[msg.sender] >= _attribution,\n            \"ERROR_WITHDRAW-ATTRIBUTION_BADCONDITOONS\"\n        );\n        _retVal = (_attribution * valueAll()) / totalAttributions;\n\n        attributions[msg.sender] -= _attribution;\n        totalAttributions -= _attribution;\n\n        if (available() < _retVal) {\n            uint256 _shortage = _retVal - available();\n            _unutilize(_shortage);\n        }\n\n        balance -= _retVal;\n        IERC20(token).safeTransfer(_to, _retVal);\n    }\n\n    /**\n     * @notice an address that has balance in the vault can transfer value denominated in attribution\n     * @param _amount amount of attribution to transfer\n     * @param _destination reciepient of attribution\n     */\n    function transferAttribution(uint256 _amount, address _destination)\n        external\n        override\n    {\n        require(_destination != address(0), \"ERROR_ZERO_ADDRESS\");\n\n        require(\n            _amount != 0 && attributions[msg.sender] >= _amount,\n            \"ERROR_TRANSFER-ATTRIBUTION_BADCONDITOONS\"\n        );\n\n        attributions[msg.sender] -= _amount;\n        attributions[_destination] += _amount;\n    }\n\n    /**\n     * @notice the controller can utilize all available stored funds\n     * @return _amount amount of tokens utilized\n     */\n    function utilize() external override returns (uint256 _amount) {\n        if (keeper != address(0)) {\n            require(msg.sender == keeper, \"ERROR_NOT_KEEPER\");\n        }\n        _amount = available(); //balance\n        if (_amount > 0) {\n            IERC20(token).safeTransfer(address(controller), _amount);\n            balance -= _amount;\n            controller.earn(address(token), _amount);\n        }\n    }\n\n    /**\n     * @notice get attribution number for the specified address\n     * @param _target target address\n     * @return amount of attritbution\n     */\n\n    function attributionOf(address _target)\n        external\n        view\n        override\n        returns (uint256)\n    {\n        return attributions[_target];\n    }\n\n    /**\n     * @notice get all attribution number for this contract\n     * @return amount of all attribution\n     */\n    function attributionAll() external view returns (uint256) {\n        return totalAttributions;\n    }\n\n    /**\n     * @notice Convert attribution number into underlying assset value\n     * @param _attribution amount of attribution\n     * @return token value of input attribution\n     */\n    function attributionValue(uint256 _attribution)\n        external\n        view\n        override\n        returns (uint256)\n    {\n        if (totalAttributions > 0 && _attribution > 0) {\n            return (_attribution * valueAll()) / totalAttributions;\n        } else {\n            return 0;\n        }\n    }\n\n    /**\n     * @notice return underlying value of the specified address\n     * @param _target target address\n     * @return token value of target address\n     */\n    function underlyingValue(address _target)\n        public\n        view\n        override\n        returns (uint256)\n    {\n        if (attributions[_target] > 0) {\n            return (valueAll() * attributions[_target]) / totalAttributions;\n        } else {\n            return 0;\n        }\n    }\n\n    /**\n     * @notice return underlying value of this contract\n     * @return all token value of the vault\n     */\n    function valueAll() public view returns (uint256) {\n        if (address(controller) != address(0)) {\n            return balance + controller.valueAll();\n        } else {\n            return balance;\n        }\n    }\n\n    /**\n     * @notice internal function to unutilize the funds and keep utilization rate\n     * @param _amount amount to withdraw from controller\n     */\n    function _unutilize(uint256 _amount) internal {\n        require(address(controller) != address(0), \"ERROR_CONTROLLER_NOT_SET\");\n\n        controller.withdraw(address(this), _amount);\n        balance += _amount;\n    }\n\n    /**\n     * @notice return how much funds in this contract is available to be utilized\n     * @return available balance to utilize\n     */\n    function available() public view returns (uint256) {\n        return balance - totalDebt;\n    }\n\n    /**\n     * @notice return how much price for each attribution\n     * @return value of one share of attribution\n     */\n    function getPricePerFullShare() public view returns (uint256) {\n        return (valueAll() * MAGIC_SCALE_1E6) / totalAttributions;\n    }\n\n    /**\n     * onlyOwner\n     */\n\n    /**\n     * @notice withdraw redundant token stored in this contract\n     * @param _token token address\n     * @param _to beneficiary's address\n     */\n    function withdrawRedundant(address _token, address _to)\n        external\n        override\n        onlyOwner\n    {\n        if (\n            _token == address(token) &&\n            balance < IERC20(token).balanceOf(address(this))\n        ) {\n            uint256 _redundant = IERC20(token).balanceOf(address(this)) -\n                balance;\n            IERC20(token).safeTransfer(_to, _redundant);\n        } else if (IERC20(_token).balanceOf(address(this)) > 0) {\n            IERC20(_token).safeTransfer(\n                _to,\n                IERC20(_token).balanceOf(address(this))\n            );\n        }\n    }\n\n    /**\n     * @notice admin function to set controller address\n     * @param _controller address of the controller\n     */\n    function setController(address _controller) public override onlyOwner {\n        require(_controller != address(0), \"ERROR_ZERO_ADDRESS\");\n\n        if (address(controller) != address(0)) {\n            controller.migrate(address(_controller));\n            controller = IController(_controller);\n        } else {\n            controller = IController(_controller);\n        }\n\n        emit ControllerSet(_controller);\n    }\n\n    /**\n     * @notice the controller can utilize all available stored funds\n     * @param _keeper keeper address\n     */\n    function setKeeper(address _keeper) external override onlyOwner {\n        if (keeper != _keeper) {\n            keeper = _keeper;\n        }\n    }\n}"
}