@drift-labs/sdk 0.2.0-master.4 → 0.2.0-master.40

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 (234) hide show
  1. package/README.md +27 -27
  2. package/lib/accounts/bulkAccountLoader.d.ts +2 -0
  3. package/lib/accounts/bulkAccountLoader.js +36 -29
  4. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  5. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  6. package/lib/accounts/bulkUserSubscription.d.ts +2 -2
  7. package/lib/accounts/bulkUserSubscription.js +0 -1
  8. package/lib/accounts/fetch.d.ts +2 -1
  9. package/lib/accounts/fetch.js +9 -1
  10. package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
  11. package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
  12. package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
  13. package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
  14. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  15. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  16. package/lib/accounts/types.d.ts +26 -15
  17. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
  18. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
  19. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  20. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  21. package/lib/addresses/marketAddresses.d.ts +1 -3
  22. package/lib/addresses/marketAddresses.js +1 -1
  23. package/lib/addresses/pda.d.ts +15 -9
  24. package/lib/addresses/pda.js +73 -35
  25. package/lib/adminClient.d.ts +65 -0
  26. package/lib/adminClient.js +637 -0
  27. package/lib/config.d.ts +9 -9
  28. package/lib/config.js +25 -21
  29. package/lib/constants/numericConstants.d.ts +30 -12
  30. package/lib/constants/numericConstants.js +41 -21
  31. package/lib/constants/perpMarkets.d.ts +18 -0
  32. package/lib/constants/{markets.js → perpMarkets.js} +7 -7
  33. package/lib/constants/spotMarkets.d.ts +19 -0
  34. package/lib/constants/spotMarkets.js +53 -0
  35. package/lib/dlob/DLOB.d.ts +82 -0
  36. package/lib/dlob/DLOB.js +696 -0
  37. package/lib/dlob/DLOBNode.d.ts +54 -0
  38. package/lib/dlob/DLOBNode.js +77 -0
  39. package/lib/dlob/NodeList.d.ts +27 -0
  40. package/lib/dlob/NodeList.js +144 -0
  41. package/lib/driftClient.d.ts +233 -0
  42. package/lib/driftClient.js +2096 -0
  43. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
  44. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  45. package/lib/events/eventList.js +3 -0
  46. package/lib/events/eventSubscriber.d.ts +5 -2
  47. package/lib/events/eventSubscriber.js +25 -11
  48. package/lib/events/fetchLogs.d.ts +13 -2
  49. package/lib/events/fetchLogs.js +40 -12
  50. package/lib/events/pollingLogProvider.d.ts +2 -1
  51. package/lib/events/pollingLogProvider.js +7 -3
  52. package/lib/events/sort.js +8 -11
  53. package/lib/events/types.d.ts +9 -3
  54. package/lib/events/types.js +6 -0
  55. package/lib/events/webSocketLogProvider.js +1 -1
  56. package/lib/examples/makeTradeExample.js +30 -18
  57. package/lib/factory/bigNum.d.ts +8 -4
  58. package/lib/factory/bigNum.js +109 -19
  59. package/lib/idl/drift.json +8250 -0
  60. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  61. package/lib/index.d.ts +30 -13
  62. package/lib/index.js +30 -13
  63. package/lib/math/amm.d.ts +9 -6
  64. package/lib/math/amm.js +91 -38
  65. package/lib/math/conversion.js +1 -1
  66. package/lib/math/exchangeStatus.d.ts +4 -0
  67. package/lib/math/exchangeStatus.js +18 -0
  68. package/lib/math/funding.d.ts +6 -6
  69. package/lib/math/funding.js +23 -21
  70. package/lib/math/insurance.d.ts +4 -0
  71. package/lib/math/insurance.js +27 -0
  72. package/lib/math/margin.d.ts +11 -0
  73. package/lib/math/margin.js +82 -0
  74. package/lib/math/market.d.ts +14 -9
  75. package/lib/math/market.js +70 -10
  76. package/lib/math/oracles.d.ts +4 -0
  77. package/lib/math/oracles.js +36 -8
  78. package/lib/math/orders.d.ts +14 -6
  79. package/lib/math/orders.js +90 -17
  80. package/lib/math/position.d.ts +27 -13
  81. package/lib/math/position.js +92 -36
  82. package/lib/math/repeg.js +17 -8
  83. package/lib/math/spotBalance.d.ts +22 -0
  84. package/lib/math/spotBalance.js +192 -0
  85. package/lib/math/spotMarket.d.ts +4 -0
  86. package/lib/math/spotMarket.js +8 -0
  87. package/lib/math/spotPosition.d.ts +6 -0
  88. package/lib/math/spotPosition.js +23 -0
  89. package/lib/math/trade.d.ts +10 -10
  90. package/lib/math/trade.js +27 -31
  91. package/lib/oracles/pythClient.js +1 -1
  92. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  93. package/lib/oracles/switchboardClient.js +1 -1
  94. package/lib/orderParams.d.ts +4 -4
  95. package/lib/orderParams.js +12 -4
  96. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  97. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  98. package/lib/serum/serumSubscriber.d.ts +27 -0
  99. package/lib/serum/serumSubscriber.js +56 -0
  100. package/lib/serum/types.d.ts +11 -0
  101. package/lib/{clearingHouseUserConfig.js → serum/types.js} +0 -0
  102. package/lib/slot/SlotSubscriber.d.ts +7 -0
  103. package/lib/slot/SlotSubscriber.js +3 -0
  104. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
  105. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
  106. package/lib/tx/retryTxSender.d.ts +1 -1
  107. package/lib/tx/retryTxSender.js +13 -4
  108. package/lib/tx/types.d.ts +1 -1
  109. package/lib/tx/utils.js +1 -1
  110. package/lib/types.d.ts +589 -196
  111. package/lib/types.js +108 -17
  112. package/lib/user.d.ts +226 -0
  113. package/lib/user.js +949 -0
  114. package/lib/userConfig.d.ts +14 -0
  115. package/lib/userConfig.js +2 -0
  116. package/lib/userMap/userMap.d.ts +41 -0
  117. package/lib/userMap/userMap.js +85 -0
  118. package/lib/userMap/userStatsMap.d.ts +19 -0
  119. package/lib/userMap/userStatsMap.js +68 -0
  120. package/lib/userName.d.ts +1 -0
  121. package/lib/userName.js +3 -2
  122. package/lib/userStats.d.ts +18 -0
  123. package/lib/userStats.js +49 -0
  124. package/lib/userStatsConfig.d.ts +14 -0
  125. package/lib/userStatsConfig.js +2 -0
  126. package/lib/util/computeUnits.js +1 -1
  127. package/lib/util/getTokenAddress.d.ts +2 -0
  128. package/lib/util/getTokenAddress.js +9 -0
  129. package/package.json +10 -3
  130. package/src/accounts/bulkAccountLoader.ts +44 -34
  131. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  132. package/src/accounts/bulkUserSubscription.ts +2 -3
  133. package/src/accounts/fetch.ts +27 -2
  134. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
  135. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  136. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  137. package/src/accounts/types.ts +35 -15
  138. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  139. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  140. package/src/addresses/marketAddresses.ts +3 -4
  141. package/src/addresses/pda.ts +105 -33
  142. package/src/adminClient.ts +1207 -0
  143. package/src/config.ts +37 -31
  144. package/src/constants/numericConstants.ts +58 -24
  145. package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
  146. package/src/constants/spotMarkets.ts +73 -0
  147. package/src/dlob/DLOB.ts +1123 -0
  148. package/src/dlob/DLOBNode.ts +155 -0
  149. package/src/dlob/NodeList.ts +195 -0
  150. package/src/driftClient.ts +3564 -0
  151. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  152. package/src/events/eventList.ts +3 -0
  153. package/src/events/eventSubscriber.ts +36 -14
  154. package/src/events/fetchLogs.ts +55 -13
  155. package/src/events/pollingLogProvider.ts +11 -3
  156. package/src/events/sort.ts +11 -15
  157. package/src/events/types.ts +21 -2
  158. package/src/events/webSocketLogProvider.ts +1 -1
  159. package/src/examples/makeTradeExample.ts +44 -28
  160. package/src/factory/bigNum.ts +150 -22
  161. package/src/idl/drift.json +8250 -0
  162. package/src/idl/pyth.json +98 -2
  163. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  164. package/src/index.ts +30 -13
  165. package/src/math/amm.ts +161 -48
  166. package/src/math/conversion.ts +2 -2
  167. package/src/math/exchangeStatus.ts +31 -0
  168. package/src/math/funding.ts +41 -31
  169. package/src/math/insurance.ts +35 -0
  170. package/src/math/margin.ts +133 -0
  171. package/src/math/market.ts +143 -14
  172. package/src/math/oracles.ts +63 -9
  173. package/src/math/orders.ts +163 -26
  174. package/src/math/position.ts +136 -58
  175. package/src/math/repeg.ts +19 -9
  176. package/src/math/spotBalance.ts +319 -0
  177. package/src/math/spotMarket.ts +9 -0
  178. package/src/math/spotPosition.ts +47 -0
  179. package/src/math/trade.ts +33 -37
  180. package/src/oracles/pythClient.ts +2 -2
  181. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  182. package/src/oracles/switchboardClient.ts +2 -2
  183. package/src/orderParams.ts +16 -8
  184. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  185. package/src/serum/serumSubscriber.ts +99 -0
  186. package/src/serum/types.ts +13 -0
  187. package/src/slot/SlotSubscriber.ts +11 -1
  188. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
  189. package/src/tx/retryTxSender.ts +16 -5
  190. package/src/tx/types.ts +2 -1
  191. package/src/tx/utils.ts +1 -1
  192. package/src/types.ts +572 -178
  193. package/src/user.ts +1582 -0
  194. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  195. package/src/userMap/userMap.ts +124 -0
  196. package/src/userMap/userStatsMap.ts +108 -0
  197. package/src/userName.ts +2 -1
  198. package/src/userStats.ts +75 -0
  199. package/src/userStatsConfig.ts +18 -0
  200. package/src/util/computeUnits.ts +1 -1
  201. package/src/util/getTokenAddress.ts +18 -0
  202. package/tests/bn/test.ts +46 -11
  203. package/tests/dlob/helpers.ts +611 -0
  204. package/tests/dlob/test.ts +4588 -0
  205. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  206. package/lib/admin.d.ts +0 -44
  207. package/lib/admin.js +0 -433
  208. package/lib/clearingHouse.d.ts +0 -133
  209. package/lib/clearingHouse.js +0 -931
  210. package/lib/clearingHouseUser.d.ts +0 -187
  211. package/lib/clearingHouseUser.js +0 -643
  212. package/lib/clearingHouseUserConfig.d.ts +0 -14
  213. package/lib/constants/banks.d.ts +0 -16
  214. package/lib/constants/banks.js +0 -34
  215. package/lib/constants/markets.d.ts +0 -19
  216. package/lib/idl/clearing_house.json +0 -3998
  217. package/lib/math/bankBalance.d.ts +0 -9
  218. package/lib/math/bankBalance.js +0 -75
  219. package/lib/math/state.d.ts +0 -8
  220. package/lib/math/state.js +0 -15
  221. package/lib/orders.d.ts +0 -8
  222. package/lib/orders.js +0 -134
  223. package/src/admin.ts +0 -722
  224. package/src/clearingHouse.ts +0 -1451
  225. package/src/clearingHouseUser.ts +0 -989
  226. package/src/constants/banks.ts +0 -43
  227. package/src/idl/clearing_house.json +0 -3998
  228. package/src/math/bankBalance.ts +0 -112
  229. package/src/math/state.ts +0 -14
  230. package/src/math/utils.js +0 -27
  231. package/src/math/utils.js.map +0 -1
  232. package/src/orders.ts +0 -244
  233. package/src/util/computeUnits.js +0 -17
  234. package/src/util/computeUnits.js.map +0 -1
