{
    "Function": "unwind",
    "File": "contracts/collateral/OverlayV1OVLCollateral.sol",
    "Parent Contracts": [
        "node_modules/@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol",
        "node_modules/@openzeppelin/contracts/token/ERC1155/ERC1155.sol",
        "node_modules/@openzeppelin/contracts/interfaces/draft-IERC6093.sol",
        "node_modules/@openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol",
        "node_modules/@openzeppelin/contracts/token/ERC1155/IERC1155.sol",
        "node_modules/@openzeppelin/contracts/utils/introspection/ERC165.sol",
        "node_modules/@openzeppelin/contracts/utils/introspection/IERC165.sol",
        "node_modules/@openzeppelin/contracts/utils/Context.sol"
    ],
    "High-Level Calls": [
        "Position",
        "Position",
        "IOverlayV1Market",
        "IOverlayV1Market",
        "IOverlayTokenNew",
        "FixedPoint",
        "IOverlayV1Market",
        "IOverlayV1Mothership",
        "IOverlayTokenNew"
    ],
    "Internal Calls": [
        "balanceOf",
        "require(bool,string)",
        "_burn",
        "totalSupply",
        "require(bool,string)"
    ],
    "Library Calls": [
        "mulUp",
        "oi",
        "notional"
    ],
    "Low-Level Calls": [],
    "Code": "function unwind (\n        uint256 _positionId,\n        uint256 _shares\n    ) external {\n\n        require( 0 < _shares && _shares <= balanceOf(msg.sender, _positionId), \"OVLV1:!shares\");\n\n        Position.Info storage pos = positions[_positionId];\n\n        require(0 < pos.oiShares, \"OVLV1:liquidated\");\n\n        {\n\n        (   uint _oi,\n            uint _oiShares,\n            uint _priceFrame ) = IOverlayV1Market(pos.market)\n                .exitData(\n                    pos.isLong,\n                    pos.pricePoint\n                );\n\n        uint _totalPosShares = totalSupply(_positionId);\n\n        uint _userOiShares = _shares;\n        uint _userNotional = _shares * pos.notional(_oi, _oiShares, _priceFrame) / _totalPosShares;\n        uint _userDebt = _shares * pos.debt / _totalPosShares;\n        uint _userCost = _shares * pos.cost / _totalPosShares;\n        uint _userOi = _shares * pos.oi(_oi, _oiShares) / _totalPosShares;\n\n        emit Unwind(pos.market, _positionId, _userOi, _userDebt);\n\n        // TODO: think through edge case of underwater position ... and fee adjustments ...\n        uint _feeAmount = _userNotional.mulUp(mothership.fee());\n\n        uint _userValueAdjusted = _userNotional - _feeAmount;\n        if (_userValueAdjusted > _userDebt) _userValueAdjusted -= _userDebt;\n        else _userValueAdjusted = 0;\n\n        fees += _feeAmount; // adds to fee pot, which is transferred on update\n\n        pos.debt -= _userDebt;\n        pos.cost -= _userCost;\n        pos.oiShares -= _userOiShares;\n\n        // ovl.transfer(msg.sender, _userCost);\n\n        // mint/burn excess PnL = valueAdjusted - cost\n        if (_userCost < _userValueAdjusted) {\n\n            ovl.transferMint(\n                msg.sender, \n                _userCost, \n                _userValueAdjusted - _userCost\n            );\n\n        } else {\n\n            ovl.transferBurn(\n                msg.sender, \n                _userValueAdjusted, \n                _userCost - _userValueAdjusted\n            );\n\n        }\n\n\n        IOverlayV1Market(pos.market).exitOI(\n            pos.isLong,\n            _userOi,\n            _userOiShares,\n            _userCost < _userValueAdjusted ? _userValueAdjusted - _userCost : 0,\n            _userCost < _userValueAdjusted ? 0 : _userCost - _userValueAdjusted\n        );\n\n        }\n\n        _burn(msg.sender, _positionId, _shares);\n\n    }"
}