@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
@@ -0,0 +1,611 @@
1
+ import { Connection, Keypair, PublicKey } from '@solana/web3.js';
2
+ import {
3
+ AMM,
4
+ AssetTier,
5
+ PerpPosition,
6
+ BN,
7
+ DriftClient,
8
+ User,
9
+ PerpMarketAccount,
10
+ SpotMarketAccount,
11
+ MarketStatus,
12
+ ContractType,
13
+ OracleSource,
14
+ DevnetSpotMarkets,
15
+ BASE_PRECISION,
16
+ QUOTE_PRECISION,
17
+ AMM_TO_QUOTE_PRECISION_RATIO,
18
+ StateAccount,
19
+ UserMapInterface,
20
+ Wallet,
21
+ OrderRecord,
22
+ ExchangeStatus,
23
+ ZERO,
24
+ ContractTier,
25
+ } from '../../src';
26
+
27
+ export const mockPerpPosition: PerpPosition = {
28
+ baseAssetAmount: new BN(0),
29
+ lastCumulativeFundingRate: new BN(0),
30
+ marketIndex: 0,
31
+ quoteAssetAmount: new BN(0),
32
+ quoteBreakEvenAmount: new BN(0),
33
+ quoteEntryAmount: new BN(0),
34
+ openOrders: 0,
35
+ openBids: new BN(0),
36
+ openAsks: new BN(0),
37
+ settledPnl: new BN(0),
38
+ lpShares: new BN(0),
39
+ remainderBaseAssetAmount: 0,
40
+ lastNetBaseAssetAmountPerLp: new BN(0),
41
+ lastNetQuoteAssetAmountPerLp: new BN(0),
42
+ };
43
+
44
+ export const mockAMM: AMM = {
45
+ /* these values create a bid/ask price of 12 */
46
+ baseAssetReserve: new BN(1).mul(BASE_PRECISION),
47
+ quoteAssetReserve: new BN(12)
48
+ .mul(QUOTE_PRECISION)
49
+ .mul(AMM_TO_QUOTE_PRECISION_RATIO),
50
+ sqrtK: new BN(1),
51
+ pegMultiplier: new BN(1),
52
+ maxSlippageRatio: 1_000_000,
53
+
54
+ cumulativeFundingRate: new BN(0),
55
+ lastFundingRate: new BN(0),
56
+ lastFundingRateTs: new BN(0),
57
+ lastMarkPriceTwap: new BN(0),
58
+ lastMarkPriceTwap5min: new BN(0),
59
+ lastMarkPriceTwapTs: new BN(0),
60
+ historicalOracleData: {
61
+ lastOraclePrice: new BN(0),
62
+ lastOracleConf: new BN(0),
63
+ lastOracleDelay: new BN(0),
64
+ lastOraclePriceTwap: new BN(0),
65
+ lastOraclePriceTwap5min: new BN(0),
66
+ lastOraclePriceTwapTs: new BN(0),
67
+ },
68
+ lastOracleReservePriceSpreadPct: new BN(0),
69
+ lastOracleConfPct: new BN(0),
70
+ oracle: PublicKey.default,
71
+ oracleSource: OracleSource.PYTH,
72
+ fundingPeriod: new BN(0),
73
+ cumulativeFundingRateLong: new BN(0),
74
+ cumulativeFundingRateShort: new BN(0),
75
+ totalFeeMinusDistributions: new BN(0),
76
+ totalFeeWithdrawn: new BN(0),
77
+ totalFee: new BN(0),
78
+ userLpShares: new BN(0),
79
+ baseAssetAmountWithUnsettledLp: new BN(0),
80
+ orderStepSize: new BN(0),
81
+ orderTickSize: new BN(1),
82
+ last24hAvgFundingRate: new BN(0),
83
+ lastFundingRateShort: new BN(0),
84
+ lastFundingRateLong: new BN(0),
85
+ concentrationCoef: new BN(0),
86
+ lastTradeTs: new BN(0),
87
+ lastOracleNormalisedPrice: new BN(0),
88
+ maxOpenInterest: new BN(0),
89
+ totalLiquidationFee: new BN(0),
90
+ maxFillReserveFraction: 0,
91
+ baseSpread: 0,
92
+ curveUpdateIntensity: 0,
93
+ baseAssetAmountWithAmm: new BN(0),
94
+ baseAssetAmountLong: new BN(0),
95
+ baseAssetAmountShort: new BN(0),
96
+ quoteAssetAmount: new BN(0),
97
+ terminalQuoteAssetReserve: new BN(0),
98
+ feePool: {
99
+ scaledBalance: new BN(0),
100
+ marketIndex: 0,
101
+ },
102
+ totalExchangeFee: new BN(0),
103
+ totalMmFee: new BN(0),
104
+ netRevenueSinceLastFunding: new BN(0),
105
+ lastUpdateSlot: new BN(0),
106
+ lastOracleValid: true,
107
+ lastBidPriceTwap: new BN(0),
108
+ lastAskPriceTwap: new BN(0),
109
+ longSpread: 0,
110
+ shortSpread: 0,
111
+ maxSpread: 0,
112
+ ammJitIntensity: 0,
113
+ maxBaseAssetReserve: new BN(0),
114
+ minBaseAssetReserve: new BN(0),
115
+ cumulativeSocialLoss: new BN(0),
116
+ baseAssetAmountPerLp: new BN(0),
117
+ quoteAssetAmountPerLp: new BN(0),
118
+
119
+ quoteBreakEvenAmountLong: new BN(0),
120
+ quoteBreakEvenAmountShort: new BN(0),
121
+ quoteEntryAmountLong: new BN(0),
122
+ quoteEntryAmountShort: new BN(0),
123
+
124
+ markStd: new BN(0),
125
+ longIntensityCount: 0,
126
+ longIntensityVolume: new BN(0),
127
+ shortIntensityCount: 0,
128
+ shortIntensityVolume: new BN(0),
129
+ volume24h: new BN(0),
130
+ minOrderSize: new BN(0),
131
+ maxPositionSize: new BN(0),
132
+
133
+ bidBaseAssetReserve: new BN(0),
134
+ bidQuoteAssetReserve: new BN(0),
135
+ askBaseAssetReserve: new BN(0),
136
+ askQuoteAssetReserve: new BN(0),
137
+ };
138
+
139
+ export const mockPerpMarkets: Array<PerpMarketAccount> = [
140
+ {
141
+ status: MarketStatus.INITIALIZED,
142
+ name: [],
143
+ contractType: ContractType.PERPETUAL,
144
+ contractTier: ContractTier.A,
145
+ expiryTs: new BN(0),
146
+ expiryPrice: new BN(0),
147
+ marketIndex: 0,
148
+ pubkey: PublicKey.default,
149
+ amm: mockAMM,
150
+ numberOfUsersWithBase: 0,
151
+ numberOfUsers: 0,
152
+ marginRatioInitial: 0,
153
+ marginRatioMaintenance: 0,
154
+ nextFillRecordId: new BN(0),
155
+ pnlPool: {
156
+ scaledBalance: new BN(0),
157
+ marketIndex: 0,
158
+ },
159
+ ifLiquidationFee: 0,
160
+ liquidatorFee: 0,
161
+ imfFactor: 0,
162
+ nextFundingRateRecordId: new BN(0),
163
+ nextCurveRecordId: new BN(0),
164
+ unrealizedPnlImfFactor: 0,
165
+ unrealizedPnlMaxImbalance: ZERO,
166
+ unrealizedPnlInitialAssetWeight: 0,
167
+ unrealizedPnlMaintenanceAssetWeight: 0,
168
+ insuranceClaim: {
169
+ revenueWithdrawSinceLastSettle: new BN(0),
170
+ maxRevenueWithdrawPerPeriod: new BN(0),
171
+ lastRevenueWithdrawTs: new BN(0),
172
+ quoteSettledInsurance: new BN(0),
173
+ quoteMaxInsurance: new BN(0),
174
+ },
175
+ },
176
+ {
177
+ status: MarketStatus.INITIALIZED,
178
+ contractTier: ContractTier.A,
179
+ nextFundingRateRecordId: new BN(0),
180
+ nextCurveRecordId: new BN(0),
181
+ name: [],
182
+ contractType: ContractType.PERPETUAL,
183
+ expiryTs: new BN(0),
184
+ expiryPrice: new BN(0),
185
+ marketIndex: 1,
186
+ pubkey: PublicKey.default,
187
+ amm: mockAMM,
188
+ numberOfUsersWithBase: 0,
189
+ numberOfUsers: 0,
190
+ marginRatioInitial: 0,
191
+ marginRatioMaintenance: 0,
192
+ nextFillRecordId: new BN(0),
193
+ pnlPool: {
194
+ scaledBalance: new BN(0),
195
+ marketIndex: 0,
196
+ },
197
+ ifLiquidationFee: 0,
198
+ liquidatorFee: 0,
199
+ imfFactor: 0,
200
+ unrealizedPnlImfFactor: 0,
201
+ unrealizedPnlMaxImbalance: ZERO,
202
+ unrealizedPnlInitialAssetWeight: 0,
203
+ unrealizedPnlMaintenanceAssetWeight: 0,
204
+ insuranceClaim: {
205
+ revenueWithdrawSinceLastSettle: new BN(0),
206
+ maxRevenueWithdrawPerPeriod: new BN(0),
207
+ lastRevenueWithdrawTs: new BN(0),
208
+ quoteSettledInsurance: new BN(0),
209
+ quoteMaxInsurance: new BN(0),
210
+ },
211
+ },
212
+ {
213
+ status: MarketStatus.INITIALIZED,
214
+ contractTier: ContractTier.A,
215
+ nextFundingRateRecordId: new BN(0),
216
+ nextCurveRecordId: new BN(0),
217
+ name: [],
218
+ contractType: ContractType.PERPETUAL,
219
+ expiryTs: new BN(0),
220
+ expiryPrice: new BN(0),
221
+ marketIndex: 2,
222
+ pubkey: PublicKey.default,
223
+ amm: mockAMM,
224
+ numberOfUsersWithBase: 0,
225
+ numberOfUsers: 0,
226
+ marginRatioInitial: 0,
227
+ marginRatioMaintenance: 0,
228
+ nextFillRecordId: new BN(0),
229
+ pnlPool: {
230
+ scaledBalance: new BN(0),
231
+ marketIndex: 0,
232
+ },
233
+ ifLiquidationFee: 0,
234
+ liquidatorFee: 0,
235
+ imfFactor: 0,
236
+ unrealizedPnlImfFactor: 0,
237
+ unrealizedPnlMaxImbalance: ZERO,
238
+ unrealizedPnlInitialAssetWeight: 0,
239
+ unrealizedPnlMaintenanceAssetWeight: 0,
240
+ insuranceClaim: {
241
+ revenueWithdrawSinceLastSettle: new BN(0),
242
+ maxRevenueWithdrawPerPeriod: new BN(0),
243
+ lastRevenueWithdrawTs: new BN(0),
244
+ quoteSettledInsurance: new BN(0),
245
+ quoteMaxInsurance: new BN(0),
246
+ },
247
+ },
248
+ ];
249
+
250
+ export const mockSpotMarkets: Array<SpotMarketAccount> = [
251
+ {
252
+ status: MarketStatus.ACTIVE,
253
+ assetTier: AssetTier.COLLATERAL,
254
+ maxTokenDeposits: new BN(100),
255
+ marketIndex: 0,
256
+ pubkey: PublicKey.default,
257
+ mint: DevnetSpotMarkets[0].mint,
258
+ vault: PublicKey.default,
259
+ revenuePool: {
260
+ scaledBalance: new BN(0),
261
+ marketIndex: 0,
262
+ },
263
+ insuranceFund: {
264
+ vault: PublicKey.default,
265
+ totalShares: new BN(0),
266
+ userShares: new BN(0),
267
+ sharesBase: new BN(0),
268
+ unstakingPeriod: new BN(0),
269
+ lastRevenueSettleTs: new BN(0),
270
+ revenueSettlePeriod: new BN(0),
271
+ totalFactor: 0,
272
+ userFactor: 0,
273
+ },
274
+ ifLiquidationFee: 0,
275
+ liquidatorFee: 0,
276
+ decimals: 6,
277
+ optimalUtilization: 0,
278
+ optimalBorrowRate: 0,
279
+ maxBorrowRate: 0,
280
+ cumulativeDepositInterest: new BN(0),
281
+ cumulativeBorrowInterest: new BN(0),
282
+ depositBalance: new BN(0),
283
+ borrowBalance: new BN(0),
284
+ lastInterestTs: new BN(0),
285
+ lastTwapTs: new BN(0),
286
+ oracle: PublicKey.default,
287
+ initialAssetWeight: 0,
288
+ maintenanceAssetWeight: 0,
289
+ initialLiabilityWeight: 0,
290
+ maintenanceLiabilityWeight: 0,
291
+ imfFactor: 0,
292
+ withdrawGuardThreshold: new BN(0),
293
+ depositTokenTwap: new BN(0),
294
+ borrowTokenTwap: new BN(0),
295
+ utilizationTwap: new BN(0),
296
+ orderStepSize: new BN(0),
297
+ orderTickSize: new BN(0),
298
+ nextFillRecordId: new BN(0),
299
+ nextDepositRecordId: new BN(0),
300
+ ordersEnabled: true,
301
+ spotFeePool: {
302
+ scaledBalance: new BN(0),
303
+ marketIndex: 0,
304
+ },
305
+ totalSpotFee: new BN(0),
306
+ oracleSource: OracleSource.PYTH,
307
+ historicalOracleData: {
308
+ lastOraclePrice: new BN(0),
309
+ lastOracleConf: new BN(0),
310
+ lastOracleDelay: new BN(0),
311
+ lastOraclePriceTwap: new BN(0),
312
+ lastOraclePriceTwap5min: new BN(0),
313
+ lastOraclePriceTwapTs: new BN(0),
314
+ },
315
+ historicalIndexData: {
316
+ lastIndexBidPrice: new BN(0),
317
+ lastIndexAskPrice: new BN(0),
318
+ lastIndexPriceTwap: new BN(0),
319
+ lastIndexPriceTwap5min: new BN(0),
320
+ lastIndexPriceTwapTs: new BN(0),
321
+ },
322
+ },
323
+ {
324
+ status: MarketStatus.ACTIVE,
325
+ assetTier: AssetTier.CROSS,
326
+ maxTokenDeposits: new BN(100),
327
+ marketIndex: 1,
328
+ pubkey: PublicKey.default,
329
+ mint: DevnetSpotMarkets[1].mint,
330
+ vault: PublicKey.default,
331
+ revenuePool: {
332
+ scaledBalance: new BN(0),
333
+ marketIndex: 0,
334
+ },
335
+ insuranceFund: {
336
+ vault: PublicKey.default,
337
+ totalShares: new BN(0),
338
+ userShares: new BN(0),
339
+ sharesBase: new BN(0),
340
+ unstakingPeriod: new BN(0),
341
+ lastRevenueSettleTs: new BN(0),
342
+ revenueSettlePeriod: new BN(0),
343
+ totalFactor: 0,
344
+ userFactor: 0,
345
+ },
346
+ ifLiquidationFee: 0,
347
+ liquidatorFee: 0,
348
+ decimals: 9,
349
+ optimalUtilization: 0,
350
+ optimalBorrowRate: 0,
351
+ maxBorrowRate: 0,
352
+ cumulativeDepositInterest: new BN(0),
353
+ cumulativeBorrowInterest: new BN(0),
354
+ depositBalance: new BN(0),
355
+ borrowBalance: new BN(0),
356
+ lastInterestTs: new BN(0),
357
+ lastTwapTs: new BN(0),
358
+ oracle: PublicKey.default,
359
+ initialAssetWeight: 0,
360
+ maintenanceAssetWeight: 0,
361
+ initialLiabilityWeight: 0,
362
+ maintenanceLiabilityWeight: 0,
363
+ imfFactor: 0,
364
+ withdrawGuardThreshold: new BN(0),
365
+ depositTokenTwap: new BN(0),
366
+ borrowTokenTwap: new BN(0),
367
+ utilizationTwap: new BN(0),
368
+ orderStepSize: new BN(0),
369
+ orderTickSize: new BN(0),
370
+ nextFillRecordId: new BN(0),
371
+ nextDepositRecordId: new BN(0),
372
+ ordersEnabled: true,
373
+ spotFeePool: {
374
+ scaledBalance: new BN(0),
375
+ marketIndex: 0,
376
+ },
377
+ totalSpotFee: new BN(0),
378
+ oracleSource: OracleSource.PYTH,
379
+ historicalOracleData: {
380
+ lastOraclePrice: new BN(0),
381
+ lastOracleConf: new BN(0),
382
+ lastOracleDelay: new BN(0),
383
+ lastOraclePriceTwap: new BN(0),
384
+ lastOraclePriceTwap5min: new BN(0),
385
+ lastOraclePriceTwapTs: new BN(0),
386
+ },
387
+ historicalIndexData: {
388
+ lastIndexBidPrice: new BN(0),
389
+ lastIndexAskPrice: new BN(0),
390
+ lastIndexPriceTwap: new BN(0),
391
+ lastIndexPriceTwap5min: new BN(0),
392
+ lastIndexPriceTwapTs: new BN(0),
393
+ },
394
+ },
395
+ {
396
+ status: MarketStatus.ACTIVE,
397
+ assetTier: AssetTier.PROTECTED,
398
+ maxTokenDeposits: new BN(100),
399
+ marketIndex: 2,
400
+ pubkey: PublicKey.default,
401
+ mint: DevnetSpotMarkets[2].mint,
402
+ vault: PublicKey.default,
403
+ revenuePool: {
404
+ scaledBalance: new BN(0),
405
+ marketIndex: 0,
406
+ },
407
+ insuranceFund: {
408
+ vault: PublicKey.default,
409
+ totalShares: new BN(0),
410
+ userShares: new BN(0),
411
+ sharesBase: new BN(0),
412
+ unstakingPeriod: new BN(0),
413
+ lastRevenueSettleTs: new BN(0),
414
+ revenueSettlePeriod: new BN(0),
415
+ totalFactor: 0,
416
+ userFactor: 0,
417
+ },
418
+ ifLiquidationFee: 0,
419
+ liquidatorFee: 0,
420
+ decimals: 6,
421
+ optimalUtilization: 0,
422
+ optimalBorrowRate: 0,
423
+ maxBorrowRate: 0,
424
+ cumulativeDepositInterest: new BN(0),
425
+ cumulativeBorrowInterest: new BN(0),
426
+ depositBalance: new BN(0),
427
+ borrowBalance: new BN(0),
428
+ lastInterestTs: new BN(0),
429
+ lastTwapTs: new BN(0),
430
+ oracle: PublicKey.default,
431
+ initialAssetWeight: 0,
432
+ maintenanceAssetWeight: 0,
433
+ initialLiabilityWeight: 0,
434
+ maintenanceLiabilityWeight: 0,
435
+ imfFactor: 0,
436
+ withdrawGuardThreshold: new BN(0),
437
+ depositTokenTwap: new BN(0),
438
+ borrowTokenTwap: new BN(0),
439
+ utilizationTwap: new BN(0),
440
+ orderStepSize: new BN(0),
441
+ orderTickSize: new BN(0),
442
+ nextFillRecordId: new BN(0),
443
+ nextDepositRecordId: new BN(0),
444
+ ordersEnabled: true,
445
+ spotFeePool: {
446
+ scaledBalance: new BN(0),
447
+ marketIndex: 0,
448
+ },
449
+ totalSpotFee: new BN(0),
450
+ oracleSource: OracleSource.PYTH,
451
+ historicalOracleData: {
452
+ lastOraclePrice: new BN(0),
453
+ lastOracleConf: new BN(0),
454
+ lastOracleDelay: new BN(0),
455
+ lastOraclePriceTwap: new BN(0),
456
+ lastOraclePriceTwap5min: new BN(0),
457
+ lastOraclePriceTwapTs: new BN(0),
458
+ },
459
+ historicalIndexData: {
460
+ lastIndexBidPrice: new BN(0),
461
+ lastIndexAskPrice: new BN(0),
462
+ lastIndexPriceTwap: new BN(0),
463
+ lastIndexPriceTwap5min: new BN(0),
464
+ lastIndexPriceTwapTs: new BN(0),
465
+ },
466
+ },
467
+ ];
468
+
469
+ export const mockStateAccount: StateAccount = {
470
+ admin: PublicKey.default,
471
+ defaultMarketOrderTimeInForce: 0,
472
+ defaultSpotAuctionDuration: 0,
473
+ discountMint: PublicKey.default,
474
+ exchangeStatus: ExchangeStatus.ACTIVE,
475
+ liquidationMarginBufferRatio: 0,
476
+ lpCooldownTime: new BN(0),
477
+ minPerpAuctionDuration: 0,
478
+ numberOfMarkets: 0,
479
+ numberOfSpotMarkets: 0,
480
+ oracleGuardRails: {
481
+ priceDivergence: {
482
+ markOracleDivergenceNumerator: new BN(0),
483
+ markOracleDivergenceDenominator: new BN(0),
484
+ },
485
+ validity: {
486
+ slotsBeforeStaleForAmm: new BN(0),
487
+ slotsBeforeStaleForMargin: new BN(0),
488
+ confidenceIntervalMaxSize: new BN(0),
489
+ tooVolatileRatio: new BN(0),
490
+ },
491
+ useForLiquidations: true,
492
+ },
493
+ perpFeeStructure: {
494
+ feeTiers: [
495
+ {
496
+ feeNumerator: 0,
497
+ feeDenominator: 0,
498
+ makerRebateNumerator: 0,
499
+ makerRebateDenominator: 0,
500
+ referrerRewardNumerator: 0,
501
+ referrerRewardDenominator: 0,
502
+ refereeFeeNumerator: 0,
503
+ refereeFeeDenominator: 0,
504
+ },
505
+ ],
506
+ makerRebateNumerator: new BN(0),
507
+ makerRebateDenominator: new BN(0),
508
+ fillerRewardStructure: {
509
+ rewardNumerator: new BN(0),
510
+ rewardDenominator: new BN(0),
511
+ timeBasedRewardLowerBound: new BN(0),
512
+ },
513
+ flatFillerFee: new BN(0),
514
+ referrerRewardEpochUpperBound: new BN(0),
515
+ },
516
+ settlementDuration: 0,
517
+ signer: PublicKey.default,
518
+ signerNonce: 0,
519
+ spotFeeStructure: {
520
+ feeTiers: [
521
+ {
522
+ feeNumerator: 0,
523
+ feeDenominator: 0,
524
+ makerRebateNumerator: 0,
525
+ makerRebateDenominator: 0,
526
+ referrerRewardNumerator: 0,
527
+ referrerRewardDenominator: 0,
528
+ refereeFeeNumerator: 0,
529
+ refereeFeeDenominator: 0,
530
+ },
531
+ ],
532
+ makerRebateNumerator: new BN(0),
533
+ makerRebateDenominator: new BN(0),
534
+ fillerRewardStructure: {
535
+ rewardNumerator: new BN(0),
536
+ rewardDenominator: new BN(0),
537
+ timeBasedRewardLowerBound: new BN(0),
538
+ },
539
+ flatFillerFee: new BN(0),
540
+ referrerRewardEpochUpperBound: new BN(0),
541
+ },
542
+ srmVault: PublicKey.default,
543
+ whitelistMint: PublicKey.default,
544
+ };
545
+
546
+ export class MockUserMap implements UserMapInterface {
547
+ private userMap = new Map<string, User>();
548
+ private userAccountToAuthority = new Map<string, string>();
549
+ private driftClient: DriftClient;
550
+
551
+ constructor() {
552
+ this.userMap = new Map();
553
+ this.userAccountToAuthority = new Map();
554
+ this.driftClient = new DriftClient({
555
+ connection: new Connection('http://localhost:8899'),
556
+ wallet: new Wallet(new Keypair()),
557
+ programID: PublicKey.default,
558
+ });
559
+ }
560
+
561
+ public async fetchAllUsers(): Promise<void> {}
562
+
563
+ public async addPubkey(userAccountPublicKey: PublicKey): Promise<void> {
564
+ const user = new User({
565
+ driftClient: this.driftClient,
566
+ userAccountPublicKey: userAccountPublicKey,
567
+ });
568
+ this.userMap.set(userAccountPublicKey.toBase58(), user);
569
+ }
570
+
571
+ // mock function
572
+ public addUserAccountAuthority(
573
+ userAccountPublicKey: PublicKey,
574
+ authorityPublicKey: PublicKey
575
+ ): void {
576
+ if (!this.userMap.has(userAccountPublicKey.toBase58())) {
577
+ this.addPubkey(userAccountPublicKey);
578
+ }
579
+ this.userAccountToAuthority.set(
580
+ userAccountPublicKey.toBase58(),
581
+ authorityPublicKey.toBase58()
582
+ );
583
+ }
584
+
585
+ public has(key: string): boolean {
586
+ return this.userMap.has(key);
587
+ }
588
+
589
+ public get(_key: string): User | undefined {
590
+ return undefined;
591
+ }
592
+
593
+ public async mustGet(_key: string): Promise<User> {
594
+ return new User({
595
+ driftClient: this.driftClient,
596
+ userAccountPublicKey: PublicKey.default,
597
+ });
598
+ }
599
+
600
+ public getUserAuthority(key: string): PublicKey | undefined {
601
+ return new PublicKey(
602
+ this.userAccountToAuthority.get(key) || PublicKey.default.toBase58()
603
+ );
604
+ }
605
+
606
+ public async updateWithOrderRecord(_record: OrderRecord): Promise<void> {}
607
+
608
+ public values(): IterableIterator<User> {
609
+ return this.userMap.values();
610
+ }
611
+ }