feat: adjust primitives for position adjustment (0.13.0)#57
Conversation
adjustTaker/adjustMaker/adjustMargin now await receipts and throw on revert (aligned with open/close); estimateTakerAdjust quotes an arbitrary signed delta via the client-side swap model; buildAdjustTakerCalls/ buildAdjustMakerCalls bundle the conditional USDC approve for gasless userOp batching. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis PR adds an ChangesPerp Adjust Feature
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant estimateTakerAdjust
participant PerpCityContext
participant simulateTakerSwap
Caller->>estimateTakerAdjust: estimateTakerAdjust(perpAddress, perpDelta)
estimateTakerAdjust->>estimateTakerAdjust: validate perpDelta != 0
estimateTakerAdjust->>PerpCityContext: getPerpData(perpAddress)
PerpCityContext-->>estimateTakerAdjust: pool state
estimateTakerAdjust->>simulateTakerSwap: simulate(perpDelta, state)
simulateTakerSwap-->>estimateTakerAdjust: usdDelta, fillPrice, exceedsLiquidity
estimateTakerAdjust-->>Caller: EstimateTakerAdjustResult
sequenceDiagram
participant Caller
participant buildAdjustTakerCalls
participant PublicClient
participant USDC
Caller->>buildAdjustTakerCalls: params(marginDelta, perpDelta, amt1Limit)
buildAdjustTakerCalls->>PublicClient: readContract(allowance)
PublicClient-->>buildAdjustTakerCalls: allowance
alt marginDelta > 0 and allowance insufficient
buildAdjustTakerCalls->>USDC: build approve(PERP, marginDelta)
end
buildAdjustTakerCalls->>buildAdjustTakerCalls: build adjustTaker call
buildAdjustTakerCalls-->>Caller: CallData[]
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/functions/perp-actions.ts (1)
400-401: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicated revert-check snippet across adjustTaker/adjustMaker.
The receipt-wait-and-throw block is now repeated verbatim in
adjustTakerandadjustMaker(and reportedly inopenTakerPosition/closePositionper the PR description). Consider extracting a small helper, e.g.waitForReceiptOrThrow(context.publicClient, txHash), to keep this logic single-sourced.♻️ Proposed helper extraction
+async function waitForReceiptOrThrow( + publicClient: PublicClient, + txHash: Hex +): Promise<void> { + const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash }); + if (receipt.status === "reverted") throw new Error(`Transaction reverted. Hash: ${txHash}`); +}Then in
adjustTaker/adjustMaker:- const receipt = await context.publicClient.waitForTransactionReceipt({ hash: txHash }); - if (receipt.status === "reverted") throw new Error(`Transaction reverted. Hash: ${txHash}`); + await waitForReceiptOrThrow(context.publicClient, txHash);Also applies to: 431-432
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/functions/perp-actions.ts` around lines 400 - 401, The receipt wait and reverted-transaction check is duplicated in `adjustTaker` and `adjustMaker` (and likely the similar position functions), so extract it into a shared helper such as `waitForReceiptOrThrow` that takes `context.publicClient` and `txHash`. Update `adjustTaker`, `adjustMaker`, and any other callers like `openTakerPosition`/`closePosition` to use the helper so the transaction receipt logic is single-sourced and consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/functions/perp-actions.ts`:
- Around line 400-401: The receipt wait and reverted-transaction check is
duplicated in `adjustTaker` and `adjustMaker` (and likely the similar position
functions), so extract it into a shared helper such as `waitForReceiptOrThrow`
that takes `context.publicClient` and `txHash`. Update `adjustTaker`,
`adjustMaker`, and any other callers like `openTakerPosition`/`closePosition` to
use the helper so the transaction receipt logic is single-sourced and
consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: adcabfea-5257-4043-be51-32d463a0b631
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (5)
package.jsonsrc/__tests__/unit/adjust-primitives.test.tssrc/functions/calldata.tssrc/functions/perp-actions.tssrc/types/entity-data.ts
Phase 4 of the position-adjustment plan (perpcity-client thoughts/shared/plans/2026-07-08-position-adjustment.md).
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes
Chores