@drift-labs/sdk 0.2.0-temp.1 → 2.0.0

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 (274) hide show
  1. package/README.md +27 -29
  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/pollingUserAccountSubscriber.d.ts +1 -1
  15. package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
  16. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  17. package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
  18. package/lib/accounts/types.d.ts +26 -15
  19. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
  20. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
  21. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  22. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  23. package/lib/addresses/marketAddresses.d.ts +1 -3
  24. package/lib/addresses/marketAddresses.js +1 -1
  25. package/lib/addresses/pda.d.ts +15 -9
  26. package/lib/addresses/pda.js +73 -35
  27. package/lib/adminClient.d.ts +65 -0
  28. package/lib/adminClient.js +637 -0
  29. package/lib/config.d.ts +10 -10
  30. package/lib/config.js +26 -22
  31. package/lib/constants/numericConstants.d.ts +29 -12
  32. package/lib/constants/numericConstants.js +40 -21
  33. package/lib/constants/perpMarkets.d.ts +18 -0
  34. package/lib/constants/{markets.js → perpMarkets.js} +20 -9
  35. package/lib/constants/spotMarkets.d.ts +19 -0
  36. package/lib/constants/spotMarkets.js +63 -0
  37. package/lib/dlob/DLOB.d.ts +82 -0
  38. package/lib/dlob/DLOB.js +694 -0
  39. package/lib/dlob/DLOBNode.d.ts +54 -0
  40. package/lib/dlob/DLOBNode.js +77 -0
  41. package/lib/dlob/NodeList.d.ts +27 -0
  42. package/lib/dlob/NodeList.js +144 -0
  43. package/lib/driftClient.d.ts +234 -0
  44. package/lib/driftClient.js +2108 -0
  45. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
  46. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  47. package/lib/events/eventList.js +3 -0
  48. package/lib/events/eventSubscriber.d.ts +5 -2
  49. package/lib/events/eventSubscriber.js +25 -11
  50. package/lib/events/fetchLogs.d.ts +13 -2
  51. package/lib/events/fetchLogs.js +45 -14
  52. package/lib/events/pollingLogProvider.d.ts +2 -1
  53. package/lib/events/pollingLogProvider.js +7 -3
  54. package/lib/events/sort.js +8 -11
  55. package/lib/events/types.d.ts +11 -3
  56. package/lib/events/types.js +8 -0
  57. package/lib/events/webSocketLogProvider.js +1 -1
  58. package/lib/examples/makeTradeExample.js +30 -18
  59. package/lib/factory/bigNum.d.ts +8 -4
  60. package/lib/factory/bigNum.js +109 -19
  61. package/lib/idl/drift.json +8392 -0
  62. package/lib/index.d.ts +29 -12
  63. package/lib/index.js +29 -12
  64. package/lib/math/amm.d.ts +9 -6
  65. package/lib/math/amm.js +91 -38
  66. package/lib/math/conversion.js +1 -1
  67. package/lib/math/exchangeStatus.d.ts +4 -0
  68. package/lib/math/exchangeStatus.js +18 -0
  69. package/lib/math/funding.d.ts +6 -6
  70. package/lib/math/funding.js +23 -21
  71. package/lib/math/insurance.d.ts +4 -0
  72. package/lib/math/insurance.js +27 -0
  73. package/lib/math/margin.d.ts +11 -0
  74. package/lib/math/margin.js +82 -0
  75. package/lib/math/market.d.ts +14 -9
  76. package/lib/math/market.js +70 -10
  77. package/lib/math/oracles.d.ts +4 -0
  78. package/lib/math/oracles.js +36 -8
  79. package/lib/math/orders.d.ts +16 -6
  80. package/lib/math/orders.js +97 -17
  81. package/lib/math/position.d.ts +27 -13
  82. package/lib/math/position.js +91 -37
  83. package/lib/math/repeg.js +17 -8
  84. package/lib/math/spotBalance.d.ts +22 -0
  85. package/lib/math/spotBalance.js +192 -0
  86. package/lib/math/spotMarket.d.ts +4 -0
  87. package/lib/math/spotMarket.js +8 -0
  88. package/lib/math/spotPosition.d.ts +6 -0
  89. package/lib/math/spotPosition.js +23 -0
  90. package/lib/math/trade.d.ts +10 -10
  91. package/lib/math/trade.js +27 -31
  92. package/lib/oracles/pythClient.js +1 -1
  93. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  94. package/lib/oracles/switchboardClient.js +1 -1
  95. package/lib/orderParams.d.ts +4 -4
  96. package/lib/orderParams.js +12 -4
  97. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  98. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  99. package/lib/serum/serumSubscriber.d.ts +27 -0
  100. package/lib/serum/serumSubscriber.js +56 -0
  101. package/lib/serum/types.d.ts +11 -0
  102. package/{src/oracles → lib/serum}/types.js +0 -0
  103. package/lib/tokenFaucet.d.ts +1 -0
  104. package/lib/tokenFaucet.js +23 -12
  105. package/lib/tx/retryTxSender.d.ts +1 -1
  106. package/lib/tx/retryTxSender.js +13 -4
  107. package/lib/tx/types.d.ts +1 -1
  108. package/lib/types.d.ts +631 -222
  109. package/lib/types.js +137 -24
  110. package/lib/user.d.ts +228 -0
  111. package/lib/user.js +959 -0
  112. package/lib/userConfig.d.ts +14 -0
  113. package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
  114. package/lib/userMap/userMap.d.ts +41 -0
  115. package/lib/userMap/userMap.js +85 -0
  116. package/lib/userMap/userStatsMap.d.ts +19 -0
  117. package/lib/userMap/userStatsMap.js +68 -0
  118. package/lib/userName.d.ts +1 -0
  119. package/lib/userName.js +3 -2
  120. package/lib/userStats.d.ts +18 -0
  121. package/lib/userStats.js +49 -0
  122. package/lib/userStatsConfig.d.ts +14 -0
  123. package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
  124. package/lib/util/getTokenAddress.d.ts +2 -0
  125. package/lib/util/getTokenAddress.js +9 -0
  126. package/package.json +12 -5
  127. package/src/accounts/bulkAccountLoader.ts +44 -34
  128. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  129. package/src/accounts/bulkUserSubscription.ts +2 -3
  130. package/src/accounts/fetch.ts +27 -2
  131. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
  132. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  133. package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
  134. package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
  135. package/src/accounts/types.ts +35 -15
  136. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  137. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  138. package/src/addresses/marketAddresses.ts +3 -4
  139. package/src/addresses/pda.ts +105 -33
  140. package/src/adminClient.ts +1207 -0
  141. package/src/config.ts +41 -34
  142. package/src/constants/numericConstants.ts +59 -26
  143. package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
  144. package/src/constants/spotMarkets.ts +83 -0
  145. package/src/dlob/DLOB.ts +1120 -0
  146. package/src/dlob/DLOBNode.ts +155 -0
  147. package/src/dlob/NodeList.ts +195 -0
  148. package/src/driftClient.ts +3594 -0
  149. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  150. package/src/events/eventList.ts +3 -0
  151. package/src/events/eventSubscriber.ts +36 -14
  152. package/src/events/fetchLogs.ts +60 -15
  153. package/src/events/pollingLogProvider.ts +11 -3
  154. package/src/events/sort.ts +11 -15
  155. package/src/events/types.ts +27 -2
  156. package/src/events/webSocketLogProvider.ts +1 -1
  157. package/src/examples/makeTradeExample.js +152 -75
  158. package/src/examples/makeTradeExample.ts +44 -28
  159. package/src/factory/bigNum.ts +150 -22
  160. package/src/idl/drift.json +8392 -0
  161. package/src/idl/pyth.json +98 -2
  162. package/src/index.ts +29 -12
  163. package/src/math/amm.ts +161 -48
  164. package/src/math/conversion.ts +2 -2
  165. package/src/math/exchangeStatus.ts +31 -0
  166. package/src/math/funding.ts +41 -31
  167. package/src/math/insurance.ts +35 -0
  168. package/src/math/margin.ts +133 -0
  169. package/src/math/market.ts +143 -14
  170. package/src/math/oracles.ts +63 -9
  171. package/src/math/orders.ts +168 -26
  172. package/src/math/position.ts +133 -59
  173. package/src/math/repeg.ts +19 -9
  174. package/src/math/spotBalance.ts +319 -0
  175. package/src/math/spotMarket.ts +9 -0
  176. package/src/math/spotPosition.ts +47 -0
  177. package/src/math/trade.ts +33 -37
  178. package/src/oracles/pythClient.ts +2 -2
  179. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  180. package/src/oracles/switchboardClient.ts +2 -2
  181. package/src/orderParams.ts +16 -8
  182. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  183. package/src/serum/serumSubscriber.ts +99 -0
  184. package/src/serum/types.ts +13 -0
  185. package/src/tokenFaucet.ts +38 -15
  186. package/src/tx/retryTxSender.ts +16 -5
  187. package/src/tx/types.ts +2 -1
  188. package/src/types.ts +594 -195
  189. package/src/user.ts +1599 -0
  190. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  191. package/src/userMap/userMap.ts +124 -0
  192. package/src/userMap/userStatsMap.ts +108 -0
  193. package/src/userName.ts +2 -1
  194. package/src/userStats.ts +75 -0
  195. package/src/userStatsConfig.ts +18 -0
  196. package/src/util/getTokenAddress.ts +18 -0
  197. package/tests/bn/test.ts +46 -11
  198. package/tests/dlob/helpers.ts +619 -0
  199. package/tests/dlob/test.ts +4586 -0
  200. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  201. package/lib/admin.d.ts +0 -44
  202. package/lib/admin.js +0 -438
  203. package/lib/clearingHouse.d.ts +0 -146
  204. package/lib/clearingHouse.js +0 -1154
  205. package/lib/clearingHouseUser.d.ts +0 -187
  206. package/lib/clearingHouseUser.js +0 -634
  207. package/lib/clearingHouseUserConfig.d.ts +0 -14
  208. package/lib/constants/banks.d.ts +0 -16
  209. package/lib/constants/banks.js +0 -41
  210. package/lib/constants/markets.d.ts +0 -19
  211. package/lib/idl/clearing_house.json +0 -4464
  212. package/lib/math/bankBalance.d.ts +0 -9
  213. package/lib/math/bankBalance.js +0 -75
  214. package/lib/math/state.d.ts +0 -8
  215. package/lib/math/state.js +0 -15
  216. package/lib/orders.d.ts +0 -8
  217. package/lib/orders.js +0 -134
  218. package/src/accounts/bulkAccountLoader.js +0 -197
  219. package/src/accounts/bulkUserSubscription.js +0 -33
  220. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  221. package/src/accounts/pollingOracleSubscriber.js +0 -93
  222. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  223. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  224. package/src/accounts/types.js +0 -10
  225. package/src/accounts/utils.js +0 -7
  226. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  227. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  228. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  229. package/src/addresses/marketAddresses.js +0 -26
  230. package/src/admin.js +0 -517
  231. package/src/admin.ts +0 -730
  232. package/src/clearingHouse.ts +0 -1828
  233. package/src/clearingHouseUser.ts +0 -978
  234. package/src/clearingHouseUserConfig.js +0 -2
  235. package/src/config.js +0 -67
  236. package/src/constants/banks.js +0 -42
  237. package/src/constants/banks.ts +0 -50
  238. package/src/constants/markets.js +0 -42
  239. package/src/constants/numericConstants.js +0 -41
  240. package/src/events/eventSubscriber.js +0 -139
  241. package/src/events/fetchLogs.js +0 -50
  242. package/src/events/pollingLogProvider.js +0 -64
  243. package/src/events/sort.js +0 -44
  244. package/src/events/txEventCache.js +0 -71
  245. package/src/events/types.js +0 -20
  246. package/src/events/webSocketLogProvider.js +0 -41
  247. package/src/factory/bigNum.js +0 -390
  248. package/src/factory/oracleClient.js +0 -20
  249. package/src/idl/clearing_house.json +0 -4464
  250. package/src/index.js +0 -69
  251. package/src/math/amm.js +0 -369
  252. package/src/math/auction.js +0 -42
  253. package/src/math/bankBalance.ts +0 -112
  254. package/src/math/conversion.js +0 -11
  255. package/src/math/funding.js +0 -248
  256. package/src/math/oracles.js +0 -26
  257. package/src/math/repeg.js +0 -128
  258. package/src/math/state.js +0 -15
  259. package/src/math/state.ts +0 -14
  260. package/src/math/trade.js +0 -253
  261. package/src/math/utils.js +0 -26
  262. package/src/math/utils.js.map +0 -1
  263. package/src/mockUSDCFaucet.js +0 -280
  264. package/src/oracles/oracleClientCache.js +0 -19
  265. package/src/oracles/pythClient.js +0 -46
  266. package/src/oracles/quoteAssetOracleClient.js +0 -32
  267. package/src/oracles/switchboardClient.js +0 -69
  268. package/src/orderParams.js +0 -20
  269. package/src/orders.ts +0 -245
  270. package/src/slot/SlotSubscriber.js +0 -39
  271. package/src/tokenFaucet.js +0 -189
  272. package/src/types.js +0 -125
  273. package/src/userName.js +0 -20
  274. package/src/wallet.js +0 -35
