{
    "Function": "removeMargin",
    "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": [
        "IClearingHouse",
        "SafeCast",
        "SafeCast",
        "SafeERC20",
        "IClearingHouse"
    ],
    "Internal Calls": [
        "_transferOutVusd",
        "_msgSender",
        "_blockTimestamp",
        "require(bool,string)",
        "whenNotPaused",
        "require(bool,string)",
        "require(bool,string)"
    ],
    "Library Calls": [
        "safeTransfer",
        "toInt256",
        "toInt256"
    ],
    "Low-Level Calls": [],
    "Code": "function removeMargin(uint idx, uint256 amount) override external whenNotPaused {\n        address trader = _msgSender();\n\n        // credit funding payments\n        clearingHouse.updatePositions(trader);\n\n        require(margin[VUSD_IDX][trader] >= 0, \"Cannot remove margin when vusd balance is negative\");\n        require(margin[idx][trader] >= amount.toInt256(), \"Insufficient balance\");\n\n        margin[idx][trader] -= amount.toInt256();\n\n        // Check minimum margin requirement after withdrawal\n        require(clearingHouse.isAboveMinAllowableMargin(trader), \"MA.removeMargin.Below_MM\");\n\n        if (idx == VUSD_IDX) {\n            _transferOutVusd(trader, amount);\n        } else {\n            supportedCollateral[idx].token.safeTransfer(trader, amount);\n        }\n        emit MarginRemoved(trader, idx, amount, _blockTimestamp());\n    }"
}