Take perpetual balance from spot state for unified accounts - #838
Merged
Conversation
Unified account and portfolio margin keep the trading balance in the spot clearinghouse, so the perpetual clearinghouse reports zeros for them and the socket kept overwriting the balance with those zeros. Subscribe to spotState for such accounts and take the balance from tokenToAvailableAfterMaintenance, which is what Hyperliquid reports as available. The clearinghouse stays the source of the balance for standard accounts, so their equity keeps updating with position PnL over the socket.
For unified accounts the perpetual collateral is the spot balance itself, so adding it to the wallet total counted the same money twice. Store the account mode per wallet when the observer resolves it, and skip the perpetual term for such wallets. Standard accounts keep a separate pot and stay unchanged.
The spot balance reported the full total as available and ignored the held part, so sending offered funds that back an open position or a resting order. Take the amount Hyperliquid reports as available after maintenance margin when it is present, and fall back to total minus hold otherwise.
gemdev111
reviewed
Aug 5, 2026
gemdev111
reviewed
Aug 5, 2026
gemdev111
approved these changes
Aug 5, 2026
The account mode gateway call now returns a result instead of silently falling back to standard, which would drop the spot state subscription for a unified wallet. Both apps fall back to the mode stored for the wallet instead.
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.
Perpetual balance jumped between the real value and zero for wallets with unified account mode enabled. Their trading money lives in the spot balance, while the app read it from the perpetual account, which is empty for such wallets.
Now the balance for those wallets comes from the spot state and the available amount is the one Hyperliquid itself reports. The same money is no longer counted twice in the wallet total, and sending no longer offers the part that backs an open position. Wallets in the standard mode keep working as before.
Closes #675