@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.30

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 (208) hide show
  1. package/README.md +13 -13
  2. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  3. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  4. package/lib/accounts/bulkUserSubscription.js +0 -1
  5. package/lib/accounts/fetch.d.ts +2 -1
  6. package/lib/accounts/fetch.js +9 -1
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +16 -16
  8. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
  9. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  10. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  11. package/lib/accounts/types.d.ts +22 -11
  12. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
  13. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
  14. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  15. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  16. package/lib/addresses/marketAddresses.d.ts +1 -3
  17. package/lib/addresses/pda.d.ts +10 -4
  18. package/lib/addresses/pda.js +51 -14
  19. package/lib/admin.d.ts +34 -22
  20. package/lib/admin.js +182 -73
  21. package/lib/clearingHouse.d.ts +120 -42
  22. package/lib/clearingHouse.js +1505 -254
  23. package/lib/clearingHouseConfig.d.ts +4 -4
  24. package/lib/clearingHouseUser.d.ts +50 -38
  25. package/lib/clearingHouseUser.js +410 -190
  26. package/lib/clearingHouseUserStats.d.ts +18 -0
  27. package/lib/clearingHouseUserStats.js +49 -0
  28. package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
  29. package/lib/clearingHouseUserStatsConfig.js +2 -0
  30. package/lib/config.d.ts +7 -9
  31. package/lib/config.js +21 -21
  32. package/lib/constants/numericConstants.d.ts +18 -12
  33. package/lib/constants/numericConstants.js +28 -21
  34. package/lib/constants/perpMarkets.d.ts +18 -0
  35. package/lib/constants/{markets.js → perpMarkets.js} +7 -7
  36. package/lib/constants/spotMarkets.d.ts +19 -0
  37. package/lib/constants/spotMarkets.js +53 -0
  38. package/lib/dlob/DLOB.d.ts +73 -0
  39. package/lib/dlob/DLOB.js +553 -0
  40. package/lib/dlob/DLOBNode.d.ts +52 -0
  41. package/lib/dlob/DLOBNode.js +82 -0
  42. package/lib/dlob/NodeList.d.ts +26 -0
  43. package/lib/dlob/NodeList.js +138 -0
  44. package/lib/events/eventList.js +3 -0
  45. package/lib/events/eventSubscriber.d.ts +4 -2
  46. package/lib/events/eventSubscriber.js +16 -9
  47. package/lib/events/fetchLogs.d.ts +10 -1
  48. package/lib/events/fetchLogs.js +27 -7
  49. package/lib/events/pollingLogProvider.d.ts +2 -1
  50. package/lib/events/pollingLogProvider.js +6 -2
  51. package/lib/events/sort.js +8 -11
  52. package/lib/events/types.d.ts +7 -2
  53. package/lib/events/types.js +5 -0
  54. package/lib/examples/makeTradeExample.js +20 -8
  55. package/lib/factory/bigNum.d.ts +1 -0
  56. package/lib/factory/bigNum.js +34 -10
  57. package/lib/idl/clearing_house.json +4313 -1433
  58. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  59. package/lib/index.d.ts +21 -6
  60. package/lib/index.js +25 -6
  61. package/lib/math/amm.d.ts +8 -5
  62. package/lib/math/amm.js +68 -46
  63. package/lib/math/auction.js +4 -1
  64. package/lib/math/conversion.js +1 -1
  65. package/lib/math/funding.d.ts +6 -6
  66. package/lib/math/funding.js +11 -10
  67. package/lib/math/insurance.d.ts +4 -0
  68. package/lib/math/insurance.js +27 -0
  69. package/lib/math/margin.d.ts +11 -0
  70. package/lib/math/margin.js +77 -0
  71. package/lib/math/market.d.ts +14 -9
  72. package/lib/math/market.js +69 -7
  73. package/lib/math/oracles.d.ts +4 -0
  74. package/lib/math/oracles.js +36 -8
  75. package/lib/math/orders.d.ts +6 -2
  76. package/lib/math/orders.js +78 -3
  77. package/lib/math/position.d.ts +21 -13
  78. package/lib/math/position.js +76 -36
  79. package/lib/math/repeg.js +14 -5
  80. package/lib/math/spotBalance.d.ts +22 -0
  81. package/lib/math/spotBalance.js +193 -0
  82. package/lib/math/spotMarket.d.ts +4 -0
  83. package/lib/math/spotMarket.js +8 -0
  84. package/lib/math/spotPosition.d.ts +6 -0
  85. package/lib/math/spotPosition.js +23 -0
  86. package/lib/math/trade.d.ts +10 -10
  87. package/lib/math/trade.js +22 -29
  88. package/lib/oracles/pythClient.js +1 -1
  89. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  90. package/lib/oracles/switchboardClient.js +1 -1
  91. package/lib/orderParams.d.ts +14 -5
  92. package/lib/orderParams.js +12 -92
  93. package/lib/serum/serumSubscriber.d.ts +23 -0
  94. package/lib/serum/serumSubscriber.js +41 -0
  95. package/lib/serum/types.d.ts +11 -0
  96. package/lib/serum/types.js +2 -0
  97. package/lib/slot/SlotSubscriber.d.ts +7 -0
  98. package/lib/slot/SlotSubscriber.js +3 -0
  99. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
  100. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
  101. package/lib/tx/retryTxSender.d.ts +1 -1
  102. package/lib/tx/retryTxSender.js +13 -4
  103. package/lib/tx/types.d.ts +1 -1
  104. package/lib/tx/utils.js +1 -1
  105. package/lib/types.d.ts +474 -123
  106. package/lib/types.js +99 -5
  107. package/lib/userMap/userMap.d.ts +25 -0
  108. package/lib/userMap/userMap.js +73 -0
  109. package/lib/userMap/userStatsMap.d.ts +19 -0
  110. package/lib/userMap/userStatsMap.js +68 -0
  111. package/lib/util/computeUnits.js +1 -1
  112. package/lib/util/getTokenAddress.d.ts +2 -0
  113. package/lib/util/getTokenAddress.js +9 -0
  114. package/package.json +9 -5
  115. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  116. package/src/accounts/bulkUserSubscription.ts +0 -1
  117. package/src/accounts/fetch.ts +27 -2
  118. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
  119. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  120. package/src/accounts/types.ts +31 -11
  121. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
  122. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  123. package/src/addresses/marketAddresses.ts +1 -2
  124. package/src/addresses/pda.ts +88 -14
  125. package/src/admin.ts +333 -128
  126. package/src/assert/assert.js +9 -0
  127. package/src/clearingHouse.ts +2464 -458
  128. package/src/clearingHouseConfig.ts +4 -3
  129. package/src/clearingHouseUser.ts +747 -291
  130. package/src/clearingHouseUserStats.ts +75 -0
  131. package/src/clearingHouseUserStatsConfig.ts +18 -0
  132. package/src/config.ts +30 -31
  133. package/src/constants/numericConstants.ts +41 -25
  134. package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
  135. package/src/constants/spotMarkets.ts +72 -0
  136. package/src/dlob/DLOB.ts +868 -0
  137. package/src/dlob/DLOBNode.ts +162 -0
  138. package/src/dlob/NodeList.ts +185 -0
  139. package/src/events/eventList.js +77 -0
  140. package/src/events/eventList.ts +3 -0
  141. package/src/events/eventSubscriber.ts +20 -12
  142. package/src/events/fetchLogs.ts +35 -8
  143. package/src/events/pollingLogProvider.ts +10 -2
  144. package/src/events/sort.ts +11 -15
  145. package/src/events/types.ts +16 -1
  146. package/src/examples/makeTradeExample.js +157 -0
  147. package/src/examples/makeTradeExample.ts +32 -14
  148. package/src/factory/bigNum.ts +42 -13
  149. package/src/idl/clearing_house.json +4313 -1433
  150. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  151. package/src/index.ts +21 -6
  152. package/src/math/amm.ts +136 -66
  153. package/src/math/auction.ts +5 -1
  154. package/src/math/conversion.ts +2 -2
  155. package/src/math/funding.ts +20 -18
  156. package/src/math/insurance.ts +35 -0
  157. package/src/math/margin.ts +127 -0
  158. package/src/math/market.ts +138 -12
  159. package/src/math/oracles.ts +63 -9
  160. package/src/math/orders.ts +138 -4
  161. package/src/math/position.ts +119 -58
  162. package/src/math/repeg.ts +16 -6
  163. package/src/math/spotBalance.ts +316 -0
  164. package/src/math/spotMarket.ts +9 -0
  165. package/src/math/spotPosition.ts +47 -0
  166. package/src/math/trade.ts +43 -49
  167. package/src/oracles/pythClient.ts +2 -2
  168. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  169. package/src/oracles/switchboardClient.ts +2 -2
  170. package/src/orderParams.ts +24 -137
  171. package/src/serum/serumSubscriber.ts +80 -0
  172. package/src/serum/types.ts +13 -0
  173. package/src/slot/SlotSubscriber.ts +11 -1
  174. package/src/token/index.js +38 -0
  175. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
  176. package/src/tx/retryTxSender.ts +16 -5
  177. package/src/tx/types.js +2 -0
  178. package/src/tx/types.ts +2 -1
  179. package/src/tx/utils.js +17 -0
  180. package/src/tx/utils.ts +1 -1
  181. package/src/types.ts +477 -125
  182. package/src/userMap/userMap.ts +100 -0
  183. package/src/userMap/userStatsMap.ts +110 -0
  184. package/src/util/computeUnits.js +21 -11
  185. package/src/util/computeUnits.ts +1 -1
  186. package/src/util/getTokenAddress.js +9 -0
  187. package/src/util/getTokenAddress.ts +18 -0
  188. package/src/util/promiseTimeout.js +14 -0
  189. package/src/util/tps.js +27 -0
  190. package/tests/bn/test.ts +12 -3
  191. package/tests/dlob/helpers.ts +374 -0
  192. package/tests/dlob/test.ts +2865 -0
  193. package/lib/constants/banks.d.ts +0 -16
  194. package/lib/constants/banks.js +0 -34
  195. package/lib/constants/markets.d.ts +0 -19
  196. package/lib/math/bankBalance.d.ts +0 -9
  197. package/lib/math/bankBalance.js +0 -75
  198. package/lib/math/state.d.ts +0 -8
  199. package/lib/math/state.js +0 -15
  200. package/lib/orders.d.ts +0 -8
  201. package/lib/orders.js +0 -134
  202. package/src/constants/banks.ts +0 -43
  203. package/src/math/bankBalance.ts +0 -112
  204. package/src/math/state.ts +0 -14
  205. package/src/math/utils.js +0 -27
  206. package/src/math/utils.js.map +0 -1
  207. package/src/orders.ts +0 -244
  208. package/src/util/computeUnits.js.map +0 -1
