nqs_sdk.bindings.protocols.uniswap_v3.uniswap_utils module¶
- exception nqs_sdk.bindings.protocols.uniswap_v3.uniswap_utils.InvalidPriceError[source]¶
Bases:
ValueError
Raised when price value is invalid (not strictly positive and finite).
- nqs_sdk.bindings.protocols.uniswap_v3.uniswap_utils.price_to_tick(price, decimals0, decimals1, tick_spacing=1, lower=True)[source]¶
Convert a price to a tick value for Uniswap V3.
- Parameters:
price (
Decimal
) – The price to convert (must be strictly positive and finite)decimals0 (
int
) – Number of decimals for token0decimals1 (
int
) – Number of decimals for token1tick_spacing (
int
) – Spacing between valid ticks (default: 1)lower (
bool
) – Whether to round down (True) or up (False) when between ticks
- Return type:
int
- Returns:
The corresponding tick value
- Raises:
InvalidPriceError – When the price is not strictly positive and finite (e.g., negative, zero, infinity, or NaN)
- nqs_sdk.bindings.protocols.uniswap_v3.uniswap_utils.tick_to_price(tick, decimals0, decimals1)[source]¶
- Return type:
Decimal
- nqs_sdk.bindings.protocols.uniswap_v3.uniswap_utils.calculate_max_amounts(price_lower, price, price_upper, amount0, amount1)[source]¶
Calculate the maximum liquidity that can be minted for a Uniswap V3 position.
This function determines the maximum amount of liquidity that can be created from the available token amounts (amount0 and amount1) for a concentrated liquidity position with specified price bounds.
- Parameters:
price_lower (Decimal) – Lower price bound of the position (must be positive)
price (Decimal) – Current price of the pool (must be positive)
price_upper (Decimal) – Upper price bound of the position (must be > price_lower)
amount0 (Decimal) – Available amount of token0 (must be >= 0)
amount1 (Decimal) – Available amount of token1 (must be >= 0)
- Returns:
Maximum liquidity that can be minted with the given token amounts
- Return type:
Decimal
- Raises:
AssertionError – If any of the following conditions are not met: - amount0 >= 0 - amount1 >= 0 - price_lower < price_upper - calculated liquidity >= 0
Notes
The calculation depends on where the current price falls relative to the position bounds:
If price <= price_lower: Only token0 is needed, liquidity is limited by amount0
If price_lower < price < price_upper: Both tokens are needed, liquidity is limited by whichever token provides less liquidity
If price >= price_upper: Only token1 is needed, liquidity is limited by amount1
This follows the standard Uniswap V3 concentrated liquidity formulas: - L = Δx / (1/√P - 1/√P_upper) for token0 - L = Δy / (√P - √P_lower) for token1
Example
>>> from decimal import Decimal >>> price_lower = Decimal('1.5') >>> price = Decimal('2.0') >>> price_upper = Decimal('2.5') >>> amount0 = Decimal('100') >>> amount1 = Decimal('200') >>> liquidity = calculate_max_amounts(price_lower, price, price_upper, amount0, amount1)
- nqs_sdk.bindings.protocols.uniswap_v3.uniswap_utils.token_amounts_from_liquidity(price_lower, price, price_upper, liquidity_amount)[source]¶
- Return type:
Tuple
[Decimal
,Decimal
]