CP-14881: add adjust margin (add/remove) to isolated perps positions - #4010
Open
bogdandobritoiu wants to merge 2 commits into
Open
CP-14881: add adjust margin (add/remove) to isolated perps positions#4010bogdandobritoiu wants to merge 2 commits into
bogdandobritoiu wants to merge 2 commits into
Conversation
bogdandobritoiu
requested review from
B0Y3R-AVA,
atn4z7,
onghwan and
ruijia1in
as code owners
July 23, 2026 20:43
bogdandobritoiu
marked this pull request as draft
July 23, 2026 20:46
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an Adjust margin modal flow for isolated perpetuals positions in core-mobile, including UI entry points from the Manage position screen and new economics utilities (ported from core-web) to compute removable margin and margin-based liquidation estimates. It also extends k2-alpine input presets/segmented control to support the new UI requirements.
Changes:
- Add isolated-position “Margin” entry point on Manage screen and a new
PerpetualsAdjustMarginScreenmodal to add/remove isolated margin. - Port/introduce perps economics helpers (
maxRemovableMarginUsd,estimateLiquidationPriceFromMargin,floorToDecimals) with unit tests. - Extend
k2-alpineTokenUnitInputWidgetpresets to support percent-of-live-balance presets andSegmentedControlto accept a custom height.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/k2-alpine/src/components/TokenUnitInput/TokenUnitInputWidget.tsx | Adds percent-based presets resolved against live balance; includes zero-max no-op behavior. |
| packages/k2-alpine/src/components/SegmentedControl/SegmentedControl.tsx | Adds optional height prop for custom segment row height. |
| packages/core-mobile/app/new/routes/(signedIn)/(modals)/perpetualsManage/adjustMargin.tsx | Expo Router entrypoint exporting the new Adjust Margin screen. |
| packages/core-mobile/app/new/routes/(signedIn)/(modals)/perpetualsManage/_layout.tsx | Registers the adjustMargin modal route in the perps manage stack. |
| packages/core-mobile/app/new/features/trade/perpetuals/utils/economics.ts | Adds economics helpers for removable margin, liquidation estimate from margin, and safe flooring. |
| packages/core-mobile/app/new/features/trade/perpetuals/utils/economics.test.ts | Adds unit tests covering the new economics helpers. |
| packages/core-mobile/app/new/features/trade/perpetuals/screens/PerpetualsWithdrawScreen.tsx | Adds padding for loading/error modal states. |
| packages/core-mobile/app/new/features/trade/perpetuals/screens/PerpetualsManageScreen.tsx | Adds isolated-only Margin row showing live marginUsed and navigation to Adjust Margin. |
| packages/core-mobile/app/new/features/trade/perpetuals/screens/PerpetualsBalanceScreen.tsx | Adds padding for loading/error modal states. |
| packages/core-mobile/app/new/features/trade/perpetuals/screens/PerpetualsAdjustMarginScreen.tsx | Implements the Adjust Margin modal UI + submission via updateIsolatedMargin. |
| packages/core-mobile/app/new/features/trade/perpetuals/components/PositionCard.tsx | Uses SplitButton for Market/Limit close actions in expanded position card. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Coverage report ✅2/2 packages passed thresholds 🟢 @avalabs/core-mobile
🟢 @avalabs/k2-alpine
Artifacts and threshold sources
Source run: Mobile PR |
- remove-mode caption shows the removable max, not account balance - TokenUnitInputWidget: percent presets take precedence over value (per docs), zero no-op scoped to percent presets - floorToDecimals: clamp decimals to a safe non-negative integer Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Comment on lines
+148
to
+152
| const safeDecimals = Number.isFinite(decimals) | ||
| ? Math.max(0, Math.trunc(decimals)) | ||
| : 0 | ||
| const factor = 10 ** safeDecimals | ||
| return Math.max(0, Math.floor(value * factor) / factor) |
Comment on lines
+261
to
+270
| <SplitButton | ||
| left={{ | ||
| children: 'Market close', | ||
| onPress: handleMarketClose | ||
| }} | ||
| right={{ | ||
| children: 'Limit close', | ||
| onPress: handleLimitClose | ||
| }} | ||
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Ticket: CP-14881
Adds the Adjust margin flow for isolated perps positions (Figma), mirroring core-web's
AdjustMarginModal.marginUsed, rendered only for isolated positions (cross positions share account-wide collateral). Drills into/perpetualsManage/adjustMargin.PerpetualsAdjustMarginScreen: Add/Remove segmented toggle, amount input with 25%/50%/100% presets of the active mode's max, details rows (current margin, margin available to add/remove, liquidation price with a live current → projected preview), and a Confirm gated by the enable-trading flow. Submits a signed USD delta via the previously unusedupdateIsolatedMargin(positive adds, negative removes). The position re-resolves live over WS while the sheet is open.utils/economics.ts, unit-tested):maxRemovableMarginUsd— Hyperliquid's transfer floormax(notional/setLeverage, 0.1·notional), unrealized PnL counted toward equity, 2% price-drift cushion, capped at deposited margin (prevents HL's "insufficient margin for reduction" reject).estimateLiquidationPriceFromMargin— margin-based liquidation estimate for the projection.floorToDecimals— the 100% preset can never round up past the true cap.TokenUnitInputWidgetpresets supportpercent(fraction of the livebalance, resolved at press time so they track balance updates); percentage presets no-op at zero max instead of writing a literal "0".SegmentedControlgains an optionalheightprop.SplitButton; padding fixes on the Balance/Withdraw loading and error states.No new dependencies.
Screenshots/Videos
TODO: add iOS/Android screenshots and recording.
Testing
Dev Testing
Happy path:
Edge cases:
Bitrise: iOS - 9405 / Android - 9405
QA Testing
Checklist
Please check all that apply (if applicable)
🤖 Generated with Claude Code