Skip to content

Check Leverage on Withdrawals and Updates (backport #3153)#3216

Open
mergify[bot] wants to merge 1 commit intorelease/protocol/v9.xfrom
mergify/bp/release/protocol/v9.x/pr-3153
Open

Check Leverage on Withdrawals and Updates (backport #3153)#3216
mergify[bot] wants to merge 1 commit intorelease/protocol/v9.xfrom
mergify/bp/release/protocol/v9.x/pr-3153

Conversation

@mergify
Copy link
Copy Markdown
Contributor

@mergify mergify Bot commented Oct 24, 2025

Changelist

Now that we use leverage in our margining calculations - we should also ensure the same collateral checks are being applied in other flows. The 2 main ones here are validating that withdrawals and incoming leverage updates respect this.

For withdrawals, we get the check for free since it lives in the core risk calculation. The test creates 2 identical positions in 2 identical subaccounts, except one has a lower leverage set, and ensure that the withdrawable balance is lower than one without leverage set.

For incoming leverage updates, we take the new configuration and run it against the same risk checks as order placement, but with empty position updates. This will use the new leverage map to check if the position passes initial collateralization checks. If it does, we update the leverage map with the new configuration.

Test Plan

Added tests in leverage_e2e_test to test both these cases.

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced leverage update validation to verify margin requirements before applying changes, preventing operations that would violate collateralization constraints.
    • Added comprehensive error handling for leverage updates that conflict with margin requirements.
  • Tests

    • Added end-to-end tests validating withdrawal scenarios with leverage positions and margin constraints.
    • Added tests verifying leverage update behavior with existing positions.

This is an automatic backport of pull request #3153 done by [Mergify](https://mergify.com).

(cherry picked from commit 70ac841)

# Conflicts:
#	protocol/x/clob/keeper/leverage_e2e_test.go
#	protocol/x/subaccounts/keeper/leverage.go
#	protocol/x/subaccounts/types/errors.go
@mergify mergify Bot added the conflicts label Oct 24, 2025
@mergify mergify Bot requested a review from a team as a code owner October 24, 2025 18:59
@mergify
Copy link
Copy Markdown
Contributor Author

mergify Bot commented Oct 24, 2025

Cherry-pick of 70ac841 has failed:

On branch mergify/bp/release/protocol/v9.x/pr-3153
Your branch is up to date with 'origin/release/protocol/v9.x'.

You are currently cherry-picking commit 70ac8412.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   protocol/x/subaccounts/keeper/subaccount.go

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	deleted by us:   protocol/x/clob/keeper/leverage_e2e_test.go
	deleted by us:   protocol/x/subaccounts/keeper/leverage.go
	both modified:   protocol/x/subaccounts/types/errors.go

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 24, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

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

Development

Successfully merging this pull request may close these issues.

1 participant