@drift-labs/sdk 2.84.0-beta.5 → 2.84.0-beta.6

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.
@@ -82,6 +82,9 @@ class TxHandler {
82
82
  isVersionedTransaction(tx) {
83
83
  return (tx === null || tx === void 0 ? void 0 : tx.message) && true;
84
84
  }
85
+ isLegacyTransaction(tx) {
86
+ return !this.isVersionedTransaction(tx);
87
+ }
85
88
  getTxSigFromSignedTx(signedTx) {
86
89
  if (this.isVersionedTransaction(signedTx)) {
87
90
  return bs58_1.default.encode(Buffer.from(signedTx.signatures[0]));
@@ -152,7 +155,7 @@ class TxHandler {
152
155
  }
153
156
  return;
154
157
  }
155
- const fullTxData = txData.map((tx) => {
158
+ const signedTxData = txData.map((tx) => {
156
159
  const lastValidBlockHeight = this.blockHashToLastValidBlockHeightLookup[tx.blockHash];
157
160
  return {
158
161
  ...tx,
@@ -160,8 +163,9 @@ class TxHandler {
160
163
  };
161
164
  });
162
165
  if (this.onSignedCb) {
163
- this.onSignedCb(fullTxData);
166
+ this.onSignedCb(signedTxData);
164
167
  }
168
+ return signedTxData;
165
169
  }
166
170
  /**
167
171
  * Gets transaction params with extra processing applied, like using the simulated compute units or using a dynamically calculated compute unit price.
@@ -225,8 +229,12 @@ class TxHandler {
225
229
  tx.SIGNATURE_BLOCK_AND_EXPIRY = recentBlockhash;
226
230
  return tx;
227
231
  }
228
- generateLegacyTransaction(ixs) {
229
- return new web3_js_1.Transaction().add(...ixs);
232
+ generateLegacyTransaction(ixs, recentBlockhash) {
233
+ const tx = new web3_js_1.Transaction().add(...ixs);
234
+ if (recentBlockhash) {
235
+ tx.recentBlockhash = recentBlockhash.blockhash;
236
+ }
237
+ return tx;
230
238
  }
231
239
  /**
232
240
  * Accepts multiple instructions and builds a transaction for each. Prevents needing to spam RPC with requests for the same blockhash.
@@ -302,7 +310,7 @@ class TxHandler {
302
310
  return this.generateLegacyVersionedTransaction(recentBlockhash, allIx);
303
311
  }
304
312
  else {
305
- return this.generateLegacyTransaction(allIx);
313
+ return this.generateLegacyTransaction(allIx, recentBlockhash);
306
314
  }
307
315
  }
308
316
  else {
@@ -332,30 +340,6 @@ class TxHandler {
332
340
  }
333
341
  return tx.add(instruction);
334
342
  }
335
- /**
336
- * Build a map of transactions from an array of instructions for multiple transactions.
337
- * @param txsToSign
338
- * @param keys
339
- * @param wallet
340
- * @param commitment
341
- * @returns
342
- */
343
- async buildTransactionMap(txsToSign, keys, wallet, commitment, recentBlockhash) {
344
- var _a, _b;
345
- recentBlockhash = recentBlockhash
346
- ? recentBlockhash
347
- : await this.getLatestBlockhashForTransaction();
348
- this.addHashAndExpiryToLookup(recentBlockhash);
349
- for (const tx of txsToSign) {
350
- if (!tx)
351
- continue;
352
- tx.recentBlockhash = recentBlockhash.blockhash;
353
- tx.feePayer = (_a = wallet === null || wallet === void 0 ? void 0 : wallet.publicKey) !== null && _a !== void 0 ? _a : (_b = this.wallet) === null || _b === void 0 ? void 0 : _b.publicKey;
354
- // @ts-ignore
355
- tx.SIGNATURE_BLOCK_AND_EXPIRY = recentBlockhash;
356
- }
357
- return this.getSignedTransactionMap(txsToSign, keys, wallet);
358
- }
359
343
  /**
360
344
  * Get a map of signed and prepared transactions from an array of legacy transactions
361
345
  * @param txsToSign
@@ -364,13 +348,13 @@ class TxHandler {
364
348
  * @param commitment
365
349
  * @returns
366
350
  */
367
- async getPreparedAndSignedLegacyTransactionMap(txsToSign, keys, wallet, commitment, recentBlockhash) {
351
+ async getPreparedAndSignedLegacyTransactionMap(txsMap, wallet, commitment, recentBlockhash) {
368
352
  var _a, _b;
369
353
  recentBlockhash = recentBlockhash
370
354
  ? recentBlockhash
371
355
  : await this.getLatestBlockhashForTransaction();
372
356
  this.addHashAndExpiryToLookup(recentBlockhash);
373
- for (const tx of txsToSign) {
357
+ for (const tx of Object.values(txsMap)) {
374
358
  if (!tx)
375
359
  continue;
376
360
  tx.recentBlockhash = recentBlockhash.blockhash;
@@ -378,7 +362,7 @@ class TxHandler {
378
362
  // @ts-ignore
379
363
  tx.SIGNATURE_BLOCK_AND_EXPIRY = recentBlockhash;
380
364
  }
381
- return this.getSignedTransactionMap(txsToSign, keys, wallet);
365
+ return this.getSignedTransactionMap(txsMap, wallet);
382
366
  }
383
367
  /**
384
368
  * Get a map of signed transactions from an array of transactions to sign.
@@ -387,44 +371,59 @@ class TxHandler {
387
371
  * @param wallet
388
372
  * @returns
389
373
  */
390
- async getSignedTransactionMap(txsToSign, keys, wallet) {
374
+ async getSignedTransactionMap(txsToSignMap, wallet) {
391
375
  var _a;
392
376
  [wallet] = this.getProps(wallet);
393
- const signedTxMap = {};
394
- const keysWithTx = [];
395
- txsToSign.forEach((tx, index) => {
396
- if (tx == undefined) {
397
- signedTxMap[keys[index]] = undefined;
398
- }
399
- else {
400
- keysWithTx.push(keys[index]);
377
+ const txsToSignEntries = Object.entries(txsToSignMap);
378
+ // Create a map of the same keys as the input map, but with the values set to undefined. We'll populate the filtered (non-undefined) values with signed transactions.
379
+ const signedTxMap = txsToSignEntries.reduce((acc, [key]) => {
380
+ acc[key] = undefined;
381
+ return acc;
382
+ }, {});
383
+ const filteredTxEntries = txsToSignEntries.filter(([_, tx]) => !!tx);
384
+ // Extra handling for legacy transactions
385
+ for (const [_key, tx] of filteredTxEntries) {
386
+ if (this.isLegacyTransaction(tx)) {
387
+ tx.feePayer = wallet.publicKey;
401
388
  }
402
- });
389
+ }
403
390
  (_a = this.preSignedCb) === null || _a === void 0 ? void 0 : _a.call(this);
404
- const filteredTxs = txsToSign
405
- .map((tx) => {
406
- return tx;
407
- })
408
- .filter((tx) => tx !== undefined);
409
- const signedTxs = await wallet.signAllTransactions(filteredTxs);
410
- signedTxs.forEach((signedTx, index) => {
391
+ const signedFilteredTxs = await wallet.signAllTransactions(filteredTxEntries.map(([_, tx]) => tx));
392
+ signedFilteredTxs.forEach((signedTx, index) => {
411
393
  var _a;
412
394
  // @ts-ignore
413
395
  signedTx.SIGNATURE_BLOCK_AND_EXPIRY =
414
396
  // @ts-ignore
415
- (_a = txsToSign[index]) === null || _a === void 0 ? void 0 : _a.SIGNATURE_BLOCK_AND_EXPIRY;
397
+ (_a = filteredTxEntries[index][1]) === null || _a === void 0 ? void 0 : _a.SIGNATURE_BLOCK_AND_EXPIRY;
416
398
  });
417
- this.handleSignedTxData(signedTxs.map((signedTx) => {
399
+ const signedTxData = this.handleSignedTxData(signedFilteredTxs.map((signedTx) => {
418
400
  return {
419
401
  txSig: this.getTxSigFromSignedTx(signedTx),
420
402
  signedTx,
421
403
  blockHash: this.getBlockhashFromSignedTx(signedTx),
422
404
  };
423
405
  }));
424
- signedTxs.forEach((signedTx, index) => {
425
- signedTxMap[keysWithTx[index]] = signedTx;
406
+ filteredTxEntries.forEach(([key], index) => {
407
+ const signedTx = signedFilteredTxs[index];
408
+ // @ts-ignore
409
+ signedTxMap[key] = signedTx;
410
+ });
411
+ return { signedTxMap, signedTxData };
412
+ }
413
+ /**
414
+ * Accepts multiple instructions and builds a transaction for each. Prevents needing to spam RPC with requests for the same blockhash.
415
+ * @param props
416
+ * @returns
417
+ */
418
+ async buildTransactionsMap(props) {
419
+ const builtTxs = await this.buildBulkTransactions({
420
+ ...props,
421
+ instructions: Object.values(props.instructionsMap),
426
422
  });
427
- return signedTxMap;
423
+ return Object.keys(props.instructionsMap).reduce((acc, key, index) => {
424
+ acc[key] = builtTxs[index];
425
+ return acc;
426
+ }, {});
428
427
  }
429
428
  /**
430
429
  * Builds and signs transactions from a given array of instructions for multiple transactions.
@@ -432,10 +431,10 @@ class TxHandler {
432
431
  * @returns
433
432
  */
434
433
  async buildAndSignTransactionMap(props) {
435
- const transactions = await this.buildBulkTransactions(props);
434
+ const builtTxs = await this.buildTransactionsMap(props);
436
435
  const preppedTransactions = await (props.txVersion === 'legacy'
437
- ? this.getPreparedAndSignedLegacyTransactionMap(transactions, props.keys, props.wallet, props.preFlightCommitment)
438
- : this.getSignedTransactionMap(transactions, props.keys, props.wallet));
436
+ ? this.getPreparedAndSignedLegacyTransactionMap(builtTxs, props.wallet, props.preFlightCommitment)
437
+ : this.getSignedTransactionMap(builtTxs, props.wallet));
439
438
  return preppedTransactions;
440
439
  }
441
440
  }
package/lib/types.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
3
3
  import { BN } from '.';
4
+ export type MappedRecord<A extends Record<string, unknown>, B> = {
5
+ [K in keyof A]: B;
6
+ };
4
7
  export declare enum ExchangeStatus {
5
8
  ACTIVE = 0,
6
9
  DEPOSIT_PAUSED = 1,
@@ -154,6 +157,15 @@ export declare class OracleSource {
154
157
  static readonly PYTH_1M: {
155
158
  pyth1M: {};
156
159
  };
160
+ static readonly PYTH_PULL: {
161
+ pythPull: {};
162
+ };
163
+ static readonly PYTH_1K_PULL: {
164
+ pyth1KPull: {};
165
+ };
166
+ static readonly PYTH_1M_PULL: {
167
+ pyth1MPull: {};
168
+ };
157
169
  static readonly SWITCHBOARD: {
158
170
  switchboard: {};
159
171
  };
@@ -163,6 +175,9 @@ export declare class OracleSource {
163
175
  static readonly PYTH_STABLE_COIN: {
164
176
  pythStableCoin: {};
165
177
  };
178
+ static readonly PYTH_STABLE_COIN_PULL: {
179
+ pythStableCoinPull: {};
180
+ };
166
181
  static readonly Prelaunch: {
167
182
  prelaunch: {};
168
183
  };
package/lib/types.js CHANGED
@@ -105,9 +105,13 @@ exports.OracleSource = OracleSource;
105
105
  OracleSource.PYTH = { pyth: {} };
106
106
  OracleSource.PYTH_1K = { pyth1K: {} };
107
107
  OracleSource.PYTH_1M = { pyth1M: {} };
108
+ OracleSource.PYTH_PULL = { pythPull: {} };
109
+ OracleSource.PYTH_1K_PULL = { pyth1KPull: {} };
110
+ OracleSource.PYTH_1M_PULL = { pyth1MPull: {} };
108
111
  OracleSource.SWITCHBOARD = { switchboard: {} };
109
112
  OracleSource.QUOTE_ASSET = { quoteAsset: {} };
110
113
  OracleSource.PYTH_STABLE_COIN = { pythStableCoin: {} };
114
+ OracleSource.PYTH_STABLE_COIN_PULL = { pythStableCoinPull: {} };
111
115
  OracleSource.Prelaunch = { prelaunch: {} };
112
116
  class OrderType {
113
117
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.84.0-beta.5",
3
+ "version": "2.84.0-beta.6",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -34,10 +34,11 @@
34
34
  "access": "public"
35
35
  },
36
36
  "dependencies": {
37
- "@coral-xyz/anchor": "0.29.0",
37
+ "@coral-xyz/anchor": "0.28.0",
38
38
  "@ellipsis-labs/phoenix-sdk": "^1.4.2",
39
39
  "@project-serum/serum": "^0.13.38",
40
40
  "@pythnetwork/client": "2.5.3",
41
+ "@pythnetwork/pyth-solana-receiver": "^0.7.0",
41
42
  "@solana/spl-token": "^0.3.7",
42
43
  "@solana/web3.js": "1.92.3",
43
44
  "strict-event-emitter-types": "^2.0.0",
@@ -273,6 +273,16 @@ export const MainnetSpotMarkets: SpotMarketConfig[] = [
273
273
  precisionExp: NINE,
274
274
  launchTs: 1716595200000,
275
275
  },
276
+ {
277
+ symbol: 'USDY',
278
+ marketIndex: 18,
279
+ oracle: new PublicKey('DiqUGbq5CV8Tjcae1whjrX97qPo6gU7BKAvKNFc2vrX8'),
280
+ oracleSource: OracleSource.SWITCHBOARD,
281
+ mint: new PublicKey('A1KLoBrKBde8Ty9qtNQUtq3C2ortoC3u7twggz7sEto6'),
282
+ precision: new BN(10).pow(SIX),
283
+ precisionExp: SIX,
284
+ launchTs: 1718811089000,
285
+ },
276
286
  ];
277
287
 
278
288
  export const SpotMarkets: { [key in DriftEnv]: SpotMarketConfig[] } = {