@drift-labs/sdk 2.96.0-beta.2 → 2.96.0-beta.21

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.
Files changed (110) hide show
  1. package/README.md +2 -0
  2. package/VERSION +1 -1
  3. package/bun.lockb +0 -0
  4. package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +5 -3
  5. package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -1
  6. package/lib/accounts/types.d.ts +5 -8
  7. package/lib/accounts/types.js +7 -1
  8. package/lib/accounts/utils.d.ts +7 -0
  9. package/lib/accounts/utils.js +33 -1
  10. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
  11. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +8 -7
  12. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
  13. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
  14. package/lib/config.d.ts +5 -1
  15. package/lib/config.js +9 -1
  16. package/lib/constants/perpMarkets.js +21 -0
  17. package/lib/constants/spotMarkets.js +12 -1
  18. package/lib/driftClient.d.ts +44 -9
  19. package/lib/driftClient.js +181 -61
  20. package/lib/driftClientConfig.d.ts +2 -6
  21. package/lib/events/eventSubscriber.js +9 -8
  22. package/lib/events/types.js +1 -5
  23. package/lib/idl/drift.json +169 -1
  24. package/lib/index.d.ts +1 -0
  25. package/lib/index.js +1 -0
  26. package/lib/math/margin.d.ts +16 -1
  27. package/lib/math/margin.js +67 -1
  28. package/lib/orderParams.js +8 -8
  29. package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -2
  30. package/lib/orderSubscriber/OrderSubscriber.js +4 -19
  31. package/lib/orderSubscriber/types.d.ts +0 -9
  32. package/lib/tokenFaucet.js +2 -1
  33. package/lib/tx/baseTxSender.js +2 -2
  34. package/lib/tx/fastSingleTxSender.js +2 -2
  35. package/lib/tx/forwardOnlyTxSender.js +2 -2
  36. package/lib/tx/retryTxSender.js +2 -2
  37. package/lib/tx/txHandler.js +10 -7
  38. package/lib/tx/whileValidTxSender.d.ts +2 -4
  39. package/lib/tx/whileValidTxSender.js +16 -17
  40. package/lib/types.d.ts +21 -1
  41. package/lib/types.js +6 -1
  42. package/lib/user.d.ts +4 -1
  43. package/lib/user.js +13 -13
  44. package/lib/userConfig.d.ts +1 -6
  45. package/lib/userMap/userMap.js +0 -14
  46. package/lib/userMap/userMapConfig.d.ts +0 -7
  47. package/lib/userStatsConfig.d.ts +0 -6
  48. package/lib/util/TransactionConfirmationManager.d.ts +14 -0
  49. package/lib/util/TransactionConfirmationManager.js +96 -0
  50. package/package.json +4 -5
  51. package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
  52. package/src/accounts/types.ts +6 -9
  53. package/src/accounts/utils.ts +42 -0
  54. package/src/accounts/webSocketAccountSubscriber.ts +1 -1
  55. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +43 -8
  56. package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
  57. package/src/config.ts +15 -1
  58. package/src/constants/perpMarkets.ts +22 -0
  59. package/src/constants/spotMarkets.ts +14 -1
  60. package/src/driftClient.ts +423 -91
  61. package/src/driftClientConfig.ts +2 -7
  62. package/src/events/eventSubscriber.ts +18 -11
  63. package/src/events/types.ts +1 -5
  64. package/src/idl/drift.json +169 -1
  65. package/src/index.ts +1 -0
  66. package/src/math/margin.ts +137 -1
  67. package/src/orderParams.ts +20 -12
  68. package/src/orderSubscriber/OrderSubscriber.ts +1 -15
  69. package/src/orderSubscriber/types.ts +0 -10
  70. package/src/tokenFaucet.ts +2 -2
  71. package/src/tx/baseTxSender.ts +2 -2
  72. package/src/tx/fastSingleTxSender.ts +2 -2
  73. package/src/tx/forwardOnlyTxSender.ts +2 -2
  74. package/src/tx/retryTxSender.ts +2 -2
  75. package/src/tx/txHandler.ts +8 -2
  76. package/src/tx/whileValidTxSender.ts +23 -26
  77. package/src/types.ts +30 -1
  78. package/src/user.ts +35 -13
  79. package/src/userConfig.ts +1 -7
  80. package/src/userMap/userMap.ts +1 -17
  81. package/src/userMap/userMapConfig.ts +0 -8
  82. package/src/userStatsConfig.ts +0 -7
  83. package/src/util/TransactionConfirmationManager.ts +155 -0
  84. package/tests/ci/idl.ts +12 -3
  85. package/tests/ci/verifyConstants.ts +13 -0
  86. package/tests/tx/TransactionConfirmationManager.test.ts +286 -0
  87. package/lib/accounts/grpcAccountSubscriber.d.ts +0 -16
  88. package/lib/accounts/grpcAccountSubscriber.js +0 -155
  89. package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +0 -13
  90. package/lib/accounts/grpcDriftClientAccountSubscriber.js +0 -96
  91. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +0 -10
  92. package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +0 -30
  93. package/lib/accounts/grpcProgramAccountSubscriber.d.ts +0 -19
  94. package/lib/accounts/grpcProgramAccountSubscriber.js +0 -161
  95. package/lib/accounts/grpcUserAccountSubscriber.d.ts +0 -10
  96. package/lib/accounts/grpcUserAccountSubscriber.js +0 -28
  97. package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +0 -10
  98. package/lib/accounts/grpcUserStatsAccountSubscriber.js +0 -28
  99. package/lib/orderSubscriber/grpcSubscription.d.ts +0 -25
  100. package/lib/orderSubscriber/grpcSubscription.js +0 -68
  101. package/lib/userMap/grpcSubscription.d.ts +0 -26
  102. package/lib/userMap/grpcSubscription.js +0 -42
  103. package/src/accounts/grpcAccountSubscriber.ts +0 -158
  104. package/src/accounts/grpcDriftClientAccountSubscriber.ts +0 -196
  105. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -62
  106. package/src/accounts/grpcProgramAccountSubscriber.ts +0 -181
  107. package/src/accounts/grpcUserAccountSubscriber.ts +0 -48
  108. package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -51
  109. package/src/orderSubscriber/grpcSubscription.ts +0 -126
  110. package/src/userMap/grpcSubscription.ts +0 -83
