Skip to content

fix: validate minimum margin for position opens#69

Merged
rdave8 merged 2 commits into
mainfrom
fix/min-opening-margin-validation
Jul 16, 2026
Merged

fix: validate minimum margin for position opens#69
rdave8 merged 2 commits into
mainfrom
fix/min-opening-margin-validation

Conversation

@rdave8

@rdave8 rdave8 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add reusable opening-margin validation for maker and taker positions.
  • Check scaled margin against MIN_OPENING_MARGIN.
  • Return ValidationError::InvalidMargin before transaction handling when margin is below the protocol minimum.

Context

The contracts require both openMaker and openTaker margins to be at least 5 USDC. Previously, the SDK only required a positive margin, allowing invalid parameters to reach contract simulation and revert with MarginTooLow.

Testing

  • Added boundary coverage immediately below, at, and above the minimum.

Summary by CodeRabbit

  • Bug Fixes
    • Enforced the protocol’s minimum opening margin when opening maker and taker positions.
    • Updated margin validation to reject values below the required threshold (around the 5.0 USDC boundary), preventing invalid position-opening requests.
    • Made margin handling consistent across maker and taker openings.
  • Tests
    • Added a unit test covering failure below the minimum and success at/above it.

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 5d13450f-b485-45c2-aa59-02ed29fba252

📥 Commits

Reviewing files that changed from the base of the PR and between 62514b6 and 8409527.

📒 Files selected for processing (1)
  • src/client/trades.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/client/trades.rs

📝 Walkthrough

Walkthrough

Opening margin handling scales values to six decimals and enforces the protocol minimum for taker and maker position openings, with boundary-focused unit tests.

Changes

Opening Margin Validation

Layer / File(s) Summary
Margin validation helper
src/client/trades.rs
Adds scale_opening_margin and imports the protocol minimum and six-decimal scaling helpers.
Trade entrypoint enforcement
src/client/trades.rs
Updates taker and maker opening paths to use minimum-margin validation and tests values below, at, and near the 5.0 boundary.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: enforcing a minimum margin when opening positions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/min-opening-margin-validation

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

@Praz314159 Praz314159 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the dedup

@rdave8
rdave8 merged commit e6aaa24 into main Jul 16, 2026
8 checks passed
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.

2 participants