@drift-labs/sdk 0.1.18-orders.4 → 0.1.18-orders.8

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.
@@ -91,8 +91,10 @@ class BulkAccountLoader {
91
91
  callback(e);
92
92
  }
93
93
  }
94
- this.loadPromiseResolver();
95
- this.loadPromise = undefined;
94
+ finally {
95
+ this.loadPromiseResolver();
96
+ this.loadPromise = undefined;
97
+ }
96
98
  });
97
99
  }
98
100
  loadChunk(accountsToLoad) {
@@ -54,11 +54,14 @@ class PollingUserAccountSubscriber {
54
54
  eventType: 'userPositionsData',
55
55
  });
56
56
  const userOrdersPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, userPublicKey);
57
- this.accountsToPoll.set(userOrdersPublicKey.toString(), {
58
- key: 'userOrders',
59
- publicKey: userOrdersPublicKey,
60
- eventType: 'userOrdersData',
61
- });
57
+ const userOrdersExist = (yield this.program.provider.connection.getParsedAccountInfo(userOrdersPublicKey)).value !== null;
58
+ if (userOrdersExist) {
59
+ this.accountsToPoll.set(userOrdersPublicKey.toString(), {
60
+ key: 'userOrders',
61
+ publicKey: userOrdersPublicKey,
62
+ eventType: 'userOrdersData',
63
+ });
64
+ }
62
65
  for (const [_, accountToPoll] of this.accountsToPoll) {
63
66
  accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
64
67
  const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
@@ -50,6 +50,11 @@ export declare class ClearingHouseUser {
50
50
  * @returns Order
51
51
  */
52
52
  getOrder(orderId: BN): Order | undefined;
53
+ /**
54
+ * @param userOrderId
55
+ * @returns Order
56
+ */
57
+ getOrderByUserOrderId(userOrderId: number): Order | undefined;
53
58
  getUserAccountPublicKey(): Promise<PublicKey>;
54
59
  getUserOrdersAccountPublicKey(): Promise<PublicKey>;
55
60
  exists(): Promise<boolean>;
@@ -100,6 +100,13 @@ class ClearingHouseUser {
100
100
  getOrder(orderId) {
101
101
  return this.getUserOrdersAccount().orders.find((order) => order.orderId.eq(orderId));
102
102
  }
103
+ /**
104
+ * @param userOrderId
105
+ * @returns Order
106
+ */
107
+ getOrderByUserOrderId(userOrderId) {
108
+ return this.getUserOrdersAccount().orders.find((order) => order.userOrderId === userOrderId);
109
+ }
103
110
  getUserAccountPublicKey() {
104
111
  return __awaiter(this, void 0, void 0, function* () {
105
112
  if (this.userAccountPublicKey) {
package/lib/config.js CHANGED
@@ -5,7 +5,7 @@ exports.configs = {
5
5
  devnet: {
6
6
  ENV: 'devnet',
7
7
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
8
- CLEARING_HOUSE_PROGRAM_ID: '3PfbDmWxR6e2rJ2brhSv7KJyUrHbSCu63d3FHqdLhxUJ',
8
+ CLEARING_HOUSE_PROGRAM_ID: 'AsW7LnXB9UA1uec9wi9MctYTgTz7YH9snhxd16GsFaGX',
9
9
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
10
10
  },
11
11
  'mainnet-beta': {
@@ -99,6 +99,14 @@ exports.Markets = [
99
99
  mainnetPythOracle: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
100
100
  launchTs: 1644382122000,
101
101
  },
102
+ {
103
+ symbol: 'LTC-PERP',
104
+ baseAssetSymbol: 'LTC',
105
+ marketIndex: new __1.BN(12),
106
+ devnetPythOracle: 'BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU',
107
+ mainnetPythOracle: '8RMnV1eD55iqUFJLMguPkYBkq8DCtx81XcmAja93LvRR',
108
+ launchTs: 1645027429000,
109
+ },
102
110
  // {
103
111
  // symbol: 'mSOL-PERP',
104
112
  // baseAssetSymbol: 'mSOL',
@@ -2,6 +2,7 @@
2
2
  import { BN } from '../';
3
3
  export declare const ZERO: BN;
4
4
  export declare const ONE: BN;
5
+ export declare const TWO: BN;
5
6
  export declare const TEN_THOUSAND: BN;
6
7
  export declare const BN_MAX: BN;
7
8
  export declare const MAX_LEVERAGE: BN;
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.ONE = exports.ZERO = void 0;
3
+ exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.TWO = exports.ONE = exports.ZERO = void 0;
4
4
  const __1 = require("../");
5
5
  exports.ZERO = new __1.BN(0);
6
6
  exports.ONE = new __1.BN(1);
7
+ exports.TWO = new __1.BN(2);
7
8
  exports.TEN_THOUSAND = new __1.BN(10000);
8
9
  exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
9
10
  exports.MAX_LEVERAGE = new __1.BN(5);
@@ -572,7 +572,7 @@
572
572
  },
573
573
  {
574
574
  "name": "markets",
575
- "isMut": true,
575
+ "isMut": false,
576
576
  "isSigner": false
577
577
  },
578
578
  {
@@ -628,7 +628,7 @@
628
628
  },
629
629
  {
630
630
  "name": "markets",
631
- "isMut": true,
631
+ "isMut": false,
632
632
  "isSigner": false
633
633
  },
634
634
  {
@@ -22,9 +22,11 @@ function isOrderRiskIncreasing(user, order) {
22
22
  types_1.isVariant(order.direction, 'short')) {
23
23
  return true;
24
24
  }
25
+ const baseAssetAmountToFill = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
25
26
  // if order will flip position
26
- if (position.baseAssetAmount.abs().gt(order.baseAssetAmountFilled)) {
27
+ if (baseAssetAmountToFill.gt(position.baseAssetAmount.abs().mul(numericConstants_1.TWO))) {
27
28
  return true;
28
29
  }
30
+ return false;
29
31
  }
30
32
  exports.isOrderRiskIncreasing = isOrderRiskIncreasing;
package/lib/orders.d.ts CHANGED
@@ -3,4 +3,4 @@ import { Market, Order, UserAccount, UserPosition } from './types';
3
3
  import { BN } from '.';
4
4
  export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: Market, order: Order): [UserAccount, UserPosition, Market] | null;
5
5
  export declare function calculateAmountToTradeForLimit(market: Market, order: Order): BN;
6
- export declare function calculateAmountToTradeForStopLimit(market: Market, order: Order): BN;
6
+ export declare function calculateAmountToTradeForTriggerLimit(market: Market, order: Order): BN;
package/lib/orders.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateAmountToTradeForStopLimit = exports.calculateAmountToTradeForLimit = exports.calculateNewStateAfterOrder = void 0;
3
+ exports.calculateAmountToTradeForTriggerLimit = exports.calculateAmountToTradeForLimit = exports.calculateNewStateAfterOrder = void 0;
4
4
  const types_1 = require("./types");
5
5
  const market_1 = require("./math/market");
6
6
  const numericConstants_1 = require("./constants/numericConstants");
@@ -83,15 +83,15 @@ function calculateAmountToTrade(market, order) {
83
83
  if (types_1.isVariant(order.orderType, 'limit')) {
84
84
  return calculateAmountToTradeForLimit(market, order);
85
85
  }
86
- else if (types_1.isVariant(order.orderType, 'stopLimit')) {
87
- return calculateAmountToTradeForStopLimit(market, order);
86
+ else if (types_1.isVariant(order.orderType, 'triggerLimit')) {
87
+ return calculateAmountToTradeForTriggerLimit(market, order);
88
88
  }
89
89
  else if (types_1.isVariant(order.orderType, 'market')) {
90
90
  // should never be a market order queued
91
91
  return numericConstants_1.ZERO;
92
92
  }
93
93
  else {
94
- return calculateAmountToTradeForStop(market, order);
94
+ return calculateAmountToTradeForTriggerMarket(market, order);
95
95
  }
96
96
  }
97
97
  function calculateAmountToTradeForLimit(market, order) {
@@ -106,21 +106,21 @@ function calculateAmountToTradeForLimit(market, order) {
106
106
  : maxAmountToTrade;
107
107
  }
108
108
  exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
109
- function calculateAmountToTradeForStopLimit(market, order) {
109
+ function calculateAmountToTradeForTriggerLimit(market, order) {
110
110
  if (order.baseAssetAmountFilled.eq(numericConstants_1.ZERO)) {
111
- const baseAssetAmount = calculateAmountToTradeForStop(market, order);
111
+ const baseAssetAmount = calculateAmountToTradeForTriggerMarket(market, order);
112
112
  if (baseAssetAmount.eq(numericConstants_1.ZERO)) {
113
113
  return numericConstants_1.ZERO;
114
114
  }
115
115
  }
116
116
  return calculateAmountToTradeForLimit(market, order);
117
117
  }
118
- exports.calculateAmountToTradeForStopLimit = calculateAmountToTradeForStopLimit;
118
+ exports.calculateAmountToTradeForTriggerLimit = calculateAmountToTradeForTriggerLimit;
119
119
  function isSameDirection(firstDirection, secondDirection) {
120
120
  return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
121
121
  (types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
122
122
  }
123
- function calculateAmountToTradeForStop(market, order) {
123
+ function calculateAmountToTradeForTriggerMarket(market, order) {
124
124
  return isTriggerConditionSatisfied(market, order)
125
125
  ? order.baseAssetAmount
126
126
  : numericConstants_1.ZERO;
package/lib/types.d.ts CHANGED
@@ -356,6 +356,7 @@ export declare type Order = {
356
356
  referrer: PublicKey;
357
357
  postOnly: boolean;
358
358
  immediateOrCancel: boolean;
359
+ oraclePriceOffset: BN;
359
360
  };
360
361
  export declare type OrderParams = {
361
362
  orderType: OrderType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.18-orders.4",
3
+ "version": "0.1.18-orders.8",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -117,10 +117,10 @@ export class BulkAccountLoader {
117
117
  for (const [_, callback] of this.errorCallbacks) {
118
118
  callback(e);
119
119
  }
120
+ } finally {
121
+ this.loadPromiseResolver();
122
+ this.loadPromise = undefined;
120
123
  }
121
-
122
- this.loadPromiseResolver();
123
- this.loadPromise = undefined;
124
124
  }
125
125
 
126
126
  async loadChunk(accountsToLoad: AccountToLoad[]): Promise<void> {
@@ -89,11 +89,19 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
89
89
  userPublicKey
90
90
  );
91
91
 
92
- this.accountsToPoll.set(userOrdersPublicKey.toString(), {
93
- key: 'userOrders',
94
- publicKey: userOrdersPublicKey,
95
- eventType: 'userOrdersData',
96
- });
92
+ const userOrdersExist =
93
+ (
94
+ await this.program.provider.connection.getParsedAccountInfo(
95
+ userOrdersPublicKey
96
+ )
97
+ ).value !== null;
98
+ if (userOrdersExist) {
99
+ this.accountsToPoll.set(userOrdersPublicKey.toString(), {
100
+ key: 'userOrders',
101
+ publicKey: userOrdersPublicKey,
102
+ eventType: 'userOrdersData',
103
+ });
104
+ }
97
105
 
98
106
  for (const [_, accountToPoll] of this.accountsToPoll) {
99
107
  accountToPoll.callbackId = this.accountLoader.addAccount(
@@ -155,6 +155,16 @@ export class ClearingHouseUser {
155
155
  );
156
156
  }
157
157
 
158
+ /**
159
+ * @param userOrderId
160
+ * @returns Order
161
+ */
162
+ public getOrderByUserOrderId(userOrderId: number): Order | undefined {
163
+ return this.getUserOrdersAccount().orders.find(
164
+ (order) => order.userOrderId === userOrderId
165
+ );
166
+ }
167
+
158
168
  public async getUserAccountPublicKey(): Promise<PublicKey> {
159
169
  if (this.userAccountPublicKey) {
160
170
  return this.userAccountPublicKey;
package/src/config.ts CHANGED
@@ -11,7 +11,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
11
11
  devnet: {
12
12
  ENV: 'devnet',
13
13
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
14
- CLEARING_HOUSE_PROGRAM_ID: '3PfbDmWxR6e2rJ2brhSv7KJyUrHbSCu63d3FHqdLhxUJ',
14
+ CLEARING_HOUSE_PROGRAM_ID: 'AsW7LnXB9UA1uec9wi9MctYTgTz7YH9snhxd16GsFaGX',
15
15
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
16
16
  },
17
17
  'mainnet-beta': {
@@ -106,6 +106,14 @@ export const Markets: Market[] = [
106
106
  mainnetPythOracle: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
107
107
  launchTs: 1644382122000,
108
108
  },
109
+ {
110
+ symbol: 'LTC-PERP',
111
+ baseAssetSymbol: 'LTC',
112
+ marketIndex: new BN(12),
113
+ devnetPythOracle: 'BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU',
114
+ mainnetPythOracle: '8RMnV1eD55iqUFJLMguPkYBkq8DCtx81XcmAja93LvRR',
115
+ launchTs: 1645027429000,
116
+ },
109
117
  // {
110
118
  // symbol: 'mSOL-PERP',
111
119
  // baseAssetSymbol: 'mSOL',
@@ -2,6 +2,7 @@ import { BN } from '../';
2
2
 
3
3
  export const ZERO = new BN(0);
4
4
  export const ONE = new BN(1);
5
+ export const TWO = new BN(2);
5
6
  export const TEN_THOUSAND = new BN(10000);
6
7
  export const BN_MAX = new BN(Number.MAX_SAFE_INTEGER);
7
8
 
@@ -572,7 +572,7 @@
572
572
  },
573
573
  {
574
574
  "name": "markets",
575
- "isMut": true,
575
+ "isMut": false,
576
576
  "isSigner": false
577
577
  },
578
578
  {
@@ -628,7 +628,7 @@
628
628
  },
629
629
  {
630
630
  "name": "markets",
631
- "isMut": true,
631
+ "isMut": false,
632
632
  "isSigner": false
633
633
  },
634
634
  {
@@ -1,6 +1,6 @@
1
1
  import { ClearingHouseUser } from '../clearingHouseUser';
2
2
  import { isVariant, Order } from '../types';
3
- import { ZERO } from '../constants/numericConstants';
3
+ import { ZERO, TWO } from '../constants/numericConstants';
4
4
 
5
5
  export function isOrderRiskIncreasing(
6
6
  user: ClearingHouseUser,
@@ -32,8 +32,13 @@ export function isOrderRiskIncreasing(
32
32
  return true;
33
33
  }
34
34
 
35
+ const baseAssetAmountToFill = order.baseAssetAmount.sub(
36
+ order.baseAssetAmountFilled
37
+ );
35
38
  // if order will flip position
36
- if (position.baseAssetAmount.abs().gt(order.baseAssetAmountFilled)) {
39
+ if (baseAssetAmountToFill.gt(position.baseAssetAmount.abs().mul(TWO))) {
37
40
  return true;
38
41
  }
42
+
43
+ return false;
39
44
  }
package/src/orders.ts CHANGED
@@ -160,13 +160,13 @@ function calculateAmountSwapped(
160
160
  function calculateAmountToTrade(market: Market, order: Order): BN {
161
161
  if (isVariant(order.orderType, 'limit')) {
162
162
  return calculateAmountToTradeForLimit(market, order);
163
- } else if (isVariant(order.orderType, 'stopLimit')) {
164
- return calculateAmountToTradeForStopLimit(market, order);
163
+ } else if (isVariant(order.orderType, 'triggerLimit')) {
164
+ return calculateAmountToTradeForTriggerLimit(market, order);
165
165
  } else if (isVariant(order.orderType, 'market')) {
166
166
  // should never be a market order queued
167
167
  return ZERO;
168
168
  } else {
169
- return calculateAmountToTradeForStop(market, order);
169
+ return calculateAmountToTradeForTriggerMarket(market, order);
170
170
  }
171
171
  }
172
172
 
@@ -190,12 +190,15 @@ export function calculateAmountToTradeForLimit(
190
190
  : maxAmountToTrade;
191
191
  }
192
192
 
193
- export function calculateAmountToTradeForStopLimit(
193
+ export function calculateAmountToTradeForTriggerLimit(
194
194
  market: Market,
195
195
  order: Order
196
196
  ): BN {
197
197
  if (order.baseAssetAmountFilled.eq(ZERO)) {
198
- const baseAssetAmount = calculateAmountToTradeForStop(market, order);
198
+ const baseAssetAmount = calculateAmountToTradeForTriggerMarket(
199
+ market,
200
+ order
201
+ );
199
202
  if (baseAssetAmount.eq(ZERO)) {
200
203
  return ZERO;
201
204
  }
@@ -214,7 +217,10 @@ function isSameDirection(
214
217
  );
215
218
  }
216
219
 
217
- function calculateAmountToTradeForStop(market: Market, order: Order): BN {
220
+ function calculateAmountToTradeForTriggerMarket(
221
+ market: Market,
222
+ order: Order
223
+ ): BN {
218
224
  return isTriggerConditionSatisfied(market, order)
219
225
  ? order.baseAssetAmount
220
226
  : ZERO;
package/src/types.ts CHANGED
@@ -359,6 +359,7 @@ export type Order = {
359
359
  referrer: PublicKey;
360
360
  postOnly: boolean;
361
361
  immediateOrCancel: boolean;
362
+ oraclePriceOffset: BN;
362
363
  };
363
364
 
364
365
  export type OrderParams = {