@@ -27,16 +27,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.DriftClient = void 0;
30
+ const anchor = __importStar(require("@coral-xyz/anchor"));
30
31
  const anchor_1 = require("@coral-xyz/anchor");
31
32
  const anchor_30_1 = require("@coral-xyz/anchor-30");
32
33
  const bs58_1 = __importDefault(require("bs58"));
33
34
  const spl_token_1 = require("@solana/spl-token");
34
35
  const types_1 = require("./types");
35
- const anchor = __importStar(require("@coral-xyz/anchor"));
36
36
  const drift_json_1 = __importDefault(require("./idl/drift.json"));
37
37
  const web3_js_1 = require("@solana/web3.js");
38
38
  const events_1 = require("events");
39
39
  const pda_1 = require("./addresses/pda");
40
+ const types_2 = require("./accounts/types");
40
41
  const numericConstants_1 = require("./constants/numericConstants");
41
42
  const position_1 = require("./math/position");
42
43
  const spotBalance_1 = require("./math/spotBalance");
@@ -62,13 +63,11 @@ const txHandler_1 = require("./tx/txHandler");
62
63
  const pyth_solana_receiver_1 = require("@pythnetwork/pyth-solana-receiver");
63
64
  const price_service_sdk_1 = require("@pythnetwork/price-service-sdk");
64
65
  const address_1 = require("@pythnetwork/pyth-solana-receiver/lib/address");
65
- const config_2 = require("./config");
66
66
  const pythPullOracleUtils_1 = require("./util/pythPullOracleUtils");
67
67
  const utils_2 = require("./tx/utils");
68
68
  const pyth_solana_receiver_json_1 = __importDefault(require("./idl/pyth_solana_receiver.json"));
69
69
  const on_demand_1 = require("@switchboard-xyz/on-demand");
