@drift-labs/sdk 0.2.0-master.35 → 0.2.0-master.37

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.
@@ -6,8 +6,8 @@ import {
6
6
  ZERO,
7
7
  } from '../constants/numericConstants';
8
8
  import { PerpMarketAccount, isVariant } from '../types';
9
- import { calculateReservePrice } from './market';
10
9
  import { OraclePriceData } from '../oracles/types';
10
+ import { calculateBidAskPrice } from './amm';
11
11
 
12
12
  /**
13
13
  *
@@ -48,10 +48,8 @@ export async function calculateAllEstimatedFundingRate(
48
48
  secondsInHour,
49
49
  BN.max(ZERO, secondsInHour.sub(timeSinceLastMarkChange))
50
50
  );
51
- const baseAssetPriceWithMantissa = calculateReservePrice(
52
- market,
53
- oraclePriceData
54
- );
51
+ const [bid, ask] = calculateBidAskPrice(market.amm, oraclePriceData);
52
+ const baseAssetPriceWithMantissa = bid.add(ask).div(new BN(2));
55
53
 
56
54
  const markTwapWithMantissa = markTwapTimeSinceLastUpdate
57
55
  .mul(lastMarkTwapWithMantissa)
@@ -131,7 +131,7 @@ export function calculateMarketMarginRatio(
131
131
  case 'Initial':
132
132
  marginRatio = calculateSizePremiumLiabilityWeight(
133
133
  size,
134
- market.imfFactor,
134
+ new BN(market.imfFactor),
135
135
  new BN(market.marginRatioInitial),
136
136
  MARGIN_PRECISION
137
137
  ).toNumber();
@@ -139,7 +139,7 @@ export function calculateMarketMarginRatio(
139
139
  case 'Maintenance':
140
140
  marginRatio = calculateSizePremiumLiabilityWeight(
141
141
  size,
142
- market.imfFactor,
142
+ new BN(market.imfFactor),
143
143
  new BN(market.marginRatioMaintenance),
144
144
  MARGIN_PRECISION
145
145
  ).toNumber();
@@ -176,7 +176,7 @@ export function calculateUnrealizedAssetWeight(
176
176
 
177
177
  assetWeight = calculateSizeDiscountAssetWeight(
178
178
  unrealizedPnl,
179
- market.unrealizedPnlImfFactor,
179
+ new BN(market.unrealizedPnlImfFactor),
180
180
  assetWeight
181
181
  );
182
182
  break;
@@ -208,9 +208,7 @@ export function calculateNetUserPnl(
208
208
  .div(BASE_PRECISION)
209
209
  .div(PRICE_TO_QUOTE_PRECISION);
210
210
 
211
- const netUserCostBasis = perpMarket.amm.quoteAssetAmountLong
212
- .add(perpMarket.amm.quoteAssetAmountShort)
213
- .sub(perpMarket.amm.cumulativeSocialLoss);
211
+ const netUserCostBasis = perpMarket.amm.quoteAssetAmount;
214
212
 
215
213
  const netUserPnl = netUserPositionValue.add(netUserCostBasis);
216
214
 
@@ -209,13 +209,18 @@ export function calculateBaseAssetAmountForAmmToFulfill(
209
209
  return ZERO;
210
210
  }
211
211
 
212
- const limitPrice = getLimitPrice(order, oraclePriceData, slot);
213
- const baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(
214
- order,
215
- market,
216
- limitPrice,
217
- oraclePriceData
218
- );
212
+ const limitPrice = getOptionalLimitPrice(order, oraclePriceData, slot);
213
+ let baseAssetAmount;
214
+ if (limitPrice !== undefined) {
215
+ baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(
216
+ order,
217
+ market,
218
+ limitPrice,
219
+ oraclePriceData
220
+ );
221
+ } else {
222
+ baseAssetAmount = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
223
+ }
219
224
 
220
225
  const maxBaseAssetAmount = calculateMaxBaseAssetAmountFillable(
221
226
  market.amm,
@@ -249,8 +254,11 @@ export function calculateBaseAssetAmountToFillUpToLimitPrice(
249
254
  return ZERO;
250
255
  }
251
256
 
252
- return baseAssetAmount.gt(order.baseAssetAmount)
253
- ? order.baseAssetAmount
257
+ const baseAssetAmountUnfilled = order.baseAssetAmount.sub(
258
+ order.baseAssetAmountFilled
259
+ );
260
+ return baseAssetAmount.gt(baseAssetAmountUnfilled)
261
+ ? baseAssetAmountUnfilled
254
262
  : baseAssetAmount;
255
263
  }
256
264
 
@@ -205,7 +205,24 @@ export function positionIsAvailable(position: PerpPosition): boolean {
205
205
  /**
206
206
  *
207
207
  * @param userPosition
208
- * @returns Precision: PRICE_PRECISION (10^10)
208
+ * @returns Precision: PRICE_PRECISION (10^6)
209
+ */
210
+ export function calculateBreakEvenPrice(userPosition: PerpPosition): BN {
211
+ if (userPosition.baseAssetAmount.eq(ZERO)) {
212
+ return ZERO;
213
+ }
214
+
215
+ return userPosition.quoteBreakEvenAmount
216
+ .mul(PRICE_PRECISION)
217
+ .mul(AMM_TO_QUOTE_PRECISION_RATIO)
218
+ .div(userPosition.baseAssetAmount)
219
+ .abs();
220
+ }
221
+
222
+ /**
223
+ *
224
+ * @param userPosition
225
+ * @returns Precision: PRICE_PRECISION (10^6)
209
226
  */
