Closed
perpetuals: fix mark-price scaling bug, add ReduceCollateral, expand test coverage#42
Conversation
Copilot created this pull request from a session on behalf of
faddat
June 17, 2026 21:48
View session
|
|
||
| let tx = Transaction::sign( | ||
| &alice_key, | ||
| 0, |
|
|
||
| let tx_wrong_nonce = Transaction::sign( | ||
| &alice_key, | ||
| 1, // expected 0 |
| 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, |
…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>
Contributor
|
@copilot fix the actions that don't pass |
Contributor
Author
Fixed. The |
Contributor
|
@copilot fix actions that don't pass |
Copilot stopped work on behalf of
faddat due to an error
June 18, 2026 00:07
Collaborator
|
Closing in favor of #79 |
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.
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 causedquantity_from_collateralto always round to zero, returningInvalidCollateralon everyopen_positioncall. 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
* PRICE_SCALEfrom all test mark prices. Mark prices are plain quote-asset integers (50_000for $50k BTC);PRICE_SCALE = 1_000_000_000is only the precision divisor in quantity arithmetic.48_000to40_000. At 5× leverage + 5% maintenance margin, equity at 48k is800vs. maintenance240— not liquidatable. At 40k equity reaches0.New:
ReduceCollateraloperationSymmetric counterpart to
AddCollateral(wire tag6). Owners can withdraw excess margin, with a guard that simulates the post-reduction position and rejects if it would fall into liquidatable territory:Added
CollateralReductionWouldCauseLiquidationas a distinct error variant (separate fromCollateralUnderflowwhich covers arithmetic underflow whenamount > collateral).Test coverage (6 → 25)
encode_sizematches wire length, stable discriminant constants, signed-transaction roundtrip + verify