70
- const switchboard_on_demand_30_json_1 = __importDefault(require("./idl/switchboard_on_demand_30.json"));
71
- const grpcDriftClientAccountSubscriber_1 = require("./accounts/grpcDriftClientAccountSubscriber");
70
+ const ed = __importStar(require("@noble/ed25519"));
72
71
  /**
73
72
  * # DriftClient
74
73
  * This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
@@ -81,7 +80,7 @@ class DriftClient {
81
80
  this._isSubscribed = val;
82
81
  }
83
82
  constructor(config) {
84
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19;
83
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7;
85
84
  this.users = new Map();
86
85
  this._isSubscribed = false;
87
86
  this.perpMarketLastSlotCache = new Map();
@@ -91,7 +90,7 @@ class DriftClient {
91
90
  this.connection = config.connection;
92
91
  this.wallet = config.wallet;
93
92
  this.opts = config.opts || {
94
- ...anchor_1.AnchorProvider.defaultOptions(),
93
+ ...config_1.DEFAULT_CONFIRMATION_OPTS,
95
94
  commitment: (_a = config === null || config === void 0 ? void 0 : config.connection) === null || _a === void 0 ? void 0 : _a.commitment,
96
95
  preflightCommitment: (_b = config === null || config === void 0 ? void 0 : config.connection) === null || _b === void 0 ? void 0 : _b.commitment, // At the moment this ensures that our transaction simulations (which use Connection object) will use the same commitment level as our Transaction blockhashes (which use these opts)
97
96
  };
@@ -110,6 +109,7 @@ class DriftClient {
110
109
  this.txHandler =
111
110
  (_m = config === null || config === void 0 ? void 0 : config.txHandler) !== null && _m !== void 0 ? _m : new txHandler_1.TxHandler({
112
111
  connection: this.connection,
112
+ // @ts-ignore
113
113
  wallet: this.provider.wallet,
114
114
  confirmationOptions: this.opts,
115
115
  opts: {
@@ -144,32 +144,18 @@ class DriftClient {
144
144
  accountLoader: config.accountSubscription.accountLoader,
145
145
  };
146
146
  }
147
- else if (((_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.type) === 'grpc') {
148
- this.userAccountSubscriptionConfig = {
149
- type: 'grpc',
150
- resubTimeoutMs: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.resubTimeoutMs,
151
- logResubMessages: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.logResubMessages,
152
- configs: (_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.configs,
153
- };
154
- this.userStatsAccountSubscriptionConfig = {
155
- type: 'grpc',
156
- resubTimeoutMs: (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.resubTimeoutMs,
157
- logResubMessages: (_v = config.accountSubscription) === null || _v === void 0 ? void 0 : _v.logResubMessages,
158
- configs: (_w = config.accountSubscription) === null || _w === void 0 ? void 0 : _w.configs,
159
- };
160
- }
161
147
  else {
162
148
  this.userAccountSubscriptionConfig = {
163
149
  type: 'websocket',
164
- resubTimeoutMs: (_x = config.accountSubscription) === null || _x === void 0 ? void 0 : _x.resubTimeoutMs,
165
- logResubMessages: (_y = config.accountSubscription) === null || _y === void 0 ? void 0 : _y.logResubMessages,
166
- commitment: (_z = config.accountSubscription) === null || _z === void 0 ? void 0 : _z.commitment,
150
+ resubTimeoutMs: (_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.resubTimeoutMs,
151
+ logResubMessages: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.logResubMessages,
152
+ commitment: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.commitment,
167
153
  };
168
154
  this.userStatsAccountSubscriptionConfig = {
169
155
  type: 'websocket',
170
- resubTimeoutMs: (_0 = config.accountSubscription) === null || _0 === void 0 ? void 0 : _0.resubTimeoutMs,
171
- logResubMessages: (_1 = config.accountSubscription) === null || _1 === void 0 ? void 0 : _1.logResubMessages,
172
- commitment: (_2 = config.accountSubscription) === null || _2 === void 0 ? void 0 : _2.commitment,
156
+ resubTimeoutMs: (_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.resubTimeoutMs,
157
+ logResubMessages: (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.logResubMessages,
158
+ commitment: (_v = config.accountSubscription) === null || _v === void 0 ? void 0 : _v.commitment,
173
159
  };
174
160
  }
175
161
  if (config.userStats) {
@@ -183,23 +169,18 @@ class DriftClient {
183
169
  if (config.env && !this.marketLookupTable) {
184
170
  this.marketLookupTable = new web3_js_1.PublicKey(config_1.configs[config.env].MARKET_LOOKUP_TABLE);
185
171
  }
172
+ const delistedMarketSetting = config.delistedMarketSetting || types_2.DelistedMarketSetting.Subscribe;
186
173
  const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
187
174
  config.spotMarketIndexes === undefined &&
188
175
  config.oracleInfos === undefined;
189
- if (((_3 = config.accountSubscription) === null || _3 === void 0 ? void 0 : _3.type) === 'polling') {
190
- this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_4 = config.perpMarketIndexes) !== null && _4 !== void 0 ? _4 : [], (_5 = config.spotMarketIndexes) !== null && _5 !== void 0 ? _5 : [], (_6 = config.oracleInfos) !== null && _6 !== void 0 ? _6 : [], noMarketsAndOraclesSpecified);
191
- }
192
- else if (((_7 = config.accountSubscription) === null || _7 === void 0 ? void 0 : _7.type) === 'grpc') {
193
- this.accountSubscriber = new grpcDriftClientAccountSubscriber_1.gprcDriftClientAccountSubscriber(config.accountSubscription.configs, this.program, (_8 = config.perpMarketIndexes) !== null && _8 !== void 0 ? _8 : [], (_9 = config.spotMarketIndexes) !== null && _9 !== void 0 ? _9 : [], (_10 = config.oracleInfos) !== null && _10 !== void 0 ? _10 : [], noMarketsAndOraclesSpecified, {
194
- resubTimeoutMs: (_11 = config.accountSubscription) === null || _11 === void 0 ? void 0 : _11.resubTimeoutMs,
195
- logResubMessages: (_12 = config.accountSubscription) === null || _12 === void 0 ? void 0 : _12.logResubMessages,
196
- });
176
+ if (((_w = config.accountSubscription) === null || _w === void 0 ? void 0 : _w.type) === 'polling') {
177
+ this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_x = config.perpMarketIndexes) !== null && _x !== void 0 ? _x : [], (_y = config.spotMarketIndexes) !== null && _y !== void 0 ? _y : [], (_z = config.oracleInfos) !== null && _z !== void 0 ? _z : [], noMarketsAndOraclesSpecified, delistedMarketSetting);
197
178
  }
198
179
  else {
199
- this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_13 = config.perpMarketIndexes) !== null && _13 !== void 0 ? _13 : [], (_14 = config.spotMarketIndexes) !== null && _14 !== void 0 ? _14 : [], (_15 = config.oracleInfos) !== null && _15 !== void 0 ? _15 : [], noMarketsAndOraclesSpecified, {
200
- resubTimeoutMs: (_16 = config.accountSubscription) === null || _16 === void 0 ? void 0 : _16.resubTimeoutMs,
201
- logResubMessages: (_17 = config.accountSubscription) === null || _17 === void 0 ? void 0 : _17.logResubMessages,
202
- }, (_18 = config.accountSubscription) === null || _18 === void 0 ? void 0 : _18.commitment);
180
+ this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_0 = config.perpMarketIndexes) !== null && _0 !== void 0 ? _0 : [], (_1 = config.spotMarketIndexes) !== null && _1 !== void 0 ? _1 : [], (_2 = config.oracleInfos) !== null && _2 !== void 0 ? _2 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
181
+ resubTimeoutMs: (_3 = config.accountSubscription) === null || _3 === void 0 ? void 0 : _3.resubTimeoutMs,
182
+ logResubMessages: (_4 = config.accountSubscription) === null || _4 === void 0 ? void 0 : _4.logResubMessages,
183
+ }, (_5 = config.accountSubscription) === null || _5 === void 0 ? void 0 : _5.commitment);
203
184
  }
204
185
  this.eventEmitter = this.accountSubscriber.eventEmitter;
205
186
  this.metricsEventEmitter = new events_1.EventEmitter();
@@ -207,12 +188,14 @@ class DriftClient {
207
188
  this.enableMetricsEvents = true;
208
189
  }
209
190
  this.txSender =
210
- (_19 = config.txSender) !== null && _19 !== void 0 ? _19 : new retryTxSender_1.RetryTxSender({
191
+ (_6 = config.txSender) !== null && _6 !== void 0 ? _6 : new retryTxSender_1.RetryTxSender({
211
192
  connection: this.connection,
212
193
  wallet: this.wallet,
213
194
  opts: this.opts,
214
195
  txHandler: this.txHandler,
215
196
  });
197
+ this.sbOnDemandProgramdId =
198
+ config_1.configs[(_7 = config.env) !== null && _7 !== void 0 ? _7 : 'mainnet-beta'].SB_ON_DEMAND_PID;
216
199
  }
217
200
  getUserMapKey(subAccountId, authority) {
218
201
  return `${subAccountId}_${authority.toString()}`;
@@ -1139,7 +1122,7 @@ class DriftClient {
1139
1122
  data: Buffer.from([0x1]),
1140
1123
  });
1141
1124
  }
1142
- async createDepositTxn(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly = false, txParams) {
1125
+ async getDepositTxnIx(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly = false) {
1143
1126
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1144
1127
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
1145
1128
  const signerAuthority = this.wallet.publicKey;
@@ -1156,6 +1139,10 @@ class DriftClient {
1156
1139
  if (createWSOLTokenAccount) {
1157
1140
  instructions.push((0, spl_token_1.createCloseAccountInstruction)(associatedTokenAccount, signerAuthority, signerAuthority, []));
1158
1141
  }
1142
+ return instructions;
1143
+ }
1144
+ async createDepositTxn(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly = false, txParams) {
1145
+ const instructions = await this.getDepositTxnIx(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly);
1159
1146
  txParams = { ...(txParams !== null && txParams !== void 0 ? txParams : this.txParams), computeUnits: 600000 };
1160
1147
  const tx = await this.buildTransaction(instructions, txParams);
1161
1148
  return tx;
@@ -1263,7 +1250,7 @@ class DriftClient {
1263
1250
  getAssociatedTokenAccountCreationIx(tokenMintAddress, associatedTokenAddress, tokenProgram) {
1264
1251
  return (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.wallet.publicKey, associatedTokenAddress, this.wallet.publicKey, tokenMintAddress, tokenProgram);
1265
1252
  }
1266
- async createInitializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, txParams, customMaxMarginRatio) {
1253
+ async createInitializeUserAccountAndDepositCollateralIxs(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, customMaxMarginRatio) {
1267
1254
  const ixs = [];
1268
1255
  const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
1269
1256
  const spotMarket = this.getSpotMarketAccount(marketIndex);
@@ -1309,6 +1296,13 @@ class DriftClient {
1309
1296
  if (createWSOLTokenAccount) {
1310
1297
  ixs.push((0, spl_token_1.createCloseAccountInstruction)(wsolTokenAccount, authority, authority, []));
1311
1298
  }
1299
+ return {
1300
+ ixs,
1301
+ userAccountPublicKey,
1302
+ };
1303
+ }
1304
+ async createInitializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, txParams, customMaxMarginRatio) {
1305
+ const { ixs, userAccountPublicKey } = await this.createInitializeUserAccountAndDepositCollateralIxs(amount, userTokenAccount, marketIndex, subAccountId, name, fromSubAccountId, referrerInfo, donateAmount, customMaxMarginRatio);
1312
1306
  const tx = await this.buildTransaction(ixs, txParams);
1313
1307
  return [tx, userAccountPublicKey];
1314
1308
  }
@@ -1616,7 +1610,7 @@ class DriftClient {
1616
1610
  direction,
1617
1611
  baseAssetAmount: amount,
1618
1612
  price: limitPrice,
1619
- }, undefined, undefined, undefined, subAccountId);
1613
+ }, undefined, undefined, undefined, undefined, subAccountId);
1620
1614
  }
1621
1615
  async sendSignedTx(tx, opts) {
1622
1616
  const { txSig } = await this.sendTransaction(tx, undefined, opts !== null && opts !== void 0 ? opts : this.opts, true);
@@ -1684,13 +1678,21 @@ class DriftClient {
1684
1678
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
1685
1679
  return txSig;
1686
1680
  }
1687
- async getPlacePerpOrderIx(orderParams, subAccountId) {
1681
+ async getPlacePerpOrderIx(orderParams, subAccountId, depositToTradeArgs) {
1688
1682
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
1689
- const user = await this.getUserAccountPublicKey(subAccountId);
1683
+ const isDepositToTradeTx = depositToTradeArgs !== undefined;
1684
+ const user = isDepositToTradeTx
1685
+ ? (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.authority, subAccountId)
1686
+ : await this.getUserAccountPublicKey(subAccountId);
1690
1687
  const remainingAccounts = this.getRemainingAccounts({
1691
- userAccounts: [this.getUserAccount(subAccountId)],
1692
- useMarketLastSlotCache: true,
1688
+ userAccounts: (depositToTradeArgs === null || depositToTradeArgs === void 0 ? void 0 : depositToTradeArgs.isMakingNewAccount)
1689
+ ? []
1690
+ : [this.getUserAccount(subAccountId)],
1691
+ useMarketLastSlotCache: false,
1693
1692
  readablePerpMarketIndex: orderParams.marketIndex,
1693
+ readableSpotMarketIndexes: isDepositToTradeTx
1694
+ ? [depositToTradeArgs === null || depositToTradeArgs === void 0 ? void 0 : depositToTradeArgs.depositMarketIndex]
1695
+ : undefined,
1694
1696
  });
1695
1697
  return await this.program.instruction.placePerpOrder(orderParams, {
1696
1698
  accounts: {
@@ -2726,8 +2728,8 @@ class DriftClient {
2726
2728
  remainingAccounts,
2727
2729
  });
2728
2730
  }
2729
- async placeAndTakePerpOrder(orderParams, makerInfo, referrerInfo, txParams, subAccountId) {
2730
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId), txParams), [], this.opts);
2731
+ async placeAndTakePerpOrder(orderParams, makerInfo, referrerInfo, successCondition, txParams, subAccountId) {
2732
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, successCondition, subAccountId), txParams), [], this.opts);
2731
2733
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2732
2734
  return txSig;
2733
2735
  }
@@ -2743,7 +2745,7 @@ class DriftClient {
2743
2745
  let earlyExitFailedPlaceAndTakeSim = false;
2744
2746
  const prepPlaceAndTakeTx = async () => {
2745
2747
  var _a;
2746
- const placeAndTakeIx = await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId);
2748
+ const placeAndTakeIx = await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, undefined, subAccountId);
2747
2749
  placeAndTakeIxs.push(placeAndTakeIx);
2748
2750
  if (bracketOrdersParams.length > 0) {
2749
2751
  const bracketOrdersIx = await this.getPlaceOrdersIx(bracketOrdersParams, subAccountId);
@@ -2802,7 +2804,9 @@ class DriftClient {
2802
2804
  if (!txsToSign) {
2803
2805
  return null;
2804
2806
  }
2805
- const signedTxs = (await this.txHandler.getSignedTransactionMap(txsToSign, this.provider.wallet)).signedTxMap;
2807
+ const signedTxs = (await this.txHandler.getSignedTransactionMap(txsToSign,
2808
+ // @ts-ignore
2809
+ this.provider.wallet)).signedTxMap;
2806
2810
  const { txSig, slot } = await this.sendTransaction(signedTxs.placeAndTakeTx, [], this.opts, true);
2807
2811
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2808
2812
  return {
@@ -2811,7 +2815,7 @@ class DriftClient {
2811
2815
  signedSettlePnlTx: signedTxs.settlePnlTx,
2812
2816
  };
2813
2817
  }
2814
- async getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId) {
2818
+ async getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, successCondition, subAccountId) {
2815
2819
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
2816
2820
  const userStatsPublicKey = await this.getUserStatsAccountPublicKey();
2817
2821
  const user = await this.getUserAccountPublicKey(subAccountId);
@@ -2857,7 +2861,7 @@ class DriftClient {
2857
2861
  });
2858
2862
  }
2859
2863
  }
2860
- return await this.program.instruction.placeAndTakePerpOrder(orderParams, null, {
2864
+ return await this.program.instruction.placeAndTakePerpOrder(orderParams, successCondition !== null && successCondition !== void 0 ? successCondition : null, {
2861
2865
  accounts: {
2862
2866
  state: await this.getStatePublicKey(),
2863
2867
  user,
@@ -2909,6 +2913,123 @@ class DriftClient {
2909
2913
  remainingAccounts,
2910
2914
  });
2911
2915
  }
2916
+ encodeSwiftServerMessage(message) {
2917
+ const messageWithBuffer = {
2918
+ slot: message.slot,
2919
+ swiftOrderSignature: message.swiftOrderSignature,
2920
+ };
2921
+ return this.program.coder.types.encode('SwiftServerMessage', messageWithBuffer);
2922
+ }
2923
+ decodeSwiftServerMessage(encodedMessage) {
2924
+ const decodedSwiftMessage = this.program.coder.types.decode('SwiftServerMessage', encodedMessage);
2925
+ return {
2926
+ slot: decodedSwiftMessage.slot,
2927
+ swiftOrderSignature: decodedSwiftMessage.swiftSignature,
2928
+ };
2929
+ }
2930
+ async signSwiftServerMessage(message) {
2931
+ const swiftServerMessage = Uint8Array.from(this.encodeSwiftServerMessage(message));
2932
+ return await this.signMessage(swiftServerMessage);
2933
+ }
2934
+ async signSwiftOrderParamsMessage(orderParamsMessage) {
2935
+ const takerOrderParamsMessage = Uint8Array.from(this.encodeSwiftOrderParamsMessage(orderParamsMessage));
2936
+ return await this.signMessage(takerOrderParamsMessage);
2937
+ }
2938
+ encodeSwiftOrderParamsMessage(orderParamsMessage) {
2939
+ return this.program.coder.types.encode('SwiftOrderParamsMessage', orderParamsMessage);
2940
+ }
2941
+ decodeSwiftOrderParamsMessage(encodedMessage) {
2942
+ return this.program.coder.types.decode('SwiftOrderParamsMessage', encodedMessage);
2943
+ }
2944
+ async signMessage(message, keypair = this.wallet.payer) {
2945
+ return Buffer.from(await ed.sign(message, keypair.secretKey.slice(0, 32)));
2946
+ }
2947
+ async placeSwiftTakerOrder(swiftServerMessage, swiftSignature, swiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo, txParams) {
2948
+ const ixs = await this.getPlaceSwiftTakerPerpOrderIxs(swiftServerMessage, swiftSignature, swiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo);
2949
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(ixs, txParams), [], this.opts);
2950
+ return txSig;
2951
+ }
2952
+ async getPlaceSwiftTakerPerpOrderIxs(encodedSwiftServerMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo) {
2953
+ const remainingAccounts = this.getRemainingAccounts({
2954
+ userAccounts: [takerInfo.takerUserAccount],
2955
+ useMarketLastSlotCache: true,
2956
+ readablePerpMarketIndex: marketIndex,
2957
+ });
2958
+ const swiftServerSignatureIx = web3_js_1.Ed25519Program.createInstructionWithPublicKey({
2959
+ publicKey: new web3_js_1.PublicKey(config_1.SWIFT_ID).toBytes(),
2960
+ signature: Uint8Array.from(swiftSignature),
2961
+ message: Uint8Array.from(encodedSwiftServerMessage),
2962
+ });
2963
+ const swiftOrderParamsSignatureIx = web3_js_1.Ed25519Program.createInstructionWithPublicKey({
2964
+ publicKey: takerInfo.takerUserAccount.authority.toBytes(),
2965
+ signature: Uint8Array.from(swiftOrderParamsSignature),
2966
+ message: Uint8Array.from(encodedSwiftOrderParamsMessage),
2967
+ });
2968
+ const placeTakerSwiftPerpOrderIx = await this.program.instruction.placeSwiftTakerOrder(encodedSwiftServerMessage, encodedSwiftOrderParamsMessage, swiftSignature, {
2969
+ accounts: {
2970
+ state: await this.getStatePublicKey(),
2971
+ user: takerInfo.taker,
2972
+ userStats: takerInfo.takerStats,
2973
+ authority: this.wallet.publicKey,
2974
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
2975
+ },
2976
+ remainingAccounts,
2977
+ });
2978
+ return [
2979
+ swiftServerSignatureIx,
2980
+ swiftOrderParamsSignatureIx,
2981
+ placeTakerSwiftPerpOrderIx,
2982
+ ];
2983
+ }
2984
+ async placeAndMakeSwiftPerpOrder(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, txParams, subAccountId) {
2985
+ const ixs = await this.getPlaceAndMakeSwiftPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, subAccountId);
2986
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(ixs, txParams), [], this.opts);
2987
+ this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2988
+ return txSig;
2989
+ }
2990
+ async getPlaceAndMakeSwiftPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, subAccountId) {
2991
+ const [swiftServerSignatureIx, swiftOrderSignatureIx, placeTakerSwiftPerpOrderIx,] = await this.getPlaceSwiftTakerPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, orderParams.marketIndex, takerInfo);
2992
+ orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
2993
+ const userStatsPublicKey = this.getUserStatsAccountPublicKey();
2994
+ const user = await this.getUserAccountPublicKey(subAccountId);
2995
+ const remainingAccounts = this.getRemainingAccounts({
2996
+ userAccounts: [
2997
+ this.getUserAccount(subAccountId),
2998
+ takerInfo.takerUserAccount,
2999
+ ],
3000
+ useMarketLastSlotCache: true,
3001
+ writablePerpMarketIndexes: [orderParams.marketIndex],
3002
+ });
3003
+ if (referrerInfo) {
3004
+ remainingAccounts.push({
3005
+ pubkey: referrerInfo.referrer,
3006
+ isWritable: true,
3007
+ isSigner: false,
3008
+ });
3009
+ remainingAccounts.push({
3010
+ pubkey: referrerInfo.referrerStats,
3011
+ isWritable: true,
3012
+ isSigner: false,
3013
+ });
3014
+ }
3015
+ const placeAndMakeIx = await this.program.instruction.placeAndMakePerpOrder(orderParams, takerExpectedOrderId, {
3016
+ accounts: {
3017
+ state: await this.getStatePublicKey(),
3018
+ user,
3019
+ userStats: userStatsPublicKey,
3020
+ taker: takerInfo.taker,
3021
+ takerStats: takerInfo.takerStats,
3022
+ authority: this.wallet.publicKey,
3023
+ },
3024
+ remainingAccounts,
3025
+ });
3026
+ return [
3027
+ swiftServerSignatureIx,
3028
+ swiftOrderSignatureIx,
3029
+ placeTakerSwiftPerpOrderIx,
3030
+ placeAndMakeIx,
3031
+ ];
3032
+ }
2912
3033
  async preparePlaceAndTakeSpotOrder(orderParams, fulfillmentConfig, makerInfo, referrerInfo, txParams, subAccountId) {
2913
3034
  const tx = await this.buildTransaction(await this.getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo, subAccountId), txParams);
2914
3035
  return {
@@ -3036,7 +3157,7 @@ class DriftClient {
3036
3157
  baseAssetAmount: userPosition.baseAssetAmount.abs(),
3037
3158
  reduceOnly: true,
3038
3159
  price: limitPrice,
3039
- }, undefined, undefined, undefined, subAccountId);
3160
+ }, undefined, undefined, undefined, undefined, subAccountId);
3040
3161
  }
3041
3162
  /**
3042
3163
  * Modifies an open order by closing it and replacing it with a new order.
@@ -3930,9 +4051,10 @@ class DriftClient {
3930
4051
  }
3931
4052
  return this.receiverProgram;
3932
4053
  }
3933
- getSwitchboardOnDemandProgram() {
4054
+ async getSwitchboardOnDemandProgram() {
4055
+ const idl = (await anchor_30_1.Program.fetchIdl(this.sbOnDemandProgramdId, this.provider));
3934
4056
  if (this.sbOnDemandProgram === undefined) {
3935
- this.sbOnDemandProgram = new anchor_30_1.Program(switchboard_on_demand_30_json_1.default, this.provider);
4057
+ this.sbOnDemandProgram = new anchor_30_1.Program(idl, this.provider);
3936
4058
  }
3937
4059
  return this.sbOnDemandProgram;
3938
4060
  }
@@ -3973,7 +4095,7 @@ class DriftClient {
3973
4095
  postIxs.push(this.program.instruction.postMultiPythPullOracleUpdatesAtomic(encodedParams, {
3974
4096
  accounts: {
3975
4097
  keeper: this.wallet.publicKey,
3976
- pythSolanaReceiver: config_2.DRIFT_ORACLE_RECEIVER_ID,
4098
+ pythSolanaReceiver: config_1.DRIFT_ORACLE_RECEIVER_ID,
3977
4099
  guardianSet,
3978
4100
  },
3979
4101
  remainingAccounts,
@@ -3996,7 +4118,7 @@ class DriftClient {
3996
4118
  return this.program.instruction.postPythPullOracleUpdateAtomic(feedIdBuffer, encodedParams, {
3997
4119
  accounts: {
3998
4120
  keeper: this.wallet.publicKey,
3999
- pythSolanaReceiver: config_2.DRIFT_ORACLE_RECEIVER_ID,
4121
+ pythSolanaReceiver: config_1.DRIFT_ORACLE_RECEIVER_ID,
4000
4122
  guardianSet,
4001
4123
  priceFeed: (0, pda_1.getPythPullOraclePublicKey)(this.program.programId, feedIdBuffer),
4002
4124
  },
@@ -4024,14 +4146,14 @@ class DriftClient {
4024
4146
  return this.program.instruction.updatePythPullOracle(feedIdBuffer, encodedParams, {
4025
4147
  accounts: {
4026
4148
  keeper: this.wallet.publicKey,
4027
- pythSolanaReceiver: config_2.DRIFT_ORACLE_RECEIVER_ID,
4149
+ pythSolanaReceiver: config_1.DRIFT_ORACLE_RECEIVER_ID,
4028
4150
  encodedVaa: encodedVaaAddress,
4029
4151
  priceFeed: (0, pda_1.getPythPullOraclePublicKey)(this.program.programId, feedIdBuffer),
4030
4152
  },
4031
4153
  });
4032
4154
  }
4033
4155
  async getPostSwitchboardOnDemandUpdateAtomicIx(feed, numSignatures = 3) {
4034
- const program = this.getSwitchboardOnDemandProgram();
4156
+ const program = await this.getSwitchboardOnDemandProgram();
4035
4157
  const feedAccount = new on_demand_1.PullFeed(program, feed);
4036
4158
  if (!this.sbProgramFeedConfigs) {
4037
4159
  this.sbProgramFeedConfigs = new Map();
@@ -4042,8 +4164,6 @@ class DriftClient {
4042
4164
  }
4043
4165
  const [pullIx, _responses, success] = await feedAccount.fetchUpdateIx({
4044
4166
  numSignatures,
4045
- // @ts-ignore :: TODO someone needs to look at this
4046
- feedConfigs: this.sbProgramFeedConfigs.get(feed.toString()),
4047
4167
  });
4048
4168
  if (!success) {
4049
4169
  return undefined;
@@ -5,7 +5,7 @@ import { BulkAccountLoader } from './accounts/bulkAccountLoader';
5
5
  import { DriftEnv } from './config';
6
6
  import { TxSender } from './tx/types';
7
7
  import { TxHandler, TxHandlerConfig } from './tx/txHandler';
8
- import { GrpcConfigs } from './accounts/types';
8
+ import { DelistedMarketSetting } from './accounts/types';
9
9
  export type DriftClientConfig = {
10
10
  connection: Connection;
11
11
  wallet: IWallet;
@@ -30,6 +30,7 @@ export type DriftClientConfig = {
30
30
  txParams?: TxParams;
31
31
  enableMetricsEvents?: boolean;
32
32
  txHandlerConfig?: TxHandlerConfig;
33
+ delistedMarketSetting?: DelistedMarketSetting;
33
34
  };
34
35
  export type DriftClientSubscriptionConfig = {
35
36
  type: 'websocket';
@@ -39,9 +40,4 @@ export type DriftClientSubscriptionConfig = {
39
40
  } | {
40
41
  type: 'polling';
41
42
  accountLoader: BulkAccountLoader;
42
- } | {
43
- type: 'grpc';
44
- configs: GrpcConfigs;
45
- resubTimeoutMs?: number;
46
- logResubMessages?: boolean;
47
43
  };
@@ -28,23 +28,24 @@ class EventSubscriber {
28
28
  this.initializeLogProvider();
29
29
  }
30
30
  initializeLogProvider(subscribe = false) {
31
+ const logProviderConfig = this.options.logProviderConfig;
31
32
  if (this.currentProviderType === 'websocket') {
32
- const logProviderConfig = this.options
33
- .logProviderConfig;
34
33
  this.logProvider = new webSocketLogProvider_1.WebSocketLogProvider(
35
34
  // @ts-ignore
36
- this.connection, this.address, this.options.commitment, logProviderConfig.resubTimeoutMs);
35
+ this.connection, this.address, this.options.commitment, this.options.logProviderConfig.resubTimeoutMs);
37
36
  }
38
37
  else if (this.currentProviderType === 'polling') {
39
- const logProviderConfig = this.options
40
- .logProviderConfig;
38
+ const frequency = 'frequency' in logProviderConfig
39
+ ? logProviderConfig.frequency
40
+ : logProviderConfig.fallbackFrequency;
41
+ const batchSize = 'batchSize' in logProviderConfig
42
+ ? logProviderConfig.batchSize
43
+ : logProviderConfig.fallbackBatchSize;
41
44
  this.logProvider = new pollingLogProvider_1.PollingLogProvider(
42
45
  // @ts-ignore
43
- this.connection, this.address, this.options.commitment, logProviderConfig.frequency, logProviderConfig.batchSize);
46
+ this.connection, this.address, this.options.commitment, frequency, batchSize);
44
47
  }
45
48
  else if (this.currentProviderType === 'events-server') {
46
- const logProviderConfig = this.options
47
- .logProviderConfig;
48
49
  this.logProvider = new eventsServerLogProvider_1.EventsServerLogProvider(logProviderConfig.url, this.options.eventTypes, this.options.address ? this.options.address.toString() : undefined);
49
50
  }
50
51
  else {
@@ -25,10 +25,6 @@ exports.DefaultEventSubscriptionOptions = {
25
25
  commitment: 'confirmed',
26
26
  maxTx: 4096,
27
27
  logProviderConfig: {
28
- type: 'events-server',
29
- url: 'wss://events.drift.trade/ws',
30
- maxReconnectAttempts: 5,
31
- fallbackFrequency: 1000,
32
- fallbackBatchSize: 100,
28
+ type: 'websocket',
33
29
  },
34
30
  };