@drift-labs/sdk 2.131.0-beta.6 → 2.131.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/VERSION +1 -1
- package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +53 -0
- package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +453 -0
- package/lib/browser/idl/drift.json +36 -5
- package/lib/browser/orderSubscriber/WebsocketSubscription.js +2 -2
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +54 -0
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -0
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +453 -0
- package/lib/node/idl/drift.json +36 -5
- package/lib/node/orderSubscriber/WebsocketSubscription.d.ts.map +1 -1
- package/lib/node/orderSubscriber/WebsocketSubscription.js +2 -2
- package/package.json +1 -1
- package/src/accounts/README_WebSocketProgramAccountSubscriberV2.md +135 -0
- package/src/accounts/webSocketProgramAccountSubscriberV2.ts +596 -0
- package/src/idl/drift.json +37 -6
- package/src/orderSubscriber/WebsocketSubscription.ts +3 -3
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# WebSocketProgramAccountSubscriberV2
|
|
2
|
+
|
|
3
|
+
This is a new implementation of the WebSocket program account subscriber that utilizes the [gill](https://www.npmjs.com/package/gill) library for improved RPC and WebSocket functionality, with additional smart polling logic for specific accounts.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The `WebSocketProgramAccountSubscriberV2` class provides the same interface as the original `WebSocketProgramAccountSubscriber` but uses gill's modern TypeScript client library for Solana blockchain interactions. Additionally, it implements smart polling logic for accounts that don't update frequently (like long-tail markets) to prevent missing updates.
|
|
8
|
+
|
|
9
|
+
## Key Features
|
|
10
|
+
|
|
11
|
+
1. **Gill Integration**: Uses gill's `createSolanaClient` for RPC and WebSocket functionality
|
|
12
|
+
2. **Smart Polling**: Optional polling for specific accounts that don't update frequently
|
|
13
|
+
3. **Missed Update Detection**: Automatically detects when updates are missed and resubscribes
|
|
14
|
+
4. **Configurable Polling**: 30-second default polling interval, customizable per instance
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
The usage is similar to the original `WebSocketProgramAccountSubscriber` with additional options:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { WebSocketProgramAccountSubscriberV2 } from './accounts/webSocketProgramAccountSubscriberV2';
|
|
22
|
+
|
|
23
|
+
// Create subscriber with optional accounts to poll
|
|
24
|
+
const subscriber = new WebSocketProgramAccountSubscriberV2(
|
|
25
|
+
'perpMarket', // account name
|
|
26
|
+
'perpMarket', // account discriminator
|
|
27
|
+
program, // Anchor program instance
|
|
28
|
+
decodeBuffer, // decode function
|
|
29
|
+
{ filters: [] }, // options
|
|
30
|
+
resubOpts, // optional resubscription options
|
|
31
|
+
[longTailMarket1, longTailMarket2] // optional list of accounts to poll
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// Subscribe to program account changes
|
|
35
|
+
await subscriber.subscribe((accountId, data, context, buffer) => {
|
|
36
|
+
console.log('Account updated:', accountId.toBase58(), data);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Add more accounts to poll dynamically
|
|
40
|
+
subscriber.addAccountToPoll(newMarketPublicKey);
|
|
41
|
+
|
|
42
|
+
// Remove accounts from polling
|
|
43
|
+
subscriber.removeAccountFromPoll(oldMarketPublicKey);
|
|
44
|
+
|
|
45
|
+
// Change polling interval
|
|
46
|
+
subscriber.setPollingInterval(60000); // 60 seconds
|
|
47
|
+
|
|
48
|
+
// Unsubscribe when done
|
|
49
|
+
await subscriber.unsubscribe();
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Implementation Details
|
|
53
|
+
|
|
54
|
+
### Gill Integration
|
|
55
|
+
|
|
56
|
+
The implementation uses gill's `createSolanaClient` function to create RPC and WebSocket clients:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { createSolanaClient } from 'gill';
|
|
60
|
+
|
|
61
|
+
const { rpc, rpcSubscriptions } = createSolanaClient({
|
|
62
|
+
urlOrMoniker: rpcUrl, // or "mainnet", "devnet", etc.
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Smart Polling Logic
|
|
67
|
+
|
|
68
|
+
1. **Account Selection**: Only accounts in the `accountsToMonitor` set are monitored
|
|
69
|
+
2. **Monitoring Period**: Default 30 seconds before starting to poll an account
|
|
70
|
+
3. **WebSocket Tracking**: Tracks the last WebSocket notification time for each account
|
|
71
|
+
4. **Conditional Polling**: Only starts polling if no WebSocket notification received in 30 seconds
|
|
72
|
+
5. **Batch Polling**: Uses `getMultipleAccounts` to poll all accounts in a single RPC call
|
|
73
|
+
6. **Dynamic Polling**: Stops polling individual accounts when WebSocket notifications are received
|
|
74
|
+
7. **Missed Update Detection**: Compares current slot and buffer with cached data
|
|
75
|
+
8. **Automatic Resubscription**: If a missed update is detected, the entire subscription is resubscribed
|
|
76
|
+
|
|
77
|
+
### Key Differences from Original
|
|
78
|
+
|
|
79
|
+
1. **RPC Client**: Uses gill's `rpc` client for account fetching
|
|
80
|
+
2. **WebSocket Subscriptions**: Uses gill's `rpcSubscriptions` for real-time updates
|
|
81
|
+
3. **Address Handling**: Converts `PublicKey` to gill's `Address` type for compatibility
|
|
82
|
+
4. **Response Formatting**: Converts gill responses to match the expected `AccountInfo<Buffer>` format
|
|
83
|
+
5. **Abort Signal**: Utilizes AbortSignal nodejs/web class to shutdown websocket connection synchronously
|
|
84
|
+
6. **Smart Polling**: Implements polling logic for specific accounts to prevent missed updates
|
|
85
|
+
|
|
86
|
+
## Configuration Options
|
|
87
|
+
|
|
88
|
+
### Constructor Parameters
|
|
89
|
+
|
|
90
|
+
- `subscriptionName`: Name for logging purposes
|
|
91
|
+
- `accountDiscriminator`: Account discriminator for decoding
|
|
92
|
+
- `program`: Anchor program instance
|
|
93
|
+
- `decodeBufferFn`: Function to decode account data
|
|
94
|
+
- `options`: Subscription options (filters, commitment)
|
|
95
|
+
- `resubOpts`: Resubscription options
|
|
96
|
+
- `accountsToPoll`: Optional array of PublicKeys to poll
|
|
97
|
+
|
|
98
|
+
### Polling Configuration
|
|
99
|
+
|
|
100
|
+
- **Default Monitoring Period**: 30 seconds before starting to poll
|
|
101
|
+
- **WebSocket Tracking**: Records timestamp of last WebSocket notification per account
|
|
102
|
+
- **Conditional Polling**: Only polls accounts that haven't received WebSocket updates recently
|
|
103
|
+
- **Batch Polling**: Uses `getMultipleAccounts` for efficient polling of multiple accounts
|
|
104
|
+
- **Dynamic Management**: Automatically starts/stops polling based on WebSocket activity
|
|
105
|
+
- **Detection Logic**: Compares slot numbers and buffer contents
|
|
106
|
+
- **Resubscription**: Triggers when missed updates are detected
|
|
107
|
+
- **Logging**: Optional logging of polling events
|
|
108
|
+
|
|
109
|
+
## Current Limitations
|
|
110
|
+
|
|
111
|
+
1. **Gill API Compatibility**: Some type mismatches exist with the current gill version
|
|
112
|
+
2. **Account Key Handling**: The account key extraction from gill notifications needs refinement
|
|
113
|
+
3. **Encoding Types**: Some encoding type comparisons need to be resolved
|
|
114
|
+
|
|
115
|
+
## Future Improvements
|
|
116
|
+
|
|
117
|
+
1. **Better Gill Integration**: Resolve remaining type compatibility issues
|
|
118
|
+
2. **Enhanced Logging**: More detailed logging for debugging
|
|
119
|
+
3. **Performance Optimization**: Optimize polling frequency based on account activity
|
|
120
|
+
4. **Batch Polling**: Poll multiple accounts in a single RPC call
|
|
121
|
+
|
|
122
|
+
## Migration from V1
|
|
123
|
+
|
|
124
|
+
The V2 implementation maintains the same interface as V1, making migration straightforward:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// V1
|
|
128
|
+
const subscriber = new WebSocketProgramAccountSubscriber(...);
|
|
129
|
+
|
|
130
|
+
// V2 (same interface)
|
|
131
|
+
const subscriber = new WebSocketProgramAccountSubscriberV2(...);
|
|
132
|
+
|
|
133
|
+
// V2 with polling (new feature)
|
|
134
|
+
const subscriber = new WebSocketProgramAccountSubscriberV2(..., accountsToPoll);
|
|
135
|
+
```
|