Skip to content

Conversation

@LukasDeco
Copy link
Collaborator

@LukasDeco LukasDeco commented Oct 30, 2025

This PR updates the SDK’s margin calculation logic to:

  1. Support isolated positions, and
  2. Align more closely with the on-chain margin logic for improved consistency.
  3. Bonus purpose: make margin and collateral calculations more efficient

Previously, the SDK would loop through user perp and spot positions multiple times, doing a lot of redundant work. With the new methods added, this PR refactors those flows so that margin is computed in a single, cleaner pass — reducing duplication and making the results better match the on-chain behavior. Through the new User.getMarginCalculation method, this is the central logic for calculating collateral and margin requirement. Now methods like User.getFreeCollateral call this function internally.

Commits:

  • refactor: margin calc matches on chain func
  • fix: hadnle iso balance undefined
  • feat: margin calc tests

@LukasDeco
Copy link
Collaborator Author

@crispheaney does the User.calculateFreeCollateralDeltaForPerp function need to change to support isolated positions? I don't think so but I wanted to double check.

@crispheaney
Copy link
Member

calculateFreeCollateralDeltaForPerp doesnt have to change since it doesnt look at deposits at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants