Provide Liquidity
Open and manage maker (liquidity provider) positions with the PerpCity TypeScript SDK.
A maker position provides concentrated liquidity to a market's pool over a price range. Takers trade against it, and the maker earns trading fees and utilization fees, while taking on the inventory risk of the range. Maker positions are unlevered: margin must cover the full value of the range. See Liquidity for the mechanics.
Open a maker position
import {
approveUsdc,
calculateAlignedTicks,
estimateLiquidity,
openMakerPosition,
scale6Decimals,
} from '@strobelabs/perpcity-sdk';
const perpAddress = process.env.PERP_ADDRESS as `0x${string}`;
const margin = 500; // $500 USDC
const priceLower = 40;
const priceUpper = 60;
// Ticks must sit on the market's tick spacing; this rounds the range outward
const { tickSpacing } = await context.getPerpData(perpAddress);
const { alignedTickLower, alignedTickUpper } = calculateAlignedTicks(priceLower, priceUpper, tickSpacing);
// Largest liquidity the margin can collateralize over the range
// (simulated against the contract's health check)
const liquidity = await estimateLiquidity(
context,
perpAddress,
alignedTickLower,
alignedTickUpper,
scale6Decimals(margin),
);
const maxAmt1In = margin; // max USDC the pool may take
// openMakerPosition only approves the margin; the pool can also take up to maxAmt1In
await approveUsdc(context, scale6Decimals(margin + maxAmt1In), perpAddress);
const position = await openMakerPosition(context, perpAddress, {
margin,
priceLower,
priceUpper,
liquidity,
maxAmt0In: 10_000, // max perp tokens the pool may take (number = human units)
maxAmt1In,
});
console.log(`Opened maker position ${position.positionId}`);openMakerPosition aligns the prices to ticks itself, sends the transaction, and returns an OpenPosition read from the MakerOpened event. It approves only the margin, while the pool can also take up to maxAmt1In USDC, which is why the example approves margin + maxAmt1In first.
| Parameter | Type | Description |
|---|---|---|
margin | number | USDC margin in human units |
priceLower, priceUpper | number | Price range |
liquidity | bigint | Liquidity to add, for example from estimateLiquidity |
maxAmt0In, maxAmt1In | number or bigint | Slippage caps on the perp and USDC amounts the pool takes; a number is scaled ×1e6, a bigint is raw |
calculateLiquidityForTargetRatio computes liquidity for a target margin ratio without the on-chain simulation.
Adjust or remove liquidity
import { adjustMaker } from '@strobelabs/perpcity-sdk';
await adjustMaker(context, perpAddress, {
posId: position.positionId,
marginDelta: 0n, // raw USDC
liquidityDelta: -liquidity / 2n, // remove half
amt0Limit: 0n, // when removing: minimum perp tokens out (0 = no check)
amt1Limit: 0n, // when removing: minimum USDC out (0 = no check)
});When adding liquidity the two limits are maximums the pool may take; when removing they are minimums you must receive.
Removing all liquidity closes the position; see Close Positions. If the range holds a perp inventory at that point, the same position ID becomes a taker position holding that inventory, which you then close like any taker.