{
    "Function": "computeFunding",
    "File": "contracts/market/OverlayV1OI.sol",
    "Parent Contracts": [],
    "High-Level Calls": [
        "FixedPoint",
        "FixedPoint",
        "FixedPoint",
        "FixedPoint",
        "FixedPoint"
    ],
    "Internal Calls": [],
    "Library Calls": [
        "powUp",
        "sub",
        "mulDown",
        "mulUp",
        "mulDown"
    ],
    "Low-Level Calls": [],
    "Code": "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    }"
}