@drift-labs/sdk 2.22.0-beta.1 → 2.22.0-beta.3

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.
@@ -64,6 +64,16 @@ exports.DevnetPerpMarkets = [
64
64
  launchTs: 1677690149000,
65
65
  oracleSource: __1.OracleSource.PYTH,
66
66
  },
67
+ {
68
+ fullName: 'Arbitrum',
69
+ category: ['L2', 'Infra'],
70
+ symbol: 'ARB-PERP',
71
+ baseAssetSymbol: 'ARB',
72
+ marketIndex: 6,
73
+ oracle: new web3_js_1.PublicKey('4mRGHzjGerQNWKXyQAmr9kWqb9saPPHKqo1xziXGQ5Dh'),
74
+ launchTs: 1679501812000,
75
+ oracleSource: __1.OracleSource.PYTH,
76
+ },
67
77
  ];
68
78
  exports.MainnetPerpMarkets = [
69
79
  {
@@ -126,6 +136,16 @@ exports.MainnetPerpMarkets = [
126
136
  launchTs: 1677690149000,
127
137
  oracleSource: __1.OracleSource.PYTH,
128
138
  },
139
+ {
140
+ fullName: 'Arbitrum',
141
+ category: ['L2', 'Infra'],
142
+ symbol: 'ARB-PERP',
143
+ baseAssetSymbol: 'ARB',
144
+ marketIndex: 6,
145
+ oracle: new web3_js_1.PublicKey('5HRrdmghsnU3i2u5StaKaydS7eq3vnKVKwXMzCNKsc4C'),
146
+ launchTs: 1679501812000,
147
+ oracleSource: __1.OracleSource.PYTH,
148
+ },
129
149
  ];
130
150
  exports.PerpMarkets = {
131
151
  devnet: exports.DevnetPerpMarkets,
@@ -91,7 +91,7 @@ export declare class DriftClient {
91
91
  * @param activeSubAccountId
92
92
  */
93
93
  updateWallet(newWallet: IWallet, subAccountIds?: number[], activeSubAccountId?: number): Promise<void>;
94
- switchActiveUser(subAccountId: number): Promise<void>;
94
+ switchActiveUser(subAccountId: number): void;
95
95
  addUser(subAccountId: number): Promise<void>;
96
96
  initializeUserAccount(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
97
97
  getInitializeUserInstructions(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[PublicKey, TransactionInstruction]>;
@@ -170,9 +170,11 @@ export declare class DriftClient {
170
170
  * @param userAccountPublicKey
171
171
  * @param userAccount
172
172
  * @param makerInfo
173
+ * @param txParams
174
+ * @param bracketOrdersParams
173
175
  * @returns
174
176
  */
175
- sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo, txParams?: TxParams): Promise<{
177
+ sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo | MakerInfo[], txParams?: TxParams, bracketOrdersParams?: OptionalOrderParams[], referrerInfo?: ReferrerInfo): Promise<{
176
178
  txSig: TransactionSignature;
177
179
  signedFillTx: Transaction;
178
180
  }>;
@@ -296,7 +298,7 @@ export declare class DriftClient {
296
298
  settlePNLs(users: {
297
299
  settleeUserAccountPublicKey: PublicKey;
298
300
  settleeUserAccount: UserAccount;
299
- }[], marketIndex: number): Promise<TransactionSignature>;
301
+ }[], marketIndexes: number[]): Promise<TransactionSignature>;
300
302
  settlePNL(settleeUserAccountPublicKey: PublicKey, settleeUserAccount: UserAccount, marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
301
303
  settlePNLIx(settleeUserAccountPublicKey: PublicKey, settleeUserAccount: UserAccount, marketIndex: number): Promise<TransactionInstruction>;
302
304
  liquidatePerp(userAccountPublicKey: PublicKey, userAccount: UserAccount, marketIndex: number, maxBaseAssetAmount: BN, limitPrice?: BN, txParams?: TxParams): Promise<TransactionSignature>;
@@ -212,6 +212,7 @@ class DriftClient {
212
212
  getPerpMarketAccounts() {
213
213
  return this.accountSubscriber
214
214
  .getMarketAccountsAndSlots()
215
+ .filter((value) => value !== undefined)
215
216
  .map((value) => value.data);
216
217
  }
217
218
  getSpotMarketAccount(marketIndex) {
@@ -228,6 +229,7 @@ class DriftClient {
228
229
  getSpotMarketAccounts() {
229
230
  return this.accountSubscriber
230
231
  .getSpotMarketAccountsAndSlots()
232
+ .filter((value) => value !== undefined)
231
233
  .map((value) => value.data);
232
234
  }
233
235
  getQuoteSpotMarketAccount() {
@@ -284,7 +286,7 @@ class DriftClient {
284
286
  this.activeSubAccountId = activeSubAccountId;
285
287
  this.userStatsAccountPublicKey = undefined;
286
288
  }
287
- async switchActiveUser(subAccountId) {
289
+ switchActiveUser(subAccountId) {
288
290
  this.activeSubAccountId = subAccountId;
289
291
  }
290
292
  async addUser(subAccountId) {
@@ -1109,16 +1111,21 @@ class DriftClient {
1109
1111
  * @param userAccountPublicKey
1110
1112
  * @param userAccount
1111
1113
  * @param makerInfo
1114
+ * @param txParams
1115
+ * @param bracketOrdersParams
1112
1116
  * @returns
1113
1117
  */
1114
- async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams) {
1118
+ async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams, bracketOrdersParams = new Array(), referrerInfo) {
1115
1119
  const marketIndex = orderParams.marketIndex;
1116
1120
  const orderId = userAccount.nextOrderId;
1117
1121
  const marketOrderTx = (0, utils_1.wrapInTx)(await this.getPlacePerpOrderIx(orderParams), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
1122
+ for (const bracketOrderParams of bracketOrdersParams) {
1123
+ marketOrderTx.add(await this.getPlacePerpOrderIx(bracketOrderParams));
1124
+ }
1118
1125
  const fillTx = (0, utils_1.wrapInTx)(await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
1119
1126
  orderId,
1120
1127
  marketIndex,
1121
- }, makerInfo), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
1128
+ }, makerInfo, referrerInfo), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
1122
1129
  // Apply the latest blockhash to the txs so that we can sign before sending them
1123
1130
  const currentBlockHash = (await this.connection.getLatestBlockhash('finalized')).blockhash;
1124
1131
  marketOrderTx.recentBlockhash = currentBlockHash;
@@ -2050,10 +2057,12 @@ class DriftClient {
2050
2057
  }
2051
2058
  return txSig;
2052
2059
  }
2053
- async settlePNLs(users, marketIndex) {
2060
+ async settlePNLs(users, marketIndexes) {
2054
2061
  const ixs = [];
2055
2062
  for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
2056
- ixs.push(await this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex));
2063
+ for (const marketIndex of marketIndexes) {
2064
+ ixs.push(await this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex));
2065
+ }
2057
2066
  }
2058
2067
  const tx = new web3_js_1.Transaction()
2059
2068
  .add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.22.0-beta.1",
2
+ "version": "2.22.0-beta.3",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -5119,7 +5119,7 @@
5119
5119
  "fields": [
5120
5120
  {
5121
5121
  "name": "revenueWithdrawSinceLastSettle",
5122
- "type": "u64"
5122
+ "type": "i64"
5123
5123
  },
5124
5124
  {
5125
5125
  "name": "maxRevenueWithdrawPerPeriod",
@@ -122,7 +122,8 @@ exports.calculateNetUserPnl = calculateNetUserPnl;
122
122
  function calculateNetUserPnlImbalance(perpMarket, spotMarket, oraclePriceData) {
123
123
  const netUserPnl = calculateNetUserPnl(perpMarket, oraclePriceData);
124
124
  const pnlPool = (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
125
- const imbalance = netUserPnl.sub(pnlPool);
125
+ const feePool = (0, spotBalance_1.getTokenAmount)(perpMarket.amm.feePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
126
+ const imbalance = netUserPnl.sub(pnlPool.add(feePool));
126
127
  return imbalance;
127
128
  }
128
129
  exports.calculateNetUserPnlImbalance = calculateNetUserPnlImbalance;
@@ -83,8 +83,7 @@ function calculatePositionPNL(market, perpPosition, withFunding = false, oracleP
83
83
  exports.calculatePositionPNL = calculatePositionPNL;
84
84
  function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData) {
85
85
  const unrealizedPnl = calculatePositionPNL(market, perpPosition, true, oraclePriceData);
86
- const fundingPnL = calculatePositionFundingPNL(market, perpPosition);
87
- let unsettledPnl = unrealizedPnl.add(fundingPnL);
86
+ let unsettledPnl = unrealizedPnl;
88
87
  if (unrealizedPnl.gt(numericConstants_1.ZERO)) {
89
88
  const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, (0, market_1.calculateNetUserPnlImbalance)(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
90
89
  const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount.sub(perpPosition.quoteEntryAmount), numericConstants_1.ZERO).add(excessPnlPool);
@@ -206,14 +206,10 @@ function calculateWithdrawLimit(spotMarket, now) {
206
206
  .add(marketDepositTokenAmount.mul(sinceLast))
207
207
  .div(sinceLast.add(sinceStart));
208
208
  const maxBorrowTokens = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(6)), borrowTokenTwapLive.add(marketDepositTokenAmount.div(new anchor_1.BN(10)))), marketDepositTokenAmount.sub(marketDepositTokenAmount.div(new anchor_1.BN(5))))); // between ~15-80% utilization with friction on twap
209
- const minDepositTokens = depositTokenTwapLive.sub(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(5)), anchor_1.BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)));
210
- let withdrawLimit = anchor_1.BN.max(marketDepositTokenAmount.sub(minDepositTokens), numericConstants_1.ZERO);
209
+ const minDepositTokens = depositTokenTwapLive.sub(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(4)), anchor_1.BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)));
210
+ const withdrawLimit = anchor_1.BN.max(marketDepositTokenAmount.sub(minDepositTokens), numericConstants_1.ZERO);
211
211
  let borrowLimit = maxBorrowTokens.sub(marketBorrowTokenAmount);
212
- borrowLimit = anchor_1.BN.min(borrowLimit, marketDepositTokenAmount.sub(minDepositTokens));
213
212
  borrowLimit = anchor_1.BN.min(borrowLimit, marketDepositTokenAmount.sub(marketBorrowTokenAmount));
214
- if (borrowLimit.eq(numericConstants_1.ZERO)) {
215
- withdrawLimit = numericConstants_1.ZERO;
216
- }
217
213
  if (withdrawLimit.eq(numericConstants_1.ZERO)) {
218
214
  borrowLimit = numericConstants_1.ZERO;
219
215
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.22.0-beta.1",
3
+ "version": "2.22.0-beta.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -60,4 +60,4 @@
60
60
  "engines": {
61
61
  "node": ">=12"
62
62
  }
63
- }
63
+ }
@@ -74,6 +74,16 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
74
74
  launchTs: 1677690149000, //todo
75
75
  oracleSource: OracleSource.PYTH,
76
76
  },
77
+ {
78
+ fullName: 'Arbitrum',
79
+ category: ['L2', 'Infra'],
80
+ symbol: 'ARB-PERP',
81
+ baseAssetSymbol: 'ARB',
82
+ marketIndex: 6,
83
+ oracle: new PublicKey('4mRGHzjGerQNWKXyQAmr9kWqb9saPPHKqo1xziXGQ5Dh'),
84
+ launchTs: 1679501812000, //todo
85
+ oracleSource: OracleSource.PYTH,
86
+ },
77
87
  ];
78
88
 
79
89
  export const MainnetPerpMarkets: PerpMarketConfig[] = [
@@ -137,6 +147,16 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
137
147
  launchTs: 1677690149000, //todo
138
148
  oracleSource: OracleSource.PYTH,
139
149
  },
150
+ {
151
+ fullName: 'Arbitrum',
152
+ category: ['L2', 'Infra'],
153
+ symbol: 'ARB-PERP',
154
+ baseAssetSymbol: 'ARB',
155
+ marketIndex: 6,
156
+ oracle: new PublicKey('5HRrdmghsnU3i2u5StaKaydS7eq3vnKVKwXMzCNKsc4C'),
157
+ launchTs: 1679501812000, //todo
158
+ oracleSource: OracleSource.PYTH,
159
+ },
140
160
  ];
141
161
 
142
162
  export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
@@ -360,6 +360,7 @@ export class DriftClient {
360
360
  public getPerpMarketAccounts(): PerpMarketAccount[] {
361
361
  return this.accountSubscriber
362
362
  .getMarketAccountsAndSlots()
363
+ .filter((value) => value !== undefined)
363
364
  .map((value) => value.data);
364
365
  }
365
366
 
@@ -383,6 +384,7 @@ export class DriftClient {
383
384
  public getSpotMarketAccounts(): SpotMarketAccount[] {
384
385
  return this.accountSubscriber
385
386
  .getSpotMarketAccountsAndSlots()
387
+ .filter((value) => value !== undefined)
386
388
  .map((value) => value.data);
387
389
  }
388
390
 
@@ -479,7 +481,7 @@ export class DriftClient {
479
481
  this.userStatsAccountPublicKey = undefined;
480
482
  }
481
483
 
482
- public async switchActiveUser(subAccountId: number): Promise<void> {
484
+ public switchActiveUser(subAccountId: number) {
483
485
  this.activeSubAccountId = subAccountId;
484
486
  }
485
487
 
@@ -1946,14 +1948,18 @@ export class DriftClient {
1946
1948
  * @param userAccountPublicKey
1947
1949
  * @param userAccount
1948
1950
  * @param makerInfo
1951
+ * @param txParams
1952
+ * @param bracketOrdersParams
1949
1953
  * @returns
1950
1954
  */
1951
1955
  public async sendMarketOrderAndGetSignedFillTx(
1952
1956
  orderParams: OptionalOrderParams,
1953
1957
  userAccountPublicKey: PublicKey,
1954
1958
  userAccount: UserAccount,
1955
- makerInfo?: MakerInfo,
1956
- txParams?: TxParams
1959
+ makerInfo?: MakerInfo | MakerInfo[],
1960
+ txParams?: TxParams,
1961
+ bracketOrdersParams = new Array<OptionalOrderParams>(),
1962
+ referrerInfo?: ReferrerInfo
1957
1963
  ): Promise<{ txSig: TransactionSignature; signedFillTx: Transaction }> {
1958
1964
  const marketIndex = orderParams.marketIndex;
1959
1965
  const orderId = userAccount.nextOrderId;
@@ -1963,6 +1969,10 @@ export class DriftClient {
1963
1969
  txParams?.computeUnits,
1964
1970
  txParams?.computeUnitsPrice
1965
1971
  );
1972
+ for (const bracketOrderParams of bracketOrdersParams) {
1973
+ marketOrderTx.add(await this.getPlacePerpOrderIx(bracketOrderParams));
1974
+ }
1975
+
1966
1976
  const fillTx = wrapInTx(
1967
1977
  await this.getFillPerpOrderIx(
1968
1978
  userAccountPublicKey,
@@ -1971,7 +1981,8 @@ export class DriftClient {
1971
1981
  orderId,
1972
1982
  marketIndex,
1973
1983
  },
1974
- makerInfo
1984
+ makerInfo,
1985
+ referrerInfo
1975
1986
  ),
1976
1987
  txParams?.computeUnits,
1977
1988
  txParams?.computeUnitsPrice
@@ -3487,17 +3498,19 @@ export class DriftClient {
3487
3498
  settleeUserAccountPublicKey: PublicKey;
3488
3499
  settleeUserAccount: UserAccount;
3489
3500
  }[],
3490
- marketIndex: number
3501
+ marketIndexes: number[]
3491
3502
  ): Promise<TransactionSignature> {
3492
3503
  const ixs = [];
3493
3504
  for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
3494
- ixs.push(
3495
- await this.settlePNLIx(
3496
- settleeUserAccountPublicKey,
3497
- settleeUserAccount,
3498
- marketIndex
3499
- )
3500
- );
3505
+ for (const marketIndex of marketIndexes) {
3506
+ ixs.push(
3507
+ await this.settlePNLIx(
3508
+ settleeUserAccountPublicKey,
3509
+ settleeUserAccount,
3510
+ marketIndex
3511
+ )
3512
+ );
3513
+ }
3501
3514
  }
3502
3515
 
3503
3516
  const tx = new Transaction()
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.22.0-beta.1",
2
+ "version": "2.22.0-beta.3",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -5119,7 +5119,7 @@
5119
5119
  "fields": [
5120
5120
  {
5121
5121
  "name": "revenueWithdrawSinceLastSettle",
5122
- "type": "u64"
5122
+ "type": "i64"
5123
5123
  },
5124
5124
  {
5125
5125
  "name": "maxRevenueWithdrawPerPeriod",
@@ -253,8 +253,13 @@ export function calculateNetUserPnlImbalance(
253
253
  spotMarket,
254
254
  SpotBalanceType.DEPOSIT
255
255
  );
256
+ const feePool = getTokenAmount(
257
+ perpMarket.amm.feePool.scaledBalance,
258
+ spotMarket,
259
+ SpotBalanceType.DEPOSIT
260
+ );
256
261
 
257
- const imbalance = netUserPnl.sub(pnlPool);
262
+ const imbalance = netUserPnl.sub(pnlPool.add(feePool));
258
263
 
259
264
  return imbalance;
260
265
  }
@@ -139,9 +139,7 @@ export function calculateClaimablePnl(
139
139
  oraclePriceData
140
140
  );
141
141
 
142
- const fundingPnL = calculatePositionFundingPNL(market, perpPosition);
143
-
144
- let unsettledPnl = unrealizedPnl.add(fundingPnL);
142
+ let unsettledPnl = unrealizedPnl;
145
143
  if (unrealizedPnl.gt(ZERO)) {
146
144
  const excessPnlPool = BN.max(
147
145
  ZERO,
@@ -345,30 +345,22 @@ export function calculateWithdrawLimit(
345
345
 
346
346
  const minDepositTokens = depositTokenTwapLive.sub(
347
347
  BN.max(
348
- depositTokenTwapLive.div(new BN(5)),
348
+ depositTokenTwapLive.div(new BN(4)),
349
349
  BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)
350
350
  )
351
351
  );
352
352
 
353
- let withdrawLimit = BN.max(
353
+ const withdrawLimit = BN.max(
354
354
  marketDepositTokenAmount.sub(minDepositTokens),
355
355
  ZERO
356
356
  );
357
357
 
358
358
  let borrowLimit = maxBorrowTokens.sub(marketBorrowTokenAmount);
359
- borrowLimit = BN.min(
360
- borrowLimit,
361
- marketDepositTokenAmount.sub(minDepositTokens)
362
- );
363
359
  borrowLimit = BN.min(
364
360
  borrowLimit,
365
361
  marketDepositTokenAmount.sub(marketBorrowTokenAmount)
366
362
  );
367
363
 
368
- if (borrowLimit.eq(ZERO)) {
369
- withdrawLimit = ZERO;
370
- }
371
-
372
364
  if (withdrawLimit.eq(ZERO)) {
373
365
  borrowLimit = ZERO;
374
366
  }