@drift-labs/sdk 0.1.18-orders.1 → 0.1.18-orders.5

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 (94) hide show
  1. package/README.md +2 -1
  2. package/lib/accounts/bulkAccountLoader.d.ts +31 -0
  3. package/lib/accounts/bulkAccountLoader.js +175 -0
  4. package/lib/accounts/bulkUserSubscription.d.ts +7 -0
  5. package/lib/accounts/bulkUserSubscription.js +28 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
  8. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
  9. package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
  10. package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
  11. package/lib/accounts/pollingUserAccountSubscriber.js +136 -0
  12. package/lib/accounts/types.d.ts +34 -25
  13. package/lib/accounts/utils.d.ts +1 -0
  14. package/lib/accounts/utils.js +7 -0
  15. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -2
  16. package/lib/accounts/webSocketAccountSubscriber.js +43 -12
  17. package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +6 -4
  18. package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +5 -4
  19. package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +3 -1
  20. package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +10 -7
  21. package/lib/addresses.d.ts +2 -2
  22. package/lib/addresses.js +4 -4
  23. package/lib/admin.d.ts +3 -0
  24. package/lib/admin.js +61 -13
  25. package/lib/clearingHouse.d.ts +20 -4
  26. package/lib/clearingHouse.js +90 -10
  27. package/lib/clearingHouseUser.d.ts +12 -4
  28. package/lib/clearingHouseUser.js +35 -25
  29. package/lib/config.js +1 -1
  30. package/lib/constants/markets.d.ts +2 -1
  31. package/lib/constants/markets.js +35 -14
  32. package/lib/constants/numericConstants.d.ts +3 -1
  33. package/lib/constants/numericConstants.js +15 -17
  34. package/lib/examples/makeTradeExample.js +1 -1
  35. package/lib/factory/clearingHouse.d.ts +25 -0
  36. package/lib/factory/clearingHouse.js +64 -0
  37. package/lib/factory/clearingHouseUser.d.ts +19 -0
  38. package/lib/factory/clearingHouseUser.js +34 -0
  39. package/lib/idl/clearing_house.json +366 -17
  40. package/lib/index.d.ts +8 -3
  41. package/lib/index.js +9 -3
  42. package/lib/math/conversion.d.ts +1 -1
  43. package/lib/math/funding.js +4 -1
  44. package/lib/math/insuranceFund.d.ts +2 -1
  45. package/lib/math/insuranceFund.js +3 -6
  46. package/lib/math/position.d.ts +2 -1
  47. package/lib/math/position.js +2 -5
  48. package/lib/math/utils.d.ts +2 -1
  49. package/lib/math/utils.js +3 -3
  50. package/lib/mockUSDCFaucet.d.ts +2 -1
  51. package/lib/orderParams.d.ts +3 -3
  52. package/lib/orderParams.js +28 -8
  53. package/lib/orders.d.ts +3 -2
  54. package/lib/orders.js +8 -8
  55. package/lib/token/index.d.ts +3 -0
  56. package/lib/token/index.js +38 -0
  57. package/lib/types.d.ts +20 -8
  58. package/lib/types.js +2 -2
  59. package/package.json +11 -3
  60. package/src/accounts/bulkAccountLoader.ts +215 -0
  61. package/src/accounts/bulkUserSubscription.ts +28 -0
  62. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
  63. package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
  64. package/src/accounts/pollingUserAccountSubscriber.ts +194 -0
  65. package/src/accounts/types.ts +40 -30
  66. package/src/accounts/utils.ts +3 -0
  67. package/src/accounts/webSocketAccountSubscriber.ts +67 -17
  68. package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +9 -6
  69. package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +10 -5
  70. package/src/addresses.ts +6 -4
  71. package/src/admin.ts +73 -20
  72. package/src/clearingHouse.ts +125 -25
  73. package/src/clearingHouseUser.ts +28 -8
  74. package/src/config.ts +1 -1
  75. package/src/constants/markets.ts +25 -1
  76. package/src/constants/numericConstants.ts +2 -1
  77. package/src/examples/makeTradeExample.ts +4 -1
  78. package/src/factory/clearingHouse.ts +125 -0
  79. package/src/factory/clearingHouseUser.ts +73 -0
  80. package/src/idl/clearing_house.json +366 -17
  81. package/src/index.ts +8 -3
  82. package/src/math/conversion.ts +1 -1
  83. package/src/math/funding.ts +5 -1
  84. package/src/math/insuranceFund.ts +1 -1
  85. package/src/math/position.ts +1 -1
  86. package/src/math/utils.ts +1 -1
  87. package/src/mockUSDCFaucet.ts +1 -1
  88. package/src/orderParams.ts +30 -7
  89. package/src/orders.ts +13 -7
  90. package/src/token/index.ts +37 -0
  91. package/src/types.ts +17 -6
  92. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
  93. package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
  94. package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
