fix(sdk): optional chain getSpotMarketAccountAndSlot return in DriftClient (closes #2137)#2186
Conversation
…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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughUpdates Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ 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. Comment |
Summary
Closes #2137.
getSpotMarketAccountandforceGetSpotMarketAccountaccess.dataon the return value ofaccountSubscriber.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 {
}
public async forceGetSpotMarketAccount(
marketIndex: number
): Promise<SpotMarketAccount | undefined> {
await this.accountSubscriber.fetch();
```
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit