@drift-labs/sdk 2.0.2 → 2.0.4

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.
package/README.md CHANGED
@@ -228,3 +228,4 @@ Drift Protocol v1 is licensed under [Apache 2.0](./LICENSE).
228
228
  Unless you explicitly state otherwise, any contribution intentionally submitted
229
229
  for inclusion in Drift SDK by you, as defined in the Apache-2.0 license, shall be
230
230
  licensed as above, without any additional terms or conditions.
231
+
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
3
4
  import StrictEventEmitter from 'strict-event-emitter-types';
4
5
  import { EventEmitter } from 'events';
@@ -5955,6 +5955,9 @@
5955
5955
  {
5956
5956
  "name": "RiskingIncreasingOrder"
5957
5957
  },
5958
+ {
5959
+ "name": "ReduceOnlyOrderIncreasedPosition"
5960
+ },
5958
5961
  {
5959
5962
  "name": "OrderFillWithSerum"
5960
5963
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { User } from '../user';
3
- import { PerpMarketAccount, Order } from '../types';
3
+ import { PerpMarketAccount, AMM, Order } from '../types';
4
4
  import { BN } from '@project-serum/anchor';
5
5
  import { OraclePriceData } from '../oracles/types';
6
6
  export declare function isOrderRiskIncreasing(user: User, order: Order): boolean;
@@ -12,7 +12,7 @@ export declare function getOptionalLimitPrice(order: Order, oraclePriceData: Ora
12
12
  export declare function hasLimitPrice(order: Order, slot: number): boolean;
13
13
  export declare function isFillableByVAMM(order: Order, market: PerpMarketAccount, oraclePriceData: OraclePriceData, slot: number, ts: number): boolean;
14
14
  export declare function calculateBaseAssetAmountForAmmToFulfill(order: Order, market: PerpMarketAccount, oraclePriceData: OraclePriceData, slot: number): BN;
15
- export declare function calculateBaseAssetAmountToFillUpToLimitPrice(order: Order, market: PerpMarketAccount, limitPrice: BN, oraclePriceData: OraclePriceData): BN;
15
+ export declare function calculateBaseAssetAmountToFillUpToLimitPrice(order: Order, amm: AMM, limitPrice: BN, oraclePriceData: OraclePriceData): BN;
16
16
  export declare function isOrderExpired(order: Order, ts: number): boolean;
17
17
  export declare function isMarketOrder(order: Order): boolean;
18
18
  export declare function isLimitOrder(order: Order): boolean;
@@ -135,19 +135,20 @@ function calculateBaseAssetAmountForAmmToFulfill(order, market, oraclePriceData,
135
135
  }
136
136
  const limitPrice = getOptionalLimitPrice(order, oraclePriceData, slot);
137
137
  let baseAssetAmount;
138
+ const updatedAMM = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
138
139
  if (limitPrice !== undefined) {
139
- baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(order, market, limitPrice, oraclePriceData);
140
+ baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(order, updatedAMM, limitPrice, oraclePriceData);
140
141
  }
141
142
  else {
142
143
  baseAssetAmount = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
143
144
  }
144
- const maxBaseAssetAmount = (0, amm_1.calculateMaxBaseAssetAmountFillable)(market.amm, order.direction);
145
+ const maxBaseAssetAmount = (0, amm_1.calculateMaxBaseAssetAmountFillable)(updatedAMM, order.direction);
145
146
  return anchor_1.BN.min(maxBaseAssetAmount, baseAssetAmount);
146
147
  }
147
148
  exports.calculateBaseAssetAmountForAmmToFulfill = calculateBaseAssetAmountForAmmToFulfill;
148
- function calculateBaseAssetAmountToFillUpToLimitPrice(order, market, limitPrice, oraclePriceData) {
149
- const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice, order.direction, oraclePriceData);
150
- const baseAssetAmount = standardizeBaseAssetAmount(maxAmountToTrade, market.amm.orderStepSize);
149
+ function calculateBaseAssetAmountToFillUpToLimitPrice(order, amm, limitPrice, oraclePriceData) {
150
+ const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(amm, limitPrice, order.direction, oraclePriceData);
151
+ const baseAssetAmount = standardizeBaseAssetAmount(maxAmountToTrade, amm.orderStepSize);
151
152
  // Check that directions are the same
152
153
  const sameDirection = isSameDirection(direction, order.direction);
153
154
  if (!sameDirection) {
package/lib/types.d.ts CHANGED
@@ -234,6 +234,9 @@ export declare class OrderActionExplanation {
234
234
  static readonly ORDER_FILLED_WITH_SERUM: {
235
235
  orderFillWithSerum: {};
236
236
  };
237
+ static readonly REDUCE_ONLY_ORDER_INCREASED_POSITION: {
238
+ reduceOnlyOrderIncreasedPosition: {};
239
+ };
237
240
  }
238
241
  export declare class OrderTriggerCondition {
239
242
  static readonly ABOVE: {
package/lib/types.js CHANGED
@@ -140,6 +140,9 @@ OrderActionExplanation.RISK_INCREASING_ORDER = {
140
140
  OrderActionExplanation.ORDER_FILLED_WITH_SERUM = {
141
141
  orderFillWithSerum: {},
142
142
  };
143
+ OrderActionExplanation.REDUCE_ONLY_ORDER_INCREASED_POSITION = {
144
+ reduceOnlyOrderIncreasedPosition: {},
145
+ };
143
146
  class OrderTriggerCondition {
144
147
  }
145
148
  exports.OrderTriggerCondition = OrderTriggerCondition;
package/lib/user.js CHANGED
@@ -602,7 +602,7 @@ class User {
602
602
  return totalAssetValue.mul(numericConstants_1.TEN_THOUSAND).div(totalLiabilityValue);
603
603
  }
604
604
  canBeLiquidated() {
605
- const totalCollateral = this.getTotalCollateral();
605
+ const totalCollateral = this.getTotalCollateral('Maintenance');
606
606
  // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
607
607
  let liquidationBuffer = undefined;
608
608
  const isBeingLiquidated = (0, types_1.isVariant)(this.getUserAccount().status, 'beingLiquidated');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -5955,6 +5955,9 @@
5955
5955
  {
5956
5956
  "name": "RiskingIncreasingOrder"
5957
5957
  },
5958
+ {
5959
+ "name": "ReduceOnlyOrderIncreasedPosition"
5960
+ },
5958
5961
  {
5959
5962
  "name": "OrderFillWithSerum"
5960
5963
  }
@@ -3,6 +3,7 @@ import {
3
3
  isOneOfVariant,
4
4
  isVariant,
5
5
  PerpMarketAccount,
6
+ AMM,
6
7
  Order,
7
8
  PositionDirection,
8
9
  } from '../types';
@@ -13,6 +14,7 @@ import { getAuctionPrice, isAuctionComplete } from './auction';
13
14
  import {
14
15
  calculateMaxBaseAssetAmountFillable,
15
16
  calculateMaxBaseAssetAmountToTrade,
17
+ calculateUpdatedAMM,
16
18
  } from './amm';
17
19
 
18
20
  export function isOrderRiskIncreasing(user: User, order: Order): boolean {
@@ -202,10 +204,12 @@ export function calculateBaseAssetAmountForAmmToFulfill(
202
204
 
203
205
  const limitPrice = getOptionalLimitPrice(order, oraclePriceData, slot);
204
206
  let baseAssetAmount;
207
+
208
+ const updatedAMM = calculateUpdatedAMM(market.amm, oraclePriceData);
205
209
  if (limitPrice !== undefined) {
206
210
  baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(
207
211
  order,
208
- market,
212
+ updatedAMM,
209
213
  limitPrice,
210
214
  oraclePriceData
211
215
  );
@@ -214,7 +218,7 @@ export function calculateBaseAssetAmountForAmmToFulfill(
214
218
  }
215
219
 
216
220
  const maxBaseAssetAmount = calculateMaxBaseAssetAmountFillable(
217
- market.amm,
221
+ updatedAMM,
218
222
  order.direction
219
223
  );
220
224
 
@@ -223,12 +227,12 @@ export function calculateBaseAssetAmountForAmmToFulfill(
223
227
 
224
228
  export function calculateBaseAssetAmountToFillUpToLimitPrice(
225
229
  order: Order,
226
- market: PerpMarketAccount,
230
+ amm: AMM,
227
231
  limitPrice: BN,
228
232
  oraclePriceData: OraclePriceData
229
233
  ): BN {
230
234
  const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
231
- market.amm,
235
+ amm,
232
236
  limitPrice,
233
237
  order.direction,
234
238
  oraclePriceData
@@ -236,7 +240,7 @@ export function calculateBaseAssetAmountToFillUpToLimitPrice(
236
240
 
237
241
  const baseAssetAmount = standardizeBaseAssetAmount(
238
242
  maxAmountToTrade,
239
- market.amm.orderStepSize
243
+ amm.orderStepSize
240
244
  );
241
245
 
242
246
  // Check that directions are the same
package/src/types.ts CHANGED
@@ -139,6 +139,9 @@ export class OrderActionExplanation {
139
139
  static readonly ORDER_FILLED_WITH_SERUM = {
140
140
  orderFillWithSerum: {},
141
141
  };
142
+ static readonly REDUCE_ONLY_ORDER_INCREASED_POSITION = {
143
+ reduceOnlyOrderIncreasedPosition: {},
144
+ };
142
145
  }
143
146
 
144
147
  export class OrderTriggerCondition {
package/src/user.ts CHANGED
@@ -1021,7 +1021,7 @@ export class User {
1021
1021
  }
1022
1022
 
1023
1023
  public canBeLiquidated(): boolean {
1024
- const totalCollateral = this.getTotalCollateral();
1024
+ const totalCollateral = this.getTotalCollateral('Maintenance');
1025
1025
 
1026
1026
  // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
1027
1027
  let liquidationBuffer = undefined;