package/lib/index.d.ts CHANGED
@@ -1,15 +1,20 @@
1
1
  import { BN } from '@project-serum/anchor';
2
+ import { PublicKey } from '@solana/web3.js';
2
3
  export * from './mockUSDCFaucet';
3
4
  export * from './pythClient';
4
5
  export * from './types';
5
6
  export * from './constants/markets';
6
- export * from './accounts/defaultHistoryAccountSubscriber';
7
- export * from './accounts/defaultClearingHouseAccountSubscriber';
7
+ export * from './accounts/webSocketClearingHouseAccountSubscriber';
8
+ export * from './accounts/bulkAccountLoader';
9
+ export * from './accounts/pollingClearingHouseAccountSubscriber';
10
+ export * from './accounts/pollingTokenAccountSubscriber';
8
11
  export * from './accounts/types';
9
12
  export * from './addresses';
10
13
  export * from './admin';
11
14
  export * from './clearingHouseUser';
12
15
  export * from './clearingHouse';
16
+ export * from './factory/clearingHouse';
17
+ export * from './factory/clearingHouseUser';
13
18
  export * from './math/conversion';
14
19
  export * from './math/funding';
15
20
  export * from './math/insuranceFund';
@@ -27,4 +32,4 @@ export * from './config';
27
32
  export * from './constants/numericConstants';
28
33
  export * from './util/computeUnits';
29
34
  export * from './util/tps';
30
- export { BN };
35
+ export { BN, PublicKey };
package/lib/index.js CHANGED
@@ -10,20 +10,26 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.BN = void 0;
13
+ exports.PublicKey = exports.BN = void 0;
14
14
  const anchor_1 = require("@project-serum/anchor");
15
15
  Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
16
+ const web3_js_1 = require("@solana/web3.js");
17
+ Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
16
18
  __exportStar(require("./mockUSDCFaucet"), exports);
17
19
  __exportStar(require("./pythClient"), exports);
18
20
  __exportStar(require("./types"), exports);
19
21
  __exportStar(require("./constants/markets"), exports);
20
- __exportStar(require("./accounts/defaultHistoryAccountSubscriber"), exports);
21
- __exportStar(require("./accounts/defaultClearingHouseAccountSubscriber"), exports);
22
+ __exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
23
+ __exportStar(require("./accounts/bulkAccountLoader"), exports);
24
+ __exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), exports);
25
+ __exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
22
26
  __exportStar(require("./accounts/types"), exports);
23
27
  __exportStar(require("./addresses"), exports);
24
28
  __exportStar(require("./admin"), exports);
25
29
  __exportStar(require("./clearingHouseUser"), exports);
26
30
  __exportStar(require("./clearingHouse"), exports);
31
+ __exportStar(require("./factory/clearingHouse"), exports);
32
+ __exportStar(require("./factory/clearingHouseUser"), exports);
27
33
  __exportStar(require("./math/conversion"), exports);
