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

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