package/lib/types.js CHANGED
@@ -1,23 +1,71 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DefaultOrderParams = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
3
+ exports.DefaultOrderParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.MarketStatus = exports.ExchangeStatus = void 0;
4
4
  const _1 = require(".");
5
5
  // # Utility Types / Enums / Constants
6
+ class ExchangeStatus {
7
+ }
8
+ exports.ExchangeStatus = ExchangeStatus;
9
+ ExchangeStatus.ACTIVE = { active: {} };
10
+ ExchangeStatus.FUNDINGPAUSED = { fundingPaused: {} };
11
+ ExchangeStatus.AMMPAUSED = { ammPaused: {} };
12
+ ExchangeStatus.FILLPAUSED = { fillPaused: {} };
13
+ ExchangeStatus.LIQPAUSED = { liqPaused: {} };
14
+ ExchangeStatus.WITHDRAWPAUSED = { withdrawPaused: {} };
15
+ ExchangeStatus.PAUSED = { paused: {} };
16
+ class MarketStatus {
17
+ }
18
+ exports.MarketStatus = MarketStatus;
19
+ MarketStatus.INITIALIZED = { initialized: {} };
20
+ MarketStatus.ACTIVE = { active: {} };
21
+ MarketStatus.FUNDINGPAUSED = { fundingPaused: {} };
22
+ MarketStatus.AMMPAUSED = { ammPaused: {} };
23
+ MarketStatus.FILLPAUSED = { fillPaused: {} };
24
+ MarketStatus.WITHDRAWPAUSED = { withdrawPaused: {} };
25
+ MarketStatus.REDUCEONLY = { reduceOnly: {} };
26
+ MarketStatus.SETTLEMENT = { settlement: {} };
27
+ MarketStatus.DELISTED = { delisted: {} };
28
+ class ContractType {
29
+ }
30
+ exports.ContractType = ContractType;
31
+ ContractType.PERPETUAL = { perpetual: {} };
32
+ ContractType.FUTURE = { future: {} };
33
+ class ContractTier {
34
+ }
35
+ exports.ContractTier = ContractTier;
36
+ ContractTier.A = { a: {} };
37
+ ContractTier.B = { b: {} };
38
+ ContractTier.C = { c: {} };
39
+ ContractTier.SPECULATIVE = { speculative: {} };
40
+ ContractTier.ISOLATED = { isolated: {} };
41
+ class AssetTier {
42
+ }
43
+ exports.AssetTier = AssetTier;
44
+ AssetTier.COLLATERAL = { collateral: {} };
45
+ AssetTier.PROTECTED = { protected: {} };
46
+ AssetTier.CROSS = { cross: {} };
47
+ AssetTier.ISOLATED = { isolated: {} };
48
+ AssetTier.UNLISTED = { unlisted: {} };
6
49
  class SwapDirection {
7
50
  }
