{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/dex/math/VaderMath.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "library VaderMath {\r\n    /* ========== CONSTANTS ========== */\r\n\r\n    uint256 public constant ONE = 1 ether;\r\n\r\n    /* ========== LIBRARY FUNCTIONS ========== */\r\n\r\n    /**\r\n     * @dev Calculates the amount of liquidity units for the {vaderDeposited}\r\n     * and {assetDeposited} amounts across {totalPoolUnits}.\r\n     *\r\n     * The {vaderBalance} and {assetBalance} are taken into account in order to\r\n     * calculate any necessary slippage adjustment.\r\n     */\r\n    function calculateLiquidityUnits(\r\n        uint256 vaderDeposited,\r\n        uint256 vaderBalance,\r\n        uint256 assetDeposited,\r\n        uint256 assetBalance,\r\n        uint256 totalPoolUnits\r\n    ) public pure returns (uint256) {\r\n        // slipAdjustment\r\n        uint256 slip = calculateSlipAdjustment(\r\n            vaderDeposited,\r\n            vaderBalance,\r\n            assetDeposited,\r\n            assetBalance\r\n        );\r\n\r\n        // (Va + vA)\r\n        uint256 poolUnitFactor = (vaderBalance * assetDeposited) +\r\n            (vaderDeposited * assetBalance);\r\n\r\n        // 2VA\r\n        uint256 denominator = ONE * 2 * vaderBalance * assetBalance;\r\n\r\n        // P * [(Va + vA) / (2 * V * A)] * slipAdjustment\r\n        return ((totalPoolUnits * poolUnitFactor) / denominator) * slip;\r\n    }\r\n\r\n    /**\r\n    * @dev Calculates the necessary slippage adjustment for the {vaderDeposited} and {assetDeposited}\r\n    * amounts across the total {vaderBalance} and {assetBalance} amounts.\r\n    */\r\n    function calculateSlipAdjustment(\r\n        uint256 vaderDeposited,\r\n        uint256 vaderBalance,\r\n        uint256 assetDeposited,\r\n        uint256 assetBalance\r\n    ) public pure returns (uint256) {\r\n        // Va\r\n        uint256 vaderAsset = vaderBalance * assetDeposited;\r\n\r\n        // aV\r\n        uint256 assetVader = assetBalance * vaderDeposited;\r\n\r\n        // (v + V) * (a + A)\r\n        uint256 denominator = (vaderDeposited + vaderBalance) *\r\n            (assetDeposited + assetBalance);\r\n\r\n        // 1 - [|Va - aV| / (v + V) * (a + A)]\r\n        return ONE - (delta(vaderAsset, assetVader) / denominator);\r\n    }\r\n\r\n    /**\r\n    * @dev Calculates the loss based on the supplied {releasedVader} and {releasedAsset}\r\n    * compared to the supplied {originalVader} and {originalAsset}.\r\n    */\r\n    function calculateLoss(\r\n        uint256 originalVader,\r\n        uint256 originalAsset,\r\n        uint256 releasedVader,\r\n        uint256 releasedAsset\r\n    ) public pure returns (uint256 loss) {\r\n        //\r\n        // TODO: Vader Formula Differs https://github.com/vetherasset/vaderprotocol-contracts/blob/main/contracts/Utils.sol#L347-L356\r\n        //\r\n\r\n        // [(A0 * P1) + V0]\r\n        uint256 originalValue = ((originalAsset * releasedVader) /\r\n            releasedAsset) + originalVader;\r\n\r\n        // [(A1 * P1) + V1]\r\n        uint256 releasedValue = ((releasedAsset * releasedVader) /\r\n            releasedAsset) + releasedVader;\r\n\r\n        // [(A0 * P1) + V0] - [(A1 * P1) + V1]\r\n        if (originalValue > releasedValue) loss = originalValue - releasedValue;\r\n    }\r\n\r\n    /**\r\n    * @dev Calculates the {amountOut} of the swap based on the supplied {amountIn}\r\n    * across the supplied {reserveIn} and {reserveOut} amounts.\r\n    */\r\n    function calculateSwap(\r\n        uint256 amountIn,\r\n        uint256 reserveIn,\r\n        uint256 reserveOut\r\n    ) public pure returns (uint256 amountOut) {\r\n        // x * Y * X\r\n        uint256 numerator = amountIn * reserveIn * reserveOut;\r\n\r\n        // (x + X) ^ 2\r\n        uint256 denominator = pow(amountIn + reserveIn);\r\n\r\n        amountOut = numerator / denominator;\r\n    }\r\n\r\n    /**\r\n    * @dev Calculates the {amountIn} of the swap based on the supplied {amountOut}\r\n    * across the supplied {reserveIn} and {reserveOut} amounts.\r\n    */\r\n    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    }\r\n\r\n    /**\r\n    * @dev Calculates the difference between the supplied {a} and {b} values as a positive number.\r\n    */\r\n    function delta(uint256 a, uint256 b) public pure returns (uint256) {\r\n        return a > b ? a - b : b - a;\r\n    }\r\n\r\n    /**\r\n    * @dev Calculates the power of 2 of the supplied {a} value.\r\n    */\r\n    function pow(uint256 a) public pure returns (uint256) {\r\n        return a * a;\r\n    }\r\n\r\n    /**\r\n    * @dev Calculates the square root {c} of the supplied {a} value utilizing the Babylonian method:\r\n    * https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method\r\n    */\r\n    function root(uint256 a) public pure returns (uint256 c) {\r\n        if (a > 3) {\r\n            c = a;\r\n            uint256 x = a / 2 + 1;\r\n            while (x < c) {\r\n                c = x;\r\n                x = (a / x + x) / 2;\r\n            }\r\n        } else if (a != 0) {\r\n            c = 1;\r\n        }\r\n    }\r\n}"
}