Skip to content

fix(sdk): optional chain getSpotMarketAccountAndSlot return in DriftClient (closes #2137)#2186

Open
cryptomotifs wants to merge 1 commit into
drift-labs:masterfrom
cryptomotifs:fix/2137-spot-market-optional-chaining
Open

fix(sdk): optional chain getSpotMarketAccountAndSlot return in DriftClient (closes #2137)#2186
cryptomotifs wants to merge 1 commit into
drift-labs:masterfrom
cryptomotifs:fix/2137-spot-market-optional-chaining

Conversation

@cryptomotifs
Copy link
Copy Markdown

@cryptomotifs cryptomotifs commented Apr 25, 2026

Summary

Closes #2137.

getSpotMarketAccount and forceGetSpotMarketAccount access .data on the return value of accountSubscriber.getSpotMarketAccountAndSlot(marketIndex) without first checking the `accountAndSlot` shape. When the subscriber hasn't yet seen the account (or the market index isn't known), the subscriber returns `undefined` and the unguarded `.data` access throws `TypeError: Cannot read properties of undefined (reading 'data')` at runtime.

Both methods are already typed as `... | undefined`, so propagating `undefined` via optional chaining is consistent with the public contract.

Diff

`sdk/src/driftClient.ts` — 4 additions, 2 deletions.

```diff
public getSpotMarketAccount(
marketIndex: number
): SpotMarketAccount | undefined {

  •   return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
    
  •   return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
    
  •   	?.data;
    

    }

    public async forceGetSpotMarketAccount(
    marketIndex: number
    ): Promise<SpotMarketAccount | undefined> {
    await this.accountSubscriber.fetch();

  •   return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
    
  •   return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
    
  •   	?.data;
    
    }
    ```

Test plan

  • Type-checks: return type already `SpotMarketAccount | undefined`; `?.data` preserves it.
  • CI: existing test suite should pass unchanged (no new code paths, behavior strictly safer).
  • No call sites need to change — all existing callers already handle `undefined` since the typed signature is unchanged.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved resilience of spot market account lookups to gracefully handle missing subscriber data, preventing errors when data is unavailable and returning undefined instead of throwing exceptions.

…lient

Closes drift-labs#2137.

`getSpotMarketAccount` and `forceGetSpotMarketAccount` access `.data`
on the return value of `accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)`
without first checking the `accountAndSlot` shape. When the subscriber
hasn't yet seen the account (or the market index isn't yet known),
the subscriber returns `undefined` and the unguarded `.data` access
throws `TypeError: Cannot read properties of undefined (reading 'data')`
at runtime.

The two methods are already typed as `... | undefined`, so propagating
`undefined` via optional chaining is consistent with the public
contract.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a0a12aaa-8561-434c-afd0-83cd71a593dd

📥 Commits

Reviewing files that changed from the base of the PR and between 0ae3e3b and 5fb992e.

📒 Files selected for processing (1)
  • sdk/src/driftClient.ts

Walkthrough

Updates DriftClient spot-market account accessor methods to use optional chaining when accessing subscriber data, preventing runtime errors when getSpotMarketAccountAndSlot returns no result.

Changes

Cohort / File(s) Summary
Spot Market Account Accessors
sdk/src/driftClient.ts
Added optional chaining (?.data) to getSpotMarketAccount and forceGetSpotMarketAccount methods to safely handle cases where subscriber data is unavailable, returning undefined instead of throwing.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A careful hop through data streams,
With question marks in defensive schemes,
No crash when subscribers fade to nil,
Just gentle undefined—graceful still! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding optional chaining to getSpotMarketAccountAndSlot return in DriftClient.
Linked Issues check ✅ Passed The pull request fully implements the requirement from issue #2137 by adding optional chaining to both getSpotMarketAccount and forceGetSpotMarketAccount methods.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the requirements in issue #2137; no extraneous modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Minor driftClient improvement

1 participant