8
51
  exports.SwapDirection = SwapDirection;
9
52
  SwapDirection.ADD = { add: {} };
10
53
  SwapDirection.REMOVE = { remove: {} };
11
- class BankBalanceType {
54
+ class SpotBalanceType {
12
55
  }
13
- exports.BankBalanceType = BankBalanceType;
14
- BankBalanceType.DEPOSIT = { deposit: {} };
15
- BankBalanceType.BORROW = { borrow: {} };
56
+ exports.SpotBalanceType = SpotBalanceType;
57
+ SpotBalanceType.DEPOSIT = { deposit: {} };
58
+ SpotBalanceType.BORROW = { borrow: {} };
16
59
  class PositionDirection {
17
60
  }
18
61
  exports.PositionDirection = PositionDirection;
19
62
  PositionDirection.LONG = { long: {} };
20
63
  PositionDirection.SHORT = { short: {} };
64
+ class DepositDirection {
65
+ }
66
+ exports.DepositDirection = DepositDirection;
67
+ DepositDirection.DEPOSIT = { deposit: {} };
68
+ DepositDirection.WITHDRAW = { withdraw: {} };
21
69
  class OracleSource {
22
70
  }
23
71
  exports.OracleSource = OracleSource;
@@ -31,6 +79,11 @@ OrderType.LIMIT = { limit: {} };
31
79
  OrderType.TRIGGER_MARKET = { triggerMarket: {} };
32
80
  OrderType.TRIGGER_LIMIT = { triggerLimit: {} };
33
81
  OrderType.MARKET = { market: {} };
82
+ class MarketType {
83
+ }
84
+ exports.MarketType = MarketType;
85
+ MarketType.SPOT = { spot: {} };
86
+ MarketType.PERP = { perp: {} };
34
87
  class OrderStatus {
35
88
  }
36
89
  exports.OrderStatus = OrderStatus;
@@ -56,20 +109,32 @@ class OrderActionExplanation {
56
109
  }
57
110
  exports.OrderActionExplanation = OrderActionExplanation;
58
111
  OrderActionExplanation.NONE = { none: {} };
59
- OrderActionExplanation.BREACHED_MARGIN_REQUIREMENT = {
60
- breachedMarginRequirement: {},
61
- };
62
112
  OrderActionExplanation.ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
63
113
  oraclePriceBreachedLimitPrice: {},
64
114
  };
