Skip to content

fix: map cashValue to unrealizedPnL in polymarket_us#582

Merged
realfishsam merged 1 commit into
mainfrom
fix/533-polymarket-us-pnl
May 24, 2026
Merged

fix: map cashValue to unrealizedPnL in polymarket_us#582
realfishsam merged 1 commit into
mainfrom
fix/533-polymarket-us-pnl

Conversation

@realfishsam
Copy link
Copy Markdown
Contributor

Fixes #533

@realfishsam
Copy link
Copy Markdown
Contributor Author

PR Review: PASS (NOT VERIFIED)

What This Does

Maps the Polymarket US cashValue field from the position object to compute currentPrice and unrealizedPnL, replacing the previous hard-coded zeros with real mark-to-market values derived from cashValue - abs(cost).

Blast Radius

  • Polymarket US exchange: core/src/exchanges/polymarket_us/normalizer.ts (position normalization only)
  • No type changes, no OpenAPI changes, no SDK changes -- the unified Position type already has currentPrice and unrealizedPnL fields

Findings

  1. Division-by-zero guard is present. size > 0 ? cashValue / size : 0 correctly guards the currentPrice calculation.

  2. fromAmount(pos.cashValue) assumes cashValue exists on the SDK type. The polymarket-us SDK's UserPosition type may not declare cashValue in its TypeScript definitions. If it does not, this will need a cast or type extension. The code compiles if fromAmount accepts undefined gracefully (returning 0), but this should be verified. A grep for cashValue in the codebase returns zero results on the main branch, suggesting this is a new field being accessed for the first time.

  3. PnL formula is correct. unrealizedPnL = cashValue - abs(cost) is the standard mark-to-market formula: current value minus entry cost.

  4. Comment quality is good. The inline comments explain the derivation clearly.

PMXT Pipeline Check

  • Field propagation: OK (Position type already has these fields, no new fields added)
  • OpenAPI sync: N/A (no BaseExchange.ts change)
  • Type safety: OK (assuming pos.cashValue exists at runtime)

Semver Impact

patch -- fills in previously-zero fields with real data, no API shape change

Risk

  • pos.cashValue existence at runtime is unverified. If the Polymarket US SDK does not include cashValue on the position object, fromAmount(undefined) must return 0 for this to not crash. Test against a live account with open positions.
  • For positions where cashValue is 0 or negative (fully underwater), verify the sign conventions produce correct PnL reporting.

@realfishsam realfishsam merged commit 2f4feac into main May 24, 2026
11 of 12 checks passed
@realfishsam realfishsam deleted the fix/533-polymarket-us-pnl branch May 24, 2026 17:04
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.

Spec drift: polymarket_us — unrealized PnL hardcoded to 0, cashValue field available but unmapped

1 participant