Skip to content

Commit

Permalink
use SafeCast
Browse files Browse the repository at this point in the history
  • Loading branch information
snreynolds committed Nov 6, 2024
1 parent ae60121 commit cb47d77
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/libraries/LiquidityAmounts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ pragma solidity ^0.8.20;

import {FullMath} from "@uniswap/v4-core/src/libraries/FullMath.sol";
import {FixedPoint96} from "@uniswap/v4-core/src/libraries/FixedPoint96.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";

/// @notice Provides functions for computing liquidity amounts from token amounts and prices
library LiquidityAmounts {
/// @notice Downcasts uint256 to uint128
/// @param x The uint258 to be downcasted
/// @return y The passed value, downcasted to uint128
function toUint128(uint256 x) private pure returns (uint128 y) {
require((y = uint128(x)) == x, "liquidity overflow");
}
using SafeCast for uint256;

/// @notice Computes the amount of liquidity received for a given amount of token0 and price range
/// @dev Calculates amount0 * (sqrt(upper) * sqrt(lower)) / (sqrt(upper) - sqrt(lower))
Expand All @@ -26,7 +22,7 @@ library LiquidityAmounts {
{
if (sqrtPriceAX96 > sqrtPriceBX96) (sqrtPriceAX96, sqrtPriceBX96) = (sqrtPriceBX96, sqrtPriceAX96);
uint256 intermediate = FullMath.mulDiv(sqrtPriceAX96, sqrtPriceBX96, FixedPoint96.Q96);
return toUint128(FullMath.mulDiv(amount0, intermediate, sqrtPriceBX96 - sqrtPriceAX96));
return FullMath.mulDiv(amount0, intermediate, sqrtPriceBX96 - sqrtPriceAX96).toUint128();
}

/// @notice Computes the amount of liquidity received for a given amount of token1 and price range
Expand All @@ -41,7 +37,7 @@ library LiquidityAmounts {
returns (uint128 liquidity)
{
if (sqrtPriceAX96 > sqrtPriceBX96) (sqrtPriceAX96, sqrtPriceBX96) = (sqrtPriceBX96, sqrtPriceAX96);
return toUint128(FullMath.mulDiv(amount1, FixedPoint96.Q96, sqrtPriceBX96 - sqrtPriceAX96));
return FullMath.mulDiv(amount1, FixedPoint96.Q96, sqrtPriceBX96 - sqrtPriceAX96).toUint128();
}

/// @notice Computes the maximum amount of liquidity received for a given amount of token0, token1, the current
Expand Down

0 comments on commit cb47d77

Please sign in to comment.