Skip to content

Extract applyImportedTrades from chain-sync into 18b-chain-apply.js #59

@heyitsStylez

Description

@heyitsStylez

What to build

syncRysk and syncHypersurface in src/js/18-chain-sync.js both mix three concerns: parsing raw API responses, applying trades to the global trades[] array, and triggering save() + render(). The apply logic — dedup by txHash, open/close split, close-trade matching, outcome correction — has real invariants and zero test coverage.

Extract the apply logic into a new file src/js/18b-chain-apply.js with two pure, dual-exported functions:

applyCloseTrade(tradesArray, closeTrade) → boolean

applyImportedTrades(tradesArray, openTrades, closeTrades, synced)
  → { added, closedCount, corrected }

applyImportedTrades behaviour:

  • Pushes each open trade to tradesArray after stripping the isClose flag (not part of the canonical trade shape)
  • Calls applyCloseTrade for each close trade; counts successes
  • Runs outcome correction for both RYSK and HSFC: any trade in tradesArray whose t.expiry ISO date is in the past and whose outcome is 'OPEN' is corrected to 'EXPIRED' and counted in corrected
  • Mutates synced Set in-place with newly processed txHashes

applyCloseTrade is extracted from 18-chain-sync.js and parameterised on tradesArray instead of closing over the global.

syncRysk and syncHypersurface become thin wrappers: fetch → parse → applyImportedTrades(trades, ...) → if added + closedCount + corrected > 0: save(); render(); saveSynced(synced).

Acceptance criteria

  • src/js/18b-chain-apply.js exists with applyCloseTrade and applyImportedTrades, both dual-exported
  • isClose field is absent from trades pushed to tradesArray
  • Outcome correction runs for both RYSK and HSFC platform trades (not only Rysk)
  • syncRysk and syncHypersurface contain no dedup, split, or correction logic — delegated entirely to applyImportedTrades
  • test/unit/chain-apply.test.js added with passing pure-node tests (no jsdom) covering: already-synced txHash is skipped; close trade matches open trade and sets outcome to CLOSED; unknown close trade with no match is a no-op (closedCount: 0); OPEN trade with past expiry is corrected to EXPIRED; correction applies to both RYSK and HSFC trades; isClose absent from pushed trades
  • python3 build.py --check passes
  • npm test passes

Blocked by

None — can start immediately.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions