Positions
Concentrated liquidity positions define a price range [tickLower, tickUpper] in which your liquidity is active. The SDK provides the Position class and NonfungiblePositionManager for all position operations.
Creating a Position
Use Position.fromAmounts to model a position from desired token amounts. Ticks must be multiples of tickSpacing — use nearestUsableTick to align them.
import { Position, nearestUsableTick, DEFAULT_TICK_SPACING } from '@hydrexfi/hydrex-sdk';
const position = Position.fromAmounts({
pool,
tickLower: nearestUsableTick(tickCurrent - DEFAULT_TICK_SPACING * 10, DEFAULT_TICK_SPACING),
tickUpper: nearestUsableTick(tickCurrent + DEFAULT_TICK_SPACING * 10, DEFAULT_TICK_SPACING),
amount0: '1000000', // desired token0 amount
amount1: '500000000000000000', // desired token1 amount
useFullPrecision: true,
});
// Read token amounts at current tick
const { amount0, amount1 } = position.mintAmounts;Minting a New Position
Call NonfungiblePositionManager.addCallParameters without a tokenId to mint a new NFT position.
import {
NonfungiblePositionManager,
Percent,
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
ChainId
} from '@hydrexfi/hydrex-sdk';
// Mint a new position
const { calldata, value } = NonfungiblePositionManager.addCallParameters(position, {
slippageTolerance: new Percent(50, 10_000),
recipient: '0xYourWallet',
deadline: Math.floor(Date.now() / 1000) + 1200,
});
// Send tx to NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[ChainId.Base]Increasing Liquidity
Pass a tokenId to add liquidity to an existing NFT position.
// Increase liquidity on an existing position (tokenId: '42')
const { calldata, value } = NonfungiblePositionManager.addCallParameters(position, {
tokenId: '42',
slippageTolerance: new Percent(50, 10_000),
deadline: Math.floor(Date.now() / 1000) + 1200,
});Collecting Fees
Collect accrued trading fees from a position at any time without removing liquidity.
// Collect accrued fees
const { calldata, value } = NonfungiblePositionManager.collectCallParameters({
tokenId: '42',
recipient: '0xYourWallet',
expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(USDC, '0'),
expectedCurrencyOwed1: CurrencyAmount.fromRawAmount(WETH, '0'),
});Removing Liquidity
Remove some or all liquidity. The collectOptions field lets you claim fees in the same transaction.
// Remove 100% of liquidity + collect fees in one call
const { calldata, value } = NonfungiblePositionManager.removeCallParameters(position, {
tokenId: '42',
liquidityPercentage: new Percent(100, 100),
slippageTolerance: new Percent(50, 10_000),
deadline: Math.floor(Date.now() / 1000) + 1200,
collectOptions: {
expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(USDC, '0'),
expectedCurrencyOwed1: CurrencyAmount.fromRawAmount(WETH, '0'),
recipient: '0xYourWallet',
},
});Two-step withdrawal
collectOptions.