@@ -0,0 +1,694 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DLOB = void 0;
4
+ const NodeList_1 = require("./NodeList");
5
+ const __1 = require("..");
6
+ const exchangeStatus_1 = require("../math/exchangeStatus");
7
+ class DLOB {
8
+ /**
9
+ *
10
+ * @param perpMarkets The perp markets to maintain a DLOB for
11
+ * @param spotMarkets The spot markets to maintain a DLOB for
12
+ * @param userMap map of all users
13
+ * @param silent set to true to prevent logging on inserts and removals
14
+ */
15
+ constructor(perpMarkets, spotMarkets, stateAccount, userMap, silent) {
16
+ this.openOrders = new Map();
17
+ this.orderLists = new Map();
18
+ this.marketIndexToAccount = new Map();
19
+ this.silent = false;
20
+ this.initialized = false;
21
+ this.stateAccount = stateAccount;
22
+ this.userMap = userMap;
23
+ this.silent = silent;
24
+ this.openOrders.set('perp', new Set());
25
+ this.openOrders.set('spot', new Set());
26
+ this.orderLists.set('perp', new Map());
27
+ this.orderLists.set('spot', new Map());
28
+ this.marketIndexToAccount.set('perp', new Map());
29
+ this.marketIndexToAccount.set('spot', new Map());
30
+ for (const market of perpMarkets) {
31
+ const marketIndex = market.marketIndex;
32
+ this.marketIndexToAccount.get('perp').set(marketIndex, market);
33
+ this.orderLists.get('perp').set(marketIndex, {
34
+ limit: {
35
+ ask: new NodeList_1.NodeList('limit', 'asc'),
36
+ bid: new NodeList_1.NodeList('limit', 'desc'),
37
+ },
38
+ floatingLimit: {
39
+ ask: new NodeList_1.NodeList('floatingLimit', 'asc'),
40
+ bid: new NodeList_1.NodeList('floatingLimit', 'desc'),
41
+ },
42
+ market: {
43
+ ask: new NodeList_1.NodeList('market', 'asc'),
44
+ bid: new NodeList_1.NodeList('market', 'asc'), // always sort ascending for market orders
45
+ },
46
+ trigger: {
47
+ above: new NodeList_1.NodeList('trigger', 'asc'),
48
+ below: new NodeList_1.NodeList('trigger', 'desc'),
49
+ },
50
+ });
51
+ }
52
+ for (const market of spotMarkets) {
53
+ const marketIndex = market.marketIndex;
54
+ this.marketIndexToAccount.get('spot').set(marketIndex, market);
55
+ this.orderLists.get('spot').set(marketIndex, {
56
+ limit: {
57
+ ask: new NodeList_1.NodeList('limit', 'asc'),
58
+ bid: new NodeList_1.NodeList('limit', 'desc'),
59
+ },
60
+ floatingLimit: {
61
+ ask: new NodeList_1.NodeList('floatingLimit', 'asc'),
62
+ bid: new NodeList_1.NodeList('floatingLimit', 'desc'),
63
+ },
64
+ market: {
65
+ ask: new NodeList_1.NodeList('market', 'asc'),
66
+ bid: new NodeList_1.NodeList('market', 'asc'), // always sort ascending for market orders
67
+ },
68
+ trigger: {
69
+ above: new NodeList_1.NodeList('trigger', 'asc'),
70
+ below: new NodeList_1.NodeList('trigger', 'desc'),
71
+ },
72
+ });
73
+ }
74
+ }
75
+ clear() {
76
+ for (const marketType of this.openOrders.keys()) {
77
+ this.openOrders.get(marketType).clear();
78
+ }
79
+ this.openOrders.clear();
80
+ for (const marketType of this.orderLists.keys()) {
81
+ for (const marketIndex of this.orderLists.get(marketType).keys()) {
82
+ const marketNodeLists = this.orderLists
83
+ .get(marketType)
84
+ .get(marketIndex);
85
+ for (const side of Object.keys(marketNodeLists)) {
86
+ for (const orderType of Object.keys(marketNodeLists[side])) {
87
+ marketNodeLists[side][orderType].clear();
88
+ }
89
+ }
90
+ }
91
+ }
92
+ this.orderLists.clear();
93
+ for (const marketType of this.marketIndexToAccount.keys()) {
94
+ this.marketIndexToAccount.get(marketType).clear();
95
+ }
96
+ this.marketIndexToAccount.clear();
97
+ }
98
+ /**
99
+ * initializes a new DLOB instance
100
+ *
101
+ * @returns a promise that resolves when the DLOB is initialized
102
+ */
103
+ async init() {
104
+ if (this.initialized) {
105
+ return false;
106
+ }
107
+ // initialize the dlob with the user map (prevents hitting getProgramAccounts)
108
+ for (const user of this.userMap.values()) {
109
+ const userAccount = user.getUserAccount();
110
+ const userAccountPubkey = user.getUserAccountPublicKey();
111
+ for (const order of userAccount.orders) {
112
+ this.insertOrder(order, userAccountPubkey);
113
+ }
114
+ }
115
+ this.initialized = true;
116
+ return true;
117
+ }
118
+ insertOrder(order, userAccount, onInsert) {
119
+ var _a;
120
+ if ((0, __1.isVariant)(order.status, 'init')) {
121
+ return;
122
+ }
123
+ const marketType = (0, __1.getVariant)(order.marketType);
124
+ if ((0, __1.isVariant)(order.status, 'open')) {
125
+ this.openOrders
126
+ .get(marketType)
127
+ .add((0, NodeList_1.getOrderSignature)(order.orderId, userAccount));
128
+ }
129
+ (_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, this.marketIndexToAccount.get(marketType).get(order.marketIndex), userAccount);
130
+ if (onInsert) {
131
+ onInsert();
132
+ }
133
+ }
134
+ trigger(order, userAccount, onTrigger) {
135
+ var _a;
136
+ if ((0, __1.isVariant)(order.status, 'init')) {
137
+ return;
138
+ }
139
+ const marketType = (0, __1.getVariant)(order.marketType);
140
+ const triggerList = this.orderLists.get(marketType).get(order.marketIndex)
141
+ .trigger[(0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below'];
142
+ triggerList.remove(order, userAccount);
143
+ (_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, this.marketIndexToAccount.get(marketType).get(order.marketIndex), userAccount);
144
+ if (onTrigger) {
145
+ onTrigger();
146
+ }
147
+ }
148
+ getListForOrder(order) {
149
+ const isInactiveTriggerOrder = (0, __1.mustBeTriggered)(order) && !(0, __1.isTriggered)(order);
150
+ let type;
151
+ if (isInactiveTriggerOrder) {
152
+ type = 'trigger';
153
+ }
154
+ else if ((0, __1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket'])) {
155
+ type = 'market';
156
+ }
157
+ else if (order.oraclePriceOffset !== 0) {
158
+ type = 'floatingLimit';
159
+ }
160
+ else {
161
+ type = 'limit';
162
+ }
163
+ let subType;
164
+ if (isInactiveTriggerOrder) {
165
+ subType = (0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below';
166
+ }
167
+ else {
168
+ subType = (0, __1.isVariant)(order.direction, 'long') ? 'bid' : 'ask';
169
+ }
170
+ const marketType = (0, __1.getVariant)(order.marketType);
171
+ if (!this.orderLists.has(marketType)) {
172
+ return undefined;
173
+ }
174
+ return this.orderLists.get(marketType).get(order.marketIndex)[type][subType];
175
+ }
176
+ findNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, ts, marketType, oraclePriceData) {
177
+ const marketAccount = this.marketIndexToAccount
178
+ .get((0, __1.getVariant)(marketType))
179
+ .get(marketIndex);
180
+ if ((0, exchangeStatus_1.fillPaused)(this.stateAccount, marketAccount)) {
181
+ return [];
182
+ }
183
+ // Find all the crossing nodes
184
+ const crossingNodesToFill = this.findCrossingNodesToFill(marketIndex, slot, marketType, oraclePriceData);
185
+ let fallbackCrossingNodesToFill = new Array();
186
+ if (!(0, exchangeStatus_1.ammPaused)(this.stateAccount, marketAccount)) {
187
+ fallbackCrossingNodesToFill = this.findFallbackCrossingNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, marketType, oraclePriceData);
188
+ }
189
+ // get expired market nodes
190
+ const expiredNodesToFill = this.findExpiredNodesToFill(marketIndex, ts, marketType);
191
+ return crossingNodesToFill.concat(fallbackCrossingNodesToFill, expiredNodesToFill);
192
+ }
193
+ findCrossingNodesToFill(marketIndex, slot, marketType, oraclePriceData) {
194
+ const nodesToFill = new Array();
195
+ const askGenerator = this.getAsks(marketIndex, undefined, // dont include vask
196
+ slot, marketType, oraclePriceData);
197
+ const bidGenerator = this.getBids(marketIndex, undefined, // dont include vbid
198
+ slot, marketType, oraclePriceData);
199
+ let nextAsk = askGenerator.next();
200
+ let nextBid = bidGenerator.next();
201
+ // First try to find orders that cross
202
+ while (!nextAsk.done && !nextBid.done) {
203
+ const { crossingNodes, exhaustedSide } = this.findCrossingOrders(nextAsk.value, nextBid.value, oraclePriceData, slot);
204
+ if (exhaustedSide === 'bid') {
205
+ nextBid = bidGenerator.next();
206
+ }
207
+ else if (exhaustedSide === 'ask') {
208
+ nextAsk = askGenerator.next();
209
+ }
210
+ else if (exhaustedSide === 'both') {
211
+ nextBid = bidGenerator.next();
212
+ nextAsk = askGenerator.next();
213
+ }
214
+ else if (exhaustedSide === 'nocross') {
215
+ break;
216
+ }
217
+ else {
218
+ console.error(`invalid exhaustedSide: ${exhaustedSide}`);
219
+ break;
220
+ }
221
+ for (const crossingNode of crossingNodes) {
222
+ nodesToFill.push(crossingNode);
223
+ }
224
+ }
225
+ return nodesToFill;
226
+ }
227
+ findFallbackCrossingNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, marketType, oraclePriceData) {
228
+ var _a, _b;
229
+ const nodesToFill = new Array();
230
+ const askGenerator = this.getAsks(marketIndex, undefined, // dont include vask
231
+ slot, marketType, oraclePriceData);
232
+ const bidGenerator = this.getBids(marketIndex, undefined, // dont include vbid
233
+ slot, marketType, oraclePriceData);
234
+ let nextAsk = askGenerator.next();
235
+ let nextBid = bidGenerator.next();
236
+ // check for asks that cross fallbackBid
237
+ while (!nextAsk.done && fallbackBid !== undefined) {
238
+ const askNode = nextAsk.value;
239
+ if ((0, __1.isVariant)(marketType, 'spot') && ((_a = askNode.order) === null || _a === void 0 ? void 0 : _a.postOnly)) {
240
+ nextAsk = askGenerator.next();
241
+ continue;
242
+ }
243
+ const askLimitPrice = (0, __1.getOptionalLimitPrice)(askNode.order, oraclePriceData, slot);
244
+ // order crosses if there is no limit price or it crosses fallback price
245
+ const crosses = askLimitPrice === undefined || askLimitPrice.lte(fallbackBid);
246
+ // fallback is available if auction is complete or it's a spot order
247
+ const fallbackAvailable = (0, __1.isVariant)(marketType, 'spot') || (0, __1.isAuctionComplete)(askNode.order, slot);
248
+ if (crosses && fallbackAvailable) {
249
+ nodesToFill.push({
250
+ node: askNode,
251
+ makerNode: undefined, // filled by fallback
252
+ });
253
+ }
254
+ nextAsk = askGenerator.next();
255
+ }
256
+ // check for bids that cross fallbackAsk
257
+ while (!nextBid.done && fallbackAsk !== undefined) {
258
+ const bidNode = nextBid.value;
259
+ if ((0, __1.isVariant)(marketType, 'spot') && ((_b = bidNode.order) === null || _b === void 0 ? void 0 : _b.postOnly)) {
260
+ nextBid = bidGenerator.next();
261
+ continue;
262
+ }
263
+ const bidLimitPrice = (0, __1.getOptionalLimitPrice)(bidNode.order, oraclePriceData, slot);
264
+ // order crosses if there is no limit price or it crosses fallback price
265
+ const crosses = bidLimitPrice === undefined || bidLimitPrice.gte(fallbackAsk);
266
+ // fallback is available if auction is complete or it's a spot order
267
+ const fallbackAvailable = (0, __1.isVariant)(marketType, 'spot') || (0, __1.isAuctionComplete)(bidNode.order, slot);
268
+ if (crosses && fallbackAvailable) {
269
+ nodesToFill.push({
270
+ node: bidNode,
271
+ makerNode: undefined, // filled by fallback
272
+ });
273
+ }
274
+ nextBid = bidGenerator.next();
275
+ }
276
+ return nodesToFill;
277
+ }
278
+ findExpiredNodesToFill(marketIndex, ts, marketType) {
279
+ const nodesToFill = new Array();
280
+ const marketTypeStr = (0, __1.getVariant)(marketType);
281
+ const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
282
+ // All bids/asks that can expire
283
+ const bidGenerators = [
284
+ nodeLists.limit.bid.getGenerator(),
285
+ nodeLists.floatingLimit.bid.getGenerator(),
286
+ nodeLists.market.bid.getGenerator(),
287
+ ];
288
+ const askGenerators = [
289
+ nodeLists.limit.ask.getGenerator(),
290
+ nodeLists.floatingLimit.ask.getGenerator(),
291
+ nodeLists.market.ask.getGenerator(),
292
+ ];
293
+ for (const bidGenerator of bidGenerators) {
294
+ for (const bid of bidGenerator) {
295
+ if ((0, __1.isOrderExpired)(bid.order, ts)) {
296
+ nodesToFill.push({
297
+ node: bid,
298
+ });
299
+ }
300
+ }
301
+ }
302
+ for (const askGenerator of askGenerators) {
303
+ for (const ask of askGenerator) {
304
+ if ((0, __1.isOrderExpired)(ask.order, ts)) {
305
+ nodesToFill.push({
306
+ node: ask,
307
+ });
308
+ }
309
+ }
310
+ }
311
+ return nodesToFill;
312
+ }
313
+ findJitAuctionNodesToFill(marketIndex, slot, marketType) {
314
+ const nodesToFill = new Array();
315
+ // Then see if there are orders still in JIT auction
316
+ for (const marketBid of this.getMarketBids(marketIndex, marketType)) {
317
+ if (!(0, __1.isAuctionComplete)(marketBid.order, slot)) {
318
+ nodesToFill.push({
319
+ node: marketBid,
320
+ });
321
+ }
322
+ }
323
+ for (const marketAsk of this.getMarketAsks(marketIndex, marketType)) {
324
+ if (!(0, __1.isAuctionComplete)(marketAsk.order, slot)) {
325
+ nodesToFill.push({
326
+ node: marketAsk,
327
+ });
328
+ }
329
+ }
330
+ return nodesToFill;
331
+ }
332
+ getMarketBids(marketIndex, marketType) {
333
+ const marketTypeStr = (0, __1.getVariant)(marketType);
334
+ return this.orderLists
335
+ .get(marketTypeStr)
336
+ .get(marketIndex)
337
+ .market.bid.getGenerator();
338
+ }
339
+ getMarketAsks(marketIndex, marketType) {
340
+ const marketTypeStr = (0, __1.getVariant)(marketType);
341
+ return this.orderLists
342
+ .get(marketTypeStr)
343
+ .get(marketIndex)
344
+ .market.ask.getGenerator();
345
+ }
346
+ *getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
347
+ if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
348
+ throw new Error('Must provide OraclePriceData to get spot asks');
349
+ }
350
+ const marketTypeStr = (0, __1.getVariant)(marketType);
351
+ const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
352
+ const generatorList = [
353
+ nodeLists.limit.ask.getGenerator(),
354
+ nodeLists.floatingLimit.ask.getGenerator(),
355
+ nodeLists.market.ask.getGenerator(),
356
+ ];
357
+ if (marketTypeStr === 'perp' && fallbackAsk) {
358
+ generatorList.push((0, NodeList_1.getVammNodeGenerator)(fallbackAsk));
359
+ }
360
+ if (generatorList.length === 0) {
361
+ throw new Error('No ask generators found');
362
+ }
363
+ const askGenerators = generatorList.map((generator) => {
364
+ return {
365
+ next: generator.next(),
366
+ generator,
367
+ };
368
+ });
369
+ let asksExhausted = false;
370
+ while (!asksExhausted) {
371
+ const bestGenerator = askGenerators.reduce((bestGenerator, currentGenerator) => {
372
+ if (currentGenerator.next.done) {
373
+ return bestGenerator;
374
+ }
375
+ if (bestGenerator.next.done) {
376
+ return currentGenerator;
377
+ }
378
+ const bestAskPrice = bestGenerator.next.value.getPrice(oraclePriceData, slot);
379
+ const currentAskPrice = currentGenerator.next.value.getPrice(oraclePriceData, slot);
380
+ return bestAskPrice.lt(currentAskPrice)
381
+ ? bestGenerator
382
+ : currentGenerator;
383
+ });
384
+ if (!bestGenerator.next.done) {
385
+ // skip this node if it's already completely filled
386
+ if (bestGenerator.next.value.isBaseFilled()) {
387
+ bestGenerator.next = bestGenerator.generator.next();
388
+ continue;
389
+ }
390
+ // skip order if user is being liquidated/bankrupt
391
+ if (bestGenerator.next.value.userAccount !== undefined) {
392
+ const user = this.userMap.get(bestGenerator.next.value.userAccount.toString());
393
+ if (user === null || user === void 0 ? void 0 : user.isBeingLiquidated()) {
394
+ bestGenerator.next = bestGenerator.generator.next();
395
+ continue;
396
+ }
397
+ }
398
+ yield bestGenerator.next.value;
399
+ bestGenerator.next = bestGenerator.generator.next();
400
+ }
401
+ else {
402
+ asksExhausted = true;
403
+ }
404
+ }
405
+ }
406
+ *getBids(marketIndex, fallbackBid, slot, marketType, oraclePriceData) {
407
+ if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
408
+ throw new Error('Must provide OraclePriceData to get spot bids');
409
+ }
410
+ const marketTypeStr = (0, __1.getVariant)(marketType);
411
+ const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
412
+ const generatorList = [
413
+ nodeLists.limit.bid.getGenerator(),
414
+ nodeLists.floatingLimit.bid.getGenerator(),
415
+ nodeLists.market.bid.getGenerator(),
416
+ ];
417
+ if (marketTypeStr === 'perp' && fallbackBid) {
418
+ generatorList.push((0, NodeList_1.getVammNodeGenerator)(fallbackBid));
419
+ }
420
+ if (generatorList.length === 0) {
421
+ throw new Error('No bid generators found');
422
+ }
423
+ const bidGenerators = generatorList.map((generator) => {
424
+ return {
425
+ next: generator.next(),
426
+ generator,
427
+ };
428
+ });
429
+ let bidsExhausted = false; // there will always be the fallbackBid
430
+ while (!bidsExhausted) {
431
+ const bestGenerator = bidGenerators.reduce((bestGenerator, currentGenerator) => {
432
+ if (currentGenerator.next.done) {
433
+ return bestGenerator;
434
+ }
435
+ if (bestGenerator.next.done) {
436
+ return currentGenerator;
437
+ }
438
+ const bestBidPrice = bestGenerator.next.value.getPrice(oraclePriceData, slot);
439
+ const currentBidPrice = currentGenerator.next.value.getPrice(oraclePriceData, slot);
440
+ return bestBidPrice.gt(currentBidPrice)
441
+ ? bestGenerator
442
+ : currentGenerator;
443
+ });
444
+ if (!bestGenerator.next.done) {
445
+ // skip this node if it's already completely filled
446
+ if (bestGenerator.next.value.isBaseFilled()) {
447
+ bestGenerator.next = bestGenerator.generator.next();
448
+ continue;
449
+ }
450
+ // skip order if user is being liquidated/bankrupt
451
+ if (bestGenerator.next.value.userAccount !== undefined) {
452
+ const user = this.userMap.get(bestGenerator.next.value.userAccount.toString());
453
+ if (user === null || user === void 0 ? void 0 : user.isBeingLiquidated()) {
454
+ bestGenerator.next = bestGenerator.generator.next();
455
+ continue;
456
+ }
457
+ }
458
+ yield bestGenerator.next.value;
459
+ bestGenerator.next = bestGenerator.generator.next();
460
+ }
461
+ else {
462
+ bidsExhausted = true;
463
+ }
464
+ }
465
+ }
466
+ findCrossingOrders(askNode, bidNode, oraclePriceData, slot) {
467
+ const bidPrice = bidNode.getPrice(oraclePriceData, slot);
468
+ const askPrice = askNode.getPrice(oraclePriceData, slot);
469
+ // orders don't cross - we're done walkin gup the book
470
+ if (bidPrice.lt(askPrice)) {
471
+ return {
472
+ crossingNodes: [],
473
+ exhaustedSide: 'nocross',
474
+ };
475
+ }
476
+ const bidOrder = bidNode.order;
477
+ const askOrder = askNode.order;
478
+ const bidUserAuthority = this.userMap.getUserAuthority(bidNode.userAccount.toString());
479
+ const askUserAuthority = this.userMap.getUserAuthority(askNode.userAccount.toString());
480
+ // Can't match orders from the same authority
481
+ const sameAuthority = bidUserAuthority.equals(askUserAuthority);
482
+ if (sameAuthority || (bidOrder.postOnly && askOrder.postOnly)) {
483
+ // don't have a principle way to pick which one to exhaust,
484
+ // exhaust each one 50% of the time so we can try each one against other orders
485
+ const exhaustedSide = Math.random() < 0.5 ? 'bid' : 'ask';
486
+ return {
487
+ crossingNodes: [],
488
+ exhaustedSide,
489
+ };
490
+ }
491
+ const { takerNode, makerNode, makerSide } = this.determineMakerAndTaker(askNode, bidNode);
492
+ // If maker is market order and auction is complete, cant be maker
493
+ if ((0, __1.isMarketOrder)(makerNode.order) &&
494
+ (0, __1.isAuctionComplete)(makerNode.order, slot)) {
495
+ return {
496
+ crossingNodes: [],
497
+ exhaustedSide: makerSide,
498
+ };
499
+ }
500
+ const bidBaseRemaining = bidOrder.baseAssetAmount.sub(bidOrder.baseAssetAmountFilled);
501
+ const askBaseRemaining = askOrder.baseAssetAmount.sub(askOrder.baseAssetAmountFilled);
502
+ let exhaustedSide;
503
+ if (bidBaseRemaining.eq(askBaseRemaining)) {
504
+ exhaustedSide = 'both';
505
+ }
506
+ else if (bidBaseRemaining.gt(askBaseRemaining)) {
507
+ exhaustedSide = 'ask';
508
+ }
509
+ else {
510
+ exhaustedSide = 'bid';
511
+ }
512
+ // update the orders on DLOB as if they were fill - so we don't try to match them on the next iteration
513
+ // NOTE: if something goes wrong during the actual fill (transaction fails, i.e. due to orders already being filled)
514
+ // then we risk having a mismatch between this local DLOB and the actual DLOB state on the blockchain. This isn't
515
+ // a problem in the current implementation because we construct a new DLOB from the blockchain state every time, rather
516
+ // than updating the existing DLOB based on events.
517
+ if (exhaustedSide === 'ask') {
518
+ // bid partially filled
519
+ const newBidOrder = { ...bidOrder };
520
+ newBidOrder.baseAssetAmountFilled =
521
+ bidOrder.baseAssetAmountFilled.add(askBaseRemaining);
522
+ this.getListForOrder(newBidOrder).update(newBidOrder, bidNode.userAccount);
523
+ // ask completely filled
524
+ const newAskOrder = { ...askOrder };
525
+ newAskOrder.baseAssetAmountFilled = askOrder.baseAssetAmount;
526
+ this.getListForOrder(newAskOrder).update(newAskOrder, askNode.userAccount);
527
+ }
528
+ else if (exhaustedSide === 'bid') {
529
+ // ask partially filled
530
+ const newAskOrder = { ...askOrder };
531
+ newAskOrder.baseAssetAmountFilled =
532
+ askOrder.baseAssetAmountFilled.add(bidBaseRemaining);
533
+ this.getListForOrder(newAskOrder).update(newAskOrder, askNode.userAccount);
534
+ // bid completely filled
535
+ const newBidOrder = { ...bidOrder };
536
+ newBidOrder.baseAssetAmountFilled = bidOrder.baseAssetAmount;
537
+ this.getListForOrder(newBidOrder).update(newBidOrder, bidNode.userAccount);
538
+ }
539
+ else {
540
+ // both completely filled
541
+ const newBidOrder = { ...bidOrder };
542
+ newBidOrder.baseAssetAmountFilled = bidOrder.baseAssetAmount;
543
+ this.getListForOrder(newBidOrder).update(newBidOrder, bidNode.userAccount);
544
+ const newAskOrder = { ...askOrder };
545
+ newAskOrder.baseAssetAmountFilled = askOrder.baseAssetAmount;
546
+ this.getListForOrder(newAskOrder).update(newAskOrder, askNode.userAccount);
547
+ }
548
+ return {
549
+ crossingNodes: [
550
+ {
551
+ node: takerNode,
552
+ makerNode: makerNode,
553
+ },
554
+ ],
555
+ exhaustedSide,
556
+ };
557
+ }
558
+ determineMakerAndTaker(askNode, bidNode) {
559
+ if (bidNode.order.postOnly) {
560
+ return {
561
+ takerNode: askNode,
562
+ makerNode: bidNode,
563
+ makerSide: 'bid',
564
+ };
565
+ }
566
+ else if (askNode.order.postOnly) {
567
+ return {
568
+ takerNode: bidNode,
569
+ makerNode: askNode,
570
+ makerSide: 'ask',
571
+ };
572
+ }
573
+ else if ((0, __1.isMarketOrder)(bidNode.order) && (0, __1.isLimitOrder)(askNode.order)) {
574
+ return {
575
+ takerNode: bidNode,
576
+ makerNode: askNode,
577
+ makerSide: 'ask',
578
+ };
579
+ }
580
+ else if ((0, __1.isMarketOrder)(askNode.order) && (0, __1.isLimitOrder)(bidNode.order)) {
581
+ return {
582
+ takerNode: askNode,
583
+ makerNode: bidNode,
584
+ makerSide: 'bid',
585
+ };
586
+ }
587
+ else if (askNode.order.slot.lt(bidNode.order.slot)) {
588
+ return {
589
+ takerNode: bidNode,
590
+ makerNode: askNode,
591
+ makerSide: 'ask',
592
+ };
593
+ }
594
+ else {
595
+ return {
596
+ takerNode: askNode,
597
+ makerNode: bidNode,
598
+ makerSide: 'bid',
599
+ };
600
+ }
601
+ }
602
+ getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
603
+ return this.getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData)
604
+ .next()
605
+ .value.getPrice(oraclePriceData, slot);
606
+ }
607
+ getBestBid(marketIndex, fallbackBid, slot, marketType, oraclePriceData) {
608
+ return this.getBids(marketIndex, fallbackBid, slot, marketType, oraclePriceData)
609
+ .next()
610
+ .value.getPrice(oraclePriceData, slot);
611
+ }
612
+ findNodesToTrigger(marketIndex, slot, oraclePrice, marketType) {
613
+ if ((0, exchangeStatus_1.exchangePaused)(this.stateAccount)) {
614
+ return [];
615
+ }
616
+ const nodesToTrigger = [];
617
+ const marketTypeStr = (0, __1.getVariant)(marketType);
618
+ for (const node of this.orderLists
619
+ .get(marketTypeStr)
620
+ .get(marketIndex)
621
+ .trigger.above.getGenerator()) {
622
+ if (oraclePrice.gt(node.order.triggerPrice)) {
623
+ if ((0, __1.isAuctionComplete)(node.order, slot)) {
624
+ nodesToTrigger.push({
625
+ node: node,
626
+ });
627
+ }
628
+ }
629
+ else {
630
+ break;
631
+ }
632
+ }
633
+ for (const node of this.orderLists
634
+ .get(marketTypeStr)
635
+ .get(marketIndex)
636
+ .trigger.below.getGenerator()) {
637
+ if (oraclePrice.lt(node.order.triggerPrice)) {
638
+ if ((0, __1.isAuctionComplete)(node.order, slot)) {
639
+ nodesToTrigger.push({
640
+ node: node,
641
+ });
642
+ }
643
+ }
644
+ else {
645
+ break;
646
+ }
647
+ }
648
+ return nodesToTrigger;
649
+ }
650
+ printTopOfOrderLists(sdkConfig, driftClient, slotSubscriber, marketIndex, marketType) {
651
+ if ((0, __1.isVariant)(marketType, 'perp')) {
652
+ const market = driftClient.getPerpMarketAccount(marketIndex);
653
+ const slot = slotSubscriber.getSlot();
654
+ const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
655
+ const fallbackAsk = (0, __1.calculateAskPrice)(market, oraclePriceData);
656
+ const fallbackBid = (0, __1.calculateBidPrice)(market, oraclePriceData);
657
+ const bestAsk = this.getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData);
658
+ const bestBid = this.getBestBid(marketIndex, fallbackBid, slot, marketType, oraclePriceData);
659
+ const mid = bestAsk.add(bestBid).div(new __1.BN(2));
660
+ const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
661
+ (0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
662
+ 1) *
663
+ 100.0;
664
+ const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
665
+ (0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
666
+ 1) *
667
+ 100.0;
668
+ console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
669
+ console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
670
+ console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
671
+ console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
672
+ }
673
+ else if ((0, __1.isVariant)(marketType, 'spot')) {
674
+ const slot = slotSubscriber.getSlot();
675
+ const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
676
+ const bestAsk = this.getBestAsk(marketIndex, undefined, slot, marketType, oraclePriceData);
677
+ const bestBid = this.getBestBid(marketIndex, undefined, slot, marketType, oraclePriceData);
678
+ const mid = bestAsk.add(bestBid).div(new __1.BN(2));
679
+ const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
680
+ (0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
681
+ 1) *
682
+ 100.0;
683
+ const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
684
+ (0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
685
+ 1) *
686
+ 100.0;
687
+ console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
688
+ console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
689
+ console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
690
+ console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
691
+ }
692
+ }
693
+ }
694
+ exports.DLOB = DLOB;