Skip to content

[non-null] polymarket/normalizer.ts: unsafe Map.get()! on candle buckets #321

@realfishsam

Description

@realfishsam

Risk Level

MEDIUM

File

core/src/exchanges/polymarket/normalizer.ts

Findings

  • Line 80: const candle = buckets.get(snappedMs)!; — placed in the else branch of if (!buckets.has(snappedMs)) { buckets.set(...); } else { ... }. Logically safe because the else guarantees buckets.has(snappedMs) is true. TypeScript does not track the has/get relationship, hence the !.

What Happens When It's Wrong

In practice this should never crash given the surrounding control flow. The risk is a future refactor that restructures the if/else without removing the !, allowing candle to be undefined while lines 81–84 attempt property assignments on it.

Suggested Fix

Use a local variable narrowing to remove the assertion entirely:

const candle = buckets.get(snappedMs);
if (candle) {
    candle.high = Math.max(candle.high, price);
    candle.low = Math.min(candle.low, price);
    candle.close = price;
    candle.volume = (candle.volume || 0) + volume;
}

Found by automated non-null assertion audit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions