{
    "Function": "calculateSwapReverse",
    "File": "contracts/dex/math/VaderMath.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [
        "root",
        "require(bool,string)",
        "root"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function calculateSwapReverse(\r\n        uint256 amountOut,\r\n        uint256 reserveIn,\r\n        uint256 reserveOut\r\n    ) public pure returns (uint256 amountIn) {\r\n        // X * Y\r\n        uint256 XY = reserveIn * reserveOut;\r\n\r\n        // 2y\r\n        uint256 y2 = amountOut * 2;\r\n\r\n        // 4y\r\n        uint256 y4 = y2 * 2;\r\n\r\n        require(\r\n            y4 < reserveOut,\r\n            \"VaderMath::calculateSwapReverse: Desired Output Exceeds Maximum Output Possible (1/4 of Liquidity Pool)\"\r\n        );\r\n\r\n        // root(-X^2 * Y * (4y - Y))    =>    root(X^2 * Y * (Y - 4y)) as Y - 4y >= 0    =>    Y >= 4y holds true\r\n        uint256 numeratorA = root(XY) * root(reserveIn * (reserveOut - y4));\r\n\r\n        // X * (2y - Y)    =>    2yX - XY\r\n        uint256 numeratorB = y2 * reserveIn;\r\n        uint256 numeratorC = XY;\r\n\r\n        // -1 * (root(-X^2 * Y * (4y - Y)) + (X * (2y - Y)))    =>    -1 * (root(X^2 * Y * (Y - 4y)) + 2yX - XY)    =>    XY - root(X^2 * Y * (Y - 4y) - 2yX\r\n        uint256 numerator = numeratorC - numeratorA - numeratorB;\r\n\r\n        // 2y\r\n        uint256 denominator = y2;\r\n\r\n        amountIn = numerator / denominator;\r\n    }"
}