{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/market/OverlayV1OI.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract OverlayV1OI {\n\n    event log(string k , uint v);\n\n    using FixedPoint for uint256;\n\n    uint256 private constant ONE = 1e18;\n\n    uint256 public compoundingPeriod;\n    uint256 public compounded;\n\n    uint256 internal __oiLong__; // total long open interest\n    uint256 internal __oiShort__; // total short open interest\n\n    uint256 public oiLongShares; // total shares of long open interest outstanding\n    uint256 public oiShortShares; // total shares of short open interest outstanding\n\n    uint256 public k;\n\n    event FundingPaid(uint oiLong, uint oiShort, int fundingPaid);\n\n    constructor (\n        uint256 _compoundingPeriod\n    ) {\n\n        compoundingPeriod = _compoundingPeriod;\n\n        compounded = block.timestamp;\n\n    }\n\n    /// @notice The compounding information for computing funding.\n    /// @dev This returns the number of compoundings that have passed since\n    /// the last time funding was paid as well as the timestamp of the\n    /// current compounding epoch, which come at regular intervals according\n    /// to the compounding period.\n    /// @param _now The timestamp of the current block.\n    /// @param _compounded The last time compounding occurred.\n    /// @return compoundings_ The number of compounding periods passed since\n    /// the last time funding was compounded.\n    /// @return tCompounding_ The current compounding epoch.\n    function epochs (\n        uint _now,\n        uint _compounded\n    ) public view returns (\n        uint compoundings_,\n        uint tCompounding_\n    ) {\n\n        uint _compoundPeriod = compoundingPeriod;\n\n        compoundings_ = ( _now - _compounded ) / _compoundPeriod;\n\n        tCompounding_ = _compounded + ( compoundings_ * _compoundPeriod );\n\n    }\n\n\n    /// @notice Internal utility to pay funding from heavier to ligher side.\n    /// @dev Pure function accepting current open interest, compoundings\n    /// to perform, and funding constant.\n    /// @dev oiImbalance(period_m) = oiImbalance(period_now) * (1 - 2k) ** period_m\n    /// @param _oiLong Current open interest on the long side.\n    /// @param _oiShort Current open interest on the short side.\n    /// @param _epochs The number of compounding periods to compute for.\n    /// @param _k The funding constant.\n    /// @return oiLong_ Open interest on the long side after funding is paid.\n    /// @return oiShort_ Open interest on the short side after funding is paid.\n    /// @return fundingPaid_ Signed integer of funding paid, negative if longs\n    /// are paying shorts.\n    function computeFunding (\n        uint256 _oiLong,\n        uint256 _oiShort,\n        uint256 _epochs,\n        uint256 _k\n    ) internal pure returns (\n        uint256 oiLong_,\n        uint256 oiShort_,\n        int256  fundingPaid_\n    ) {\n\n        if (_oiLong == 0 && 0 == _oiShort) return (0, 0, 0);\n\n        if (0 == _epochs) return ( _oiLong, _oiShort, 0 );\n\n        uint _fundingFactor = ONE.sub(_k.mulUp(ONE*2));\n\n        _fundingFactor = _fundingFactor.powUp(ONE*_epochs);\n\n        uint _funder = _oiLong;\n        uint _funded = _oiShort;\n        bool payingLongs = _funder <= _funded;\n        if (payingLongs) (_funder, _funded) = (_funded, _funder);\n\n        if (_funded == 0) {\n\n            uint _oiNow = _fundingFactor.mulDown(_funder);\n            fundingPaid_ = int(_funder - _oiNow);\n            _funder = _oiNow;\n\n        } else {\n\n            // TODO: we can make an unsafe mul function here\n            uint256 _oiImbNow = _fundingFactor.mulDown(_funder - _funded);\n            uint256 _total = _funder + _funded;\n\n            fundingPaid_ = int( ( _funder - _funded ) / 2 );\n            _funder = ( _total + _oiImbNow ) / 2;\n            _funded = ( _total - _oiImbNow ) / 2;\n\n        }\n\n        ( oiLong_, oiShort_, fundingPaid_) = payingLongs\n            ? ( _funded, _funder, fundingPaid_ )\n            : ( _funder, _funded, -fundingPaid_ );\n\n    }\n\n\n    /// @notice Pays funding.\n    /// @dev Invokes internal computeFunding and sets oiLong and oiShort.\n    /// @param _k The funding constant.\n    /// @param _epochs The number of compounding periods to compute.\n    /// @return fundingPaid_ Signed integer of how much funding was paid.\n    function payFunding (\n        uint256 _k,\n        uint256 _epochs\n    ) internal returns (\n        int256 fundingPaid_\n    ) {\n\n        uint _oiLong;\n        uint _oiShort;\n\n        ( _oiLong, _oiShort, fundingPaid_ ) = computeFunding(\n            __oiLong__,\n            __oiShort__,\n            _epochs,\n            _k\n        );\n\n        __oiLong__ = _oiLong;\n        __oiShort__ = _oiShort;\n\n        emit FundingPaid(_oiLong, _oiShort, fundingPaid_);\n\n    }\n\n    /// @notice Adds open interest to one side\n    /// @dev Adds open interest to one side, asserting the cap is not breached.\n    /// @param _isLong If open interest is adding to the long or short side.\n    /// @param _openInterest Open interest to add.\n    /// @param _oiCap Open interest cap to require not to be breached.\n    function addOi(\n        bool _isLong,\n        uint256 _openInterest,\n        uint256 _oiCap\n    ) internal {\n\n        if (_isLong) {\n\n            oiLongShares += _openInterest;\n\n            uint _oiLong = __oiLong__ + _openInterest;\n\n            require(_oiLong <= _oiCap, \"OVLV1:>cap\");\n\n            __oiLong__ = _oiLong;\n\n        } else {\n\n            oiShortShares += _openInterest;\n\n            uint _oiShort = __oiShort__ + _openInterest;\n\n            require(_oiShort <= _oiCap, \"OVLV1:>cap\");\n\n            __oiShort__ = _oiShort;\n\n        }\n\n    }\n\n    /// @notice Internal function to retrieve up to date open interest.\n    /// @dev Computes the current open interest values and returns them.\n    /// @param _compoundings Number of compoundings yet to be paid in funding.\n    /// @return oiLong_ Current open interest on the long side.\n    /// @return oiShort_ Current open interest on the short side.\n    /// @return oiLongShares_ Current open interest shares on the long side.\n    /// @return oiShortShares_ Current open interest shares on the short side.\n    function _oi (\n        uint _compoundings\n    ) internal view returns (\n        uint oiLong_,\n        uint oiShort_,\n        uint oiLongShares_,\n        uint oiShortShares_\n    ) {\n\n        oiLong_ = __oiLong__;\n        oiShort_ = __oiShort__;\n        oiLongShares_ = oiLongShares;\n        oiShortShares_ = oiShortShares;\n\n        if (0 < _compoundings) {\n\n            ( oiLong_, oiShort_, ) = computeFunding(\n                oiLong_,\n                oiShort_,\n                _compoundings,\n                k\n            );\n\n        }\n\n    }\n\n    /// @notice The current open interest on both sides of the market.\n    /// @dev Returns all up to date open interest data for the market.\n    /// @return oiLong_ Current open interest on long side.\n    /// @return oiShort_ Current open interest on short side.\n    /// @return oiLongShares_ Current open interest shares on the long side.\n    /// @return oiShortShares_ Current open interest shares on the short side.\n    function oi () public view returns (\n        uint oiLong_,\n        uint oiShort_,\n        uint oiLongShares_,\n        uint oiShortShares_\n    ) {\n\n        ( uint _compoundings, ) = epochs(block.timestamp, compounded);\n\n        (   oiLong_,\n            oiShort_,\n            oiLongShares_,\n            oiShortShares_ ) = _oi(_compoundings);\n\n    }\n\n\n    /// @notice The current open interest on the long side.\n    /// @return oiLong_ The current open interest on the long side.\n    function oiLong () external view returns (uint oiLong_) {\n        (   oiLong_,,, ) = oi();\n    }\n\n\n    /// @notice The current open interest on the short side.\n    /// @return oiShort_ The current open interest on the short side.\n    function oiShort () external view returns (uint oiShort_) {\n        (  ,oiShort_,, ) = oi();\n    }\n\n}"
}