Open Positions
Open, size, and adjust leveraged taker positions with the PerpCity TypeScript SDK.
A taker position is a leveraged long or short, opened by swapping against the market's pool. You choose margin (USDC) and size (perpDelta); leverage is the ratio between the position's notional value and its margin.
Open a position
Opening takes three steps: size the position, quote it, and turn the quote into a slippage limit.
import {
derivePerpDelta,
estimateTakerPosition,
calculateTakerSlippageLimit,
openTakerPosition,
} from '@strobelabs/perpcity-sdk';
const perpAddress = process.env.PERP_ADDRESS as `0x${string}`;
const margin = 100; // $100 USDC
const leverage = 3;
const isLong = true;
// 1. Size: convert margin x leverage at the current mark price into perp tokens
const { mark } = await context.getPerpData(perpAddress);
const perpDelta = derivePerpDelta({ margin, leverage, price: mark, isLong });
// 2. Quote the swap against the pool (client-side math, no transaction)
const quote = await estimateTakerPosition(context, perpAddress, { isLong, margin, leverage });
if (quote.exceedsLiquidity) throw new Error('Order is larger than the pool can fill');
// 3. Slippage limit: max USD paid for a long, min USD received for a short (1% here)
const amt1Limit = calculateTakerSlippageLimit(quote, isLong, 1);
const position = await openTakerPosition(context, perpAddress, {
margin,
perpDelta,
amt1Limit,
});
console.log(`Opened position ${position.positionId} in tx ${position.txHash}`);openTakerPosition approves USDC for the market if needed, simulates the call, sends it, waits for the receipt, and returns an OpenPosition read from the TakerOpened event.
| Parameter | Type | Description |
|---|---|---|
margin | number | USDC margin in human units; at least $5 |
perpDelta | bigint | Signed size in raw perp tokens, 6 decimals (1_000_000n = 1 token; positive = long, negative = short) |
amt1Limit | bigint | USD leg limit in raw USDC: the most you pay (long) or the least you receive (short) |
estimateTakerPosition returns the fee-exclusive fillPrice and usdDelta, the fee-inclusive effectiveFillPrice and effectiveUsdDelta, the feeRate, and two flags: exceedsLiquidity (the swap would likely revert with PriceImpactTooHigh) and liquidityLimited (the order likely crosses into thinner liquidity, so the estimate is optimistic). For an exact multi-tick quote, use fetchPoolTickMap with simulateTakerSwapExact.
Leverage limits
Each market has margin and leverage bounds:
const { bounds } = await context.getPerpData(perpAddress);
console.log(bounds.minMargin, bounds.minTakerLeverage, bounds.maxTakerLeverage);Positions are liquidated when their margin falls below the market's liquidation ratio; see Liquidations.
Adjust a position
adjustTaker changes size and margin in one transaction. perpDelta is signed: adding to a long is positive, reducing it negative.
import { estimateTakerAdjust, calculateTakerSlippageLimit, adjustTaker } from '@strobelabs/perpcity-sdk';
const perpDelta = 500_000n; // add 0.5 perp tokens to a long (6 decimals)
const quote = await estimateTakerAdjust(context, perpAddress, { perpDelta });
const { txHash } = await adjustTaker(context, perpAddress, {
posId: position.positionId,
marginDelta: 0n, // raw USDC; positive adds margin, negative withdraws
perpDelta,
amt1Limit: calculateTakerSlippageLimit(quote, perpDelta > 0n, 1),
});To change only the margin, use adjustMargin:
import { adjustMargin } from '@strobelabs/perpcity-sdk';
await adjustMargin(context, perpAddress, position.positionId, 50_000_000n); // add $50Batched and account-abstraction flows
Every action has a calldata builder that returns { to, data, value } instead of sending, for ERC-4337 user operations or multicall: buildOpenTakerPositionCalls (includes the USDC approval when needed), buildOpenTakerPositionCall, buildAdjustTakerCall(s), buildApproveUsdcCall, and the maker equivalents.