210
227
  export function calculateEntryPrice(userPosition: PerpPosition): BN {
211
228
  if (userPosition.baseAssetAmount.eq(ZERO)) {
@@ -103,19 +103,19 @@ export function calculateAssetWeight(
103
103
  case 'Initial':
104
104
  assetWeight = calculateSizeDiscountAssetWeight(
105
105
  sizeInAmmReservePrecision,
106
- spotMarket.imfFactor,
107
- spotMarket.initialAssetWeight
106
+ new BN(spotMarket.imfFactor),
107
+ new BN(spotMarket.initialAssetWeight)
108
108
  );
109
109
  break;
110
110
  case 'Maintenance':
111
111
  assetWeight = calculateSizeDiscountAssetWeight(
112
112
  sizeInAmmReservePrecision,
113
- spotMarket.imfFactor,
114
- spotMarket.maintenanceAssetWeight
113
+ new BN(spotMarket.imfFactor),
114
+ new BN(spotMarket.maintenanceAssetWeight)
115
115
  );
116
116
  break;
117
117
  default:
118
- assetWeight = spotMarket.initialAssetWeight;
118
+ assetWeight = new BN(spotMarket.initialAssetWeight);
119
119
  break;
120
120
  }
121
121
 
@@ -145,16 +145,16 @@ export function calculateLiabilityWeight(
145
145
  case 'Initial':
146
146
  assetWeight = calculateSizePremiumLiabilityWeight(
147
147
  sizeInAmmReservePrecision,
148
- spotMarket.imfFactor,
149
- spotMarket.initialLiabilityWeight,
148
+ new BN(spotMarket.imfFactor),
149
+ new BN(spotMarket.initialLiabilityWeight),
150
150
  SPOT_MARKET_WEIGHT_PRECISION
151
151
  );
152
152
  break;
153
153
  case 'Maintenance':
154
154
  assetWeight = calculateSizePremiumLiabilityWeight(
155
155
  sizeInAmmReservePrecision,
156
- spotMarket.imfFactor,
157
- spotMarket.maintenanceLiabilityWeight,
156
+ new BN(spotMarket.imfFactor),
157
+ new BN(spotMarket.maintenanceLiabilityWeight),
158
158
  SPOT_MARKET_WEIGHT_PRECISION
159
159
  );
160
160
  break;
package/src/types.ts CHANGED
@@ -446,15 +446,16 @@ export type PerpMarketAccount = {
446
446
  pubkey: PublicKey;
447
447
  name: number[];
448
448
  amm: AMM;
449
- numberOfUsers: BN;
449
+ numberOfUsersWithBase: number;
450
+ numberOfUsers: number;
450
451
  marginRatioInitial: number;
451
452
  marginRatioMaintenance: number;
452
453
  nextFillRecordId: BN;
453
454
  pnlPool: PoolBalance;
454
- liquidatorFee: BN;
455
- ifLiquidationFee: BN;
456
- imfFactor: BN;
457
- unrealizedPnlImfFactor: BN;
455
+ liquidatorFee: number;
456
+ ifLiquidationFee: number;
457
+ imfFactor: number;
458
+ unrealizedPnlImfFactor: number;
458
459
  unrealizedPnlMaxImbalance: BN;
459
460
  unrealizedPnlInitialAssetWeight: number;
460
461
  unrealizedPnlMaintenanceAssetWeight: number;
@@ -512,7 +513,7 @@ export type SpotMarketAccount = {
512
513
 
513
514
  revenuePool: PoolBalance;
514
515
 
515
- ifLiquidationFee: BN;
516
+ ifLiquidationFee: number;
516
517
 
517
518
  decimals: number;
518
519
  optimalUtilization: number;
@@ -526,12 +527,12 @@ export type SpotMarketAccount = {
526
527
 
527
528
  lastInterestTs: BN;
528
529
  lastTwapTs: BN;
529
- initialAssetWeight: BN;
530
- maintenanceAssetWeight: BN;
531
- initialLiabilityWeight: BN;
532
- maintenanceLiabilityWeight: BN;
533
- liquidatorFee: BN;
534
- imfFactor: BN;
530
+ initialAssetWeight: number;
531
+ maintenanceAssetWeight: number;
532
+ initialLiabilityWeight: number;
533
+ maintenanceLiabilityWeight: number;
534
+ liquidatorFee: number;
535
+ imfFactor: number;
535
536
 
536
537
  withdrawGuardThreshold: BN;
537
538
  depositTokenTwap: BN;
@@ -587,8 +588,7 @@ export type AMM = {
587
588
  baseAssetAmountWithAmm: BN;
588
589
  baseAssetAmountLong: BN;
589
590
  baseAssetAmountShort: BN;
590
- quoteAssetAmountLong: BN;
591
- quoteAssetAmountShort: BN;
591
+ quoteAssetAmount: BN;
592
592
  terminalQuoteAssetReserve: BN;
593
593
  feePool: PoolBalance;
594
594
  totalExchangeFee: BN;
@@ -598,8 +598,8 @@ export type AMM = {
598
598
  lastOracleValid: boolean;
599
599
  lastBidPriceTwap: BN;
600
600
  lastAskPriceTwap: BN;
601
- longSpread: BN;
602
- shortSpread: BN;
601
+ longSpread: number;
602
+ shortSpread: number;
603
603
  maxSpread: number;
604
604
 
605
605
  baseAssetAmountPerLp: BN;
@@ -618,6 +618,7 @@ export type PerpPosition = {
618
618
  marketIndex: number;
619
619
  quoteAssetAmount: BN;
620
620
  quoteEntryAmount: BN;
621
+ quoteBreakEvenAmount: BN;
621
622
  openOrders: number;
622
623
  openBids: BN;
623
624
  openAsks: BN;
@@ -630,6 +631,7 @@ export type PerpPosition = {
630
631
 
631
632
  export type UserStatsAccount = {
632
633
  numberOfSubAccounts: number;
634
+ maxSubAccountId: number;
633
635
  makerVolume30D: BN;
634
636
  takerVolume30D: BN;
635
637
  fillerVolume30D: BN;
@@ -20,6 +20,7 @@ import {
20
20
  Wallet,
21
21
  OrderRecord,
22
22
  ExchangeStatus,
23
+ ZERO,
23
24
  } from '../../src';
24
25
 
25
26
  export const mockPerpPosition: PerpPosition = {
@@ -27,6 +28,7 @@ export const mockPerpPosition: PerpPosition = {
27
28
  lastCumulativeFundingRate: new BN(0),
28
29
  marketIndex: 0,
29
30
  quoteAssetAmount: new BN(0),
31
+ quoteBreakEvenAmount: new BN(0),
30
32
  quoteEntryAmount: new BN(0),
31
33
  openOrders: 0,
32
34
  openBids: new BN(0),
@@ -83,8 +85,7 @@ export const mockAMM: AMM = {
83
85
  baseAssetAmountWithAmm: new BN(0),
84
86
  baseAssetAmountLong: new BN(0),
85
87
  baseAssetAmountShort: new BN(0),
86
- quoteAssetAmountLong: new BN(0),
87
- quoteAssetAmountShort: new BN(0),
88
+ quoteAssetAmount: new BN(0),
88
89
  terminalQuoteAssetReserve: new BN(0),
89
90
  feePool: {
90
91
  scaledBalance: new BN(0),
@@ -97,8 +98,8 @@ export const mockAMM: AMM = {
97
98
  lastOracleValid: true,
98
99
  lastBidPriceTwap: new BN(0),
99
100
  lastAskPriceTwap: new BN(0),
100
- longSpread: new BN(0),
101
- shortSpread: new BN(0),
101
+ longSpread: 0,
102
+ shortSpread: 0,
102
103
  maxSpread: 0,
103
104
  ammJitIntensity: 0,
104
105
  maxBaseAssetReserve: new BN(0),
@@ -118,7 +119,8 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
118
119
  marketIndex: 0,
119
120
  pubkey: PublicKey.default,
120
121
  amm: mockAMM,
121
- numberOfUsers: new BN(0),
122
+ numberOfUsersWithBase: 0,
123
+ numberOfUsers: 0,
122
124
  marginRatioInitial: 0,
123
125
  marginRatioMaintenance: 0,
124
126
  nextFillRecordId: new BN(0),
@@ -126,11 +128,11 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
126
128
  scaledBalance: new BN(0),
127
129
  marketIndex: 0,
128
130
  },
129
- ifLiquidationFee: new BN(0),
130
- liquidatorFee: new BN(0),
131
- imfFactor: new BN(0),
132
- unrealizedPnlImfFactor: new BN(0),
133
- unrealizedPnlMaxImbalance: new BN(0),
131
+ ifLiquidationFee: 0,
132
+ liquidatorFee: 0,
133
+ imfFactor: 0,
134
+ unrealizedPnlImfFactor: 0,
135
+ unrealizedPnlMaxImbalance: ZERO,
134
136
  unrealizedPnlInitialAssetWeight: 0,
135
137
  unrealizedPnlMaintenanceAssetWeight: 0,
136
138
  insuranceClaim: {
@@ -150,7 +152,8 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
150
152
  marketIndex: 1,
151
153
  pubkey: PublicKey.default,
152
154
  amm: mockAMM,
153
- numberOfUsers: new BN(0),
155
+ numberOfUsersWithBase: 0,
156
+ numberOfUsers: 0,
154
157
  marginRatioInitial: 0,
155
158
  marginRatioMaintenance: 0,
156
159
  nextFillRecordId: new BN(0),
@@ -158,11 +161,11 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
158
161
  scaledBalance: new BN(0),
159
162
  marketIndex: 0,
160
163
  },
161
- ifLiquidationFee: new BN(0),
162
- liquidatorFee: new BN(0),
163
- imfFactor: new BN(0),
164
- unrealizedPnlImfFactor: new BN(0),
165
- unrealizedPnlMaxImbalance: new BN(0),
164
+ ifLiquidationFee: 0,
165
+ liquidatorFee: 0,
166
+ imfFactor: 0,
167
+ unrealizedPnlImfFactor: 0,
168
+ unrealizedPnlMaxImbalance: ZERO,
166
169
  unrealizedPnlInitialAssetWeight: 0,
167
170
  unrealizedPnlMaintenanceAssetWeight: 0,
168
171
  insuranceClaim: {
@@ -182,7 +185,8 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
182
185
  marketIndex: 2,
183
186
  pubkey: PublicKey.default,
184
187
  amm: mockAMM,
185
- numberOfUsers: new BN(0),
188
+ numberOfUsersWithBase: 0,
189
+ numberOfUsers: 0,
186
190
  marginRatioInitial: 0,
187
191
  marginRatioMaintenance: 0,
188
192
  nextFillRecordId: new BN(0),
@@ -190,11 +194,11 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
190
194
  scaledBalance: new BN(0),
191
195
  marketIndex: 0,
192
196
  },
193
- ifLiquidationFee: new BN(0),
194
- liquidatorFee: new BN(0),
195
- imfFactor: new BN(0),
196
- unrealizedPnlImfFactor: new BN(0),
197
- unrealizedPnlMaxImbalance: new BN(0),
197
+ ifLiquidationFee: 0,
198
+ liquidatorFee: 0,
199
+ imfFactor: 0,
200
+ unrealizedPnlImfFactor: 0,
201
+ unrealizedPnlMaxImbalance: ZERO,
198
202
  unrealizedPnlInitialAssetWeight: 0,
199
203
  unrealizedPnlMaintenanceAssetWeight: 0,
200
204
  insuranceClaim: {
@@ -231,8 +235,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
231
235
  totalFactor: 0,
232
236
  userFactor: 0,
233
237
  },
234
- ifLiquidationFee: new BN(0),
235
- liquidatorFee: new BN(0),
238
+ ifLiquidationFee: 0,
239
+ liquidatorFee: 0,
236
240
  decimals: 6,
237
241
  optimalUtilization: 0,
238
242
  optimalBorrowRate: 0,
@@ -244,11 +248,11 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
244
248
  lastInterestTs: new BN(0),
245
249
  lastTwapTs: new BN(0),
246
250
  oracle: PublicKey.default,
247
- initialAssetWeight: new BN(0),
248
- maintenanceAssetWeight: new BN(0),
249
- initialLiabilityWeight: new BN(0),
250
- maintenanceLiabilityWeight: new BN(0),
251
- imfFactor: new BN(0),
251
+ initialAssetWeight: 0,
252
+ maintenanceAssetWeight: 0,
253
+ initialLiabilityWeight: 0,
254
+ maintenanceLiabilityWeight: 0,
255
+ imfFactor: 0,
252
256
  withdrawGuardThreshold: new BN(0),
253
257
  depositTokenTwap: new BN(0),
254
258
  borrowTokenTwap: new BN(0),
@@ -301,8 +305,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
301
305
  totalFactor: 0,
302
306
  userFactor: 0,
303
307
  },
304
- ifLiquidationFee: new BN(0),
305
- liquidatorFee: new BN(0),
308
+ ifLiquidationFee: 0,
309
+ liquidatorFee: 0,
306
310
  decimals: 9,
307
311
  optimalUtilization: 0,
308
312
  optimalBorrowRate: 0,
@@ -314,11 +318,11 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
314
318
  lastInterestTs: new BN(0),
315
319
  lastTwapTs: new BN(0),
316
320
  oracle: PublicKey.default,
317
- initialAssetWeight: new BN(0),
318
- maintenanceAssetWeight: new BN(0),
319
- initialLiabilityWeight: new BN(0),
320
- maintenanceLiabilityWeight: new BN(0),
321
- imfFactor: new BN(0),
321
+ initialAssetWeight: 0,
322
+ maintenanceAssetWeight: 0,
323
+ initialLiabilityWeight: 0,
324
+ maintenanceLiabilityWeight: 0,
325
+ imfFactor: 0,
322
326
  withdrawGuardThreshold: new BN(0),
323
327
  depositTokenTwap: new BN(0),
324
328
  borrowTokenTwap: new BN(0),
@@ -371,8 +375,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
371
375
  totalFactor: 0,
372
376
  userFactor: 0,
373
377
  },
374
- ifLiquidationFee: new BN(0),
375
- liquidatorFee: new BN(0),
378
+ ifLiquidationFee: 0,
379
+ liquidatorFee: 0,
376
380
  decimals: 6,
377
381
  optimalUtilization: 0,
378
382
  optimalBorrowRate: 0,
@@ -384,11 +388,11 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
384
388
  lastInterestTs: new BN(0),
385
389
  lastTwapTs: new BN(0),
386
390
  oracle: PublicKey.default,
387
- initialAssetWeight: new BN(0),
388
- maintenanceAssetWeight: new BN(0),
389
- initialLiabilityWeight: new BN(0),
390
- maintenanceLiabilityWeight: new BN(0),
391
- imfFactor: new BN(0),
391
+ initialAssetWeight: 0,
392
+ maintenanceAssetWeight: 0,
393
+ initialLiabilityWeight: 0,
394
+ maintenanceLiabilityWeight: 0,
395
+ imfFactor: 0,
392
396
  withdrawGuardThreshold: new BN(0),
393
397
  depositTokenTwap: new BN(0),
394
398
  borrowTokenTwap: new BN(0),