package/src/types.ts CHANGED
@@ -1,13 +1,26 @@
1
1
  import { PublicKey, Transaction } from '@solana/web3.js';
2
- import { BN } from '.';
2
+ import { BN, ZERO } from '.';
3
3
 
4
4
  // # Utility Types / Enums / Constants
5
+
6
+ export class MarketStatus {
7
+ static readonly INITIALIZED = { initialized: {} };
8
+ static readonly REDUCEONLY = { reduceonly: {} };
9
+ static readonly SETTLEMENT = { settlement: {} };
10
+ static readonly DELISTED = { delisted: {} };
11
+ }
12
+
13
+ export class ContractType {
14
+ static readonly PERPETUAL = { perpetual: {} };
15
+ static readonly FUTURE = { future: {} };
16
+ }
17
+
5
18
  export class SwapDirection {
6
19
  static readonly ADD = { add: {} };
7
20
  static readonly REMOVE = { remove: {} };
8
21
  }
9
22
 
10
- export class BankBalanceType {
23
+ export class SpotBalanceType {
11
24
  static readonly DEPOSIT = { deposit: {} };
12
25
  static readonly BORROW = { borrow: {} };
13
26
  }
@@ -17,6 +30,11 @@ export class PositionDirection {
17
30
  static readonly SHORT = { short: {} };
18
31
  }
19
32
 
33
+ export class DepositDirection {
34
+ static readonly DEPOSIT = { deposit: {} };
35
+ static readonly WITHDRAW = { withdraw: {} };
36
+ }
37
+
20
38
  export class OracleSource {
21
39
  static readonly PYTH = { pyth: {} };
22
40
  static readonly SWITCHBOARD = { switchboard: {} };
@@ -30,6 +48,12 @@ export class OrderType {
30
48
  static readonly MARKET = { market: {} };
31
49
  }
32
50
 
51
+ export declare type MarketTypeStr = 'perp' | 'spot';
52
+ export class MarketType {
53
+ static readonly SPOT = { spot: {} };
54
+ static readonly PERP = { perp: {} };
55
+ }
56
+
33
57
  export class OrderStatus {
34
58
  static readonly INIT = { init: {} };
35
59
  static readonly OPEN = { open: {} };
@@ -51,11 +75,36 @@ export class OrderAction {
51
75
  static readonly TRIGGER = { trigger: {} };
52
76
  }
53
77
 
78
+ export class OrderActionExplanation {
79
+ static readonly NONE = { none: {} };
80
+ static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
81
+ oraclePriceBreachedLimitPrice: {},
82
+ };
83
+ static readonly MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
84
+ marketOrderFilledToLimitPrice: {},
85
+ };
86
+ static readonly CANCELED_FOR_LIQUIDATION = {
87
+ canceledForLiquidation: {},
88
+ };
89
+ static readonly MARKET_ORDER_AUCTION_EXPIRED = {
90
+ marketOrderAuctionExpired: {},
91
+ };
92
+ }
93
+
54
94
  export class OrderTriggerCondition {
55
95
  static readonly ABOVE = { above: {} };
56
96
  static readonly BELOW = { below: {} };
57
97
  }
58
98
 
99
+ export class SpotFulfillmentType {
100
+ static readonly SERUM_v3 = { serumV3: {} };
101
+ }
102
+
103
+ export class SpotFulfillmentStatus {
104
+ static readonly ENABLED = { enabled: {} };
105
+ static readonly DISABLED = { disabled: {} };
106
+ }
107
+
59
108
  export function isVariant(object: unknown, type: string) {
60
109
  return object.hasOwnProperty(type);
61
110
  }
@@ -66,6 +115,10 @@ export function isOneOfVariant(object: unknown, types: string[]) {
66
115
  }, false);