65
115
  OrderActionExplanation.MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
66
116
  marketOrderFilledToLimitPrice: {},
67
117
  };
118
+ OrderActionExplanation.CANCELED_FOR_LIQUIDATION = {
119
+ canceledForLiquidation: {},
120
+ };
121
+ OrderActionExplanation.MARKET_ORDER_AUCTION_EXPIRED = {
122
+ marketOrderAuctionExpired: {},
123
+ };
68
124
  class OrderTriggerCondition {
69
125
  }
70
126
  exports.OrderTriggerCondition = OrderTriggerCondition;
71
127
  OrderTriggerCondition.ABOVE = { above: {} };
72
128
  OrderTriggerCondition.BELOW = { below: {} };
129
+ class SpotFulfillmentType {
130
+ }
131
+ exports.SpotFulfillmentType = SpotFulfillmentType;
132
+ SpotFulfillmentType.SERUM_v3 = { serumV3: {} };
133
+ class SpotFulfillmentStatus {
134
+ }
135
+ exports.SpotFulfillmentStatus = SpotFulfillmentStatus;
136
+ SpotFulfillmentStatus.ENABLED = { enabled: {} };
137
+ SpotFulfillmentStatus.DISABLED = { disabled: {} };
73
138
  function isVariant(object, type) {
74
139
  return object.hasOwnProperty(type);
75
140
  }
