{
    "Function": "settleBadDebt",
    "File": "contracts/MarginAccount.sol",
    "Parent Contracts": [
        "contracts/legos/HubbleBase.sol",
        "node_modules/@openzeppelin/contracts/metatx/ERC2771Context.sol",
        "node_modules/@openzeppelin/contracts/security/Pausable.sol",
        "node_modules/@openzeppelin/contracts/utils/Context.sol",
        "contracts/legos/Governable.sol",
        "node_modules/@openzeppelin/contracts/proxy/utils/Initializable.sol",
        "contracts/legos/Governable.sol",
        "contracts/Interfaces.sol"
    ],
    "High-Level Calls": [
        "SafeERC20",
        "SafeCast",
        "SafeCast",
        "SafeCast",
        "IClearingHouse",
        "IInsuranceFund"
    ],
    "Internal Calls": [
        "getSpotCollateralValue",
        "whenNotPaused",
        "require(bool,string)",
        "require(bool,string)",
        "require(bool,string)",
        "_blockTimestamp"
    ],
    "Library Calls": [
        "toUint256",
        "toUint256",
        "toUint256",
        "safeTransfer"
    ],
    "Low-Level Calls": [],
    "Code": "function settleBadDebt(address trader) external whenNotPaused {\n        (uint256 notionalPosition,) = clearingHouse.getTotalNotionalPositionAndUnrealizedPnl(trader);\n        require(notionalPosition == 0, \"Liquidate positions before settling bad debt\");\n\n        // The spot value of their collateral minus their vUSD obligation is a negative value\n        require(getSpotCollateralValue(trader) < 0, \"Above bad debt threshold\");\n\n        int vusdBal = margin[VUSD_IDX][trader];\n\n        // this check is not strictly required because getSpotCollateralValue(trader) < 0 is a stronger assertion\n        require(vusdBal < 0, \"Nothing to repay\");\n\n        uint badDebt = (-vusdBal).toUint256();\n        Collateral[] memory assets = supportedCollateral;\n\n        // This pulls the obligation\n        insuranceFund.seizeBadDebt(badDebt);\n        margin[VUSD_IDX][trader] = 0;\n\n        // Insurance fund gets all the available collateral\n        uint[] memory seized = new uint[](assets.length);\n        for (uint i = 1 /* skip vusd */; i < assets.length; i++) {\n            int amount = margin[i][trader];\n            if (amount > 0) {\n                margin[i][trader] = 0;\n                assets[i].token.safeTransfer(address(insuranceFund), amount.toUint256());\n                seized[i] = amount.toUint256();\n            }\n        }\n        emit SettledBadDebt(trader, seized, badDebt, _blockTimestamp());\n    }"
}