Return realized swap deltas on trade results; fix TakerClosed binding#59
Conversation
open_taker/adjust_taker now decode the taker swap from the receipt (reusing the feed's decode_log + SwapInfo) and return realized perp_delta/usd_delta on OpenResult/AdjustTakerResult — actual fills, not estimates. Both 0.0 for maker opens / margin-only adjusts (no swap). Fixes the TakerClosed binding: the deployed contract (ahead of the v0.2.1 tag) unifies close+liquidation, so the event carries two extra fields (uint256 liqFee, bool isLiquidation). Verified against the live topic0 0xc6d156…; added a SIGNATURE_HASH assertion to abi_lock so deployment drift is caught (the prior test only checked binding self-consistency). Fork test now asserts realized deltas on open/adjust/close. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds ChangesTaker Swap Delta Extraction
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/anvil_fork.rs (1)
402-409: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAssert the close delta magnitude too.
The close test only checks signs, so a wrong decoded negative swap would pass. Since
close_taker(pos_id, 0.0005, ...)should realize about-0.0005, mirror the open/adjust magnitude check here.Proposed test tightening
- assert!( - close_result.perp_delta < 0.0 && close_result.usd_delta > 0.0, + assert!( + (close_result.perp_delta - (-0.0005)).abs() < 1e-4 && close_result.usd_delta > 0.0, "close should sell perp and receive USD, got perp_delta={} usd_delta={}", close_result.perp_delta, close_result.usd_delta );🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/anvil_fork.rs` around lines 402 - 409, The close assertion in the taker close test only checks delta signs, so a wrongly decoded negative swap could still pass. Tighten the check in the close-path test around close_taker and the TakerClosed decoding by asserting the perp_delta magnitude matches the expected close size (mirroring the open/adjust magnitude check) in addition to keeping the sign checks. Use the existing close_result.perp_delta and close_result.usd_delta assertions to validate both direction and approximate size.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/client/trades.rs`:
- Around line 122-127: `open_taker` is silently treating a missing taker swap
decode as a valid zero fill by using `parse_taker_swap(&receipt).unwrap_or((0.0,
0.0))`, which can mask ABI or log-decoding issues. Update the taker flow in
`src/client/trades.rs` so `open_taker` and non-zero `adjust_taker` require a
decoded `TakerOpened`/`TakerAdjusted`/`TakerClosed` event and fail or surface an
error when parsing is missing, while keeping the `(0.0, 0.0)` fallback only for
maker opens and margin-only adjusts. Use the existing `parse_taker_swap`,
`open_taker`, and `adjust_taker` paths to apply the stricter handling
consistently.
---
Nitpick comments:
In `@tests/anvil_fork.rs`:
- Around line 402-409: The close assertion in the taker close test only checks
delta signs, so a wrongly decoded negative swap could still pass. Tighten the
check in the close-path test around close_taker and the TakerClosed decoding by
asserting the perp_delta magnitude matches the expected close size (mirroring
the open/adjust magnitude check) in addition to keeping the sign checks. Use the
existing close_result.perp_delta and close_result.usd_delta assertions to
validate both direction and approximate size.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: a0c7fc7b-8248-499f-8724-31cd4cb3d393
📒 Files selected for processing (4)
src/client/trades.rssrc/contracts.rssrc/types.rstests/anvil_fork.rs
open_taker / adjust_taker no longer fall back to (0.0, 0.0) when parse_taker_swap returns None — that fallback silently masked the TakerClosed binding bug (a full close reported a zero fill). Both taker paths always emit a decodable event (a margin-only adjust emits a zero-delta TakerAdjusted), so a missing decode is an ABI error: surface ContractError::EventNotFound instead. open_maker's explicit 0.0 (it emits no taker swap) is unchanged. Addresses CodeRabbit review. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
open_taker/adjust_taker now decode the taker swap from the receipt (reusing the feed's decode_log + SwapInfo) and return realized perp_delta/usd_delta on OpenResult/AdjustTakerResult — actual fills, not estimates. Both 0.0 for maker opens / margin-only adjusts (no swap).
Fixes the TakerClosed binding: the deployed contract (ahead of the v0.2.1 tag) unifies close+liquidation, so the event carries two extra fields (uint256 liqFee, bool isLiquidation). Verified against the live topic0 0xc6d156…; added a SIGNATURE_HASH assertion to abi_lock so deployment drift is caught (the prior test only checked binding self-consistency). Fork test now asserts realized deltas on open/adjust/close.
Summary by CodeRabbit
perp_deltaandusd_delta.perp_delta/usd_deltaas0.0.perp_delta/usd_delta, including tolerance checks.