@@ -80,30 +145,56 @@ function isOneOfVariant(object, types) {
80
145
  }, false);
81
146
  }
82
147
  exports.isOneOfVariant = isOneOfVariant;
148
+ function getVariant(object) {
149
+ return Object.keys(object)[0];
150
+ }
151
+ exports.getVariant = getVariant;
83
152
  var TradeSide;
84
153
  (function (TradeSide) {
85
154
  TradeSide[TradeSide["None"] = 0] = "None";
86
155
  TradeSide[TradeSide["Buy"] = 1] = "Buy";
87
156
  TradeSide[TradeSide["Sell"] = 2] = "Sell";
88
157
  })(TradeSide = exports.TradeSide || (exports.TradeSide = {}));
158
+ class LPAction {
159
+ }
160
+ exports.LPAction = LPAction;
161
+ LPAction.ADD_LIQUIDITY = { addLiquidity: {} };
162
+ LPAction.REMOVE_LIQUIDITY = { removeLiquidity: {} };
163
+ LPAction.SETTLE_LIQUIDITY = { settleLiquidity: {} };
164
+ class LiquidationType {
165
+ }
166
+ exports.LiquidationType = LiquidationType;
167
+ LiquidationType.LIQUIDATE_PERP = { liquidatePerp: {} };
168
+ LiquidationType.LIQUIDATE_BORROW = { liquidateBorrow: {} };
169
+ LiquidationType.LIQUIDATE_BORROW_FOR_PERP_PNL = {
170
+ liquidateBorrowForPerpPnl: {},
171
+ };
172
+ LiquidationType.LIQUIDATE_PERP_PNL_FOR_DEPOSIT = {
173
+ liquidatePerpPnlForDeposit: {},
174
+ };
175
+ LiquidationType.PERP_BANKRUPTCY = {
176
+ perpBankruptcy: {},
177
+ };
178
+ LiquidationType.BORROW_BANKRUPTCY = {
179
+ borrowBankruptcy: {},
180
+ };
89
181
  exports.DefaultOrderParams = {
90
182
  orderType: OrderType.MARKET,
183
+ marketType: MarketType.PERP,
91
184
  userOrderId: 0,
92
185
  direction: PositionDirection.LONG,
93
186
  baseAssetAmount: _1.ZERO,
94
187
  price: _1.ZERO,
95
- marketIndex: _1.ZERO,
188
+ marketIndex: 0,
96
189
  reduceOnly: false,
97
190
  postOnly: false,
98
191
  immediateOrCancel: false,
99
- triggerPrice: _1.ZERO,
192
+ triggerPrice: null,
100
193
  triggerCondition: OrderTriggerCondition.ABOVE,
101
194
  positionLimit: _1.ZERO,
102
- oraclePriceOffset: _1.ZERO,
103
- padding0: _1.ZERO,
104
- padding1: _1.ZERO,
105
- optionalAccounts: {
106
- discountToken: false,
107
- referrer: false,
108
- },
195
+ oraclePriceOffset: null,
196
+ auctionDuration: null,
197
+ maxTs: null,
198
+ auctionStartPrice: null,
199
+ auctionEndPrice: null,
109
200
  };
