@drift-labs/sdk 0.1.34-master.4 → 0.1.36-master.0
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.
|
@@ -15,7 +15,6 @@ export declare class BulkAccountLoader {
|
|
|
15
15
|
intervalId?: NodeJS.Timer;
|
|
16
16
|
loadPromise?: Promise<void>;
|
|
17
17
|
loadPromiseResolver: () => void;
|
|
18
|
-
loggingEnabled: boolean;
|
|
19
18
|
lastTimeLoadingPromiseCleared: number;
|
|
20
19
|
constructor(connection: Connection, commitment: Commitment, pollingFrequency: number);
|
|
21
20
|
addAccount(publicKey: PublicKey, callback: (buffer: Buffer) => void): string;
|
|
@@ -29,7 +28,7 @@ export declare class BulkAccountLoader {
|
|
|
29
28
|
getAccountData(publicKey: PublicKey): Buffer | undefined;
|
|
30
29
|
startPolling(): void;
|
|
31
30
|
stopPolling(): void;
|
|
32
|
-
log(msg: string
|
|
31
|
+
log(msg: string): void;
|
|
33
32
|
updatePollingFrequency(pollingFrequency: number): void;
|
|
34
33
|
}
|
|
35
34
|
export {};
|
|
@@ -19,7 +19,6 @@ class BulkAccountLoader {
|
|
|
19
19
|
this.accountsToLoad = new Map();
|
|
20
20
|
this.accountData = new Map();
|
|
21
21
|
this.errorCallbacks = new Map();
|
|
22
|
-
this.loggingEnabled = false;
|
|
23
22
|
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
24
23
|
this.connection = connection;
|
|
25
24
|
this.commitment = commitment;
|
|
@@ -28,14 +27,11 @@ class BulkAccountLoader {
|
|
|
28
27
|
addAccount(publicKey, callback) {
|
|
29
28
|
const existingSize = this.accountsToLoad.size;
|
|
30
29
|
const callbackId = (0, uuid_1.v4)();
|
|
31
|
-
this.log(`Adding account ${publicKey.toString()} callback id ${callbackId}`);
|
|
32
30
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
33
31
|
if (existingAccountToLoad) {
|
|
34
|
-
this.log(`account already exists`);
|
|
35
32
|
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
36
33
|
}
|
|
37
34
|
else {
|
|
38
|
-
this.log(`account doesn't already exist. creating new callback map`);
|
|
39
35
|
const callbacks = new Map();
|
|
40
36
|
callbacks.set(callbackId, callback);
|
|
41
37
|
const newAccountToLoad = {
|
|
@@ -52,7 +48,6 @@ class BulkAccountLoader {
|
|
|
52
48
|
return callbackId;
|
|
53
49
|
}
|
|
54
50
|
removeAccount(publicKey, callbackId) {
|
|
55
|
-
this.log(`Removing account ${publicKey.toString()} callback id ${callbackId}`);
|
|
56
51
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
57
52
|
if (existingAccountToLoad) {
|
|
58
53
|
existingAccountToLoad.callbacks.delete(callbackId);
|
|
@@ -83,11 +78,9 @@ class BulkAccountLoader {
|
|
|
83
78
|
if (this.loadPromise) {
|
|
84
79
|
const now = Date.now();
|
|
85
80
|
if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
|
|
86
|
-
this.log(`Load promise hasnt been clearing for one minute. Clearing.`);
|
|
87
81
|
this.loadPromise = undefined;
|
|
88
82
|
}
|
|
89
83
|
else {
|
|
90
|
-
this.log(`Load promise exists. Returning early`);
|
|
91
84
|
return this.loadPromise;
|
|
92
85
|
}
|
|
93
86
|
}
|
|
@@ -95,10 +88,8 @@ class BulkAccountLoader {
|
|
|
95
88
|
this.loadPromiseResolver = resolver;
|
|
96
89
|
});
|
|
97
90
|
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
98
|
-
this.log(`Loading`);
|
|
99
91
|
try {
|
|
100
92
|
const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
|
|
101
|
-
this.log(`${chunks.length} chunks`);
|
|
102
93
|
yield Promise.all(chunks.map((chunk) => {
|
|
103
94
|
return this.loadChunk(chunk);
|
|
104
95
|
}));
|
|
@@ -109,10 +100,8 @@ class BulkAccountLoader {
|
|
|
109
100
|
for (const [_, callback] of this.errorCallbacks) {
|
|
110
101
|
callback(e);
|
|
111
102
|
}
|
|
112
|
-
this.log('finished error callbacks');
|
|
113
103
|
}
|
|
114
104
|
finally {
|
|
115
|
-
this.log(`resetting load promise`);
|
|
116
105
|
this.loadPromiseResolver();
|
|
117
106
|
this.loadPromise = undefined;
|
|
118
107
|
}
|
|
@@ -121,7 +110,6 @@ class BulkAccountLoader {
|
|
|
121
110
|
loadChunk(accountsToLoad) {
|
|
122
111
|
return __awaiter(this, void 0, void 0, function* () {
|
|
123
112
|
if (accountsToLoad.length === 0) {
|
|
124
|
-
this.log(`no accounts in chunk`);
|
|
125
113
|
return;
|
|
126
114
|
}
|
|
127
115
|
const args = [
|
|
@@ -135,7 +123,7 @@ class BulkAccountLoader {
|
|
|
135
123
|
this.connection._rpcRequest('getMultipleAccounts', args), 10 * 1000 // 30 second timeout
|
|
136
124
|
);
|
|
137
125
|
if (rpcResponse === null) {
|
|
138
|
-
this.log('request to rpc timed out'
|
|
126
|
+
this.log('request to rpc timed out');
|
|
139
127
|
return;
|
|
140
128
|
}
|
|
141
129
|
const newSlot = rpcResponse.result.context.slot;
|
|
@@ -150,7 +138,6 @@ class BulkAccountLoader {
|
|
|
150
138
|
newBuffer = Buffer.from(raw, dataType);
|
|
151
139
|
}
|
|
152
140
|
if (!oldRPCResponse) {
|
|
153
|
-
this.log('No old rpc response, updating account data');
|
|
154
141
|
this.accountData.set(key, {
|
|
155
142
|
slot: newSlot,
|
|
156
143
|
buffer: newBuffer,
|
|
@@ -159,33 +146,23 @@ class BulkAccountLoader {
|
|
|
159
146
|
continue;
|
|
160
147
|
}
|
|
161
148
|
if (newSlot <= oldRPCResponse.slot) {
|
|
162
|
-
this.log(`new slot ${newSlot} old slot ${oldRPCResponse.slot}`);
|
|
163
149
|
continue;
|
|
164
150
|
}
|
|
165
151
|
const oldBuffer = oldRPCResponse.buffer;
|
|
166
152
|
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
167
|
-
this.log('new buffer, updating account data');
|
|
168
153
|
this.accountData.set(key, {
|
|
169
154
|
slot: newSlot,
|
|
170
155
|
buffer: newBuffer,
|
|
171
156
|
});
|
|
172
157
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
173
158
|
}
|
|
174
|
-
else {
|
|
175
|
-
this.log('unable to update account for newest slot');
|
|
176
|
-
this.log('oldBuffer ' + oldBuffer);
|
|
177
|
-
this.log('newBuffer ' + newBuffer);
|
|
178
|
-
this.log('buffers equal ' + (newBuffer === null || newBuffer === void 0 ? void 0 : newBuffer.equals(oldBuffer)));
|
|
179
|
-
}
|
|
180
159
|
}
|
|
181
160
|
});
|
|
182
161
|
}
|
|
183
162
|
handleAccountCallbacks(accountToLoad, buffer) {
|
|
184
|
-
this.log('handling account callbacks');
|
|
185
163
|
for (const [_, callback] of accountToLoad.callbacks) {
|
|
186
164
|
callback(buffer);
|
|
187
165
|
}
|
|
188
|
-
this.log('finished account callbacks');
|
|
189
166
|
}
|
|
190
167
|
getAccountData(publicKey) {
|
|
191
168
|
const accountData = this.accountData.get(publicKey.toString());
|
|
@@ -195,20 +172,16 @@ class BulkAccountLoader {
|
|
|
195
172
|
if (this.intervalId) {
|
|
196
173
|
return;
|
|
197
174
|
}
|
|
198
|
-
this.log('startPolling');
|
|
199
175
|
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
200
176
|
}
|
|
201
177
|
stopPolling() {
|
|
202
178
|
if (this.intervalId) {
|
|
203
179
|
clearInterval(this.intervalId);
|
|
204
180
|
this.intervalId = undefined;
|
|
205
|
-
this.log('stopPolling');
|
|
206
181
|
}
|
|
207
182
|
}
|
|
208
|
-
log(msg
|
|
209
|
-
|
|
210
|
-
console.log(msg);
|
|
211
|
-
}
|
|
183
|
+
log(msg) {
|
|
184
|
+
console.log(msg);
|
|
212
185
|
}
|
|
213
186
|
updatePollingFrequency(pollingFrequency) {
|
|
214
187
|
this.stopPolling();
|
package/lib/constants/markets.js
CHANGED
|
@@ -221,4 +221,15 @@ exports.Markets = [
|
|
|
221
221
|
launchTs: 1651017354000,
|
|
222
222
|
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
223
223
|
},
|
|
224
|
+
{
|
|
225
|
+
fullName: 'Fantom',
|
|
226
|
+
category: ['L1', 'Infra'],
|
|
227
|
+
symbol: 'FTM-PERP',
|
|
228
|
+
baseAssetSymbol: 'FTM',
|
|
229
|
+
marketIndex: new __1.BN(20),
|
|
230
|
+
devnetPublicKey: 'BTwrLU4so1oJMViWA3BTzh8YmFwiLZ6CL4U3JryG7Q5S',
|
|
231
|
+
mainnetPublicKey: '7Dn52EY5EGE8Nvvw98KVMGPWTiTGn3PF4y24TVLyXdT9',
|
|
232
|
+
launchTs: 1651559653000,
|
|
233
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
234
|
+
},
|
|
224
235
|
];
|
package/package.json
CHANGED
|
@@ -23,7 +23,6 @@ export class BulkAccountLoader {
|
|
|
23
23
|
// to handle clients spamming load
|
|
24
24
|
loadPromise?: Promise<void>;
|
|
25
25
|
loadPromiseResolver: () => void;
|
|
26
|
-
loggingEnabled = false;
|
|
27
26
|
lastTimeLoadingPromiseCleared = Date.now();
|
|
28
27
|
|
|
29
28
|
public constructor(
|
|
@@ -43,15 +42,10 @@ export class BulkAccountLoader {
|
|
|
43
42
|
const existingSize = this.accountsToLoad.size;
|
|
44
43
|
|
|
45
44
|
const callbackId = uuidv4();
|
|
46
|
-
this.log(
|
|
47
|
-
`Adding account ${publicKey.toString()} callback id ${callbackId}`
|
|
48
|
-
);
|
|
49
45
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
50
46
|
if (existingAccountToLoad) {
|
|
51
|
-
this.log(`account already exists`);
|
|
52
47
|
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
53
48
|
} else {
|
|
54
|
-
this.log(`account doesn't already exist. creating new callback map`);
|
|
55
49
|
const callbacks = new Map<string, (buffer: Buffer) => void>();
|
|
56
50
|
callbacks.set(callbackId, callback);
|
|
57
51
|
const newAccountToLoad = {
|
|
@@ -72,9 +66,6 @@ export class BulkAccountLoader {
|
|
|
72
66
|
}
|
|
73
67
|
|
|
74
68
|
public removeAccount(publicKey: PublicKey, callbackId: string): void {
|
|
75
|
-
this.log(
|
|
76
|
-
`Removing account ${publicKey.toString()} callback id ${callbackId}`
|
|
77
|
-
);
|
|
78
69
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
79
70
|
if (existingAccountToLoad) {
|
|
80
71
|
existingAccountToLoad.callbacks.delete(callbackId);
|
|
@@ -109,10 +100,8 @@ export class BulkAccountLoader {
|
|
|
109
100
|
if (this.loadPromise) {
|
|
110
101
|
const now = Date.now();
|
|
111
102
|
if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
|
|
112
|
-
this.log(`Load promise hasnt been clearing for one minute. Clearing.`);
|
|
113
103
|
this.loadPromise = undefined;
|
|
114
104
|
} else {
|
|
115
|
-
this.log(`Load promise exists. Returning early`);
|
|
116
105
|
return this.loadPromise;
|
|
117
106
|
}
|
|
118
107
|
}
|
|
@@ -122,14 +111,11 @@ export class BulkAccountLoader {
|
|
|
122
111
|
});
|
|
123
112
|
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
124
113
|
|
|
125
|
-
this.log(`Loading`);
|
|
126
|
-
|
|
127
114
|
try {
|
|
128
115
|
const chunks = this.chunks(
|
|
129
116
|
Array.from(this.accountsToLoad.values()),
|
|
130
117
|
GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE
|
|
131
118
|
);
|
|
132
|
-
this.log(`${chunks.length} chunks`);
|
|
133
119
|
|
|
134
120
|
await Promise.all(
|
|
135
121
|
chunks.map((chunk) => {
|
|
@@ -142,9 +128,7 @@ export class BulkAccountLoader {
|
|
|
142
128
|
for (const [_, callback] of this.errorCallbacks) {
|
|
143
129
|
callback(e);
|
|
144
130
|
}
|
|
145
|
-
this.log('finished error callbacks');
|
|
146
131
|
} finally {
|
|
147
|
-
this.log(`resetting load promise`);
|
|
148
132
|
this.loadPromiseResolver();
|
|
149
133
|
this.loadPromise = undefined;
|
|
150
134
|
}
|
|
@@ -152,7 +136,6 @@ export class BulkAccountLoader {
|
|
|
152
136
|
|
|
153
137
|
async loadChunk(accountsToLoad: AccountToLoad[]): Promise<void> {
|
|
154
138
|
if (accountsToLoad.length === 0) {
|
|
155
|
-
this.log(`no accounts in chunk`);
|
|
156
139
|
return;
|
|
157
140
|
}
|
|
158
141
|
|
|
@@ -170,7 +153,7 @@ export class BulkAccountLoader {
|
|
|
170
153
|
);
|
|
171
154
|
|
|
172
155
|
if (rpcResponse === null) {
|
|
173
|
-
this.log('request to rpc timed out'
|
|
156
|
+
this.log('request to rpc timed out');
|
|
174
157
|
return;
|
|
175
158
|
}
|
|
176
159
|
|
|
@@ -189,7 +172,6 @@ export class BulkAccountLoader {
|
|
|
189
172
|
}
|
|
190
173
|
|
|
191
174
|
if (!oldRPCResponse) {
|
|
192
|
-
this.log('No old rpc response, updating account data');
|
|
193
175
|
this.accountData.set(key, {
|
|
194
176
|
slot: newSlot,
|
|
195
177
|
buffer: newBuffer,
|
|
@@ -199,33 +181,24 @@ export class BulkAccountLoader {
|
|
|
199
181
|
}
|
|
200
182
|
|
|
201
183
|
if (newSlot <= oldRPCResponse.slot) {
|
|
202
|
-
this.log(`new slot ${newSlot} old slot ${oldRPCResponse.slot}`);
|
|
203
184
|
continue;
|
|
204
185
|
}
|
|
205
186
|
|
|
206
187
|
const oldBuffer = oldRPCResponse.buffer;
|
|
207
188
|
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
208
|
-
this.log('new buffer, updating account data');
|
|
209
189
|
this.accountData.set(key, {
|
|
210
190
|
slot: newSlot,
|
|
211
191
|
buffer: newBuffer,
|
|
212
192
|
});
|
|
213
193
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
214
|
-
} else {
|
|
215
|
-
this.log('unable to update account for newest slot');
|
|
216
|
-
this.log('oldBuffer ' + oldBuffer);
|
|
217
|
-
this.log('newBuffer ' + newBuffer);
|
|
218
|
-
this.log('buffers equal ' + newBuffer?.equals(oldBuffer));
|
|
219
194
|
}
|
|
220
195
|
}
|
|
221
196
|
}
|
|
222
197
|
|
|
223
198
|
handleAccountCallbacks(accountToLoad: AccountToLoad, buffer: Buffer): void {
|
|
224
|
-
this.log('handling account callbacks');
|
|
225
199
|
for (const [_, callback] of accountToLoad.callbacks) {
|
|
226
200
|
callback(buffer);
|
|
227
201
|
}
|
|
228
|
-
this.log('finished account callbacks');
|
|
229
202
|
}
|
|
230
203
|
|
|
231
204
|
public getAccountData(publicKey: PublicKey): Buffer | undefined {
|
|
@@ -238,8 +211,6 @@ export class BulkAccountLoader {
|
|
|
238
211
|
return;
|
|
239
212
|
}
|
|
240
213
|
|
|
241
|
-
this.log('startPolling');
|
|
242
|
-
|
|
243
214
|
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
244
215
|
}
|
|
245
216
|
|
|
@@ -247,15 +218,11 @@ export class BulkAccountLoader {
|
|
|
247
218
|
if (this.intervalId) {
|
|
248
219
|
clearInterval(this.intervalId);
|
|
249
220
|
this.intervalId = undefined;
|
|
250
|
-
|
|
251
|
-
this.log('stopPolling');
|
|
252
221
|
}
|
|
253
222
|
}
|
|
254
223
|
|
|
255
|
-
public log(msg: string
|
|
256
|
-
|
|
257
|
-
console.log(msg);
|
|
258
|
-
}
|
|
224
|
+
public log(msg: string): void {
|
|
225
|
+
console.log(msg);
|
|
259
226
|
}
|
|
260
227
|
|
|
261
228
|
public updatePollingFrequency(pollingFrequency: number): void {
|
package/src/constants/markets.ts
CHANGED
|
@@ -231,4 +231,15 @@ export const Markets: MarketConfig[] = [
|
|
|
231
231
|
launchTs: 1651017354000,
|
|
232
232
|
oracleSource: OracleSource.SWITCHBOARD,
|
|
233
233
|
},
|
|
234
|
+
{
|
|
235
|
+
fullName: 'Fantom',
|
|
236
|
+
category: ['L1', 'Infra'],
|
|
237
|
+
symbol: 'FTM-PERP',
|
|
238
|
+
baseAssetSymbol: 'FTM',
|
|
239
|
+
marketIndex: new BN(20),
|
|
240
|
+
devnetPublicKey: 'BTwrLU4so1oJMViWA3BTzh8YmFwiLZ6CL4U3JryG7Q5S',
|
|
241
|
+
mainnetPublicKey: '7Dn52EY5EGE8Nvvw98KVMGPWTiTGn3PF4y24TVLyXdT9',
|
|
242
|
+
launchTs: 1651559653000,
|
|
243
|
+
oracleSource: OracleSource.PYTH,
|
|
244
|
+
},
|
|
234
245
|
];
|