Skip to content

perpetuals: fix mark-price scaling bug, add ReduceCollateral, expand test coverage#42

Closed
faddat with Copilot wants to merge 6 commits into
mainfrom
copilot/build-perpetual-swaps-module
Closed

perpetuals: fix mark-price scaling bug, add ReduceCollateral, expand test coverage#42
faddat with Copilot wants to merge 6 commits into
mainfrom
copilot/build-perpetual-swaps-module

Conversation

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

All four ledger tests were failing because test mark prices were multiplied by PRICE_SCALE — the internal fixed-point factor for quantities, not prices. This caused quantity_from_collateral to always round to zero, returning InvalidCollateral on every open_position call. The liquidation test had a second bug: a 4% price drop on a 5× leveraged position leaves equity well above the maintenance threshold, so liquidation never triggered.

Bug fixes

  • Mark price scaling: Remove * PRICE_SCALE from all test mark prices. Mark prices are plain quote-asset integers (50_000 for $50k BTC); PRICE_SCALE = 1_000_000_000 is only the precision divisor in quantity arithmetic.
  • Liquidation threshold: Drop test price from 48_000 to 40_000. At 5× leverage + 5% maintenance margin, equity at 48k is 800 vs. maintenance 240 — not liquidatable. At 40k equity reaches 0.

New: ReduceCollateral operation

Symmetric counterpart to AddCollateral (wire tag 6). Owners can withdraw excess margin, with a guard that simulates the post-reduction position and rejects if it would fall into liquidatable territory:

PerpetualOperation::ReduceCollateral { position, amount }
// Returns Err(CollateralReductionWouldCauseLiquidation) if the reduced
// position's equity would be ≤ maintenance requirement at current mark price.

Added CollateralReductionWouldCauseLiquidation as a distinct error variant (separate from CollateralUnderflow which covers arithmetic underflow when amount > collateral).

Test coverage (6 → 25)

  • Ledger: unauthorized add/close, nonce mismatch, short-side profit, add+reduce collateral round-trip, blocked reduction near liquidation threshold, underwater-position close prevention, open-interest accounting across close + liquidation
  • Transaction codec: roundtrip for all 8 operations, encode_size matches wire length, stable discriminant constants, signed-transaction roundtrip + verify
  • Genesis: JSON roundtrip, market creation from genesis, zero-price and invalid-hex rejection

Comment thread perpetuals/src/ledger.rs

let tx = Transaction::sign(
&alice_key,
0,
Comment thread perpetuals/src/ledger.rs

let tx_wrong_nonce = Transaction::sign(
&alice_key,
1, // expected 0
Comment thread perpetuals/src/transaction.rs Fixed
Comment thread perpetuals/src/transaction.rs Fixed
collateral: 1_000,
leverage_bps: 20_000,
};
let tx = Transaction::sign(&key, 0, op);
let key = PrivateKey::ed25519_from_seed(7);
let tx = Transaction::sign(
&key,
999,
faddat and others added 2 commits June 17, 2026 23:52
…phic value'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…phic value'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@faddat

faddat commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

@copilot fix the actions that don't pass

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix the actions that don't pass

Fixed. The fmt job was failing due to formatting violations in genesis.rs, ledger.rs, and transaction.rs — a trailing blank line in two files and a line-length issue in the other two. Running cargo fmt resolved all of them (commit perpetuals: fix rustfmt formatting violations).

Copilot AI requested a review from faddat June 17, 2026 22:02
@faddat

faddat commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

@copilot fix actions that don't pass

@distractedm1nd

Copy link
Copy Markdown
Collaborator

Closing in favor of #79

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.

4 participants