package/lib/user.d.ts ADDED
@@ -0,0 +1,226 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="bn.js" />
3
+ import { PublicKey } from '@solana/web3.js';
4
+ import { EventEmitter } from 'events';
5
+ import StrictEventEmitter from 'strict-event-emitter-types';
6
+ import { DriftClient } from './driftClient';
7
+ import { MarginCategory, Order, UserAccount, PerpPosition, SpotPosition } from './types';
8
+ import { UserAccountSubscriber, UserAccountEvents, DataAndSlot } from './accounts/types';
9
+ import { PositionDirection, BN, SpotMarketAccount } from '.';
10
+ import { OraclePriceData } from './oracles/types';
11
+ import { UserConfig } from './userConfig';
12
+ export declare class User {
13
+ driftClient: DriftClient;
14
+ userAccountPublicKey: PublicKey;
15
+ accountSubscriber: UserAccountSubscriber;
16
+ _isSubscribed: boolean;
17
+ eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
18
+ get isSubscribed(): boolean;
19
+ set isSubscribed(val: boolean);
20
+ constructor(config: UserConfig);
21
+ /**
22
+ * Subscribe to User state accounts
23
+ * @returns SusbcriptionSuccess result
24
+ */
25
+ subscribe(): Promise<boolean>;
26
+ /**
27
+ * Forces the accountSubscriber to fetch account updates from rpc
28
+ */
29
+ fetchAccounts(): Promise<void>;
30
+ unsubscribe(): Promise<void>;
31
+ getUserAccount(): UserAccount;
32
+ getUserAccountAndSlot(): DataAndSlot<UserAccount> | undefined;
33
+ /**
34
+ * Gets the user's current position for a given perp market. If the user has no position returns undefined
35
+ * @param marketIndex
36
+ * @returns userPerpPosition
37
+ */
38
+ getPerpPosition(marketIndex: number): PerpPosition | undefined;
39
+ /**
40
+ * Gets the user's current position for a given spot market. If the user has no position returns undefined
41
+ * @param marketIndex
42
+ * @returns userSpotPosition
43
+ */
44
+ getSpotPosition(marketIndex: number): SpotPosition | undefined;
45
+ getEmptyPosition(marketIndex: number): PerpPosition;
46
+ getClonedPosition(position: PerpPosition): PerpPosition;
47
+ /**
48
+ * @param orderId
49
+ * @returns Order
50
+ */
51
+ getOrder(orderId: number): Order | undefined;
52
+ /**
53
+ * @param userOrderId
54
+ * @returns Order
55
+ */
56
+ getOrderByUserOrderId(userOrderId: number): Order | undefined;
57
+ getUserAccountPublicKey(): PublicKey;
58
+ exists(): Promise<boolean>;
59
+ /**
60
+ * calculates the total open bids/asks in a perp market (including lps)
61
+ * @returns : open bids
62
+ * @returns : open asks
63
+ */
64
+ getPerpBidAsks(marketIndex: number): [BN, BN];
65
+ /**
66
+ * calculates the open bids and asks for an lp
67
+ * @returns : lp open bids
68
+ * @returns : lp open asks
69
+ */
70
+ getLPBidAsks(marketIndex: number): [BN, BN];
71
+ /**
72
+ * calculates the market position if the lp position was settled
73
+ * @returns : the settled userPosition
74
+ * @returns : the dust base asset amount (ie, < stepsize)
75
+ * @returns : pnl from settle
76
+ */
77
+ getSettledLPPosition(marketIndex: number): [PerpPosition, BN, BN];
78
+ /**
79
+ * calculates Buying Power = FC * MAX_LEVERAGE
80
+ * @returns : Precision QUOTE_PRECISION
81
+ */
82
+ getBuyingPower(marketIndex: number): BN;
83
+ /**
84
+ * calculates Free Collateral = Total collateral - initial margin requirement
85
+ * @returns : Precision QUOTE_PRECISION
86
+ */
87
+ getFreeCollateral(): BN;
88
+ /**
89
+ * @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
90
+ */
91
+ getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN): BN;
92
+ /**
93
+ * @returns The initial margin requirement in USDC. : QUOTE_PRECISION
94
+ */
95
+ getInitialMarginRequirement(): BN;
96
+ /**
97
+ * @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
98
+ */
99
+ getMaintenanceMarginRequirement(liquidationBuffer?: BN): BN;
100
+ /**
101
+ * calculates unrealized position price pnl
102
+ * @returns : Precision QUOTE_PRECISION
103
+ */
104
+ getUnrealizedPNL(withFunding?: boolean, marketIndex?: number, withWeightMarginCategory?: MarginCategory): BN;
105
+ /**
106
+ * calculates unrealized funding payment pnl
107
+ * @returns : Precision QUOTE_PRECISION
108
+ */
109
+ getUnrealizedFundingPNL(marketIndex?: number): BN;
110
+ getSpotMarketLiabilityValue(marketIndex?: number, marginCategory?: MarginCategory, liquidationBuffer?: BN, includeOpenOrders?: boolean): BN;
111
+ getSpotLiabilityValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory, liquidationBuffer?: BN): BN;
112
+ getSpotMarketAssetValue(marketIndex?: number, marginCategory?: MarginCategory, includeOpenOrders?: boolean): BN;
113
+ getSpotAssetValue(tokenAmount: BN, oraclePriceData: OraclePriceData, spotMarketAccount: SpotMarketAccount, marginCategory?: MarginCategory): BN;
114
+ getNetSpotMarketValue(withWeightMarginCategory?: MarginCategory): BN;
115
+ /**
116
+ * calculates TotalCollateral: collateral + unrealized pnl
117
+ * @returns : Precision QUOTE_PRECISION
118
+ */
119
+ getTotalCollateral(marginCategory?: MarginCategory): BN;
120
+ /**
121
+ * calculates sum of position value across all positions in margin system
122
+ * @returns : Precision QUOTE_PRECISION
123
+ */
124
+ getTotalPerpPositionValue(marginCategory?: MarginCategory, liquidationBuffer?: BN, includeOpenOrders?: boolean): BN;
125
+ /**
126
+ * calculates position value in margin system
127
+ * @returns : Precision QUOTE_PRECISION
128
+ */
129
+ getPerpPositionValue(marketIndex: number, oraclePriceData: OraclePriceData): BN;
130
+ getPositionSide(currentPosition: Pick<PerpPosition, 'baseAssetAmount'>): PositionDirection | undefined;
131
+ /**
132
+ * calculates average exit price (optionally for closing up to 100% of position)
133
+ * @returns : Precision PRICE_PRECISION
134
+ */
135
+ getPositionEstimatedExitPriceAndPnl(position: PerpPosition, amountToClose?: BN, useAMMClose?: boolean): [BN, BN];
136
+ /**
137
+ * calculates current user leverage across all positions
138
+ * @returns : Precision TEN_THOUSAND
139
+ */
140
+ getLeverage(): BN;
141
+ getTotalLiabilityValue(): BN;
142
+ getTotalAssetValue(): BN;
143
+ /**
144
+ * calculates max allowable leverage exceeding hitting requirement category
145
+ * @params category {Initial, Maintenance}
146
+ * @returns : Precision TEN_THOUSAND
147
+ */
148
+ getMaxLeverage(marketIndex: number, category?: MarginCategory): BN;
149
+ /**
150
+ * calculates margin ratio: total collateral / |total position value|
151
+ * @returns : Precision TEN_THOUSAND
152
+ */
153
+ getMarginRatio(): BN;
154
+ canBeLiquidated(): boolean;
155
+ /**
156
+ * Checks if any user position cumulative funding differs from respective market cumulative funding
157
+ * @returns
158
+ */
159
+ needsToSettleFundingPayment(): boolean;
160
+ /**
161
+ * Calculate the liquidation price of a position, with optional parameter to calculate the liquidation price after a trade
162
+ * @param PerpPosition
163
+ * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
164
+ * @param partial
165
+ * @returns Precision : PRICE_PRECISION
166
+ */
167
+ liquidationPrice(perpPosition: Pick<PerpPosition, 'marketIndex'>, positionBaseSizeChange?: BN): BN;
168
+ /**
169
+ * Calculates the estimated liquidation price for a position after closing a quote amount of the position.
170
+ * @param positionMarketIndex
171
+ * @param closeQuoteAmount
172
+ * @returns : Precision PRICE_PRECISION
173
+ */
174
+ liquidationPriceAfterClose(positionMarketIndex: number, closeQuoteAmount: BN): BN;
175
+ /**
176
+ * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
177
+ *
178
+ * To Calculate Max Quote Available:
179
+ *
180
+ * Case 1: SameSide
181
+ * => Remaining quote to get to maxLeverage
182
+ *
183
+ * Case 2: NOT SameSide && currentLeverage <= maxLeverage
184
+ * => Current opposite position x2 + remaining to get to maxLeverage
185
+ *
186
+ * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
187
+ * => strictly reduce current position size
188
+ *
189
+ * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
190
+ * => current position + remaining to get to maxLeverage
191
+ *
192
+ * @param targetMarketIndex
193
+ * @param tradeSide
194
+ * @returns tradeSizeAllowed : Precision QUOTE_PRECISION
195
+ */
196
+ getMaxTradeSizeUSDC(targetMarketIndex: number, tradeSide: PositionDirection): BN;
197
+ /**
198
+ * Returns the leverage ratio for the account after adding (or subtracting) the given quote size to the given position
199
+ * @param targetMarketIndex
200
+ * @param positionMarketIndex
201
+ * @param tradeQuoteAmount
202
+ * @returns leverageRatio : Precision TEN_THOUSAND
203
+ */
204
+ accountLeverageRatioAfterTrade(targetMarketIndex: number, tradeQuoteAmount: BN, tradeSide: PositionDirection, includeOpenOrders?: boolean): BN;
205
+ /**
206
+ * Calculates how much fee will be taken for a given sized trade
207
+ * @param quoteAmount
208
+ * @returns feeForQuote : Precision QUOTE_PRECISION
209
+ */
210
+ calculateFeeForQuoteAmount(quoteAmount: BN): BN;
211
+ /**
212
+ * Calculates a user's max withdrawal amounts for a spot market. If reduceOnly is true,
213
+ * it will return the max withdrawal amount without opening a liability for the user
214
+ * @param marketIndex
215
+ * @returns withdrawalLimit : Precision is the token precision for the chosen SpotMarket
216
+ */
217
+ getWithdrawalLimit(marketIndex: number, reduceOnly?: boolean): BN;
218
+ /**
219
+ * Get the total position value, excluding any position coming from the given target market
220
+ * @param marketToIgnore
221
+ * @returns positionValue : Precision QUOTE_PRECISION
222
+ */
223
+ private getTotalPerpPositionValueExcludingMarket;
224
+ private getOracleDataForPerpMarket;
225
+ private getOracleDataForSpotMarket;
226
+ }