Skip to content

Fix parseRawEvents crash when topic field is undefined#1292

Merged
Ryang-21 merged 4 commits intomasterfrom
copilot/fix-parse-raw-events-error
Dec 2, 2025
Merged

Fix parseRawEvents crash when topic field is undefined#1292
Ryang-21 merged 4 commits intomasterfrom
copilot/fix-parse-raw-events-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 25, 2025

The RPC getEvents response can return events without a topic field, causing parseRawEvents to crash with "Cannot read properties of undefined (reading 'map')".

Changes:

  • Mark topic as optional in RawEventResponse type (topic?: string[])
  • Use nullish coalescing in parser: (evt.topic ?? []).map(...) to default undefined topics to empty array
  • Add test coverage for events with missing topic field

Example:

// Real-world event from testnet block 1263568 that would crash before this fix
const event = {
  type: "contract",
  contractId: "CC5E2...",
  // topic is undefined
  value: "AAAAEAAAAAEAAAACAAAAEgAA..."
};

// Previously: TypeError: Cannot read properties of undefined (reading 'map')
// Now: parsed.events[0].topic === []
const parsed = parseRawEvents({ events: [event], ... });

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • horizon-live.stellar.org
    • Triggering command: /usr/local/bin/node /usr/local/bin/node --conditions node --conditions development /home/REDACTED/work/js-stellar-sdk/js-stellar-sdk/node_modules/tinypool/dist/entry/process.js (dns block)
  • horizon.stellar.org
    • Triggering command: /usr/local/bin/node /usr/local/bin/node --conditions node --conditions development /home/REDACTED/work/js-stellar-sdk/js-stellar-sdk/node_modules/tinypool/dist/entry/process.js (dns block)
  • private-d133c-ammmock.apiary-mock.com
    • Triggering command: /usr/local/bin/node /usr/local/bin/node --conditions node --conditions development /home/REDACTED/work/js-stellar-sdk/js-stellar-sdk/node_modules/tinypool/dist/entry/process.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>parseRawEvents: Cannot read properties of undefined (reading 'map') when topic is missing</issue_title>
<issue_description>Describe the bug
Event topic could be undefined, which is not considered in the code:

ERROR TypeError: Cannot read properties of undefined (reading 'map')
    at /app/node_modules/.pnpm/@stellar+stellar-sdk@14.3.0/node_modules/@stellar/stellar-sdk/lib/rpc/parsers.js:95:26
    at Array.map (<anonymous>)
    at parseRawEvents (/app/node_modules/.pnpm/@stellar+stellar-sdk@14.3.0/node_modules/@stellar/stellar-sdk/lib/rpc/parsers.js:89:99)

What version are you on?
14.3.0

To Reproduce
execute parseRawEvents for block 1263568 on testnet

Expected behavior
topic is considered to possibly be undefined: (evt.topic ?? []).map instead of topic.map

Additional context
The event causing problem:

curl -X POST \
-H 'Content-Type: application/json' \
-d '{
  "jsonrpc": "2.0",
  "id": 8675309,
  "method": "getEvents",
  "params": {
    "startLedger": 1263568,    
    "endLedger": 1263569,
    "pagination": {
      "limit": 20
    }
  }
}' \
https://soroban-rpc.testnet.stellar.gateway.fm | jq
{
  "jsonrpc": "2.0",
  "id": 8675309,
  "result": {
    "events": [
       ...,
      {
        "type": "contract",
        "ledger": 1263568,
        "ledgerClosedAt": "2025-10-26T22:19:10Z",
        "contractId": "CC5E2AZW4DRFDYZHI7M25QKCSMFPUD7C3425BUOW54RU3TQRKUPA64W5",
        "id": "0005426983236280320-0000000000",
        "operationIndex": 0,
        "transactionIndex": 2,
        "txHash": "8735d2c7e31b1f10037f41d723072fbfacd0bbf74ed48f71f8e4211132017123",
        "inSuccessfulContractCall": true,
        "value": "AAAAEAAAAAEAAAACAAAAEgAAAAAAAAAA6l77q0ZD1wnLxQyCbBiGOcbm8+2jMoki9jMGwk+zZ+0AAAAJAAAAAAAAAAAAAAkYTnKgAA=="
      },
      ...,
    ],
    "cursor": "0005426987531239423-4294967295",
    "latestLedger": 1291637,
    "oldestLedger": 1170678,
    "latestLedgerCloseTime": "1761657666",
    "oldestLedgerCloseTime": "1761052138"
  }
}
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Nov 25, 2025

Size Change: +4.92 kB (+0.01%)

Total Size: 42.5 MB

Filename Size Change
dist/stellar-sdk-minimal.js 5.56 MB +872 B (+0.02%)
dist/stellar-sdk-minimal.min.js 4.84 MB +358 B (+0.01%)
dist/stellar-sdk-no-axios.js 5.56 MB +872 B (+0.02%)
dist/stellar-sdk-no-axios.min.js 4.84 MB +358 B (+0.01%)
dist/stellar-sdk-no-eventsource.js 5.8 MB +872 B (+0.02%)
dist/stellar-sdk-no-eventsource.min.js 5.05 MB +358 B (+0.01%)
dist/stellar-sdk.js 5.8 MB +872 B (+0.02%)
dist/stellar-sdk.min.js 5.05 MB +358 B (+0.01%)

compressed-size-action

Copilot AI changed the title [WIP] Fix parseRawEvents to handle undefined topic Fix parseRawEvents crash when topic field is undefined Nov 25, 2025
Copilot AI requested a review from sagpatil November 25, 2025 23:06
@sagpatil sagpatil moved this from Backlog (Not Ready) to In Progress in DevX Nov 26, 2025
@Ryang-21 Ryang-21 force-pushed the copilot/fix-parse-raw-events-error branch from 59fe54e to b1c7c7a Compare December 1, 2025 16:54
@Ryang-21 Ryang-21 marked this pull request as ready for review December 1, 2025 23:02
@Ryang-21 Ryang-21 requested a review from Shaptic December 1, 2025 23:34
@Ryang-21 Ryang-21 moved this from In Progress to Needs Review in DevX Dec 2, 2025
@Ryang-21 Ryang-21 force-pushed the copilot/fix-parse-raw-events-error branch from b1c7c7a to ea2daa6 Compare December 2, 2025 15:50
@Ryang-21
Copy link
Copy Markdown
Contributor

Ryang-21 commented Dec 2, 2025

Force pushed to get verified commits

@Ryang-21 Ryang-21 merged commit 715144a into master Dec 2, 2025
10 checks passed
@Ryang-21 Ryang-21 deleted the copilot/fix-parse-raw-events-error branch December 2, 2025 17:48
@github-project-automation github-project-automation Bot moved this from Needs Review to Done in DevX Dec 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

parseRawEvents: Cannot read properties of undefined (reading 'map') when topic is missing

4 participants