67
116
  }
68
117
 
118
+ export function getVariant(object: unknown): string {
119
+ return Object.keys(object)[0];
120
+ }
121
+
69
122
  export enum TradeSide {
70
123
  None = 0,
71
124
  Buy = 1,
@@ -82,6 +135,15 @@ export type CandleResolution =
82
135
  | 'W'
83
136
  | 'M';
84
137
 
138
+ export type NewUserRecord = {
139
+ ts: BN;
140
+ userAuthority: PublicKey;
141
+ user: PublicKey;
142
+ userId: number;
143
+ name: number[];
144
+ referrer: PublicKey;
145
+ };
146
+
85
147
  export type DepositRecord = {
86
148
  ts: BN;
87
149
  userAuthority: PublicKey;
@@ -90,8 +152,10 @@ export type DepositRecord = {
90
152
  deposit?: any;
91
153
  withdraw?: any;
92
154
  };
93
- bankIndex: BN;
155
+ marketIndex: number;
94
156
  amount: BN;
157
+ oraclePrice: BN;
158
+ referrer: PublicKey;
95
159
  from?: PublicKey;
96
160
  to?: PublicKey;
97
161
  };
@@ -99,7 +163,7 @@ export type DepositRecord = {
99
163
  export type CurveRecord = {
100
164
  ts: BN;
101
165
  recordId: BN;
102
- marketIndex: BN;
166
+ marketIndex: number;
103
167
  pegMultiplierBefore: BN;
104
168
  baseAssetReserveBefore: BN;
105
169
  quoteAssetReserveBefore: BN;
@@ -116,22 +180,57 @@ export type CurveRecord = {
116
180
  tradeId: BN;
117
181
  };
118
182
 
183
+ export declare type InsuranceFundRecord = {
184
+ ts: BN;
185
+ bankIndex: BN;
186
+ marketIndex: number;
187
+ userIfFactor: BN;
188
+ totalIfFactor: BN;
189
+ vaultAmountBefore: BN;
190
+ insuranceVaultAmountBefore: BN;
191
+ amount: BN;
192
+ totalIfSharesBefore: BN;
193
+ totalIfSharesAfter: BN;
194
+ };
195
+
196
+ export type LPRecord = {
197
+ ts: BN;
198
+ user: PublicKey;
199
+ action: LPAction;
200
+ nShares: BN;
201
+ marketIndex: number;
202
+ deltaBaseAssetAmount: BN;
203
+ deltaQuoteAssetAmount: BN;
204
+ pnl: BN;
205
+ };
206
+
207
+ export class LPAction {
208
+ static readonly ADD_LIQUIDITY = { addLiquidity: {} };
209
+ static readonly REMOVE_LIQUIDITY = { removeLiquidity: {} };
210
+ static readonly SETTLE_LIQUIDITY = { settleLiquidity: {} };
211
+ }
212
+
119
213
  export type FundingRateRecord = {
120
214
  ts: BN;
121
215
  recordId: BN;
122
- marketIndex: BN;
216
+ marketIndex: number;
123
217
  fundingRate: BN;
218
+ fundingRateLong: BN;
219
+ fundingRateShort: BN;
124
220
  cumulativeFundingRateLong: BN;
125
221
  cumulativeFundingRateShort: BN;
126
222
  oraclePriceTwap: BN;
127
223
  markPriceTwap: BN;
224
+ periodRevenue: BN;
225
+ netBaseAssetAmount: BN;
226
+ netUnsettledLpBaseAssetAmount: BN;
128
227
  };
129
228
 
130
229
  export type FundingPaymentRecord = {
131
230
  ts: BN;
132
231
  userAuthority: PublicKey;
133
232
  user: PublicKey;
134
- marketIndex: BN;
233
+ marketIndex: number;
135
234
  fundingPayment: BN;
136
235
  baseAssetAmount: BN;
137
236
  userLastCumulativeFunding: BN;
@@ -142,38 +241,138 @@ export type FundingPaymentRecord = {
142
241
 
143
242
  export type LiquidationRecord = {
144
243
  ts: BN;
145
- recordId: BN;
146
- userAuthority: PublicKey;
147
244
  user: PublicKey;
148
- partial: boolean;
149
- baseAssetValue: BN;
150
- baseAssetValueClosed: BN;
151
- liquidationFee: BN;
152
- feeToLiquidator: BN;
153
- feeToInsuranceFund: BN;
154
245
  liquidator: PublicKey;
246
+ liquidationType: LiquidationType;
247
+ marginRequirement: BN;
155
248
  totalCollateral: BN;
156
- collateral: BN;
157
- unrealizedPnl: BN;
158
- marginRatio: BN;
249
+ liquidationId: number;
250
+ canceledOrderIds: BN[];
251
+ liquidatePerp: LiquidatePerpRecord;
252
+ liquidateBorrow: LiquidateBorrowRecord;
253
+ liquidateBorrowForPerpPnl: LiquidateBorrowForPerpPnlRecord;
254
+ liquidatePerpPnlForDeposit: LiquidatePerpPnlForDepositRecord;
255
+ perpBankruptcy: PerpBankruptcyRecord;
256
+ borrowBankruptcy: BorrowBankruptcyRecord;
257
+ };
258
+
259
+ export class LiquidationType {
260
+ static readonly LIQUIDATE_PERP = { liquidatePerp: {} };
261
+ static readonly LIQUIDATE_BORROW = { liquidateBorrow: {} };
262
+ static readonly LIQUIDATE_BORROW_FOR_PERP_PNL = {
263
+ liquidateBorrowForPerpPnl: {},
264
+ };
265
+ static readonly LIQUIDATE_PERP_PNL_FOR_DEPOSIT = {
266
+ liquidatePerpPnlForDeposit: {},
267
+ };
268
+ static readonly PERP_BANKRUPTCY = {
269
+ perpBankruptcy: {},
270
+ };
271
+ static readonly BORROW_BANKRUPTCY = {
272
+ borrowBankruptcy: {},
273
+ };
274
+ }
275
+
276
+ export type LiquidatePerpRecord = {
277
+ marketIndex: number;
278
+ oraclePrice: BN;
279
+ baseAssetAmount: BN;
280
+ quoteAssetAmount: BN;
281
+ lpShares: BN;
282
+ userPnl: BN;
283
+ liquidatorPnl: BN;
284
+ userOrderId: BN;
285
+ liquidatorOrderId: BN;
286
+ fillRecordId: BN;
287
+ ifFee: BN;
288
+ };
289
+
290
+ export type LiquidateBorrowRecord = {
291
+ assetMarketIndex: number;
292
+ assetPrice: BN;
293
+ assetTransfer: BN;
294
+ liabilityMarketIndex: number;
295
+ liabilityPrice: BN;
296
+ liabilityTransfer: BN;
297
+ ifFee: BN;
298
+ };
299
+
300
+ export type LiquidateBorrowForPerpPnlRecord = {
301
+ perpMarketIndex: number;
302
+ marketOraclePrice: BN;
303
+ pnlTransfer: BN;
304
+ liabilityMarketIndex: number;
305
+ liabilityPrice: BN;
306
+ liabilityTransfer: BN;
307
+ };
308
+
309
+ export type LiquidatePerpPnlForDepositRecord = {
310
+ perpMarketIndex: number;
311
+ marketOraclePrice: BN;
312
+ pnlTransfer: BN;
313
+ assetMarketIndex: number;
314
+ assetPrice: BN;
315
+ assetTransfer: BN;
316
+ };
317
+
318
+ export type PerpBankruptcyRecord = {
319
+ marketIndex: number;
320
+ pnl: BN;
321
+ cumulativeFundingRateDelta: BN;
322
+ };
323
+
324
+ export type BorrowBankruptcyRecord = {
325
+ marketIndex: number;
326
+ borrowAmount: BN;
327
+ cumulativeDepositInterestDelta: BN;
328
+ };
329
+
330
+ export type SettlePnlRecord = {
331
+ ts: BN;
332
+ user: PublicKey;
333
+ marketIndex: number;
334
+ pnl: BN;
335
+ baseAssetAmount: BN;
336
+ quoteAssetAmountAfter: BN;
337
+ quoteEntryAmount: BN;
338
+ settlePrice: BN;
159
339
  };
160
340
 
161
341
  export type OrderRecord = {
162
342
  ts: BN;
163
- taker: PublicKey;
164
- maker: PublicKey;
165
- takerOrder: Order;
166
- makerOrder: Order;
343
+ user: PublicKey;
344
+ order: Order;
345
+ };
346
+
347
+ export type OrderActionRecord = {
348
+ ts: BN;
167
349
  action: OrderAction;
168
- filler: PublicKey;
169
- fillRecordId: BN;
170
- marketIndex: BN;
171
- baseAssetAmountFilled: BN;
172
- quoteAssetAmountFilled: BN;
173
- makerRebate: BN;
174
- takerFee: BN;
175
- fillerReward: BN;
176
- quoteAssetAmountSurplus: BN;
350
+ actionExplanation: OrderActionExplanation;
351
+ marketIndex: number;
352
+ marketType: MarketType;
353
+ filler: PublicKey | null;
354
+ fillerReward: BN | null;
355
+ fillRecordId: BN | null;
356
+ baseAssetAmountFilled: BN | null;
357
+ quoteAssetAmountFilled: BN | null;
358
+ takerFee: BN | null;
359
+ makerFee: BN | null;
360
+ referrerReward: number | null;
361
+ quoteAssetAmountSurplus: BN | null;
362
+ taker: PublicKey | null;
363
+ takerOrderId: BN | null;
364
+ takerOrderDirection: PositionDirection | null;
365
+ takerOrderBaseAssetAmount: BN | null;
366
+ takerOrderCumulativeBaseAssetAmountFilled: BN | null;
367
+ takerOrderCumulativeQuoteAssetAmountFilled: BN | null;
368
+ takerOrderFee: BN | null;
369
+ maker: PublicKey | null;
370
+ makerOrderId: BN | null;
371
+ makerOrderDirection: PositionDirection | null;
372
+ makerOrderBaseAssetAmount: BN | null;
373
+ makerOrderCumulativeBaseAssetAmountFilled: BN | null;
374
+ makerOrderCumulativeQuoteAssetAmountFilled: BN | null;
375
+ makerOrderFee: BN | null;
177
376
  oraclePrice: BN;
178
377
  };
179
378
 
@@ -182,35 +381,32 @@ export type StateAccount = {
182
381
  fundingPaused: boolean;
183
382
  exchangePaused: boolean;
184
383
  adminControlsPrices: boolean;
185
- insuranceVault: PublicKey;
186
- insuranceVaultAuthority: PublicKey;
187
- insuranceVaultNonce: number;
188
- marginRatioInitial: BN;
189
- marginRatioMaintenance: BN;
190
- marginRatioPartial: BN;
191
- partialLiquidationClosePercentageNumerator: BN;
192
- partialLiquidationClosePercentageDenominator: BN;
193
- partialLiquidationPenaltyPercentageNumerator: BN;
194
- partialLiquidationPenaltyPercentageDenominator: BN;
195
- fullLiquidationPenaltyPercentageNumerator: BN;
196
- fullLiquidationPenaltyPercentageDenominator: BN;
197
- partialLiquidationLiquidatorShareDenominator: BN;
198
- fullLiquidationLiquidatorShareDenominator: BN;
199
- feeStructure: FeeStructure;
200
384
  totalFee: BN;
201
385
  totalFeeWithdrawn: BN;
202
386
  whitelistMint: PublicKey;
203
387
  discountMint: PublicKey;
204
388
  oracleGuardRails: OracleGuardRails;
205
389
  maxDeposit: BN;
206
- numberOfMarkets: BN;
207
- numberOfBanks: BN;
390
+ numberOfMarkets: number;
391
+ numberOfSpotMarkets: number;
208
392
  minOrderQuoteAssetAmount: BN;
393
+ signer: PublicKey;
394
+ signerNonce: number;
395
+ defaultMarketOrderTimeInForce: number;
396
+ minPerpAuctionDuration: number;
397
+ defaultSpotAuctionDuration: number;
398
+ liquidationMarginBufferRatio: number;
399
+ srmVault: PublicKey;
400
+ perpFeeStructure: FeeStructure;
401
+ spotFeeStructure: FeeStructure;
209
402
  };
210
403
 
211
- export type MarketAccount = {
212
- initialized: boolean;
213
- marketIndex: BN;
404
+ export type PerpMarketAccount = {
405
+ status: MarketStatus;
406
+ contractType: ContractType;
407
+ expiryTs: BN;
408
+ settlementPrice: BN;
409
+ marketIndex: number;
214
410
  pubkey: PublicKey;
215
411
  amm: AMM;
216
412
  baseAssetAmount: BN;
@@ -219,18 +415,61 @@ export type MarketAccount = {
219
415
  openInterest: BN;
220
416
  marginRatioInitial: number;
221
417
  marginRatioMaintenance: number;
222
- marginRatioPartial: number;
223
418
  nextFillRecordId: BN;
224
419
  pnlPool: PoolBalance;
420
+ liquidatorFee: BN;
421
+ ifLiquidationFee: BN;
422
+ imfFactor: BN;
423
+ unrealizedImfFactor: BN;
424
+ unrealizedMaxImbalance: BN;
425
+ unrealizedInitialAssetWeight: number;
426
+ unrealizedMaintenanceAssetWeight: number;
427
+ revenueWithdrawSinceLastSettle: BN;
428
+ maxRevenueWithdrawPerPeriod: BN;
429
+ lastRevenueWithdrawTs: BN;
430
+ quoteSettledInsurance: BN;
431
+ quoteMaxInsurance: BN;
225
432
  };
226
433
 
227
- export type BankAccount = {
228
- bankIndex: BN;
434
+ export type HistoricalOracleData = {
435
+ lastOraclePrice: BN;
436
+ lastOracleDelay: BN;
437
+ lastOracleConf: BN;
438
+ lastOraclePriceTwap: BN;
439
+ lastOraclePriceTwap5min: BN;
440
+ lastOraclePriceTwapTs: BN;
441
+ };
442
+
443
+ export type HistoricalIndexData = {
444
+ lastIndexBidPrice: BN;
445
+ lastIndexAskPrice: BN;
446
+ lastIndexPriceTwap: BN;
447
+ lastIndexPriceTwap5Min: BN;
448
+ lastIndexPriceTwapTs: BN;
449
+ };
450
+
451
+ export type SpotMarketAccount = {
452
+ marketIndex: number;
229
453
  pubkey: PublicKey;
230
454
  mint: PublicKey;
231
455
  vault: PublicKey;
232
- vaultAuthority: PublicKey;
233
- vaultAuthorityNonce: number;
456
+
457
+ oracle: PublicKey;
458
+ oracleSource: OracleSource;
459
+ historicalOracleData: HistoricalOracleData;
460
+ historicalIndexData: HistoricalIndexData;
461
+
462
+ insuranceFundVault: PublicKey;
463
+ insuranceWithdrawEscrowPeriod: BN;
464
+ revenuePool: PoolBalance;
465
+
466
+ totalIfShares: BN;
467
+ userIfShares: BN;
468
+
469
+ userIfFactor: BN;
470
+ totalIfFactor: BN;
471
+ ifLiquidationFee: BN;
472
+
234
473
  decimals: number;
235
474
  optimalUtilization: BN;
236
475
  optimalBorrowRate: BN;
@@ -239,12 +478,26 @@ export type BankAccount = {
239
478
  cumulativeBorrowInterest: BN;
240
479
  depositBalance: BN;
241
480
  borrowBalance: BN;
242
- lastUpdated: BN;
243
- oracle: PublicKey;
481
+ lastInterestTs: BN;
482
+ lastTwapTs: BN;
244
483
  initialAssetWeight: BN;
245
484
  maintenanceAssetWeight: BN;
246
485
  initialLiabilityWeight: BN;
247
486
  maintenanceLiabilityWeight: BN;
487
+ liquidatorFee: BN;
488
+ imfFactor: BN;
489
+
490
+ withdrawGuardThreshold: BN;
491
+ depositTokenTwap: BN;
492
+ borrowTokenTwap: BN;
493
+ utilizationTwap: BN;
494
+
495
+ orderStepSize: BN;
496
+ nextFillRecordId: BN;
497
+ spotFeePool: {
498
+ balance: BN;
499
+ };
500
+ totalSpotFee: BN;
248
501
  };
249
502
 
250
503
  export type PoolBalance = {
@@ -258,24 +511,34 @@ export type AMM = {
258
511
  lastFundingRate: BN;
259
512
  lastFundingRateTs: BN;
260
513
  lastMarkPriceTwap: BN;
514
+ lastMarkPriceTwap5min: BN;
261
515
  lastMarkPriceTwapTs: BN;
262
- lastOraclePriceTwap: BN;
263
- lastOraclePriceTwapTs: BN;
516
+
264
517
  oracle: PublicKey;
265
518
  oracleSource: OracleSource;
519
+ historicalOracleData: HistoricalOracleData;
520
+
521
+ lastOracleMarkSpreadPct: BN;
522
+ lastOracleConfPct: BN;
523
+
266
524
  fundingPeriod: BN;
267
525
  quoteAssetReserve: BN;
268
526
  pegMultiplier: BN;
269
527
  cumulativeFundingRateLong: BN;
270
528
  cumulativeFundingRateShort: BN;
271
- cumulativeRepegRebateLong: BN;
272
- cumulativeRepegRebateShort: BN;
529
+ cumulativeFundingRateLp: BN;
273
530
  totalFeeMinusDistributions: BN;
274
531
  totalFeeWithdrawn: BN;
275
532
  totalFee: BN;
533
+ cumulativeFundingPaymentPerLp: BN;
534
+ cumulativeFeePerLp: BN;
535
+ cumulativeNetBaseAssetAmountPerLp: BN;
536
+ userLpShares: BN;
537
+ netUnsettledLpBaseAssetAmount: BN;
276
538
  minimumQuoteAssetTradeSize: BN;
277
539
  baseAssetAmountStepSize: BN;
278
- lastOraclePrice: BN;
540
+ maxBaseAssetAmountRatio: number;
541
+ maxSlippageRatio: number;
279
542
  baseSpread: number;
280
543
  curveUpdateIntensity: number;
281
544
  netBaseAssetAmount: BN;
@@ -287,58 +550,93 @@ export type AMM = {
287
550
  totalMmFee: BN;
288
551
  netRevenueSinceLastFunding: BN;
289
552
  lastUpdateSlot: BN;
553
+ lastOracleValid: boolean;
290
554
  lastBidPriceTwap: BN;
291
555
  lastAskPriceTwap: BN;
292
556
  longSpread: BN;
293
557
  shortSpread: BN;
294
558
  maxSpread: number;
559
+ marketPosition: PerpPosition;
560
+ marketPositionPerLp: PerpPosition;
561
+ ammJitIntensity: number;
562
+ maxBaseAssetReserve: BN;
563
+ minBaseAssetReserve: BN;
564
+ cumulativeSocialLoss: BN;
295
565
  };
296
566
 
297
567
  // # User Account Types
298
- export type UserPosition = {
568
+ export type PerpPosition = {
299
569
  baseAssetAmount: BN;
570
+ remainderBaseAssetAmount: BN;
300
571
  lastCumulativeFundingRate: BN;
301
- marketIndex: BN;
572
+ marketIndex: number;
302
573
  quoteAssetAmount: BN;
303
574
  quoteEntryAmount: BN;
304
- openOrders: BN;
305
- unsettledPnl: BN;
575
+ openOrders: number;
306
576
  openBids: BN;
307
577
  openAsks: BN;
578
+ settledPnl: BN;
579
+ lpShares: BN;
580
+ lastFeePerLp: BN;
581
+ lastNetBaseAssetAmountPerLp: BN;
582
+ lastNetQuoteAssetAmountPerLp: BN;
308
583
  };
309
584
 
310
- export type UserAccount = {
311
- authority: PublicKey;
312
- name: number[];
313
- userId: number;
314
- bankBalances: UserBankBalance[];
315
- collateral: BN;
316
- cumulativeDeposits: BN;
585
+ export type UserStatsAccount = {
586
+ numberOfUsers: number;
587
+ makerVolume30D: BN;
588
+ takerVolume30D: BN;
589
+ fillerVolume30D: BN;
590
+ lastMakerVolume30DTs: BN;
591
+ lastTakerVolume30DTs: BN;
592
+ lastFillerVolume30DTs: BN;
317
593
  fees: {
318
594
  totalFeePaid: BN;
319
595
  totalFeeRebate: BN;
320
596
  totalTokenDiscount: BN;
321
- totalReferralReward: BN;
322
597
  totalRefereeDiscount: BN;
323
598
  };
324
- positions: UserPosition[];
599
+ referrer: PublicKey;
600
+ isReferrer: boolean;
601
+ totalReferrerReward: BN;
602
+ authority: PublicKey;
603
+ stakedQuoteAssetAmount: BN;
604
+ };
605
+
606
+ export type UserAccount = {
607
+ authority: PublicKey;
608
+ delegate: PublicKey;
609
+ name: number[];
610
+ userId: number;
611
+ spotPositions: SpotPosition[];
612
+ perpPositions: PerpPosition[];
325
613
  orders: Order[];
614
+ beingLiquidated: boolean;
615
+ bankrupt: boolean;
616
+ nextLiquidationId: number;
617
+ nextOrderId: BN;
618
+ customMarginRatio: number;
326
619
  };
327
620
 
328
- export type UserBankBalance = {
329
- bankIndex: BN;
330
- balanceType: BankBalanceType;
621
+ export type SpotPosition = {
622
+ marketIndex: number;
623
+ balanceType: SpotBalanceType;
331
624
  balance: BN;
625
+ openOrders: number;
626
+ openBids: BN;
627
+ openAsks: BN;
628
+ cumulativeDeposits: BN;
332
629
  };
333
630
 
334
631
  export type Order = {
335
632
  status: OrderStatus;
336
633
  orderType: OrderType;
634
+ marketType: MarketType;
337
635
  ts: BN;
338
636
  slot: BN;
339
637
  orderId: BN;
340
638
  userOrderId: number;
341
- marketIndex: BN;
639
+ marketIndex: number;
342
640
  price: BN;
343
641
  baseAssetAmount: BN;
344
642
  baseAssetAmountFilled: BN;
@@ -350,25 +648,24 @@ export type Order = {
350
648
  triggerPrice: BN;
351
649
  triggerCondition: OrderTriggerCondition;
352
650
  triggered: boolean;
353
- discountTier: OrderDiscountTier;
354
651
  existingPositionDirection: PositionDirection;
355
- referrer: PublicKey;
356
652
  postOnly: boolean;
357
653
  immediateOrCancel: boolean;
358
654
  oraclePriceOffset: BN;
359
655
  auctionDuration: number;
360
656
  auctionStartPrice: BN;
361
657
  auctionEndPrice: BN;
658
+ timeInForce: number;
362
659
  };
363
660
 
364
661
  export type OrderParams = {
365
662
  orderType: OrderType;
663
+ marketType: MarketType;
366
664
  userOrderId: number;
367
665
  direction: PositionDirection;
368
- quoteAssetAmount: BN;
369
666
  baseAssetAmount: BN;
370
667
  price: BN;
371
- marketIndex: BN;
668
+ marketIndex: number;
372
669
  reduceOnly: boolean;
373
670
  postOnly: boolean;
374
671
  immediateOrCancel: boolean;
@@ -376,19 +673,60 @@ export type OrderParams = {
376
673
  triggerCondition: OrderTriggerCondition;
377
674
  positionLimit: BN;
378
675
  oraclePriceOffset: BN;
379
- padding0: boolean;
380
- padding1: BN;
381
- optionalAccounts: {
382
- discountToken: boolean;
383
- referrer: boolean;
384
- };
676
+ auctionDuration: number | null;
677
+ timeInForce: number | null;
678
+ auctionStartPrice: BN | null;
679
+ };
680
+
681
+ export type NecessaryOrderParams = {
682
+ orderType: OrderType;
683
+ marketIndex: number;
684
+ baseAssetAmount: BN;
685
+ direction: PositionDirection;
686
+ };
687
+
688
+ export type OptionalOrderParams = {
689
+ [Property in keyof OrderParams]?: OrderParams[Property];
690
+ } & NecessaryOrderParams;
691
+
692
+ export const DefaultOrderParams = {
693
+ orderType: OrderType.MARKET,
694
+ marketType: MarketType.PERP,
695
+ userOrderId: 0,
696
+ direction: PositionDirection.LONG,
697
+ baseAssetAmount: ZERO,
698
+ price: ZERO,
699
+ marketIndex: 0,
700
+ reduceOnly: false,
701
+ postOnly: false,
702
+ immediateOrCancel: false,
703
+ triggerPrice: ZERO,
704
+ triggerCondition: OrderTriggerCondition.ABOVE,
705
+ positionLimit: ZERO,
706
+ oraclePriceOffset: ZERO,
707
+ auctionDuration: null,
708
+ timeInForce: null,
709
+ auctionStartPrice: null,
385
710
  };
386
711
 
387
712
  export type MakerInfo = {
388
713
  maker: PublicKey;
714
+ makerStats: PublicKey;
715
+ order: Order;
716
+ };
717
+
718
+ export type TakerInfo = {
719
+ taker: PublicKey;
720
+ takerStats: PublicKey;
721
+ takerUserAccount: UserAccount;
389
722
  order: Order;
390
723
  };
391
724
 
725
+ export type ReferrerInfo = {
726
+ referrer: PublicKey;
727
+ referrerStats: PublicKey;
728
+ };
729
+
392
730
  // # Misc Types
393
731
  export interface IWallet {
394
732
  signTransaction(tx: Transaction): Promise<Transaction>;
@@ -397,39 +735,29 @@ export interface IWallet {
397
735
  }
398
736
 
399
737
  export type FeeStructure = {
400
- feeNumerator: BN;
401
- feeDenominator: BN;
402
- discountTokenTiers: {
403
- firstTier: {
404
- minimumBalance: BN;
405
- discountNumerator: BN;
406
- discountDenominator: BN;
407
- };
408
- secondTier: {
409
- minimumBalance: BN;
410
- discountNumerator: BN;
411
- discountDenominator: BN;
412
- };
413
- thirdTier: {
414
- minimumBalance: BN;
415
- discountNumerator: BN;
416
- discountDenominator: BN;
417
- };
418
- fourthTier: {
419
- minimumBalance: BN;
420
- discountNumerator: BN;
421
- discountDenominator: BN;
422
- };
423
- };
424
- referralDiscount: {
425
- referrerRewardNumerator: BN;
426
- referrerRewardDenominator: BN;
427
- refereeDiscountNumerator: BN;
428
- refereeDiscountDenominator: BN;
429
- };
738
+ feeTiers: FeeTier[];
430
739
  makerRebateNumerator: BN;
431
740
  makerRebateDenominator: BN;
432
741
  fillerRewardStructure: OrderFillerRewardStructure;
742
+ flatFillerFee: BN;
743
+ referrerRewardEpochUpperBound: BN;
744
+ };
745
+
746
+ export type FeeTier = {
747
+ feeNumerator: number;
748
+ feeDenominator: number;
749
+ makerRebateNumerator: number;
750
+ makerRebateDenominator: number;
751
+ referrerRewardNumerator: number;
752
+ referrerRewardDenominator: number;
753
+ refereeFeeNumerator: number;
754
+ refereeFeeDenominator: number;
755
+ };
756
+
757
+ export type OrderFillerRewardStructure = {
758
+ rewardNumerator: BN;
759
+ rewardDenominator: BN;
760
+ timeBasedRewardLowerBound: BN;
433
761
  };
434
762
 
435
763
  export type OracleGuardRails = {
@@ -438,17 +766,41 @@ export type OracleGuardRails = {
438
766
  markOracleDivergenceDenominator: BN;
439
767
  };
440
768
  validity: {
441
- slotsBeforeStale: BN;
769
+ slotsBeforeStaleForAmm: BN;
770
+ slotsBeforeStaleForMargin: BN;
442
771
  confidenceIntervalMaxSize: BN;
443
772
  tooVolatileRatio: BN;
444
773
  };
445
774
  useForLiquidations: boolean;
446
775
  };
447
776
 
448
- export type OrderFillerRewardStructure = {
449
- rewardNumerator: BN;
450
- rewardDenominator: BN;
451
- timeBasedRewardLowerBound: BN;
777
+ export type MarginCategory = 'Initial' | 'Maintenance';
778
+
779
+ export type InsuranceFundStake = {
780
+ marketIndex: number;
781
+ authority: PublicKey;
782
+
783
+ ifShares: BN;
784
+ ifBase: BN;
785
+
786
+ lastWithdrawRequestShares: BN;
787
+ lastWithdrawRequestValue: BN;
788
+ lastWithdrawRequestTs: BN;
452
789
  };
453
790
 
454
- export type MarginCategory = 'Initial' | 'Partial' | 'Maintenance';
791
+ export type SerumV3FulfillmentConfigAccount = {
792
+ fulfillmentType: SpotFulfillmentType;
793
+ status: SpotFulfillmentStatus;
794
+ pubkey: PublicKey;
795
+ marketIndex: number;
796
+ serumProgramId: PublicKey;
797
+ serumMarket: PublicKey;
798
+ serumRequestQueue: PublicKey;
799
+ serumEventQueue: PublicKey;
800
+ serumBids: PublicKey;
801
+ serumAsks: PublicKey;
802
+ serumBaseVault: PublicKey;
803
+ serumQuoteVault: PublicKey;
804
+ serumOpenOrders: PublicKey;
805
+ serumSignerNonce: BN;
806
+ };