28
34
  __exportStar(require("./math/funding"), exports);
29
35
  __exportStar(require("./math/insuranceFund"), exports);
@@ -1,3 +1,3 @@
1
- import BN from 'bn.js';
1
+ import { BN } from '../';
2
2
  export declare const convertToNumber: (bigNumber: BN, precision?: BN) => number;
3
3
  export declare const convertBaseAssetAmountToNumber: (baseAssetAmount: BN) => number;
@@ -242,7 +242,10 @@ exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRat
242
242
  function calculateFundingPool(market) {
243
243
  // todo
244
244
  const totalFeeLB = market.amm.totalFee.div(new anchor_1.BN(2));
245
- const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions.sub(totalFeeLB));
245
+ const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
246
+ .sub(totalFeeLB)
247
+ .mul(new anchor_1.BN(2))
248
+ .div(new anchor_1.BN(3)));
246
249
  return feePool;
247
250
  }
248
251
  exports.calculateFundingPool = calculateFundingPool;
@@ -1,5 +1,6 @@
1
+ /// <reference types="bn.js" />
1
2
  import { MarketsAccount, StateAccount } from '../types';
2
- import BN from 'bn.js';
3
+ import { BN } from '../';
3
4
  import { Connection } from '@solana/web3.js';
4
5
  /**
5
6
  * In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
@@ -8,12 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.calculateInsuranceFundSize = void 0;
16
- const bn_js_1 = __importDefault(require("bn.js"));
13
+ const __1 = require("../");
17
14
  /**
18
15
  * In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
19
16
  * Thus the de facto size of the insurance fund is the amount in the insurance vault plus the sum of each markets
@@ -27,9 +24,9 @@ const bn_js_1 = __importDefault(require("bn.js"));
27
24
  function calculateInsuranceFundSize(connection, state, marketsAccount) {
28
25
  return __awaiter(this, void 0, void 0, function* () {
29
26
  const insuranceVaultPublicKey = state.insuranceVault;
30
- const insuranceVaultAmount = new bn_js_1.default((yield connection.getTokenAccountBalance(insuranceVaultPublicKey)).value.amount);
27
+ const insuranceVaultAmount = new __1.BN((yield connection.getTokenAccountBalance(insuranceVaultPublicKey)).value.amount);
31
28
  return marketsAccount.markets.reduce((insuranceVaultAmount, market) => {
32
- return insuranceVaultAmount.add(market.amm.totalFee.div(new bn_js_1.default(2)));
29
+ return insuranceVaultAmount.add(market.amm.totalFee.div(new __1.BN(2)));
33
30
  }, insuranceVaultAmount);
34
31
  });
35
32
  }
@@ -1,4 +1,5 @@
1
- import BN from 'bn.js';
1
+ /// <reference types="bn.js" />
2
+ import { BN } from '../';
2
3
  import { Market, PositionDirection, UserPosition } from '../types';
3
4
  /**
4
5
  * calculateBaseAssetValue
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateEntryPrice = exports.calculatePositionFundingPNL = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
7
- const bn_js_1 = __importDefault(require("bn.js"));
4
+ const __1 = require("../");
8
5
  const numericConstants_1 = require("../constants/numericConstants");
9
6
  const types_1 = require("../types");
10
7
  const amm_1 = require("./amm");
@@ -84,7 +81,7 @@ function calculatePositionFundingPNL(market, marketPosition) {
84
81
  .mul(marketPosition.baseAssetAmount)
85
82
  .div(numericConstants_1.AMM_RESERVE_PRECISION)
86
83
  .div(numericConstants_1.FUNDING_PAYMENT_PRECISION)
87
- .mul(new bn_js_1.default(-1));
84
+ .mul(new __1.BN(-1));
88
85
  return perPositionFundingRate;
89
86
  }
90
87
  exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
@@ -1,2 +1,3 @@
1
1
  /// <reference types="bn.js" />
2
- export declare const squareRootBN: (n: any, closeness?: import("bn.js")) => any;
2
+ import { BN } from '../';
3
+ export declare const squareRootBN: (n: any, closeness?: BN) => any;
package/lib/math/utils.js CHANGED
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.squareRootBN = void 0;
4
- const bn_js_1 = require("bn.js");
5
- const squareRootBN = (n, closeness = new bn_js_1.BN(1)) => {
4
+ const __1 = require("../");
5
+ const squareRootBN = (n, closeness = new __1.BN(1)) => {
6
6
  // Assuming the sqrt of n as n only
7
7
  let x = n;
8
8
  // The closed guess will be stored in the root
9
9
  let root;
10
10
  // To count the number of iterations
11
11
  let count = 0;
12
- const TWO = new bn_js_1.BN(2);
12
+ const TWO = new __1.BN(2);
13
13
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
14
14
  while (count < Number.MAX_SAFE_INTEGER) {
15
15
  count++;
@@ -1,8 +1,9 @@
1
+ /// <reference types="bn.js" />
1
2
  import * as anchor from '@project-serum/anchor';
2
3
  import { Program, Provider } from '@project-serum/anchor';
3
4
  import { AccountInfo } from '@solana/spl-token';
4
5
  import { ConfirmOptions, Connection, PublicKey, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
5
- import BN from 'bn.js';
6
+ import { BN } from '.';
6
7
  import { IWallet } from './types';
7
8
  export declare class MockUSDCFaucet {
8
9
  connection: Connection;
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { OrderParams, OrderTriggerCondition, PositionDirection } from './types';
3
3
  import { BN } from '@project-serum/anchor';
4
- export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean): OrderParams;
5
- export declare function getStopOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean): OrderParams;
6
- export declare function getStopLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean): OrderParams;
4
+ export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
5
+ export declare function getTriggerMarketOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
6
+ export declare function getTriggerLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
7
7
  export declare function getMarketOrderParams(marketIndex: BN, direction: PositionDirection, quoteAssetAmount: BN, baseAssetAmount: BN, reduceOnly: boolean, price?: BN, discountToken?: boolean, referrer?: boolean): OrderParams;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMarketOrderParams = exports.getStopLimitOrderParams = exports.getStopOrderParams = exports.getLimitOrderParams = void 0;
3
+ exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
4
4
  const types_1 = require("./types");
5
5
  const numericConstants_1 = require("./constants/numericConstants");
6
- function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false) {
6
+ function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
7
7
  return {
8
8
  orderType: types_1.OrderType.LIMIT,
9
+ userOrderId,
9
10
  marketIndex,
10
11
  direction,
11
12
  quoteAssetAmount: numericConstants_1.ZERO,
@@ -14,18 +15,23 @@ function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, red
14
15
  reduceOnly,
15
16
  postOnly: false,
16
17
  immediateOrCancel: false,
18
+ positionLimit: numericConstants_1.ZERO,
19
+ padding0: true,
20
+ padding1: numericConstants_1.ZERO,
17
21
  optionalAccounts: {
18
22
  discountToken,
19
23
  referrer,
20
24
  },
21
25
  triggerCondition: types_1.OrderTriggerCondition.ABOVE,
22
26
  triggerPrice: numericConstants_1.ZERO,
27
+ oraclePriceOffset: numericConstants_1.ZERO,
23
28
  };
24
29
  }
25
30
  exports.getLimitOrderParams = getLimitOrderParams;
26
- function getStopOrderParams(marketIndex, direction, baseAssetAmount, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false) {
31
+ function getTriggerMarketOrderParams(marketIndex, direction, baseAssetAmount, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
27
32
  return {
28
- orderType: types_1.OrderType.STOP,
33
+ orderType: types_1.OrderType.TRIGGER_MARKET,
34
+ userOrderId,
29
35
  marketIndex,
30
36
  direction,
31
37
  quoteAssetAmount: numericConstants_1.ZERO,
@@ -34,18 +40,23 @@ function getStopOrderParams(marketIndex, direction, baseAssetAmount, triggerPric
34
40
  reduceOnly,
35
41
  postOnly: false,
36
42
  immediateOrCancel: false,
43
+ positionLimit: numericConstants_1.ZERO,
44
+ padding0: true,
45
+ padding1: numericConstants_1.ZERO,
37
46
  optionalAccounts: {
38
47
  discountToken,
39
48
  referrer,
40
49
  },
41
50
  triggerCondition,
42
51
  triggerPrice,
52
+ oraclePriceOffset: numericConstants_1.ZERO,
43
53
  };
44
54
  }
45
- exports.getStopOrderParams = getStopOrderParams;
46
- function getStopLimitOrderParams(marketIndex, direction, baseAssetAmount, price, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false) {
55
+ exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
56
+ function getTriggerLimitOrderParams(marketIndex, direction, baseAssetAmount, price, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
47
57
  return {
48
- orderType: types_1.OrderType.STOP_LIMIT,
58
+ orderType: types_1.OrderType.TRIGGER_LIMIT,
59
+ userOrderId,
49
60
  marketIndex,
50
61
  direction,
51
62
  quoteAssetAmount: numericConstants_1.ZERO,
@@ -54,21 +65,26 @@ function getStopLimitOrderParams(marketIndex, direction, baseAssetAmount, price,
54
65
  reduceOnly,
55
66
  postOnly: false,
56
67
  immediateOrCancel: false,
68
+ positionLimit: numericConstants_1.ZERO,
69
+ padding0: true,
70
+ padding1: numericConstants_1.ZERO,
57
71
  optionalAccounts: {
58
72
  discountToken,
59
73
  referrer,
60
74
  },
61
75
  triggerCondition,
62
76
  triggerPrice,
77
+ oraclePriceOffset: numericConstants_1.ZERO,
63
78
  };
64
79
  }
65
- exports.getStopLimitOrderParams = getStopLimitOrderParams;
80
+ exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
66
81
  function getMarketOrderParams(marketIndex, direction, quoteAssetAmount, baseAssetAmount, reduceOnly, price = numericConstants_1.ZERO, discountToken = false, referrer = false) {
67
82
  if (baseAssetAmount.eq(numericConstants_1.ZERO) && quoteAssetAmount.eq(numericConstants_1.ZERO)) {
68
83
  throw Error('baseAssetAmount or quoteAssetAmount must be zero');
69
84
  }
70
85
  return {
71
86
  orderType: types_1.OrderType.MARKET,
87
+ userOrderId: 0,
72
88
  marketIndex,
73
89
  direction,
74
90
  quoteAssetAmount,
@@ -77,12 +93,16 @@ function getMarketOrderParams(marketIndex, direction, quoteAssetAmount, baseAsse
77
93
  reduceOnly,
78
94
  postOnly: false,
79
95
  immediateOrCancel: false,
96
+ positionLimit: numericConstants_1.ZERO,
97
+ padding0: true,
98
+ padding1: numericConstants_1.ZERO,
80
99
  optionalAccounts: {
81
100
  discountToken,
82
101
  referrer,
83
102
  },
84
103
  triggerCondition: types_1.OrderTriggerCondition.ABOVE,
85
104
  triggerPrice: numericConstants_1.ZERO,
105
+ oraclePriceOffset: numericConstants_1.ZERO,
86
106
  };
87
107
  }
88
108
  exports.getMarketOrderParams = getMarketOrderParams;
package/lib/orders.d.ts CHANGED
@@ -1,5 +1,6 @@
1
+ /// <reference types="bn.js" />
1
2
  import { Market, Order, UserAccount, UserPosition } from './types';
2
- import BN from 'bn.js';
3
+ import { BN } from '.';
3
4
  export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: Market, order: Order): [UserAccount, UserPosition, Market] | null;
4
5
  export declare function calculateAmountToTradeForLimit(market: Market, order: Order): BN;
5
- 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;
@@ -0,0 +1,3 @@
1
+ /// <reference types="node" />
2
+ import { AccountInfo } from '@solana/spl-token';
3
+ export declare function parseTokenAccount(data: Buffer): AccountInfo;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTokenAccount = void 0;
4
+ const spl_token_1 = require("@solana/spl-token");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ function parseTokenAccount(data) {
7
+ const accountInfo = spl_token_1.AccountLayout.decode(data);
8
+ accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
9
+ accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
10
+ accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
11
+ if (accountInfo.delegateOption === 0) {
12
+ accountInfo.delegate = null;
13
+ // eslint-disable-next-line new-cap
14
+ accountInfo.delegatedAmount = new spl_token_1.u64(0);
15
+ }
16
+ else {
17
+ accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
18
+ accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
19
+ }
20
+ accountInfo.isInitialized = accountInfo.state !== 0;
21
+ accountInfo.isFrozen = accountInfo.state === 2;
22
+ if (accountInfo.isNativeOption === 1) {
23
+ accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
24
+ accountInfo.isNative = true;
25
+ }
26
+ else {
27
+ accountInfo.rentExemptReserve = null;
28
+ accountInfo.isNative = false;
29
+ }
30
+ if (accountInfo.closeAuthorityOption === 0) {
31
+ accountInfo.closeAuthority = null;
32
+ }
33
+ else {
34
+ accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
35
+ }
36
+ return accountInfo;
37
+ }
38
+ exports.parseTokenAccount = parseTokenAccount;
package/lib/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
+ /// <reference types="bn.js" />
1
2
  import { PublicKey, Transaction } from '@solana/web3.js';
2
- import BN from 'bn.js';
3
+ import { BN } from '.';
3
4
  export declare class SwapDirection {
4
5
  static readonly ADD: {
5
6
  add: {};
@@ -28,11 +29,11 @@ export declare class OrderType {
28
29
  static readonly LIMIT: {
29
30
  limit: {};
30
31
  };
31
- static readonly STOP: {
32
- stop: {};
32
+ static readonly TRIGGER_MARKET: {
33
+ triggerMarket: {};
33
34
  };
34
- static readonly STOP_LIMIT: {
35
- stopLimit: {};
35
+ static readonly TRIGGER_LIMIT: {
36
+ triggerLimit: {};
36
37
  };
37
38
  static readonly MARKET: {
38
39
  market: {};
@@ -97,9 +98,9 @@ export declare type DepositHistoryAccount = {
97
98
  head: BN;
98
99
  depositRecords: DepositRecord[];
99
100
  };
100
- export declare type CurveHistoryAccount = {
101
+ export declare type ExtendedCurveHistoryAccount = {
101
102
  head: BN;
102
- curveRecords: CurveRecord[];
103
+ curveRecords: ExtendedCurveRecord[];
103
104
  };
104
105
  export declare type FundingRateHistoryAccount = {
105
106
  head: BN;
@@ -131,7 +132,7 @@ export declare type DepositRecord = {
131
132
  cumulativeDepositsBefore: BN;
132
133
  amount: BN;
133
134
  };
134
- export declare type CurveRecord = {
135
+ export declare type ExtendedCurveRecord = {
135
136
  ts: BN;
136
137
  recordId: BN;
137
138
  marketIndex: BN;
@@ -147,6 +148,8 @@ export declare type CurveRecord = {
147
148
  baseAssetAmountShort: BN;
148
149
  baseAssetAmount: BN;
149
150
  openInterest: BN;
151
+ oraclePrice: BN;
152
+ tradeId: BN;
150
153
  };
151
154
  export declare type TradeRecord = {
152
155
  ts: BN;
@@ -261,6 +264,7 @@ export declare type StateAccount = {
261
264
  oracleGuardRails: OracleGuardRails;
262
265
  maxDeposit: BN;
263
266
  orderState: PublicKey;
267
+ extendedCurveHistory: PublicKey;
264
268
  };
265
269
  export declare type OrderStateAccount = {
266
270
  orderHistory: PublicKey;
@@ -303,6 +307,7 @@ export declare type AMM = {
303
307
  totalFee: BN;
304
308
  minimumQuoteAssetTradeSize: BN;
305
309
  minimumBaseAssetTradeSize: BN;
310
+ lastOraclePrice: BN;
306
311
  };
307
312
  export declare type UserPosition = {
308
313
  baseAssetAmount: BN;
@@ -334,8 +339,10 @@ export declare type Order = {
334
339
  orderType: OrderType;
335
340
  ts: BN;
336
341
  orderId: BN;
342
+ userOrderId: number;
337
343
  marketIndex: BN;
338
344
  price: BN;
345
+ userBaseAssetAmount: BN;
339
346
  baseAssetAmount: BN;
340
347
  baseAssetAmountFilled: BN;
341
348
  quoteAssetAmount: BN;
@@ -352,6 +359,7 @@ export declare type Order = {
352
359
  };
353
360
  export declare type OrderParams = {
354
361
  orderType: OrderType;
362
+ userOrderId: number;
355
363
  direction: PositionDirection;
356
364
  quoteAssetAmount: BN;
357
365
  baseAssetAmount: BN;
@@ -362,6 +370,10 @@ export declare type OrderParams = {
362
370
  immediateOrCancel: boolean;
363
371
  triggerPrice: BN;
364
372
  triggerCondition: OrderTriggerCondition;
373
+ positionLimit: BN;
374
+ oraclePriceOffset: BN;
375
+ padding0: boolean;
376
+ padding1: BN;
365
377
  optionalAccounts: {
366
378
  discountToken: boolean;
367
379
  referrer: boolean;
package/lib/types.js CHANGED
@@ -21,8 +21,8 @@ class OrderType {
21
21
  }
22
22
  exports.OrderType = OrderType;
23
23
  OrderType.LIMIT = { limit: {} };
24
- OrderType.STOP = { stop: {} };
25
- OrderType.STOP_LIMIT = { stopLimit: {} };
24
+ OrderType.TRIGGER_MARKET = { triggerMarket: {} };
25
+ OrderType.TRIGGER_LIMIT = { triggerLimit: {} };
26
26
  OrderType.MARKET = { market: {} };
27
27
  class OrderStatus {
28
28
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.18-orders.1",
3
+ "version": "0.1.18-orders.5",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -10,6 +10,7 @@
10
10
  "url": "git@github.com:drift-labs/protocol-v1.git"
11
11
  },
12
12
  "scripts": {
13
+ "lint": "eslint './**/*.{ts,tsx}' --quiet",
13
14
  "build": "yarn clean && tsc",
14
15
  "clean": "rm -rf lib",
15
16
  "patch-and-pub": "npm version patch --force && npm publish"
@@ -31,8 +32,15 @@
31
32
  "@pythnetwork/client": "^2.5.1",
32
33
  "@solana/spl-token": "^0.1.6",
33
34
  "@solana/web3.js": "^1.22.0",
34
- "@types/bn.js": "^5.1.0",
35
- "strict-event-emitter-types": "^2.0.0"
35
+ "strict-event-emitter-types": "^2.0.0",
36
+ "uuid": "^8.3.2"
37
+ },
38
+ "devDependencies": {
39
+ "@typescript-eslint/eslint-plugin": "^4.28.0",
40
+ "@typescript-eslint/parser": "^4.28.0",
41
+ "eslint": "^7.29.0",
42
+ "eslint-config-prettier": "^8.3.0",
43
+ "eslint-plugin-prettier": "^3.4.0"
36
44
  },
37
45
  "description": "SDK for Drift Protocol v1",
38
46
  "engines": {