@velocity-exchange/sdk 0.3.0 → 0.6.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 (1127) hide show
  1. package/README.md +3 -3
  2. package/lib/browser/accounts/basicUserAccountSubscriber.d.ts +18 -0
  3. package/lib/browser/accounts/basicUserAccountSubscriber.js +18 -0
  4. package/lib/browser/accounts/basicUserStatsAccountSubscriber.d.ts +18 -0
  5. package/lib/browser/accounts/basicUserStatsAccountSubscriber.js +18 -0
  6. package/lib/browser/accounts/bulkAccountLoader.d.ts +52 -0
  7. package/lib/browser/accounts/bulkAccountLoader.js +51 -0
  8. package/lib/browser/accounts/bulkUserStatsSubscription.d.ts +9 -2
  9. package/lib/browser/accounts/bulkUserStatsSubscription.js +9 -2
  10. package/lib/browser/accounts/bulkUserSubscription.d.ts +8 -2
  11. package/lib/browser/accounts/bulkUserSubscription.js +8 -2
  12. package/lib/browser/accounts/customizedCadenceBulkAccountLoader.d.ts +37 -0
  13. package/lib/browser/accounts/customizedCadenceBulkAccountLoader.js +37 -0
  14. package/lib/browser/accounts/fetch.d.ts +59 -2
  15. package/lib/browser/accounts/fetch.js +57 -2
  16. package/lib/browser/accounts/grpcAccountSubscriber.d.ts +30 -0
  17. package/lib/browser/accounts/grpcAccountSubscriber.js +30 -0
  18. package/lib/browser/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +16 -0
  19. package/lib/browser/accounts/grpcInsuranceFundStakeAccountSubscriber.js +16 -0
  20. package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +60 -1
  21. package/lib/browser/accounts/grpcMultiAccountSubscriber.js +118 -37
  22. package/lib/browser/accounts/grpcMultiUserAccountSubscriber.d.ts +32 -0
  23. package/lib/browser/accounts/grpcMultiUserAccountSubscriber.js +40 -12
  24. package/lib/browser/accounts/grpcProgramAccountSubscriber.d.ts +33 -0
  25. package/lib/browser/accounts/grpcProgramAccountSubscriber.js +33 -0
  26. package/lib/browser/accounts/grpcUserAccountSubscriber.d.ts +15 -0
  27. package/lib/browser/accounts/grpcUserAccountSubscriber.js +15 -0
  28. package/lib/browser/accounts/grpcUserStatsAccountSubscriber.d.ts +15 -0
  29. package/lib/browser/accounts/grpcUserStatsAccountSubscriber.js +15 -0
  30. package/lib/browser/accounts/grpcVelocityClientAccountSubscriber.d.ts +41 -0
  31. package/lib/browser/accounts/grpcVelocityClientAccountSubscriber.js +41 -0
  32. package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +116 -0
  33. package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.js +152 -1
  34. package/lib/browser/accounts/laserProgramAccountSubscriber.d.ts +34 -0
  35. package/lib/browser/accounts/laserProgramAccountSubscriber.js +34 -0
  36. package/lib/browser/accounts/oneShotUserAccountSubscriber.d.ts +19 -0
  37. package/lib/browser/accounts/oneShotUserAccountSubscriber.js +19 -0
  38. package/lib/browser/accounts/oneShotUserStatsAccountSubscriber.d.ts +19 -0
  39. package/lib/browser/accounts/oneShotUserStatsAccountSubscriber.js +19 -0
  40. package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +31 -0
  41. package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.js +31 -0
  42. package/lib/browser/accounts/pollingOracleAccountSubscriber.d.ts +19 -0
  43. package/lib/browser/accounts/pollingOracleAccountSubscriber.js +21 -2
  44. package/lib/browser/accounts/pollingTokenAccountSubscriber.d.ts +22 -0
  45. package/lib/browser/accounts/pollingTokenAccountSubscriber.js +22 -0
  46. package/lib/browser/accounts/pollingUserAccountSubscriber.d.ts +32 -0
  47. package/lib/browser/accounts/pollingUserAccountSubscriber.js +32 -0
  48. package/lib/browser/accounts/pollingUserStatsAccountSubscriber.d.ts +22 -0
  49. package/lib/browser/accounts/pollingUserStatsAccountSubscriber.js +22 -0
  50. package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.d.ts +83 -0
  51. package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.js +84 -1
  52. package/lib/browser/accounts/testBulkAccountLoader.d.ts +8 -0
  53. package/lib/browser/accounts/testBulkAccountLoader.js +8 -0
  54. package/lib/browser/accounts/types.d.ts +149 -0
  55. package/lib/browser/accounts/types.js +13 -0
  56. package/lib/browser/accounts/utils.d.ts +18 -0
  57. package/lib/browser/accounts/utils.js +32 -16
  58. package/lib/browser/accounts/webSocketAccountSubscriber.d.ts +41 -0
  59. package/lib/browser/accounts/webSocketAccountSubscriber.js +41 -0
  60. package/lib/browser/accounts/webSocketAccountSubscriberV2.d.ts +31 -0
  61. package/lib/browser/accounts/webSocketAccountSubscriberV2.js +31 -23
  62. package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +27 -0
  63. package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +27 -0
  64. package/lib/browser/accounts/webSocketProgramAccountSubscriber.d.ts +36 -0
  65. package/lib/browser/accounts/webSocketProgramAccountSubscriber.js +36 -0
  66. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +21 -0
  67. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +25 -0
  68. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.d.ts +21 -0
  69. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.js +21 -0
  70. package/lib/browser/accounts/webSocketUserAccountSubscriber.d.ts +26 -0
  71. package/lib/browser/accounts/webSocketUserAccountSubscriber.js +26 -0
  72. package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  73. package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.js +20 -0
  74. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.d.ts +121 -0
  75. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.js +123 -2
  76. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +96 -0
  77. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.js +108 -0
  78. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.d.ts +28 -0
  79. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.js +33 -0
  80. package/lib/browser/addresses/marketAddresses.d.ts +10 -0
  81. package/lib/browser/addresses/marketAddresses.js +10 -0
  82. package/lib/browser/addresses/pda.d.ts +214 -2
  83. package/lib/browser/addresses/pda.js +217 -5
  84. package/lib/browser/adminClient.d.ts +2171 -4
  85. package/lib/browser/adminClient.js +2215 -4
  86. package/lib/browser/auctionSubscriber/auctionSubscriber.d.ts +15 -0
  87. package/lib/browser/auctionSubscriber/auctionSubscriber.js +15 -0
  88. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.d.ts +19 -0
  89. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +19 -0
  90. package/lib/browser/auctionSubscriber/index.d.ts +6 -0
  91. package/lib/browser/auctionSubscriber/index.js +6 -0
  92. package/lib/browser/auctionSubscriber/types.d.ts +7 -0
  93. package/lib/browser/blockhashSubscriber/BlockhashSubscriber.d.ts +33 -0
  94. package/lib/browser/blockhashSubscriber/BlockhashSubscriber.js +33 -0
  95. package/lib/browser/blockhashSubscriber/index.d.ts +5 -0
  96. package/lib/browser/blockhashSubscriber/index.js +5 -0
  97. package/lib/browser/blockhashSubscriber/types.d.ts +5 -0
  98. package/lib/browser/clock/clockSubscriber.d.ts +24 -0
  99. package/lib/browser/clock/clockSubscriber.js +22 -0
  100. package/lib/browser/config.d.ts +58 -4
  101. package/lib/browser/config.js +94 -28
  102. package/lib/browser/constants/numericConstants.d.ts +67 -0
  103. package/lib/browser/constants/numericConstants.js +68 -1
  104. package/lib/browser/constants/perpMarkets.d.ts +10 -0
  105. package/lib/browser/constants/perpMarkets.js +40 -908
  106. package/lib/browser/constants/spotMarkets.d.ts +13 -0
  107. package/lib/browser/constants/spotMarkets.js +16 -742
  108. package/lib/browser/constants/txConstants.d.ts +1 -0
  109. package/lib/browser/constants/txConstants.js +1 -0
  110. package/lib/browser/core/VelocityCore.d.ts +285 -8
  111. package/lib/browser/core/VelocityCore.js +281 -8
  112. package/lib/browser/core/instructions/deposit.d.ts +22 -0
  113. package/lib/browser/core/instructions/deposit.js +22 -0
  114. package/lib/browser/core/instructions/fill.d.ts +17 -0
  115. package/lib/browser/core/instructions/fill.js +17 -0
  116. package/lib/browser/core/instructions/funding.d.ts +12 -0
  117. package/lib/browser/core/instructions/funding.js +12 -0
  118. package/lib/browser/core/instructions/liquidation.d.ts +18 -0
  119. package/lib/browser/core/instructions/liquidation.js +18 -0
  120. package/lib/browser/core/instructions/orders.d.ts +28 -0
  121. package/lib/browser/core/instructions/orders.js +28 -0
  122. package/lib/browser/core/instructions/perpOrders.d.ts +106 -0
  123. package/lib/browser/core/instructions/perpOrders.js +106 -0
  124. package/lib/browser/core/instructions/settlement.d.ts +14 -0
  125. package/lib/browser/core/instructions/settlement.js +14 -0
  126. package/lib/browser/core/instructions/trigger.d.ts +15 -0
  127. package/lib/browser/core/instructions/trigger.js +15 -0
  128. package/lib/browser/core/instructions/withdraw.d.ts +21 -0
  129. package/lib/browser/core/instructions/withdraw.js +21 -0
  130. package/lib/browser/core/remainingAccounts.d.ts +47 -3
  131. package/lib/browser/core/remainingAccounts.js +20 -0
  132. package/lib/browser/core/signedMsg.d.ts +38 -0
  133. package/lib/browser/core/signedMsg.js +33 -0
  134. package/lib/browser/decode/customCoder.d.ts +39 -3
  135. package/lib/browser/decode/customCoder.js +45 -3
  136. package/lib/browser/decode/user.d.ts +22 -0
  137. package/lib/browser/decode/user.js +27 -1
  138. package/lib/browser/dlob/DLOB.d.ts +502 -53
  139. package/lib/browser/dlob/DLOB.js +540 -98
  140. package/lib/browser/dlob/DLOBNode.d.ts +86 -4
  141. package/lib/browser/dlob/DLOBNode.js +37 -4
  142. package/lib/browser/dlob/DLOBSubscriber.d.ts +39 -12
  143. package/lib/browser/dlob/DLOBSubscriber.js +42 -12
  144. package/lib/browser/dlob/NodeList.d.ts +69 -0
  145. package/lib/browser/dlob/NodeList.js +66 -0
  146. package/lib/browser/dlob/orderBookLevels.d.ts +106 -13
  147. package/lib/browser/dlob/orderBookLevels.js +84 -14
  148. package/lib/browser/dlob/types.d.ts +15 -0
  149. package/lib/browser/events/eventList.d.ts +14 -0
  150. package/lib/browser/events/eventList.js +14 -0
  151. package/lib/browser/events/eventSubscriber.d.ts +87 -10
  152. package/lib/browser/events/eventSubscriber.js +93 -8
  153. package/lib/browser/events/eventsServerLogProvider.d.ts +22 -0
  154. package/lib/browser/events/eventsServerLogProvider.js +27 -0
  155. package/lib/browser/events/fetchLogs.d.ts +31 -0
  156. package/lib/browser/events/fetchLogs.js +46 -1
  157. package/lib/browser/events/parse.d.ts +23 -0
  158. package/lib/browser/events/parse.js +23 -0
  159. package/lib/browser/events/pollingLogProvider.d.ts +24 -0
  160. package/lib/browser/events/pollingLogProvider.js +24 -0
  161. package/lib/browser/events/sort.d.ts +11 -0
  162. package/lib/browser/events/sort.js +12 -0
  163. package/lib/browser/events/txEventCache.d.ts +13 -3
  164. package/lib/browser/events/txEventCache.js +19 -7
  165. package/lib/browser/events/types.d.ts +118 -2
  166. package/lib/browser/events/types.js +11 -0
  167. package/lib/browser/events/webSocketLogProvider.d.ts +23 -0
  168. package/lib/browser/events/webSocketLogProvider.js +28 -0
  169. package/lib/browser/factory/oracleClient.js +2 -2
  170. package/lib/browser/idl/velocity.d.ts +150 -7
  171. package/lib/browser/idl/velocity.json +151 -8
  172. package/lib/browser/keypair.d.ts +8 -0
  173. package/lib/browser/keypair.js +8 -0
  174. package/lib/browser/marginCalculation.d.ts +134 -2
  175. package/lib/browser/marginCalculation.js +121 -0
  176. package/lib/browser/math/amm.d.ts +348 -29
  177. package/lib/browser/math/amm.js +310 -32
  178. package/lib/browser/math/auction.d.ts +95 -19
  179. package/lib/browser/math/auction.js +118 -28
  180. package/lib/browser/math/bankruptcy.d.ts +46 -0
  181. package/lib/browser/math/bankruptcy.js +89 -1
  182. package/lib/browser/math/builder.d.ts +64 -8
  183. package/lib/browser/math/builder.js +71 -9
  184. package/lib/browser/math/conversion.d.ts +21 -0
  185. package/lib/browser/math/conversion.js +21 -0
  186. package/lib/browser/math/exchangeStatus.d.ts +91 -0
  187. package/lib/browser/math/exchangeStatus.js +106 -3
  188. package/lib/browser/math/funding.d.ts +57 -20
  189. package/lib/browser/math/funding.js +63 -23
  190. package/lib/browser/math/insurance.d.ts +62 -0
  191. package/lib/browser/math/insurance.js +62 -0
  192. package/lib/browser/math/liquidation.d.ts +127 -11
  193. package/lib/browser/math/liquidation.js +182 -19
  194. package/lib/browser/math/margin.d.ts +79 -13
  195. package/lib/browser/math/margin.js +80 -14
  196. package/lib/browser/math/market.d.ts +135 -15
  197. package/lib/browser/math/market.js +145 -17
  198. package/lib/browser/math/oracles.d.ts +113 -0
  199. package/lib/browser/math/oracles.js +118 -1
  200. package/lib/browser/math/orders.d.ts +115 -10
  201. package/lib/browser/math/orders.js +130 -19
  202. package/lib/browser/math/position.d.ts +80 -33
  203. package/lib/browser/math/position.js +80 -33
  204. package/lib/browser/math/repeg.d.ts +48 -10
  205. package/lib/browser/math/repeg.js +48 -10
  206. package/lib/browser/math/spotBalance.d.ts +200 -5
  207. package/lib/browser/math/spotBalance.js +239 -10
  208. package/lib/browser/math/spotMarket.d.ts +36 -3
  209. package/lib/browser/math/spotMarket.js +36 -3
  210. package/lib/browser/math/spotPosition.d.ts +72 -0
  211. package/lib/browser/math/spotPosition.js +62 -0
  212. package/lib/browser/math/state.d.ts +31 -0
  213. package/lib/browser/math/state.js +31 -0
  214. package/lib/browser/math/superStake.d.ts +126 -2
  215. package/lib/browser/math/superStake.js +123 -3
  216. package/lib/browser/math/tiers.d.ts +29 -0
  217. package/lib/browser/math/tiers.js +29 -0
  218. package/lib/browser/math/trade.d.ts +102 -51
  219. package/lib/browser/math/trade.js +101 -55
  220. package/lib/browser/math/utils.d.ts +71 -9
  221. package/lib/browser/math/utils.js +71 -9
  222. package/lib/browser/memcmp.d.ts +94 -0
  223. package/lib/browser/memcmp.js +103 -2
  224. package/lib/browser/oracles/oracleClientCache.d.ts +16 -0
  225. package/lib/browser/oracles/oracleClientCache.js +16 -0
  226. package/lib/browser/oracles/oracleId.d.ts +31 -0
  227. package/lib/browser/oracles/oracleId.js +39 -8
  228. package/lib/browser/oracles/prelaunchOracleClient.d.ts +20 -0
  229. package/lib/browser/oracles/prelaunchOracleClient.js +20 -0
  230. package/lib/browser/oracles/pythClient.d.ts +31 -0
  231. package/lib/browser/oracles/pythClient.js +32 -1
  232. package/lib/browser/oracles/pythLazerClient.d.ts +34 -0
  233. package/lib/browser/oracles/pythLazerClient.js +35 -1
  234. package/lib/browser/oracles/quoteAssetOracleClient.d.ts +18 -0
  235. package/lib/browser/oracles/quoteAssetOracleClient.js +18 -0
  236. package/lib/browser/oracles/strictOraclePrice.d.ts +21 -0
  237. package/lib/browser/oracles/strictOraclePrice.js +21 -0
  238. package/lib/browser/oracles/types.d.ts +45 -0
  239. package/lib/browser/oracles/utils.d.ts +20 -0
  240. package/lib/browser/oracles/utils.js +20 -0
  241. package/lib/browser/orderParams.d.ts +42 -4
  242. package/lib/browser/orderParams.js +42 -4
  243. package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +56 -0
  244. package/lib/browser/orderSubscriber/OrderSubscriber.js +59 -0
  245. package/lib/browser/orderSubscriber/PollingSubscription.d.ts +11 -0
  246. package/lib/browser/orderSubscriber/PollingSubscription.js +11 -0
  247. package/lib/browser/orderSubscriber/WebsocketSubscription.d.ts +18 -0
  248. package/lib/browser/orderSubscriber/WebsocketSubscription.js +18 -0
  249. package/lib/browser/orderSubscriber/grpcSubscription.d.ts +16 -0
  250. package/lib/browser/orderSubscriber/grpcSubscription.js +16 -0
  251. package/lib/browser/orderSubscriber/types.d.ts +9 -0
  252. package/lib/browser/priorityFee/averageOverSlotsStrategy.d.ts +1 -0
  253. package/lib/browser/priorityFee/averageOverSlotsStrategy.js +1 -0
  254. package/lib/browser/priorityFee/averageStrategy.d.ts +1 -0
  255. package/lib/browser/priorityFee/averageStrategy.js +1 -0
  256. package/lib/browser/priorityFee/ewmaStrategy.d.ts +10 -0
  257. package/lib/browser/priorityFee/ewmaStrategy.js +10 -0
  258. package/lib/browser/priorityFee/heliusPriorityFeeMethod.d.ts +13 -0
  259. package/lib/browser/priorityFee/heliusPriorityFeeMethod.js +9 -2
  260. package/lib/browser/priorityFee/maxOverSlotsStrategy.d.ts +1 -0
  261. package/lib/browser/priorityFee/maxOverSlotsStrategy.js +1 -0
  262. package/lib/browser/priorityFee/maxStrategy.d.ts +1 -0
  263. package/lib/browser/priorityFee/maxStrategy.js +1 -0
  264. package/lib/browser/priorityFee/priorityFeeSubscriber.d.ts +39 -0
  265. package/lib/browser/priorityFee/priorityFeeSubscriber.js +39 -0
  266. package/lib/browser/priorityFee/priorityFeeSubscriberMap.d.ts +20 -1
  267. package/lib/browser/priorityFee/priorityFeeSubscriberMap.js +21 -2
  268. package/lib/browser/priorityFee/solanaPriorityFeeMethod.d.ts +12 -0
  269. package/lib/browser/priorityFee/solanaPriorityFeeMethod.js +10 -0
  270. package/lib/browser/priorityFee/types.d.ts +32 -0
  271. package/lib/browser/priorityFee/types.js +5 -0
  272. package/lib/browser/priorityFee/velocityPriorityFeeMethod.d.ts +11 -0
  273. package/lib/browser/priorityFee/velocityPriorityFeeMethod.js +8 -0
  274. package/lib/browser/pyth/pythLazerSubscriber.js +37 -27
  275. package/lib/browser/slot/SlotSubscriber.d.ts +18 -0
  276. package/lib/browser/slot/SlotSubscriber.js +16 -0
  277. package/lib/browser/slot/SlothashSubscriber.d.ts +26 -0
  278. package/lib/browser/slot/SlothashSubscriber.js +25 -1
  279. package/lib/browser/tx/baseTxSender.d.ts +163 -0
  280. package/lib/browser/tx/baseTxSender.js +174 -6
  281. package/lib/browser/tx/blockhashFetcher/baseBlockhashFetcher.d.ts +12 -0
  282. package/lib/browser/tx/blockhashFetcher/baseBlockhashFetcher.js +12 -0
  283. package/lib/browser/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts +15 -0
  284. package/lib/browser/tx/blockhashFetcher/cachedBlockhashFetcher.js +15 -0
  285. package/lib/browser/tx/blockhashFetcher/types.d.ts +4 -0
  286. package/lib/browser/tx/fastSingleTxSender.d.ts +47 -0
  287. package/lib/browser/tx/fastSingleTxSender.js +48 -1
  288. package/lib/browser/tx/forwardOnlyTxSender.d.ts +48 -0
  289. package/lib/browser/tx/forwardOnlyTxSender.js +48 -0
  290. package/lib/browser/tx/reportTransactionError.d.ts +18 -6
  291. package/lib/browser/tx/reportTransactionError.js +18 -6
  292. package/lib/browser/tx/retryTxSender.d.ts +40 -0
  293. package/lib/browser/tx/retryTxSender.js +39 -0
  294. package/lib/browser/tx/txHandler.d.ts +146 -35
  295. package/lib/browser/tx/txHandler.js +132 -32
  296. package/lib/browser/tx/txParamProcessor.d.ts +47 -0
  297. package/lib/browser/tx/txParamProcessor.js +47 -0
  298. package/lib/browser/tx/types.d.ts +46 -0
  299. package/lib/browser/tx/types.js +5 -0
  300. package/lib/browser/tx/utils.d.ts +26 -0
  301. package/lib/browser/tx/utils.js +26 -0
  302. package/lib/browser/tx/whileValidTxSender.d.ts +82 -0
  303. package/lib/browser/tx/whileValidTxSender.js +81 -0
  304. package/lib/browser/types.d.ts +785 -13
  305. package/lib/browser/types.js +126 -9
  306. package/lib/browser/user.d.ts +660 -84
  307. package/lib/browser/user.js +816 -97
  308. package/lib/browser/userMap/PollingSubscription.d.ts +10 -0
  309. package/lib/browser/userMap/PollingSubscription.js +14 -2
  310. package/lib/browser/userMap/WebsocketSubscription.d.ts +9 -0
  311. package/lib/browser/userMap/WebsocketSubscription.js +9 -0
  312. package/lib/browser/userMap/grpcSubscription.d.ts +8 -0
  313. package/lib/browser/userMap/grpcSubscription.js +8 -0
  314. package/lib/browser/userMap/referrerMap.d.ts +72 -2
  315. package/lib/browser/userMap/referrerMap.js +84 -2
  316. package/lib/browser/userMap/revenueShareEscrowMap.d.ts +37 -10
  317. package/lib/browser/userMap/revenueShareEscrowMap.js +38 -11
  318. package/lib/browser/userMap/userMap.d.ts +81 -3
  319. package/lib/browser/userMap/userMap.js +81 -4
  320. package/lib/browser/userMap/userMapConfig.d.ts +23 -0
  321. package/lib/browser/userMap/userStatsMap.d.ts +22 -0
  322. package/lib/browser/userMap/userStatsMap.js +22 -0
  323. package/lib/browser/userName.d.ts +16 -0
  324. package/lib/browser/userName.js +16 -0
  325. package/lib/browser/userStats.d.ts +28 -1
  326. package/lib/browser/userStats.js +28 -1
  327. package/lib/browser/util/TransactionConfirmationManager.d.ts +33 -0
  328. package/lib/browser/util/TransactionConfirmationManager.js +33 -0
  329. package/lib/browser/util/chainClock.d.ts +24 -0
  330. package/lib/browser/util/chainClock.js +20 -0
  331. package/lib/browser/util/computeUnits.d.ts +32 -0
  332. package/lib/browser/util/computeUnits.js +32 -0
  333. package/lib/browser/util/digest.d.ts +17 -0
  334. package/lib/browser/util/digest.js +17 -0
  335. package/lib/browser/util/ed25519Utils.d.ts +12 -2
  336. package/lib/browser/util/ed25519Utils.js +12 -2
  337. package/lib/browser/util/promiseTimeout.d.ts +9 -0
  338. package/lib/browser/util/promiseTimeout.js +9 -0
  339. package/lib/browser/velocityClient.d.ts +3363 -220
  340. package/lib/browser/velocityClient.js +3514 -275
  341. package/lib/browser/wallet.d.ts +37 -0
  342. package/lib/browser/wallet.js +37 -0
  343. package/lib/node/accounts/basicUserAccountSubscriber.d.ts +18 -0
  344. package/lib/node/accounts/basicUserAccountSubscriber.d.ts.map +1 -1
  345. package/lib/node/accounts/basicUserAccountSubscriber.js +18 -0
  346. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts +18 -0
  347. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts.map +1 -1
  348. package/lib/node/accounts/basicUserStatsAccountSubscriber.js +18 -0
  349. package/lib/node/accounts/bulkAccountLoader.d.ts +52 -0
  350. package/lib/node/accounts/bulkAccountLoader.d.ts.map +1 -1
  351. package/lib/node/accounts/bulkAccountLoader.js +51 -0
  352. package/lib/node/accounts/bulkUserStatsSubscription.d.ts +9 -2
  353. package/lib/node/accounts/bulkUserStatsSubscription.d.ts.map +1 -1
  354. package/lib/node/accounts/bulkUserStatsSubscription.js +9 -2
  355. package/lib/node/accounts/bulkUserSubscription.d.ts +8 -2
  356. package/lib/node/accounts/bulkUserSubscription.d.ts.map +1 -1
  357. package/lib/node/accounts/bulkUserSubscription.js +8 -2
  358. package/lib/node/accounts/customizedCadenceBulkAccountLoader.d.ts +37 -0
  359. package/lib/node/accounts/customizedCadenceBulkAccountLoader.d.ts.map +1 -1
  360. package/lib/node/accounts/customizedCadenceBulkAccountLoader.js +37 -0
  361. package/lib/node/accounts/fetch.d.ts +59 -2
  362. package/lib/node/accounts/fetch.d.ts.map +1 -1
  363. package/lib/node/accounts/fetch.js +57 -2
  364. package/lib/node/accounts/grpcAccountSubscriber.d.ts +30 -0
  365. package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
  366. package/lib/node/accounts/grpcAccountSubscriber.js +30 -0
  367. package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +16 -0
  368. package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  369. package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.js +16 -0
  370. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +60 -1
  371. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
  372. package/lib/node/accounts/grpcMultiAccountSubscriber.js +118 -37
  373. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts +32 -0
  374. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts.map +1 -1
  375. package/lib/node/accounts/grpcMultiUserAccountSubscriber.js +40 -12
  376. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +33 -0
  377. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts.map +1 -1
  378. package/lib/node/accounts/grpcProgramAccountSubscriber.js +33 -0
  379. package/lib/node/accounts/grpcUserAccountSubscriber.d.ts +15 -0
  380. package/lib/node/accounts/grpcUserAccountSubscriber.d.ts.map +1 -1
  381. package/lib/node/accounts/grpcUserAccountSubscriber.js +15 -0
  382. package/lib/node/accounts/grpcUserStatsAccountSubscriber.d.ts +15 -0
  383. package/lib/node/accounts/grpcUserStatsAccountSubscriber.d.ts.map +1 -1
  384. package/lib/node/accounts/grpcUserStatsAccountSubscriber.js +15 -0
  385. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts +41 -0
  386. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts.map +1 -1
  387. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.js +41 -0
  388. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +116 -0
  389. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  390. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.js +152 -1
  391. package/lib/node/accounts/laserProgramAccountSubscriber.d.ts +34 -0
  392. package/lib/node/accounts/laserProgramAccountSubscriber.d.ts.map +1 -1
  393. package/lib/node/accounts/laserProgramAccountSubscriber.js +34 -0
  394. package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts +19 -0
  395. package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts.map +1 -1
  396. package/lib/node/accounts/oneShotUserAccountSubscriber.js +19 -0
  397. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts +19 -0
  398. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts.map +1 -1
  399. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.js +19 -0
  400. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +31 -0
  401. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  402. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.js +31 -0
  403. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts +19 -0
  404. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts.map +1 -1
  405. package/lib/node/accounts/pollingOracleAccountSubscriber.js +21 -2
  406. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts +22 -0
  407. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -1
  408. package/lib/node/accounts/pollingTokenAccountSubscriber.js +22 -0
  409. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts +32 -0
  410. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts.map +1 -1
  411. package/lib/node/accounts/pollingUserAccountSubscriber.js +32 -0
  412. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts +22 -0
  413. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts.map +1 -1
  414. package/lib/node/accounts/pollingUserStatsAccountSubscriber.js +22 -0
  415. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts +83 -0
  416. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts.map +1 -1
  417. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.js +84 -1
  418. package/lib/node/accounts/testBulkAccountLoader.d.ts +8 -0
  419. package/lib/node/accounts/testBulkAccountLoader.d.ts.map +1 -1
  420. package/lib/node/accounts/testBulkAccountLoader.js +8 -0
  421. package/lib/node/accounts/types.d.ts +149 -0
  422. package/lib/node/accounts/types.d.ts.map +1 -1
  423. package/lib/node/accounts/types.js +13 -0
  424. package/lib/node/accounts/utils.d.ts +18 -0
  425. package/lib/node/accounts/utils.d.ts.map +1 -1
  426. package/lib/node/accounts/utils.js +32 -16
  427. package/lib/node/accounts/webSocketAccountSubscriber.d.ts +41 -0
  428. package/lib/node/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
  429. package/lib/node/accounts/webSocketAccountSubscriber.js +41 -0
  430. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts +31 -0
  431. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts.map +1 -1
  432. package/lib/node/accounts/webSocketAccountSubscriberV2.js +31 -23
  433. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +27 -0
  434. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  435. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +27 -0
  436. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts +36 -0
  437. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts.map +1 -1
  438. package/lib/node/accounts/webSocketProgramAccountSubscriber.js +36 -0
  439. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +21 -0
  440. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -1
  441. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +25 -0
  442. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts +21 -0
  443. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts.map +1 -1
  444. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.js +21 -0
  445. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts +26 -0
  446. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -1
  447. package/lib/node/accounts/webSocketUserAccountSubscriber.js +26 -0
  448. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  449. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts.map +1 -1
  450. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.js +20 -0
  451. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts +121 -0
  452. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts.map +1 -1
  453. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.js +123 -2
  454. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +96 -0
  455. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  456. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.js +108 -0
  457. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts +28 -0
  458. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts.map +1 -1
  459. package/lib/node/accounts/websocketProgramUserAccountSubscriber.js +33 -0
  460. package/lib/node/addresses/marketAddresses.d.ts +10 -0
  461. package/lib/node/addresses/marketAddresses.d.ts.map +1 -1
  462. package/lib/node/addresses/marketAddresses.js +10 -0
  463. package/lib/node/addresses/pda.d.ts +214 -2
  464. package/lib/node/addresses/pda.d.ts.map +1 -1
  465. package/lib/node/addresses/pda.js +217 -5
  466. package/lib/node/adminClient.d.ts +2171 -4
  467. package/lib/node/adminClient.d.ts.map +1 -1
  468. package/lib/node/adminClient.js +2215 -4
  469. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +15 -0
  470. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
  471. package/lib/node/auctionSubscriber/auctionSubscriber.js +15 -0
  472. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +19 -0
  473. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
  474. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +19 -0
  475. package/lib/node/auctionSubscriber/index.d.ts +6 -0
  476. package/lib/node/auctionSubscriber/index.d.ts.map +1 -1
  477. package/lib/node/auctionSubscriber/index.js +6 -0
  478. package/lib/node/auctionSubscriber/types.d.ts +7 -0
  479. package/lib/node/auctionSubscriber/types.d.ts.map +1 -1
  480. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts +33 -0
  481. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts.map +1 -1
  482. package/lib/node/blockhashSubscriber/BlockhashSubscriber.js +33 -0
  483. package/lib/node/blockhashSubscriber/index.d.ts +5 -0
  484. package/lib/node/blockhashSubscriber/index.d.ts.map +1 -1
  485. package/lib/node/blockhashSubscriber/index.js +5 -0
  486. package/lib/node/blockhashSubscriber/types.d.ts +5 -0
  487. package/lib/node/blockhashSubscriber/types.d.ts.map +1 -1
  488. package/lib/node/clock/clockSubscriber.d.ts +24 -0
  489. package/lib/node/clock/clockSubscriber.d.ts.map +1 -1
  490. package/lib/node/clock/clockSubscriber.js +22 -0
  491. package/lib/node/config.d.ts +58 -4
  492. package/lib/node/config.d.ts.map +1 -1
  493. package/lib/node/config.js +94 -28
  494. package/lib/node/constants/numericConstants.d.ts +67 -0
  495. package/lib/node/constants/numericConstants.d.ts.map +1 -1
  496. package/lib/node/constants/numericConstants.js +68 -1
  497. package/lib/node/constants/perpMarkets.d.ts +10 -0
  498. package/lib/node/constants/perpMarkets.d.ts.map +1 -1
  499. package/lib/node/constants/perpMarkets.js +40 -908
  500. package/lib/node/constants/spotMarkets.d.ts +13 -0
  501. package/lib/node/constants/spotMarkets.d.ts.map +1 -1
  502. package/lib/node/constants/spotMarkets.js +16 -742
  503. package/lib/node/constants/txConstants.d.ts +1 -0
  504. package/lib/node/constants/txConstants.d.ts.map +1 -1
  505. package/lib/node/constants/txConstants.js +1 -0
  506. package/lib/node/core/VelocityCore.d.ts +285 -8
  507. package/lib/node/core/VelocityCore.d.ts.map +1 -1
  508. package/lib/node/core/VelocityCore.js +281 -8
  509. package/lib/node/core/instructions/deposit.d.ts +22 -0
  510. package/lib/node/core/instructions/deposit.d.ts.map +1 -1
  511. package/lib/node/core/instructions/deposit.js +22 -0
  512. package/lib/node/core/instructions/fill.d.ts +17 -0
  513. package/lib/node/core/instructions/fill.d.ts.map +1 -1
  514. package/lib/node/core/instructions/fill.js +17 -0
  515. package/lib/node/core/instructions/funding.d.ts +12 -0
  516. package/lib/node/core/instructions/funding.d.ts.map +1 -1
  517. package/lib/node/core/instructions/funding.js +12 -0
  518. package/lib/node/core/instructions/liquidation.d.ts +18 -0
  519. package/lib/node/core/instructions/liquidation.d.ts.map +1 -1
  520. package/lib/node/core/instructions/liquidation.js +18 -0
  521. package/lib/node/core/instructions/orders.d.ts +28 -0
  522. package/lib/node/core/instructions/orders.d.ts.map +1 -1
  523. package/lib/node/core/instructions/orders.js +28 -0
  524. package/lib/node/core/instructions/perpOrders.d.ts +106 -0
  525. package/lib/node/core/instructions/perpOrders.d.ts.map +1 -1
  526. package/lib/node/core/instructions/perpOrders.js +106 -0
  527. package/lib/node/core/instructions/settlement.d.ts +14 -0
  528. package/lib/node/core/instructions/settlement.d.ts.map +1 -1
  529. package/lib/node/core/instructions/settlement.js +14 -0
  530. package/lib/node/core/instructions/trigger.d.ts +15 -0
  531. package/lib/node/core/instructions/trigger.d.ts.map +1 -1
  532. package/lib/node/core/instructions/trigger.js +15 -0
  533. package/lib/node/core/instructions/withdraw.d.ts +21 -0
  534. package/lib/node/core/instructions/withdraw.d.ts.map +1 -1
  535. package/lib/node/core/instructions/withdraw.js +21 -0
  536. package/lib/node/core/remainingAccounts.d.ts +47 -3
  537. package/lib/node/core/remainingAccounts.d.ts.map +1 -1
  538. package/lib/node/core/remainingAccounts.js +20 -0
  539. package/lib/node/core/signedMsg.d.ts +38 -0
  540. package/lib/node/core/signedMsg.d.ts.map +1 -1
  541. package/lib/node/core/signedMsg.js +33 -0
  542. package/lib/node/decode/customCoder.d.ts +39 -3
  543. package/lib/node/decode/customCoder.d.ts.map +1 -1
  544. package/lib/node/decode/customCoder.js +45 -3
  545. package/lib/node/decode/user.d.ts +22 -0
  546. package/lib/node/decode/user.d.ts.map +1 -1
  547. package/lib/node/decode/user.js +27 -1
  548. package/lib/node/dlob/DLOB.d.ts +502 -53
  549. package/lib/node/dlob/DLOB.d.ts.map +1 -1
  550. package/lib/node/dlob/DLOB.js +540 -98
  551. package/lib/node/dlob/DLOBNode.d.ts +86 -4
  552. package/lib/node/dlob/DLOBNode.d.ts.map +1 -1
  553. package/lib/node/dlob/DLOBNode.js +37 -4
  554. package/lib/node/dlob/DLOBSubscriber.d.ts +39 -12
  555. package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
  556. package/lib/node/dlob/DLOBSubscriber.js +42 -12
  557. package/lib/node/dlob/NodeList.d.ts +69 -0
  558. package/lib/node/dlob/NodeList.d.ts.map +1 -1
  559. package/lib/node/dlob/NodeList.js +66 -0
  560. package/lib/node/dlob/orderBookLevels.d.ts +106 -13
  561. package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
  562. package/lib/node/dlob/orderBookLevels.js +84 -14
  563. package/lib/node/dlob/types.d.ts +15 -0
  564. package/lib/node/dlob/types.d.ts.map +1 -1
  565. package/lib/node/events/eventList.d.ts +14 -0
  566. package/lib/node/events/eventList.d.ts.map +1 -1
  567. package/lib/node/events/eventList.js +14 -0
  568. package/lib/node/events/eventSubscriber.d.ts +87 -10
  569. package/lib/node/events/eventSubscriber.d.ts.map +1 -1
  570. package/lib/node/events/eventSubscriber.js +93 -8
  571. package/lib/node/events/eventsServerLogProvider.d.ts +22 -0
  572. package/lib/node/events/eventsServerLogProvider.d.ts.map +1 -1
  573. package/lib/node/events/eventsServerLogProvider.js +27 -0
  574. package/lib/node/events/fetchLogs.d.ts +31 -0
  575. package/lib/node/events/fetchLogs.d.ts.map +1 -1
  576. package/lib/node/events/fetchLogs.js +46 -1
  577. package/lib/node/events/parse.d.ts +23 -0
  578. package/lib/node/events/parse.d.ts.map +1 -1
  579. package/lib/node/events/parse.js +23 -0
  580. package/lib/node/events/pollingLogProvider.d.ts +24 -0
  581. package/lib/node/events/pollingLogProvider.d.ts.map +1 -1
  582. package/lib/node/events/pollingLogProvider.js +24 -0
  583. package/lib/node/events/sort.d.ts +11 -0
  584. package/lib/node/events/sort.d.ts.map +1 -1
  585. package/lib/node/events/sort.js +12 -0
  586. package/lib/node/events/txEventCache.d.ts +13 -3
  587. package/lib/node/events/txEventCache.d.ts.map +1 -1
  588. package/lib/node/events/txEventCache.js +19 -7
  589. package/lib/node/events/types.d.ts +118 -2
  590. package/lib/node/events/types.d.ts.map +1 -1
  591. package/lib/node/events/types.js +11 -0
  592. package/lib/node/events/webSocketLogProvider.d.ts +23 -0
  593. package/lib/node/events/webSocketLogProvider.d.ts.map +1 -1
  594. package/lib/node/events/webSocketLogProvider.js +28 -0
  595. package/lib/node/factory/oracleClient.js +2 -2
  596. package/lib/node/idl/velocity.d.ts +150 -7
  597. package/lib/node/idl/velocity.d.ts.map +1 -1
  598. package/lib/node/idl/velocity.json +151 -8
  599. package/lib/node/keypair.d.ts +8 -0
  600. package/lib/node/keypair.d.ts.map +1 -1
  601. package/lib/node/keypair.js +8 -0
  602. package/lib/node/marginCalculation.d.ts +134 -2
  603. package/lib/node/marginCalculation.d.ts.map +1 -1
  604. package/lib/node/marginCalculation.js +121 -0
  605. package/lib/node/math/amm.d.ts +348 -29
  606. package/lib/node/math/amm.d.ts.map +1 -1
  607. package/lib/node/math/amm.js +310 -32
  608. package/lib/node/math/auction.d.ts +95 -19
  609. package/lib/node/math/auction.d.ts.map +1 -1
  610. package/lib/node/math/auction.js +118 -28
  611. package/lib/node/math/bankruptcy.d.ts +46 -0
  612. package/lib/node/math/bankruptcy.d.ts.map +1 -1
  613. package/lib/node/math/bankruptcy.js +89 -1
  614. package/lib/node/math/builder.d.ts +64 -8
  615. package/lib/node/math/builder.d.ts.map +1 -1
  616. package/lib/node/math/builder.js +71 -9
  617. package/lib/node/math/conversion.d.ts +21 -0
  618. package/lib/node/math/conversion.d.ts.map +1 -1
  619. package/lib/node/math/conversion.js +21 -0
  620. package/lib/node/math/exchangeStatus.d.ts +91 -0
  621. package/lib/node/math/exchangeStatus.d.ts.map +1 -1
  622. package/lib/node/math/exchangeStatus.js +106 -3
  623. package/lib/node/math/funding.d.ts +57 -20
  624. package/lib/node/math/funding.d.ts.map +1 -1
  625. package/lib/node/math/funding.js +63 -23
  626. package/lib/node/math/insurance.d.ts +62 -0
  627. package/lib/node/math/insurance.d.ts.map +1 -1
  628. package/lib/node/math/insurance.js +62 -0
  629. package/lib/node/math/liquidation.d.ts +127 -11
  630. package/lib/node/math/liquidation.d.ts.map +1 -1
  631. package/lib/node/math/liquidation.js +182 -19
  632. package/lib/node/math/margin.d.ts +79 -13
  633. package/lib/node/math/margin.d.ts.map +1 -1
  634. package/lib/node/math/margin.js +80 -14
  635. package/lib/node/math/market.d.ts +135 -15
  636. package/lib/node/math/market.d.ts.map +1 -1
  637. package/lib/node/math/market.js +145 -17
  638. package/lib/node/math/oracles.d.ts +113 -0
  639. package/lib/node/math/oracles.d.ts.map +1 -1
  640. package/lib/node/math/oracles.js +118 -1
  641. package/lib/node/math/orders.d.ts +115 -10
  642. package/lib/node/math/orders.d.ts.map +1 -1
  643. package/lib/node/math/orders.js +130 -19
  644. package/lib/node/math/position.d.ts +80 -33
  645. package/lib/node/math/position.d.ts.map +1 -1
  646. package/lib/node/math/position.js +80 -33
  647. package/lib/node/math/repeg.d.ts +48 -10
  648. package/lib/node/math/repeg.d.ts.map +1 -1
  649. package/lib/node/math/repeg.js +48 -10
  650. package/lib/node/math/spotBalance.d.ts +200 -5
  651. package/lib/node/math/spotBalance.d.ts.map +1 -1
  652. package/lib/node/math/spotBalance.js +239 -10
  653. package/lib/node/math/spotMarket.d.ts +36 -3
  654. package/lib/node/math/spotMarket.d.ts.map +1 -1
  655. package/lib/node/math/spotMarket.js +36 -3
  656. package/lib/node/math/spotPosition.d.ts +72 -0
  657. package/lib/node/math/spotPosition.d.ts.map +1 -1
  658. package/lib/node/math/spotPosition.js +62 -0
  659. package/lib/node/math/state.d.ts +31 -0
  660. package/lib/node/math/state.d.ts.map +1 -1
  661. package/lib/node/math/state.js +31 -0
  662. package/lib/node/math/superStake.d.ts +126 -2
  663. package/lib/node/math/superStake.d.ts.map +1 -1
  664. package/lib/node/math/superStake.js +123 -3
  665. package/lib/node/math/tiers.d.ts +29 -0
  666. package/lib/node/math/tiers.d.ts.map +1 -1
  667. package/lib/node/math/tiers.js +29 -0
  668. package/lib/node/math/trade.d.ts +102 -51
  669. package/lib/node/math/trade.d.ts.map +1 -1
  670. package/lib/node/math/trade.js +101 -55
  671. package/lib/node/math/utils.d.ts +71 -9
  672. package/lib/node/math/utils.d.ts.map +1 -1
  673. package/lib/node/math/utils.js +71 -9
  674. package/lib/node/memcmp.d.ts +94 -0
  675. package/lib/node/memcmp.d.ts.map +1 -1
  676. package/lib/node/memcmp.js +103 -2
  677. package/lib/node/oracles/oracleClientCache.d.ts +16 -0
  678. package/lib/node/oracles/oracleClientCache.d.ts.map +1 -1
  679. package/lib/node/oracles/oracleClientCache.js +16 -0
  680. package/lib/node/oracles/oracleId.d.ts +31 -0
  681. package/lib/node/oracles/oracleId.d.ts.map +1 -1
  682. package/lib/node/oracles/oracleId.js +39 -8
  683. package/lib/node/oracles/prelaunchOracleClient.d.ts +20 -0
  684. package/lib/node/oracles/prelaunchOracleClient.d.ts.map +1 -1
  685. package/lib/node/oracles/prelaunchOracleClient.js +20 -0
  686. package/lib/node/oracles/pythClient.d.ts +31 -0
  687. package/lib/node/oracles/pythClient.d.ts.map +1 -1
  688. package/lib/node/oracles/pythClient.js +32 -1
  689. package/lib/node/oracles/pythLazerClient.d.ts +34 -0
  690. package/lib/node/oracles/pythLazerClient.d.ts.map +1 -1
  691. package/lib/node/oracles/pythLazerClient.js +35 -1
  692. package/lib/node/oracles/quoteAssetOracleClient.d.ts +18 -0
  693. package/lib/node/oracles/quoteAssetOracleClient.d.ts.map +1 -1
  694. package/lib/node/oracles/quoteAssetOracleClient.js +18 -0
  695. package/lib/node/oracles/strictOraclePrice.d.ts +21 -0
  696. package/lib/node/oracles/strictOraclePrice.d.ts.map +1 -1
  697. package/lib/node/oracles/strictOraclePrice.js +21 -0
  698. package/lib/node/oracles/types.d.ts +45 -0
  699. package/lib/node/oracles/types.d.ts.map +1 -1
  700. package/lib/node/oracles/utils.d.ts +20 -0
  701. package/lib/node/oracles/utils.d.ts.map +1 -1
  702. package/lib/node/oracles/utils.js +20 -0
  703. package/lib/node/orderParams.d.ts +42 -4
  704. package/lib/node/orderParams.d.ts.map +1 -1
  705. package/lib/node/orderParams.js +42 -4
  706. package/lib/node/orderSubscriber/OrderSubscriber.d.ts +56 -0
  707. package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
  708. package/lib/node/orderSubscriber/OrderSubscriber.js +59 -0
  709. package/lib/node/orderSubscriber/PollingSubscription.d.ts +11 -0
  710. package/lib/node/orderSubscriber/PollingSubscription.d.ts.map +1 -1
  711. package/lib/node/orderSubscriber/PollingSubscription.js +11 -0
  712. package/lib/node/orderSubscriber/WebsocketSubscription.d.ts +18 -0
  713. package/lib/node/orderSubscriber/WebsocketSubscription.d.ts.map +1 -1
  714. package/lib/node/orderSubscriber/WebsocketSubscription.js +18 -0
  715. package/lib/node/orderSubscriber/grpcSubscription.d.ts +16 -0
  716. package/lib/node/orderSubscriber/grpcSubscription.d.ts.map +1 -1
  717. package/lib/node/orderSubscriber/grpcSubscription.js +16 -0
  718. package/lib/node/orderSubscriber/types.d.ts +9 -0
  719. package/lib/node/orderSubscriber/types.d.ts.map +1 -1
  720. package/lib/node/priorityFee/averageOverSlotsStrategy.d.ts +1 -0
  721. package/lib/node/priorityFee/averageOverSlotsStrategy.d.ts.map +1 -1
  722. package/lib/node/priorityFee/averageOverSlotsStrategy.js +1 -0
  723. package/lib/node/priorityFee/averageStrategy.d.ts +1 -0
  724. package/lib/node/priorityFee/averageStrategy.d.ts.map +1 -1
  725. package/lib/node/priorityFee/averageStrategy.js +1 -0
  726. package/lib/node/priorityFee/ewmaStrategy.d.ts +10 -0
  727. package/lib/node/priorityFee/ewmaStrategy.d.ts.map +1 -1
  728. package/lib/node/priorityFee/ewmaStrategy.js +10 -0
  729. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts +13 -0
  730. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts.map +1 -1
  731. package/lib/node/priorityFee/heliusPriorityFeeMethod.js +9 -2
  732. package/lib/node/priorityFee/maxOverSlotsStrategy.d.ts +1 -0
  733. package/lib/node/priorityFee/maxOverSlotsStrategy.d.ts.map +1 -1
  734. package/lib/node/priorityFee/maxOverSlotsStrategy.js +1 -0
  735. package/lib/node/priorityFee/maxStrategy.d.ts +1 -0
  736. package/lib/node/priorityFee/maxStrategy.d.ts.map +1 -1
  737. package/lib/node/priorityFee/maxStrategy.js +1 -0
  738. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +39 -0
  739. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts.map +1 -1
  740. package/lib/node/priorityFee/priorityFeeSubscriber.js +39 -0
  741. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts +20 -1
  742. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts.map +1 -1
  743. package/lib/node/priorityFee/priorityFeeSubscriberMap.js +21 -2
  744. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts +12 -0
  745. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts.map +1 -1
  746. package/lib/node/priorityFee/solanaPriorityFeeMethod.js +10 -0
  747. package/lib/node/priorityFee/types.d.ts +32 -0
  748. package/lib/node/priorityFee/types.d.ts.map +1 -1
  749. package/lib/node/priorityFee/types.js +5 -0
  750. package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts +11 -0
  751. package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts.map +1 -1
  752. package/lib/node/priorityFee/velocityPriorityFeeMethod.js +8 -0
  753. package/lib/node/pyth/pythLazerSubscriber.d.ts.map +1 -1
  754. package/lib/node/pyth/pythLazerSubscriber.js +37 -27
  755. package/lib/node/slot/SlotSubscriber.d.ts +18 -0
  756. package/lib/node/slot/SlotSubscriber.d.ts.map +1 -1
  757. package/lib/node/slot/SlotSubscriber.js +16 -0
  758. package/lib/node/slot/SlothashSubscriber.d.ts +26 -0
  759. package/lib/node/slot/SlothashSubscriber.d.ts.map +1 -1
  760. package/lib/node/slot/SlothashSubscriber.js +25 -1
  761. package/lib/node/tx/baseTxSender.d.ts +163 -0
  762. package/lib/node/tx/baseTxSender.d.ts.map +1 -1
  763. package/lib/node/tx/baseTxSender.js +174 -6
  764. package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.d.ts +12 -0
  765. package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.d.ts.map +1 -1
  766. package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.js +12 -0
  767. package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts +15 -0
  768. package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts.map +1 -1
  769. package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.js +15 -0
  770. package/lib/node/tx/blockhashFetcher/types.d.ts +4 -0
  771. package/lib/node/tx/blockhashFetcher/types.d.ts.map +1 -1
  772. package/lib/node/tx/fastSingleTxSender.d.ts +47 -0
  773. package/lib/node/tx/fastSingleTxSender.d.ts.map +1 -1
  774. package/lib/node/tx/fastSingleTxSender.js +48 -1
  775. package/lib/node/tx/forwardOnlyTxSender.d.ts +48 -0
  776. package/lib/node/tx/forwardOnlyTxSender.d.ts.map +1 -1
  777. package/lib/node/tx/forwardOnlyTxSender.js +48 -0
  778. package/lib/node/tx/reportTransactionError.d.ts +18 -6
  779. package/lib/node/tx/reportTransactionError.d.ts.map +1 -1
  780. package/lib/node/tx/reportTransactionError.js +18 -6
  781. package/lib/node/tx/retryTxSender.d.ts +40 -0
  782. package/lib/node/tx/retryTxSender.d.ts.map +1 -1
  783. package/lib/node/tx/retryTxSender.js +39 -0
  784. package/lib/node/tx/txHandler.d.ts +146 -35
  785. package/lib/node/tx/txHandler.d.ts.map +1 -1
  786. package/lib/node/tx/txHandler.js +132 -32
  787. package/lib/node/tx/txParamProcessor.d.ts +47 -0
  788. package/lib/node/tx/txParamProcessor.d.ts.map +1 -1
  789. package/lib/node/tx/txParamProcessor.js +47 -0
  790. package/lib/node/tx/types.d.ts +46 -0
  791. package/lib/node/tx/types.d.ts.map +1 -1
  792. package/lib/node/tx/types.js +5 -0
  793. package/lib/node/tx/utils.d.ts +26 -0
  794. package/lib/node/tx/utils.d.ts.map +1 -1
  795. package/lib/node/tx/utils.js +26 -0
  796. package/lib/node/tx/whileValidTxSender.d.ts +82 -0
  797. package/lib/node/tx/whileValidTxSender.d.ts.map +1 -1
  798. package/lib/node/tx/whileValidTxSender.js +81 -0
  799. package/lib/node/types.d.ts +785 -13
  800. package/lib/node/types.d.ts.map +1 -1
  801. package/lib/node/types.js +126 -9
  802. package/lib/node/user.d.ts +660 -84
  803. package/lib/node/user.d.ts.map +1 -1
  804. package/lib/node/user.js +816 -97
  805. package/lib/node/userMap/PollingSubscription.d.ts +10 -0
  806. package/lib/node/userMap/PollingSubscription.d.ts.map +1 -1
  807. package/lib/node/userMap/PollingSubscription.js +14 -2
  808. package/lib/node/userMap/WebsocketSubscription.d.ts +9 -0
  809. package/lib/node/userMap/WebsocketSubscription.d.ts.map +1 -1
  810. package/lib/node/userMap/WebsocketSubscription.js +9 -0
  811. package/lib/node/userMap/grpcSubscription.d.ts +8 -0
  812. package/lib/node/userMap/grpcSubscription.d.ts.map +1 -1
  813. package/lib/node/userMap/grpcSubscription.js +8 -0
  814. package/lib/node/userMap/referrerMap.d.ts +72 -2
  815. package/lib/node/userMap/referrerMap.d.ts.map +1 -1
  816. package/lib/node/userMap/referrerMap.js +84 -2
  817. package/lib/node/userMap/revenueShareEscrowMap.d.ts +37 -10
  818. package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
  819. package/lib/node/userMap/revenueShareEscrowMap.js +38 -11
  820. package/lib/node/userMap/userMap.d.ts +81 -3
  821. package/lib/node/userMap/userMap.d.ts.map +1 -1
  822. package/lib/node/userMap/userMap.js +81 -4
  823. package/lib/node/userMap/userMapConfig.d.ts +23 -0
  824. package/lib/node/userMap/userMapConfig.d.ts.map +1 -1
  825. package/lib/node/userMap/userStatsMap.d.ts +22 -0
  826. package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
  827. package/lib/node/userMap/userStatsMap.js +22 -0
  828. package/lib/node/userName.d.ts +16 -0
  829. package/lib/node/userName.d.ts.map +1 -1
  830. package/lib/node/userName.js +16 -0
  831. package/lib/node/userStats.d.ts +28 -1
  832. package/lib/node/userStats.d.ts.map +1 -1
  833. package/lib/node/userStats.js +28 -1
  834. package/lib/node/util/TransactionConfirmationManager.d.ts +33 -0
  835. package/lib/node/util/TransactionConfirmationManager.d.ts.map +1 -1
  836. package/lib/node/util/TransactionConfirmationManager.js +33 -0
  837. package/lib/node/util/chainClock.d.ts +24 -0
  838. package/lib/node/util/chainClock.d.ts.map +1 -1
  839. package/lib/node/util/chainClock.js +20 -0
  840. package/lib/node/util/computeUnits.d.ts +32 -0
  841. package/lib/node/util/computeUnits.d.ts.map +1 -1
  842. package/lib/node/util/computeUnits.js +32 -0
  843. package/lib/node/util/digest.d.ts +17 -0
  844. package/lib/node/util/digest.d.ts.map +1 -1
  845. package/lib/node/util/digest.js +17 -0
  846. package/lib/node/util/ed25519Utils.d.ts +12 -2
  847. package/lib/node/util/ed25519Utils.d.ts.map +1 -1
  848. package/lib/node/util/ed25519Utils.js +12 -2
  849. package/lib/node/util/promiseTimeout.d.ts +9 -0
  850. package/lib/node/util/promiseTimeout.d.ts.map +1 -1
  851. package/lib/node/util/promiseTimeout.js +9 -0
  852. package/lib/node/velocityClient.d.ts +3363 -220
  853. package/lib/node/velocityClient.d.ts.map +1 -1
  854. package/lib/node/velocityClient.js +3514 -275
  855. package/lib/node/wallet.d.ts +37 -0
  856. package/lib/node/wallet.d.ts.map +1 -1
  857. package/lib/node/wallet.js +37 -0
  858. package/package.json +8 -3
  859. package/.prettierignore +0 -1
  860. package/CHANGELOG.md +0 -81
  861. package/build-browser.js +0 -58
  862. package/bunfig.toml +0 -3
  863. package/esbuild-shims.js +0 -12
  864. package/scripts/deposit-isolated-positions.ts +0 -110
  865. package/scripts/find-flagged-users.ts +0 -213
  866. package/scripts/grpc-client-test-comparison.ts +0 -372
  867. package/scripts/grpc-multiuser-client-test-comparison.ts +0 -158
  868. package/scripts/postbuild.js +0 -95
  869. package/scripts/single-grpc-client-test.ts +0 -284
  870. package/scripts/withdraw-isolated-positions.ts +0 -174
  871. package/src/accounts/README_WebSocketAccountSubscriberV2.md +0 -95
  872. package/src/accounts/README_WebSocketProgramAccountSubscriberV2.md +0 -135
  873. package/src/accounts/basicUserAccountSubscriber.ts +0 -61
  874. package/src/accounts/basicUserStatsAccountSubscriber.ts +0 -69
  875. package/src/accounts/bulkAccountLoader.ts +0 -306
  876. package/src/accounts/bulkUserStatsSubscription.ts +0 -33
  877. package/src/accounts/bulkUserSubscription.ts +0 -33
  878. package/src/accounts/customizedCadenceBulkAccountLoader.ts +0 -213
  879. package/src/accounts/fetch.ts +0 -143
  880. package/src/accounts/grpcAccountSubscriber.ts +0 -195
  881. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -57
  882. package/src/accounts/grpcMultiAccountSubscriber.ts +0 -483
  883. package/src/accounts/grpcMultiUserAccountSubscriber.ts +0 -290
  884. package/src/accounts/grpcProgramAccountSubscriber.ts +0 -231
  885. package/src/accounts/grpcUserAccountSubscriber.ts +0 -48
  886. package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -50
  887. package/src/accounts/grpcVelocityClientAccountSubscriber.ts +0 -216
  888. package/src/accounts/grpcVelocityClientAccountSubscriberV2.ts +0 -782
  889. package/src/accounts/laserProgramAccountSubscriber.ts +0 -232
  890. package/src/accounts/oneShotUserAccountSubscriber.ts +0 -69
  891. package/src/accounts/oneShotUserStatsAccountSubscriber.ts +0 -75
  892. package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +0 -192
  893. package/src/accounts/pollingOracleAccountSubscriber.ts +0 -132
  894. package/src/accounts/pollingTokenAccountSubscriber.ts +0 -125
  895. package/src/accounts/pollingUserAccountSubscriber.ts +0 -166
  896. package/src/accounts/pollingUserStatsAccountSubscriber.ts +0 -158
  897. package/src/accounts/pollingVelocityClientAccountSubscriber.ts +0 -691
  898. package/src/accounts/testBulkAccountLoader.ts +0 -51
  899. package/src/accounts/types.ts +0 -279
  900. package/src/accounts/utils.ts +0 -72
  901. package/src/accounts/webSocketAccountSubscriber.ts +0 -263
  902. package/src/accounts/webSocketAccountSubscriberV2.ts +0 -528
  903. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +0 -144
  904. package/src/accounts/webSocketProgramAccountSubscriber.ts +0 -208
  905. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +0 -1022
  906. package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +0 -1021
  907. package/src/accounts/webSocketUserAccountSubscriber.ts +0 -113
  908. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +0 -111
  909. package/src/accounts/webSocketVelocityClientAccountSubscriber.ts +0 -740
  910. package/src/accounts/webSocketVelocityClientAccountSubscriberV2.ts +0 -787
  911. package/src/accounts/websocketProgramUserAccountSubscriber.ts +0 -94
  912. package/src/addresses/marketAddresses.ts +0 -18
  913. package/src/addresses/pda.ts +0 -404
  914. package/src/adminClient.ts +0 -5984
  915. package/src/assert/assert.ts +0 -5
  916. package/src/auctionSubscriber/auctionSubscriber.ts +0 -71
  917. package/src/auctionSubscriber/auctionSubscriberGrpc.ts +0 -80
  918. package/src/auctionSubscriber/index.ts +0 -3
  919. package/src/auctionSubscriber/types.ts +0 -23
  920. package/src/bankrun/bankrunConnection.ts +0 -630
  921. package/src/blockhashSubscriber/BlockhashSubscriber.ts +0 -127
  922. package/src/blockhashSubscriber/index.ts +0 -1
  923. package/src/blockhashSubscriber/types.ts +0 -12
  924. package/src/clock/clockSubscriber.ts +0 -122
  925. package/src/config.ts +0 -225
  926. package/src/constants/index.ts +0 -9
  927. package/src/constants/numericConstants.ts +0 -124
  928. package/src/constants/perpMarkets.ts +0 -1042
  929. package/src/constants/spotMarkets.ts +0 -874
  930. package/src/constants/txConstants.ts +0 -1
  931. package/src/constituentMap/constituentMap.ts +0 -305
  932. package/src/constituentMap/pollingConstituentAccountSubscriber.ts +0 -99
  933. package/src/constituentMap/webSocketConstituentAccountSubscriber.ts +0 -121
  934. package/src/core/VelocityCore.ts +0 -328
  935. package/src/core/index.ts +0 -12
  936. package/src/core/instructions/deposit.ts +0 -42
  937. package/src/core/instructions/fill.ts +0 -34
  938. package/src/core/instructions/funding.ts +0 -21
  939. package/src/core/instructions/liquidation.ts +0 -37
  940. package/src/core/instructions/orders.ts +0 -56
  941. package/src/core/instructions/perpOrders.ts +0 -206
  942. package/src/core/instructions/settlement.ts +0 -26
  943. package/src/core/instructions/trigger.ts +0 -26
  944. package/src/core/instructions/withdraw.ts +0 -44
  945. package/src/core/remainingAccounts.ts +0 -276
  946. package/src/core/signedMsg.ts +0 -71
  947. package/src/decode/customCoder.ts +0 -105
  948. package/src/decode/user.ts +0 -379
  949. package/src/dlob/DLOB.ts +0 -2222
  950. package/src/dlob/DLOBNode.ts +0 -213
  951. package/src/dlob/DLOBSubscriber.ts +0 -232
  952. package/src/dlob/NodeList.ts +0 -175
  953. package/src/dlob/orderBookLevels.ts +0 -601
  954. package/src/dlob/types.ts +0 -22
  955. package/src/events/eventList.ts +0 -100
  956. package/src/events/eventSubscriber.ts +0 -440
  957. package/src/events/eventsServerLogProvider.ts +0 -161
  958. package/src/events/fetchLogs.ts +0 -169
  959. package/src/events/parse.ts +0 -248
  960. package/src/events/pollingLogProvider.ts +0 -89
  961. package/src/events/sort.ts +0 -39
  962. package/src/events/txEventCache.ts +0 -80
  963. package/src/events/types.ts +0 -217
  964. package/src/events/webSocketLogProvider.ts +0 -123
  965. package/src/factory/bigNum.ts +0 -709
  966. package/src/factory/oracleClient.ts +0 -73
  967. package/src/idl/pyth.json +0 -142
  968. package/src/idl/pyth.ts +0 -97
  969. package/src/idl/pyth_solana_receiver.json +0 -628
  970. package/src/idl/token_faucet.json +0 -229
  971. package/src/idl/token_faucet.ts +0 -197
  972. package/src/idl/velocity.json +0 -23974
  973. package/src/idl/velocity.ts +0 -23980
  974. package/src/index.ts +0 -150
  975. package/src/indicative-quotes/indicativeQuotesSender.ts +0 -233
  976. package/src/isomorphic/README.md +0 -19
  977. package/src/isomorphic/anchor.browser.ts +0 -44
  978. package/src/isomorphic/anchor.node.ts +0 -1
  979. package/src/isomorphic/anchor.ts +0 -1
  980. package/src/isomorphic/anchor29.browser.ts +0 -24
  981. package/src/isomorphic/anchor29.node.ts +0 -1
  982. package/src/isomorphic/anchor29.ts +0 -1
  983. package/src/isomorphic/grpc.browser.ts +0 -4
  984. package/src/isomorphic/grpc.node.ts +0 -130
  985. package/src/isomorphic/grpc.ts +0 -1
  986. package/src/jupiter/jupiterClient.ts +0 -502
  987. package/src/keypair.ts +0 -24
  988. package/src/margin/README.md +0 -138
  989. package/src/marginCalculation.ts +0 -256
  990. package/src/marinade/idl/idl.json +0 -1962
  991. package/src/marinade/index.ts +0 -73
  992. package/src/marinade/types.ts +0 -3925
  993. package/src/math/amm.ts +0 -1423
  994. package/src/math/auction.ts +0 -371
  995. package/src/math/bankruptcy.ts +0 -35
  996. package/src/math/builder.ts +0 -64
  997. package/src/math/conversion.ts +0 -24
  998. package/src/math/exchangeStatus.ts +0 -146
  999. package/src/math/funding.ts +0 -458
  1000. package/src/math/insurance.ts +0 -109
  1001. package/src/math/liquidation.ts +0 -223
  1002. package/src/math/margin.ts +0 -261
  1003. package/src/math/market.ts +0 -356
  1004. package/src/math/oracles.ts +0 -353
  1005. package/src/math/orders.ts +0 -378
  1006. package/src/math/position.ts +0 -334
  1007. package/src/math/repeg.ts +0 -185
  1008. package/src/math/spotBalance.ts +0 -756
  1009. package/src/math/spotMarket.ts +0 -82
  1010. package/src/math/spotPosition.ts +0 -191
  1011. package/src/math/state.ts +0 -40
  1012. package/src/math/superStake.ts +0 -479
  1013. package/src/math/tiers.ts +0 -44
  1014. package/src/math/trade.ts +0 -809
  1015. package/src/math/utils.ts +0 -121
  1016. package/src/memcmp.ts +0 -177
  1017. package/src/oracles/oracleClientCache.ts +0 -25
  1018. package/src/oracles/oracleId.ts +0 -66
  1019. package/src/oracles/prelaunchOracleClient.ts +0 -43
  1020. package/src/oracles/pythClient.ts +0 -94
  1021. package/src/oracles/pythLazerClient.ts +0 -107
  1022. package/src/oracles/quoteAssetOracleClient.ts +0 -25
  1023. package/src/oracles/strictOraclePrice.ts +0 -19
  1024. package/src/oracles/types.ts +0 -37
  1025. package/src/oracles/utils.ts +0 -25
  1026. package/src/orderParams.ts +0 -79
  1027. package/src/orderSubscriber/OrderSubscriber.ts +0 -322
  1028. package/src/orderSubscriber/PollingSubscription.ts +0 -39
  1029. package/src/orderSubscriber/WebsocketSubscription.ts +0 -119
  1030. package/src/orderSubscriber/grpcSubscription.ts +0 -139
  1031. package/src/orderSubscriber/index.ts +0 -7
  1032. package/src/orderSubscriber/types.ts +0 -55
  1033. package/src/priorityFee/averageOverSlotsStrategy.ts +0 -16
  1034. package/src/priorityFee/averageStrategy.ts +0 -12
  1035. package/src/priorityFee/ewmaStrategy.ts +0 -41
  1036. package/src/priorityFee/heliusPriorityFeeMethod.ts +0 -57
  1037. package/src/priorityFee/index.ts +0 -16
  1038. package/src/priorityFee/maxOverSlotsStrategy.ts +0 -17
  1039. package/src/priorityFee/maxStrategy.ts +0 -7
  1040. package/src/priorityFee/priorityFeeSubscriber.ts +0 -289
  1041. package/src/priorityFee/priorityFeeSubscriberMap.ts +0 -115
  1042. package/src/priorityFee/solanaPriorityFeeMethod.ts +0 -34
  1043. package/src/priorityFee/types.ts +0 -64
  1044. package/src/priorityFee/velocityPriorityFeeMethod.ts +0 -42
  1045. package/src/pyth/constants.ts +0 -9
  1046. package/src/pyth/index.ts +0 -15
  1047. package/src/pyth/pythLazerSubscriber.ts +0 -365
  1048. package/src/pyth/types.ts +0 -4451
  1049. package/src/pyth/utils.ts +0 -13
  1050. package/src/slot/SlotSubscriber.ts +0 -108
  1051. package/src/slot/SlothashSubscriber.ts +0 -137
  1052. package/src/swap/UnifiedSwapClient.ts +0 -315
  1053. package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +0 -81
  1054. package/src/swift/index.ts +0 -10
  1055. package/src/swift/signedMsgUserAccountSubscriber.ts +0 -251
  1056. package/src/swift/swiftOrderSubscriber.ts +0 -351
  1057. package/src/testClient.ts +0 -42
  1058. package/src/titan/titanClient.ts +0 -438
  1059. package/src/token/index.ts +0 -13
  1060. package/src/tokenFaucet.ts +0 -296
  1061. package/src/tx/baseTxSender.ts +0 -471
  1062. package/src/tx/blockhashFetcher/baseBlockhashFetcher.ts +0 -19
  1063. package/src/tx/blockhashFetcher/cachedBlockhashFetcher.ts +0 -90
  1064. package/src/tx/blockhashFetcher/types.ts +0 -5
  1065. package/src/tx/fastSingleTxSender.ts +0 -146
  1066. package/src/tx/forwardOnlyTxSender.ts +0 -145
  1067. package/src/tx/priorityFeeCalculator.ts +0 -117
  1068. package/src/tx/reportTransactionError.ts +0 -164
  1069. package/src/tx/retryTxSender.ts +0 -137
  1070. package/src/tx/txHandler.ts +0 -898
  1071. package/src/tx/txParamProcessor.ts +0 -178
  1072. package/src/tx/types.ts +0 -71
  1073. package/src/tx/utils.ts +0 -107
  1074. package/src/tx/whileValidTxSender.ts +0 -295
  1075. package/src/types.ts +0 -1822
  1076. package/src/user.ts +0 -4557
  1077. package/src/userConfig.ts +0 -38
  1078. package/src/userMap/PollingSubscription.ts +0 -47
  1079. package/src/userMap/WebsocketSubscription.ts +0 -84
  1080. package/src/userMap/grpcSubscription.ts +0 -85
  1081. package/src/userMap/referrerMap.ts +0 -271
  1082. package/src/userMap/revenueShareEscrowMap.ts +0 -315
  1083. package/src/userMap/userMap.ts +0 -706
  1084. package/src/userMap/userMapConfig.ts +0 -69
  1085. package/src/userMap/userStatsMap.ts +0 -371
  1086. package/src/userName.ts +0 -21
  1087. package/src/userStats.ts +0 -132
  1088. package/src/userStatsConfig.ts +0 -35
  1089. package/src/util/TransactionConfirmationManager.ts +0 -300
  1090. package/src/util/chainClock.ts +0 -41
  1091. package/src/util/computeUnits.ts +0 -69
  1092. package/src/util/digest.ts +0 -16
  1093. package/src/util/ed25519Utils.ts +0 -85
  1094. package/src/util/promiseTimeout.ts +0 -14
  1095. package/src/velocityClient.ts +0 -11612
  1096. package/src/velocityClientConfig.ts +0 -119
  1097. package/src/wallet.ts +0 -54
  1098. package/tests/VelocityCore/decode.test.ts +0 -16
  1099. package/tests/VelocityCore/fill_trigger.test.ts +0 -73
  1100. package/tests/VelocityCore/instructions.test.ts +0 -46
  1101. package/tests/VelocityCore/pdas.test.ts +0 -30
  1102. package/tests/VelocityCore/perp_orders.test.ts +0 -205
  1103. package/tests/VelocityCore/remainingAccounts.test.ts +0 -72
  1104. package/tests/VelocityCore/settlement_liquidation.test.ts +0 -69
  1105. package/tests/accounts/customizedCadenceBulkAccountLoader.test.ts +0 -211
  1106. package/tests/amm/test.ts +0 -2300
  1107. package/tests/auctions/test.ts +0 -81
  1108. package/tests/bn/test.ts +0 -355
  1109. package/tests/ci/verifyConstants.ts +0 -393
  1110. package/tests/decode/test.ts +0 -262
  1111. package/tests/decode/userAccountBufferStrings.ts +0 -102
  1112. package/tests/dlob/helpers.ts +0 -748
  1113. package/tests/dlob/test.ts +0 -6969
  1114. package/tests/events/parseLogsForCuUsage.ts +0 -139
  1115. package/tests/insurance/test.ts +0 -40
  1116. package/tests/spot/test.ts +0 -226
  1117. package/tests/tx/TransactionConfirmationManager.test.ts +0 -312
  1118. package/tests/tx/cachedBlockhashFetcher.test.ts +0 -96
  1119. package/tests/tx/priorityFeeCalculator.ts +0 -77
  1120. package/tests/tx/priorityFeeStrategy.ts +0 -95
  1121. package/tests/user/getMarginCalculation.ts +0 -361
  1122. package/tests/user/helpers.ts +0 -184
  1123. package/tests/user/liquidations.ts +0 -129
  1124. package/tests/user/marginCalculations.test.ts +0 -321
  1125. package/tests/user/test.ts +0 -829
  1126. package/tsconfig.browser.json +0 -18
  1127. package/tsconfig.json +0 -36
package/src/dlob/DLOB.ts DELETED
@@ -1,2222 +0,0 @@
1
- /**
2
- * DLOB — Decentralized Limit Order Book.
3
- *
4
- * Maintains an in-memory order book built from on-chain `User` accounts.
5
- * Used by keeper bots to identify the best maker orders to match against taker fills.
6
- *
7
- * Key types:
8
- * {@link DLOBNode} — a single order node with price/size/user info (DLOBNode.ts)
9
- * {@link DLOBSubscriber} — subscribes to on-chain accounts and keeps the DLOB live (DLOBSubscriber.ts)
10
- * {@link NodeList} — sorted linked list of DLOBNodes per side/market (NodeList.ts)
11
- * `orderBookLevels.ts` — aggregated L2/L3 book level construction for quoting
12
- */
13
- import { getOrderSignature, NodeList } from './NodeList';
14
- import { BN } from '../isomorphic/anchor';
15
- import {
16
- BASE_PRECISION,
17
- BN_MAX,
18
- PRICE_PRECISION,
19
- QUOTE_PRECISION,
20
- ZERO,
21
- } from '../constants/numericConstants';
22
- import { decodeName } from '../userName';
23
- import { DLOBNode, DLOBNodeType, TriggerOrderNode } from './DLOBNode';
24
- import { VelocityClient } from '../velocityClient';
25
- import {
26
- calculateOrderBaseAssetAmount,
27
- getLimitPrice,
28
- isOrderExpired,
29
- isRestingLimitOrder,
30
- isTriggered,
31
- mustBeTriggered,
32
- } from '../math/orders';
33
- import {
34
- getVariant,
35
- isOneOfVariant,
36
- isVariant,
37
- MarketType,
38
- MarketTypeStr,
39
- Order,
40
- PerpMarketAccount,
41
- PositionDirection,
42
- SpotMarketAccount,
43
- StateAccount,
44
- } from '../types';
45
- import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
46
- import { SlotSubscriber } from '../slot/SlotSubscriber';
47
- import { UserMap } from '../userMap/userMap';
48
- import { PublicKey } from '@solana/web3.js';
49
- import { ammPaused, exchangePaused, fillPaused } from '../math/exchangeStatus';
50
- import {
51
- createL2Levels,
52
- getL2GeneratorFromDLOBNodes,
53
- L2OrderBook,
54
- L2OrderBookGenerator,
55
- L3Level,
56
- L3OrderBook,
57
- mergeL2LevelGenerators,
58
- } from './orderBookLevels';
59
- import { isFallbackAvailableLiquiditySource } from '../math/auction';
60
- import { convertToNumber } from '../math/conversion';
61
-
62
- export type DLOBOrder = { user: PublicKey; order: Order };
63
- export type DLOBOrders = DLOBOrder[];
64
-
65
- export type MarketNodeLists = {
66
- restingLimit: {
67
- ask: NodeList<'restingLimit'>;
68
- bid: NodeList<'restingLimit'>;
69
- };
70
- floatingLimit: {
71
- ask: NodeList<'floatingLimit'>;
72
- bid: NodeList<'floatingLimit'>;
73
- };
74
- takingLimit: {
75
- ask: NodeList<'takingLimit'>;
76
- bid: NodeList<'takingLimit'>;
77
- };
78
- market: {
79
- ask: NodeList<'market'>;
80
- bid: NodeList<'market'>;
81
- };
82
- trigger: {
83
- above: NodeList<'trigger'>;
84
- below: NodeList<'trigger'>;
85
- };
86
- signedMsg: {
87
- ask: NodeList<'signedMsg'>;
88
- bid: NodeList<'signedMsg'>;
89
- };
90
- };
91
-
92
- type OrderBookCallback = () => void;
93
-
94
- /**
95
- * Receives a DLOBNode and is expected to return true if the node should
96
- * be taken into account when generating, or false otherwise.
97
- *
98
- * Currently used in functions that rely on getBestNode
99
- */
100
- export type DLOBFilterFcn = (node: DLOBNode) => boolean;
101
-
102
- export type NodeToFill = {
103
- node: DLOBNode;
104
- makerNodes: DLOBNode[];
105
- };
106
-
107
- export type NodeToTrigger = {
108
- node: TriggerOrderNode;
109
- };
110
-
111
- const SUPPORTED_ORDER_TYPES = [
112
- 'market',
113
- 'limit',
114
- 'triggerMarket',
115
- 'triggerLimit',
116
- 'oracle',
117
- ];
118
-
119
- function getOrderOrThrow(node: DLOBNode): Order {
120
- if (!node.order) {
121
- throw new Error('DLOBNode must have an order');
122
- }
123
- return node.order;
124
- }
125
-
126
- function getUserAccountOrThrow(node: DLOBNode): string {
127
- if (node.userAccount === undefined) {
128
- throw new Error('DLOBNode must have a userAccount');
129
- }
130
- return node.userAccount;
131
- }
132
-
133
- export class DLOB {
134
- openOrders = new Map<MarketTypeStr, Set<string>>();
135
- orderLists = new Map<MarketTypeStr, Map<number, MarketNodeLists>>();
136
- maxSlotForRestingLimitOrders = 0;
137
-
138
- initialized = false;
139
-
140
- public constructor() {
141
- this.init();
142
- }
143
-
144
- private init() {
145
- this.openOrders.set('perp', new Set<string>());
146
- this.openOrders.set('spot', new Set<string>());
147
- this.orderLists.set('perp', new Map<number, MarketNodeLists>());
148
- this.orderLists.set('spot', new Map<number, MarketNodeLists>());
149
- }
150
-
151
- private getOpenOrdersForMarketType(
152
- marketTypeStr: MarketTypeStr
153
- ): Set<string> {
154
- const openOrders = this.openOrders.get(marketTypeStr);
155
- if (!openOrders) {
156
- throw new Error(
157
- `DLOB has no open orders set for market type ${marketTypeStr}`
158
- );
159
- }
160
- return openOrders;
161
- }
162
-
163
- private getOrderListsForMarketType(
164
- marketTypeStr: MarketTypeStr
165
- ): Map<number, MarketNodeLists> {
166
- const orderLists = this.orderLists.get(marketTypeStr);
167
- if (!orderLists) {
168
- throw new Error(
169
- `DLOB has no order lists for market type ${marketTypeStr}`
170
- );
171
- }
172
- return orderLists;
173
- }
174
-
175
- private tryGetMarketNodeLists(
176
- marketTypeStr: MarketTypeStr,
177
- marketIndex: number
178
- ): MarketNodeLists | undefined {
179
- return this.orderLists.get(marketTypeStr)?.get(marketIndex);
180
- }
181
-
182
- private getMarketNodeLists(
183
- marketTypeStr: MarketTypeStr,
184
- marketIndex: number
185
- ): MarketNodeLists {
186
- const marketNodeLists = this.tryGetMarketNodeLists(
187
- marketTypeStr,
188
- marketIndex
189
- );
190
- if (!marketNodeLists) {
191
- throw new Error(
192
- `DLOB has no order lists for market type ${marketTypeStr} and market index ${marketIndex}`
193
- );
194
- }
195
- return marketNodeLists;
196
- }
197
-
198
- public clear() {
199
- for (const openOrders of this.openOrders.values()) {
200
- openOrders.clear();
201
- }
202
- this.openOrders.clear();
203
-
204
- for (const marketNodeListsMap of this.orderLists.values()) {
205
- for (const marketNodeLists of marketNodeListsMap.values()) {
206
- for (const side of Object.values(marketNodeLists)) {
207
- for (const nodeList of Object.values(side)) {
208
- nodeList.clear();
209
- }
210
- }
211
- }
212
- }
213
- this.orderLists.clear();
214
-
215
- this.maxSlotForRestingLimitOrders = 0;
216
-
217
- this.init();
218
- }
219
-
220
- /**
221
- * initializes a new DLOB instance
222
- *
223
- * @returns a promise that resolves when the DLOB is initialized
224
- */
225
- public async initFromUserMap(
226
- userMap: UserMap,
227
- slot: number
228
- ): Promise<boolean> {
229
- if (this.initialized) {
230
- return false;
231
- }
232
-
233
- // initialize the dlob with the user map
234
- for (const user of userMap.values()) {
235
- const userAccount = user.getUserAccountOrThrow();
236
- const userAccountPubkey = user.getUserAccountPublicKey();
237
- const userAccountPubkeyString = userAccountPubkey.toString();
238
-
239
- for (const order of userAccount.orders) {
240
- let baseAssetAmount = order.baseAssetAmount;
241
- if (order.reduceOnly) {
242
- const existingBaseAmount =
243
- userAccount.perpPositions.find(
244
- (pos) =>
245
- pos.marketIndex === order.marketIndex && pos.openOrders > 0
246
- )?.baseAssetAmount || ZERO;
247
- baseAssetAmount = calculateOrderBaseAssetAmount(
248
- order,
249
- existingBaseAmount
250
- );
251
- }
252
-
253
- this.insertOrder(order, userAccountPubkeyString, slot, baseAssetAmount);
254
- }
255
- }
256
-
257
- this.initialized = true;
258
- return true;
259
- }
260
-
261
- public insertOrder(
262
- order: Order,
263
- userAccount: string,
264
- slot: number,
265
- baseAssetAmount: BN,
266
- onInsert?: OrderBookCallback
267
- ): void {
268
- if (!isVariant(order.status, 'open')) {
269
- return;
270
- }
271
-
272
- if (!isOneOfVariant(order.orderType, SUPPORTED_ORDER_TYPES)) {
273
- return;
274
- }
275
-
276
- const marketType = getVariant(order.marketType) as MarketTypeStr;
277
-
278
- if (!this.getOrderListsForMarketType(marketType).has(order.marketIndex)) {
279
- this.addOrderList(marketType, order.marketIndex);
280
- }
281
-
282
- if (isVariant(order.status, 'open')) {
283
- this.getOpenOrdersForMarketType(marketType).add(
284
- getOrderSignature(order.orderId, userAccount)
285
- );
286
- }
287
-
288
- this.getListForOnChainOrder(order, slot)?.insert(
289
- order,
290
- marketType,
291
- userAccount,
292
- baseAssetAmount
293
- );
294
-
295
- if (onInsert) {
296
- onInsert();
297
- }
298
- }
299
-
300
- public insertSignedMsgOrder(
301
- order: Order,
302
- userAccount: string,
303
- baseAssetAmount?: BN,
304
- onInsert?: OrderBookCallback
305
- ): void {
306
- const marketType = getVariant(order.marketType) as MarketTypeStr;
307
- const marketIndex = order.marketIndex;
308
- const bidOrAsk = isVariant(order.direction, 'long') ? 'bid' : 'ask';
309
- if (!this.getOrderListsForMarketType(marketType).has(order.marketIndex)) {
310
- this.addOrderList(marketType, order.marketIndex);
311
- }
312
- this.getOpenOrdersForMarketType(marketType).add(
313
- getOrderSignature(order.orderId, userAccount)
314
- );
315
- this.getMarketNodeLists(marketType, marketIndex).signedMsg[bidOrAsk].insert(
316
- order,
317
- marketType,
318
- userAccount,
319
- baseAssetAmount
320
- );
321
- if (onInsert) {
322
- onInsert();
323
- }
324
- }
325
-
326
- addOrderList(marketType: MarketTypeStr, marketIndex: number): void {
327
- this.getOrderListsForMarketType(marketType).set(marketIndex, {
328
- restingLimit: {
329
- ask: new NodeList('restingLimit', 'asc'),
330
- bid: new NodeList('restingLimit', 'desc'),
331
- },
332
- floatingLimit: {
333
- ask: new NodeList('floatingLimit', 'asc'),
334
- bid: new NodeList('floatingLimit', 'desc'),
335
- },
336
- takingLimit: {
337
- ask: new NodeList('takingLimit', 'asc'),
338
- bid: new NodeList('takingLimit', 'asc'), // always sort ascending for market orders
339
- },
340
- market: {
341
- ask: new NodeList('market', 'asc'),
342
- bid: new NodeList('market', 'asc'), // always sort ascending for market orders
343
- },
344
- trigger: {
345
- above: new NodeList('trigger', 'asc'),
346
- below: new NodeList('trigger', 'desc'),
347
- },
348
- signedMsg: {
349
- ask: new NodeList('signedMsg', 'asc'),
350
- bid: new NodeList('signedMsg', 'asc'),
351
- },
352
- });
353
- }
354
-
355
- public delete(
356
- order: Order,
357
- userAccount: PublicKey,
358
- slot: number,
359
- onDelete?: OrderBookCallback
360
- ): void {
361
- if (!isVariant(order.status, 'open')) {
362
- return;
363
- }
364
-
365
- this.updateRestingLimitOrders(slot);
366
-
367
- this.getListForOnChainOrder(order, slot)?.remove(
368
- order,
369
- userAccount.toString()
370
- );
371
-
372
- if (onDelete) {
373
- onDelete();
374
- }
375
- }
376
-
377
- public getListForOnChainOrder(
378
- order: Order,
379
- slot: number
380
- ): NodeList<any> | undefined {
381
- const isInactiveTriggerOrder =
382
- mustBeTriggered(order) && !isTriggered(order);
383
-
384
- const marketType = getVariant(order.marketType) as MarketTypeStr;
385
-
386
- const marketNodeLists = this.tryGetMarketNodeLists(
387
- marketType,
388
- order.marketIndex
389
- );
390
- if (!marketNodeLists) {
391
- return undefined;
392
- }
393
-
394
- if (isInactiveTriggerOrder) {
395
- const subType = isVariant(order.triggerCondition, 'above')
396
- ? 'above'
397
- : 'below';
398
- return marketNodeLists.trigger[subType];
399
- }
400
-
401
- const subType = isVariant(order.direction, 'long') ? 'bid' : 'ask';
402
- if (
403
- isOneOfVariant(order.orderType, ['market', 'triggerMarket', 'oracle'])
404
- ) {
405
- return marketNodeLists.market[subType];
406
- } else if (!order.oraclePriceOffset.eq(ZERO)) {
407
- return marketNodeLists.floatingLimit[subType];
408
- } else {
409
- const isResting = isRestingLimitOrder(order, slot);
410
- return isResting
411
- ? marketNodeLists.restingLimit[subType]
412
- : marketNodeLists.takingLimit[subType];
413
- }
414
- }
415
-
416
- private getListForOnChainOrderOrThrow(
417
- order: Order,
418
- slot: number
419
- ): NodeList<any> {
420
- const list = this.getListForOnChainOrder(order, slot);
421
- if (!list) {
422
- throw new Error(
423
- `No order list found for order ${order.orderId} in market ${order.marketIndex}`
424
- );
425
- }
426
- return list;
427
- }
428
-
429
- public updateRestingLimitOrders(slot: number): void {
430
- if (slot <= this.maxSlotForRestingLimitOrders) {
431
- return;
432
- }
433
-
434
- this.maxSlotForRestingLimitOrders = slot;
435
-
436
- this.updateRestingLimitOrdersForMarketType(slot, 'perp');
437
-
438
- this.updateRestingLimitOrdersForMarketType(slot, 'spot');
439
- }
440
-
441
- updateRestingLimitOrdersForMarketType(
442
- slot: number,
443
- marketTypeStr: MarketTypeStr
444
- ): void {
445
- for (const [_, nodeLists] of this.getOrderListsForMarketType(
446
- marketTypeStr
447
- )) {
448
- const nodesToUpdate: Array<{ side: 'ask' | 'bid'; node: DLOBNode }> = [];
449
- for (const node of nodeLists.takingLimit.ask.getGenerator()) {
450
- if (!isRestingLimitOrder(getOrderOrThrow(node), slot)) {
451
- continue;
452
- }
453
-
454
- nodesToUpdate.push({
455
- side: 'ask',
456
- node,
457
- });
458
- }
459
-
460
- for (const node of nodeLists.takingLimit.bid.getGenerator()) {
461
- if (!isRestingLimitOrder(getOrderOrThrow(node), slot)) {
462
- continue;
463
- }
464
-
465
- nodesToUpdate.push({
466
- side: 'bid',
467
- node,
468
- });
469
- }
470
-
471
- for (const nodeToUpdate of nodesToUpdate) {
472
- const { side, node } = nodeToUpdate;
473
- const order = getOrderOrThrow(node);
474
- const userAccount = getUserAccountOrThrow(node);
475
- nodeLists.takingLimit[side].remove(order, userAccount);
476
- nodeLists.restingLimit[side].insert(order, marketTypeStr, userAccount);
477
- }
478
- }
479
- }
480
-
481
- public getOrder(orderId: number, userAccount: PublicKey): Order | undefined {
482
- const orderSignature = getOrderSignature(orderId, userAccount.toString());
483
- for (const nodeList of this.getNodeLists()) {
484
- const node = nodeList.get(orderSignature);
485
- if (node) {
486
- return node.order;
487
- }
488
- }
489
-
490
- return undefined;
491
- }
492
-
493
- public findNodesToFill<T extends MarketType>(
494
- marketIndex: number,
495
- fallbackBid: BN | undefined,
496
- fallbackAsk: BN | undefined,
497
- slot: number,
498
- ts: number,
499
- marketType: T,
500
- oraclePriceData: T extends { spot: unknown }
501
- ? OraclePriceData
502
- : MMOraclePriceData,
503
- stateAccount: StateAccount,
504
- marketAccount: T extends { spot: unknown }
505
- ? SpotMarketAccount
506
- : PerpMarketAccount
507
- ): NodeToFill[] {
508
- if (fillPaused(stateAccount, marketAccount)) {
509
- return [];
510
- }
511
-
512
- const isAmmPaused = ammPaused(stateAccount, marketAccount);
513
-
514
- const { makerRebateNumerator, makerRebateDenominator } =
515
- this.getMakerRebate(marketType, stateAccount, marketAccount);
516
-
517
- const takingOrderNodesToFill: Array<NodeToFill> =
518
- this.findTakingNodesToFill(
519
- marketIndex,
520
- slot,
521
- marketType,
522
- oraclePriceData,
523
- isAmmPaused,
524
- stateAccount,
525
- marketAccount,
526
- fallbackAsk,
527
- fallbackBid
528
- );
529
-
530
- const restingLimitOrderNodesToFill: Array<NodeToFill> =
531
- this.findRestingLimitOrderNodesToFill(
532
- marketIndex,
533
- slot,
534
- marketType,
535
- oraclePriceData,
536
- isAmmPaused,
537
- stateAccount,
538
- marketAccount,
539
- makerRebateNumerator,
540
- makerRebateDenominator,
541
- fallbackAsk,
542
- fallbackBid
543
- );
544
-
545
- // get expired market nodes
546
- const expiredNodesToFill = this.findExpiredNodesToFill(
547
- marketIndex,
548
- ts,
549
- marketType,
550
- new BN(slot)
551
- );
552
-
553
- const stepSize = isVariant(marketType, 'perp')
554
- ? (marketAccount as PerpMarketAccount).orderStepSize
555
- : (marketAccount as SpotMarketAccount).orderStepSize;
556
-
557
- const cancelReduceOnlyNodesToFill =
558
- this.findUnfillableReduceOnlyOrdersToCancel(
559
- marketIndex,
560
- marketType,
561
- stepSize
562
- );
563
-
564
- return this.mergeNodesToFill(
565
- restingLimitOrderNodesToFill,
566
- takingOrderNodesToFill
567
- )
568
- .concat(expiredNodesToFill)
569
- .concat(cancelReduceOnlyNodesToFill);
570
- }
571
-
572
- getMakerRebate(
573
- marketType: MarketType,
574
- stateAccount: StateAccount,
575
- marketAccount: PerpMarketAccount | SpotMarketAccount
576
- ): { makerRebateNumerator: number; makerRebateDenominator: number } {
577
- let makerRebateNumerator: number;
578
- let makerRebateDenominator: number;
579
- if (isVariant(marketType, 'perp')) {
580
- makerRebateNumerator =
581
- stateAccount.perpFeeStructure.feeTiers[0].makerRebateNumerator;
582
- makerRebateDenominator =
583
- stateAccount.perpFeeStructure.feeTiers[0].makerRebateDenominator;
584
- } else {
585
- makerRebateNumerator =
586
- stateAccount.spotFeeStructure.feeTiers[0].makerRebateNumerator;
587
- makerRebateDenominator =
588
- stateAccount.spotFeeStructure.feeTiers[0].makerRebateDenominator;
589
- }
590
-
591
- // @ts-ignore
592
- const feeAdjustment = marketAccount.feeAdjustment || 0;
593
- if (feeAdjustment !== 0) {
594
- makerRebateNumerator += (makerRebateNumerator * feeAdjustment) / 100;
595
- }
596
-
597
- return { makerRebateNumerator, makerRebateDenominator };
598
- }
599
-
600
- mergeNodesToFill(
601
- restingLimitOrderNodesToFill: NodeToFill[],
602
- takingOrderNodesToFill: NodeToFill[]
603
- ): NodeToFill[] {
604
- const mergedNodesToFill = new Map<string, NodeToFill>();
605
-
606
- const mergeNodesToFillHelper = (nodesToFillArray: NodeToFill[]) => {
607
- nodesToFillArray.forEach((nodeToFill) => {
608
- const nodeSignature = getOrderSignature(
609
- getOrderOrThrow(nodeToFill.node).orderId,
610
- getUserAccountOrThrow(nodeToFill.node)
611
- );
612
-
613
- let mergedNodeToFill = mergedNodesToFill.get(nodeSignature);
614
- if (!mergedNodeToFill) {
615
- mergedNodeToFill = {
616
- node: nodeToFill.node,
617
- makerNodes: [],
618
- };
619
- mergedNodesToFill.set(nodeSignature, mergedNodeToFill);
620
- }
621
-
622
- if (nodeToFill.makerNodes) {
623
- mergedNodeToFill.makerNodes.push(...nodeToFill.makerNodes);
624
- }
625
- });
626
- };
627
-
628
- mergeNodesToFillHelper(restingLimitOrderNodesToFill);
629
- mergeNodesToFillHelper(takingOrderNodesToFill);
630
-
631
- return Array.from(mergedNodesToFill.values());
632
- }
633
-
634
- public findRestingLimitOrderNodesToFill<T extends MarketType>(
635
- marketIndex: number,
636
- slot: number,
637
- marketType: T,
638
- oraclePriceData: T extends { spot: unknown }
639
- ? OraclePriceData
640
- : MMOraclePriceData,
641
- isAmmPaused: boolean,
642
- stateAccount: StateAccount,
643
- marketAccount: T extends { spot: unknown }
644
- ? SpotMarketAccount
645
- : PerpMarketAccount,
646
- makerRebateNumerator: number,
647
- makerRebateDenominator: number,
648
- fallbackAsk: BN | undefined,
649
- fallbackBid: BN | undefined
650
- ): NodeToFill[] {
651
- const nodesToFill = new Array<NodeToFill>();
652
-
653
- const crossingNodes = this.findCrossingRestingLimitOrders(
654
- marketIndex,
655
- slot,
656
- marketType,
657
- oraclePriceData
658
- );
659
-
660
- for (const crossingNode of crossingNodes) {
661
- nodesToFill.push(crossingNode);
662
- }
663
-
664
- if (fallbackBid && !isAmmPaused) {
665
- const askGenerator = this.getRestingLimitAsks(
666
- marketIndex,
667
- slot,
668
- marketType,
669
- oraclePriceData
670
- );
671
-
672
- const fallbackBidWithBuffer = fallbackBid.sub(
673
- fallbackBid.muln(makerRebateNumerator).divn(makerRebateDenominator)
674
- );
675
-
676
- const asksCrossingFallback = this.findNodesCrossingFallbackLiquidity(
677
- marketType,
678
- slot,
679
- oraclePriceData,
680
- askGenerator,
681
- (askPrice) => {
682
- if (askPrice === undefined) {
683
- throw new Error('Resting limit ask must have a limit price');
684
- }
685
- return askPrice.lte(fallbackBidWithBuffer);
686
- },
687
- stateAccount,
688
- marketAccount
689
- );
690
-
691
- for (const askCrossingFallback of asksCrossingFallback) {
692
- nodesToFill.push(askCrossingFallback);
693
- }
694
- }
695
-
696
- if (fallbackAsk && !isAmmPaused) {
697
- const bidGenerator = this.getRestingLimitBids(
698
- marketIndex,
699
- slot,
700
- marketType,
701
- oraclePriceData
702
- );
703
-
704
- const fallbackAskWithBuffer = fallbackAsk.add(
705
- fallbackAsk.muln(makerRebateNumerator).divn(makerRebateDenominator)
706
- );
707
-
708
- const bidsCrossingFallback = this.findNodesCrossingFallbackLiquidity(
709
- marketType,
710
- slot,
711
- oraclePriceData,
712
- bidGenerator,
713
- (bidPrice) => {
714
- if (bidPrice === undefined) {
715
- throw new Error('Resting limit bid must have a limit price');
716
- }
717
- return bidPrice.gte(fallbackAskWithBuffer);
718
- },
719
- stateAccount,
720
- marketAccount
721
- );
722
-
723
- for (const bidCrossingFallback of bidsCrossingFallback) {
724
- nodesToFill.push(bidCrossingFallback);
725
- }
726
- }
727
-
728
- return nodesToFill;
729
- }
730
-
731
- public findTakingNodesToFill<T extends MarketType>(
732
- marketIndex: number,
733
- slot: number,
734
- marketType: T,
735
- oraclePriceData: T extends { spot: unknown }
736
- ? OraclePriceData
737
- : MMOraclePriceData,
738
- isAmmPaused: boolean,
739
- state: StateAccount,
740
- marketAccount: T extends { spot: unknown }
741
- ? SpotMarketAccount
742
- : PerpMarketAccount,
743
- fallbackAsk: BN | undefined,
744
- fallbackBid?: BN | undefined
745
- ): NodeToFill[] {
746
- const nodesToFill = new Array<NodeToFill>();
747
-
748
- let takingOrderGenerator = this.getTakingAsks(
749
- marketIndex,
750
- marketType,
751
- slot,
752
- oraclePriceData
753
- );
754
-
755
- const takingAsksCrossingBids = this.findTakingNodesCrossingMakerNodes(
756
- marketIndex,
757
- slot,
758
- marketType,
759
- oraclePriceData,
760
- takingOrderGenerator,
761
- this.getRestingLimitBids.bind(this),
762
- (takerPrice, makerPrice) => {
763
- if (isVariant(marketType, 'spot')) {
764
- if (takerPrice === undefined) {
765
- return false;
766
- }
767
-
768
- if (fallbackBid && makerPrice.lt(fallbackBid)) {
769
- return false;
770
- }
771
- }
772
- return takerPrice === undefined || takerPrice.lte(makerPrice);
773
- }
774
- );
775
- for (const takingAskCrossingBid of takingAsksCrossingBids) {
776
- nodesToFill.push(takingAskCrossingBid);
777
- }
778
-
779
- if (fallbackBid && !isAmmPaused) {
780
- takingOrderGenerator = this.getTakingAsks(
781
- marketIndex,
782
- marketType,
783
- slot,
784
- oraclePriceData
785
- );
786
- const takingAsksCrossingFallback =
787
- this.findNodesCrossingFallbackLiquidity(
788
- marketType,
789
- slot,
790
- oraclePriceData,
791
- takingOrderGenerator,
792
- (takerPrice) => {
793
- return takerPrice === undefined || takerPrice.lte(fallbackBid);
794
- },
795
- state,
796
- marketAccount
797
- );
798
-
799
- for (const takingAskCrossingFallback of takingAsksCrossingFallback) {
800
- nodesToFill.push(takingAskCrossingFallback);
801
- }
802
- }
803
-
804
- takingOrderGenerator = this.getTakingBids(
805
- marketIndex,
806
- marketType,
807
- slot,
808
- oraclePriceData
809
- );
810
-
811
- const takingBidsToFill = this.findTakingNodesCrossingMakerNodes(
812
- marketIndex,
813
- slot,
814
- marketType,
815
- oraclePriceData,
816
- takingOrderGenerator,
817
- this.getRestingLimitAsks.bind(this),
818
- (takerPrice, makerPrice) => {
819
- if (isVariant(marketType, 'spot')) {
820
- if (takerPrice === undefined) {
821
- return false;
822
- }
823
-
824
- if (fallbackAsk && makerPrice.gt(fallbackAsk)) {
825
- return false;
826
- }
827
- }
828
-
829
- return takerPrice === undefined || takerPrice.gte(makerPrice);
830
- }
831
- );
832
-
833
- for (const takingBidToFill of takingBidsToFill) {
834
- nodesToFill.push(takingBidToFill);
835
- }
836
-
837
- if (fallbackAsk && !isAmmPaused) {
838
- takingOrderGenerator = this.getTakingBids(
839
- marketIndex,
840
- marketType,
841
- slot,
842
- oraclePriceData
843
- );
844
- const takingBidsCrossingFallback =
845
- this.findNodesCrossingFallbackLiquidity(
846
- marketType,
847
- slot,
848
- oraclePriceData,
849
- takingOrderGenerator,
850
- (takerPrice) => {
851
- return takerPrice === undefined || takerPrice.gte(fallbackAsk);
852
- },
853
- state,
854
- marketAccount
855
- );
856
- for (const marketBidCrossingFallback of takingBidsCrossingFallback) {
857
- nodesToFill.push(marketBidCrossingFallback);
858
- }
859
- }
860
-
861
- return nodesToFill;
862
- }
863
-
864
- public findTakingNodesCrossingMakerNodes<T extends MarketType>(
865
- marketIndex: number,
866
- slot: number,
867
- marketType: T,
868
- oraclePriceData: T extends { spot: unknown }
869
- ? OraclePriceData
870
- : MMOraclePriceData,
871
- takerNodeGenerator: Generator<DLOBNode>,
872
- makerNodeGeneratorFn: (
873
- marketIndex: number,
874
- slot: number,
875
- marketType: T,
876
- oraclePriceData: T extends { spot: unknown }
877
- ? OraclePriceData
878
- : MMOraclePriceData
879
- ) => Generator<DLOBNode>,
880
- doesCross: (takerPrice: BN | undefined, makerPrice: BN) => boolean
881
- ): NodeToFill[] {
882
- const nodesToFill = new Array<NodeToFill>();
883
-
884
- for (const takerNode of takerNodeGenerator) {
885
- const makerNodeGenerator = makerNodeGeneratorFn(
886
- marketIndex,
887
- slot,
888
- marketType,
889
- oraclePriceData
890
- );
891
-
892
- for (const makerNode of makerNodeGenerator) {
893
- // Can't match orders from the same user
894
- const sameUser = takerNode.userAccount === makerNode.userAccount;
895
- if (sameUser) {
896
- continue;
897
- }
898
-
899
- const makerPrice = makerNode.getPriceOrThrow(oraclePriceData, slot);
900
- const takerPrice = takerNode.getPrice(oraclePriceData, slot);
901
-
902
- const ordersCross = doesCross(takerPrice, makerPrice);
903
- if (!ordersCross) {
904
- // market orders aren't sorted by price, they are sorted by time, so we need to traverse
905
- // through all of em
906
- break;
907
- }
908
-
909
- nodesToFill.push({
910
- node: takerNode,
911
- makerNodes: [makerNode],
912
- });
913
-
914
- const makerOrder = getOrderOrThrow(makerNode);
915
- const takerOrder = getOrderOrThrow(takerNode);
916
-
917
- const makerBaseRemaining = makerOrder.baseAssetAmount.sub(
918
- makerOrder.baseAssetAmountFilled
919
- );
920
- const takerBaseRemaining = takerOrder.baseAssetAmount.sub(
921
- takerOrder.baseAssetAmountFilled
922
- );
923
-
924
- const baseFilled = BN.min(makerBaseRemaining, takerBaseRemaining);
925
-
926
- const newMakerOrder = { ...makerOrder };
927
- newMakerOrder.baseAssetAmountFilled =
928
- makerOrder.baseAssetAmountFilled.add(baseFilled);
929
- this.getListForOnChainOrderOrThrow(newMakerOrder, slot).update(
930
- newMakerOrder,
931
- getUserAccountOrThrow(makerNode)
932
- );
933
-
934
- const newTakerOrder = { ...takerOrder };
935
- newTakerOrder.baseAssetAmountFilled =
936
- takerOrder.baseAssetAmountFilled.add(baseFilled);
937
-
938
- if (takerNode.isSignedMsg) {
939
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
940
- const marketNodeLists = this.getMarketNodeLists(
941
- marketTypeStr,
942
- marketIndex
943
- );
944
- const orderList = isVariant(takerOrder.direction, 'long')
945
- ? marketNodeLists.signedMsg.bid
946
- : marketNodeLists.signedMsg.ask;
947
- orderList.update(newTakerOrder, getUserAccountOrThrow(takerNode));
948
- } else {
949
- this.getListForOnChainOrderOrThrow(newTakerOrder, slot).update(
950
- newTakerOrder,
951
- getUserAccountOrThrow(takerNode)
952
- );
953
- }
954
-
955
- if (
956
- newTakerOrder.baseAssetAmountFilled.eq(takerOrder.baseAssetAmount)
957
- ) {
958
- break;
959
- }
960
- }
961
- }
962
-
963
- return nodesToFill;
964
- }
965
-
966
- public findNodesCrossingFallbackLiquidity<T extends MarketType>(
967
- marketType: T,
968
- slot: number,
969
- oraclePriceData: T extends { spot: unknown }
970
- ? OraclePriceData
971
- : MMOraclePriceData,
972
- nodeGenerator: Generator<DLOBNode>,
973
- doesCross: (nodePrice: BN | undefined) => boolean,
974
- state: StateAccount,
975
- marketAccount: T extends { spot: unknown }
976
- ? SpotMarketAccount
977
- : PerpMarketAccount
978
- ): NodeToFill[] {
979
- const nodesToFill = new Array<NodeToFill>();
980
-
981
- let nextNode = nodeGenerator.next();
982
- while (!nextNode.done) {
983
- const node = nextNode.value;
984
-
985
- if (isVariant(marketType, 'spot') && node.order?.postOnly) {
986
- nextNode = nodeGenerator.next();
987
- continue;
988
- }
989
- const nodeOrder = getOrderOrThrow(node);
990
- const nodePrice = getLimitPrice(nodeOrder, oraclePriceData, slot);
991
-
992
- // order crosses if there is no limit price or it crosses fallback price
993
- const crosses = doesCross(nodePrice);
994
-
995
- // fallback is available if auction is complete or it's a spot order
996
- const fallbackAvailable =
997
- isVariant(marketType, 'spot') ||
998
- isFallbackAvailableLiquiditySource(
999
- nodeOrder,
1000
- oraclePriceData as MMOraclePriceData,
1001
- slot,
1002
- state,
1003
- marketAccount as PerpMarketAccount
1004
- );
1005
-
1006
- if (crosses && fallbackAvailable) {
1007
- nodesToFill.push({
1008
- node: node,
1009
- makerNodes: [], // filled by fallback
1010
- });
1011
- }
1012
-
1013
- nextNode = nodeGenerator.next();
1014
- }
1015
-
1016
- return nodesToFill;
1017
- }
1018
-
1019
- public findExpiredNodesToFill(
1020
- marketIndex: number,
1021
- ts: number,
1022
- marketType: MarketType,
1023
- slot?: BN
1024
- ): NodeToFill[] {
1025
- const nodesToFill = new Array<NodeToFill>();
1026
-
1027
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1028
- const nodeLists = this.tryGetMarketNodeLists(marketTypeStr, marketIndex);
1029
-
1030
- if (!nodeLists) {
1031
- return nodesToFill;
1032
- }
1033
-
1034
- // All bids/asks that can expire
1035
- // dont try to expire limit orders with tif as its inefficient use of blockspace
1036
- const bidGenerators = [
1037
- nodeLists.takingLimit.bid.getGenerator(),
1038
- nodeLists.restingLimit.bid.getGenerator(),
1039
- nodeLists.floatingLimit.bid.getGenerator(),
1040
- nodeLists.market.bid.getGenerator(),
1041
- nodeLists.signedMsg.bid.getGenerator(),
1042
- ];
1043
- const askGenerators = [
1044
- nodeLists.takingLimit.ask.getGenerator(),
1045
- nodeLists.restingLimit.ask.getGenerator(),
1046
- nodeLists.floatingLimit.ask.getGenerator(),
1047
- nodeLists.market.ask.getGenerator(),
1048
- nodeLists.signedMsg.ask.getGenerator(),
1049
- ];
1050
-
1051
- for (const bidGenerator of bidGenerators) {
1052
- for (const bid of bidGenerator) {
1053
- const bidOrder = getOrderOrThrow(bid);
1054
- if (bid.isSignedMsg) {
1055
- if (slot === undefined) {
1056
- throw new Error(
1057
- 'Must provide slot to findExpiredNodesToFill to expire signedMsg orders'
1058
- );
1059
- }
1060
- if (slot.gt(bidOrder.slot.addn(bidOrder.auctionDuration))) {
1061
- nodeLists.signedMsg.bid.remove(
1062
- bidOrder,
1063
- getUserAccountOrThrow(bid)
1064
- );
1065
- continue;
1066
- }
1067
- }
1068
- if (isOrderExpired(bidOrder, ts, true, 25)) {
1069
- nodesToFill.push({
1070
- node: bid,
1071
- makerNodes: [],
1072
- });
1073
- }
1074
- }
1075
- }
1076
-
1077
- for (const askGenerator of askGenerators) {
1078
- for (const ask of askGenerator) {
1079
- const askOrder = getOrderOrThrow(ask);
1080
- if (ask.isSignedMsg) {
1081
- if (slot === undefined) {
1082
- throw new Error(
1083
- 'Must provide slot to findExpiredNodesToFill to expire signedMsg orders'
1084
- );
1085
- }
1086
- if (slot.gt(askOrder.slot.addn(askOrder.auctionDuration))) {
1087
- nodeLists.signedMsg.ask.remove(
1088
- askOrder,
1089
- getUserAccountOrThrow(ask)
1090
- );
1091
- continue;
1092
- }
1093
- }
1094
- if (isOrderExpired(askOrder, ts, true, 25)) {
1095
- nodesToFill.push({
1096
- node: ask,
1097
- makerNodes: [],
1098
- });
1099
- }
1100
- }
1101
- }
1102
-
1103
- return nodesToFill;
1104
- }
1105
-
1106
- public findUnfillableReduceOnlyOrdersToCancel(
1107
- marketIndex: number,
1108
- marketType: MarketType,
1109
- stepSize: BN
1110
- ): NodeToFill[] {
1111
- const nodesToFill = new Array<NodeToFill>();
1112
-
1113
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1114
- const nodeLists = this.tryGetMarketNodeLists(marketTypeStr, marketIndex);
1115
-
1116
- if (!nodeLists) {
1117
- return nodesToFill;
1118
- }
1119
-
1120
- const generators = [
1121
- nodeLists.takingLimit.bid.getGenerator(),
1122
- nodeLists.restingLimit.bid.getGenerator(),
1123
- nodeLists.floatingLimit.bid.getGenerator(),
1124
- nodeLists.market.bid.getGenerator(),
1125
- nodeLists.signedMsg.bid.getGenerator(),
1126
- nodeLists.takingLimit.ask.getGenerator(),
1127
- nodeLists.restingLimit.ask.getGenerator(),
1128
- nodeLists.floatingLimit.ask.getGenerator(),
1129
- nodeLists.market.ask.getGenerator(),
1130
- nodeLists.signedMsg.ask.getGenerator(),
1131
- nodeLists.trigger.above.getGenerator(),
1132
- nodeLists.trigger.below.getGenerator(),
1133
- ];
1134
-
1135
- for (const generator of generators) {
1136
- for (const node of generator) {
1137
- if (!getOrderOrThrow(node).reduceOnly) {
1138
- continue;
1139
- }
1140
-
1141
- if (node.baseAssetAmount.lt(stepSize)) {
1142
- nodesToFill.push({
1143
- node,
1144
- makerNodes: [],
1145
- });
1146
- }
1147
- }
1148
- }
1149
-
1150
- return nodesToFill;
1151
- }
1152
- *getTakingBids<T extends MarketType>(
1153
- marketIndex: number,
1154
- marketType: T,
1155
- slot: number,
1156
- oraclePriceData: T extends { spot: unknown }
1157
- ? OraclePriceData
1158
- : MMOraclePriceData,
1159
- filterFcn?: DLOBFilterFcn
1160
- ): Generator<DLOBNode> {
1161
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1162
- const orderLists = this.tryGetMarketNodeLists(marketTypeStr, marketIndex);
1163
- if (!orderLists) {
1164
- return;
1165
- }
1166
-
1167
- this.updateRestingLimitOrders(slot);
1168
-
1169
- const generatorList = [
1170
- orderLists.market.bid.getGenerator(),
1171
- orderLists.takingLimit.bid.getGenerator(),
1172
- this.signedMsgGenerator(
1173
- orderLists.signedMsg.bid,
1174
- (x: DLOBNode) => !isRestingLimitOrder(getOrderOrThrow(x), slot)
1175
- ),
1176
- ];
1177
-
1178
- yield* this.getBestNode(
1179
- generatorList,
1180
- oraclePriceData,
1181
- slot,
1182
- (bestNode, currentNode) => {
1183
- return getOrderOrThrow(bestNode).slot.lt(
1184
- getOrderOrThrow(currentNode).slot
1185
- );
1186
- },
1187
- filterFcn
1188
- );
1189
- }
1190
-
1191
- *getTakingAsks<T extends MarketType>(
1192
- marketIndex: number,
1193
- marketType: T,
1194
- slot: number,
1195
- oraclePriceData: T extends { spot: unknown }
1196
- ? OraclePriceData
1197
- : MMOraclePriceData,
1198
- filterFcn?: DLOBFilterFcn
1199
- ): Generator<DLOBNode> {
1200
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1201
- const orderLists = this.tryGetMarketNodeLists(marketTypeStr, marketIndex);
1202
- if (!orderLists) {
1203
- return;
1204
- }
1205
-
1206
- this.updateRestingLimitOrders(slot);
1207
-
1208
- const generatorList = [
1209
- orderLists.market.ask.getGenerator(),
1210
- orderLists.takingLimit.ask.getGenerator(),
1211
- this.signedMsgGenerator(
1212
- orderLists.signedMsg.ask,
1213
- (x: DLOBNode) => !isRestingLimitOrder(getOrderOrThrow(x), slot)
1214
- ),
1215
- ];
1216
-
1217
- yield* this.getBestNode(
1218
- generatorList,
1219
- oraclePriceData,
1220
- slot,
1221
- (bestNode, currentNode) => {
1222
- return getOrderOrThrow(bestNode).slot.lt(
1223
- getOrderOrThrow(currentNode).slot
1224
- );
1225
- },
1226
- filterFcn
1227
- );
1228
- }
1229
-
1230
- protected *signedMsgGenerator(
1231
- signedMsgOrderList: NodeList<'signedMsg'>,
1232
- filter: (x: DLOBNode) => boolean
1233
- ): Generator<DLOBNode> {
1234
- for (const signedMsgOrder of signedMsgOrderList.getGenerator()) {
1235
- if (filter(signedMsgOrder)) {
1236
- yield signedMsgOrder;
1237
- }
1238
- }
1239
- }
1240
-
1241
- protected *getBestNode<T extends MarketTypeStr>(
1242
- generatorList: Array<Generator<DLOBNode>>,
1243
- oraclePriceData: T extends 'spot' ? OraclePriceData : MMOraclePriceData,
1244
- slot: number,
1245
- compareFcn: (
1246
- bestDLOBNode: DLOBNode,
1247
- currentDLOBNode: DLOBNode,
1248
- slot: number,
1249
- oraclePriceData: T extends 'spot' ? OraclePriceData : MMOraclePriceData
1250
- ) => boolean,
1251
- filterFcn?: DLOBFilterFcn
1252
- ): Generator<DLOBNode> {
1253
- const generators = generatorList.map((generator) => {
1254
- return {
1255
- next: generator.next(),
1256
- generator,
1257
- };
1258
- });
1259
-
1260
- let sideExhausted = false;
1261
- while (!sideExhausted) {
1262
- const bestGenerator = generators.reduce(
1263
- (bestGenerator, currentGenerator) => {
1264
- if (currentGenerator.next.done) {
1265
- return bestGenerator;
1266
- }
1267
-
1268
- if (bestGenerator.next.done) {
1269
- return currentGenerator;
1270
- }
1271
-
1272
- const bestValue = bestGenerator.next.value as DLOBNode;
1273
- const currentValue = currentGenerator.next.value as DLOBNode;
1274
-
1275
- return compareFcn(bestValue, currentValue, slot, oraclePriceData)
1276
- ? bestGenerator
1277
- : currentGenerator;
1278
- }
1279
- );
1280
-
1281
- if (!bestGenerator.next.done) {
1282
- // skip this node if it's already completely filled
1283
- if (bestGenerator.next.value.isBaseFilled()) {
1284
- bestGenerator.next = bestGenerator.generator.next();
1285
- continue;
1286
- }
1287
-
1288
- if (filterFcn && !filterFcn(bestGenerator.next.value)) {
1289
- bestGenerator.next = bestGenerator.generator.next();
1290
- continue;
1291
- }
1292
-
1293
- yield bestGenerator.next.value;
1294
- bestGenerator.next = bestGenerator.generator.next();
1295
- } else {
1296
- sideExhausted = true;
1297
- }
1298
- }
1299
- }
1300
-
1301
- *getRestingLimitAsks<T extends MarketType>(
1302
- marketIndex: number,
1303
- slot: number,
1304
- marketType: T,
1305
- oraclePriceData: T extends { spot: unknown }
1306
- ? OraclePriceData
1307
- : MMOraclePriceData,
1308
- filterFcn?: DLOBFilterFcn
1309
- ): Generator<DLOBNode> {
1310
- if (isVariant(marketType, 'spot') && !oraclePriceData) {
1311
- throw new Error('Must provide OraclePriceData to get spot asks');
1312
- }
1313
-
1314
- this.updateRestingLimitOrders(slot);
1315
-
1316
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1317
- const nodeLists = this.tryGetMarketNodeLists(marketTypeStr, marketIndex);
1318
-
1319
- if (!nodeLists) {
1320
- return;
1321
- }
1322
-
1323
- const generatorList = [
1324
- nodeLists.restingLimit.ask.getGenerator(),
1325
- nodeLists.floatingLimit.ask.getGenerator(),
1326
- this.signedMsgGenerator(nodeLists.signedMsg.ask, (x: DLOBNode) =>
1327
- isRestingLimitOrder(getOrderOrThrow(x), slot)
1328
- ),
1329
- ];
1330
-
1331
- yield* this.getBestNode(
1332
- generatorList,
1333
- oraclePriceData,
1334
- slot,
1335
- (bestNode, currentNode, slot, oraclePriceData) => {
1336
- return bestNode
1337
- .getPriceOrThrow(oraclePriceData, slot)
1338
- .lt(currentNode.getPriceOrThrow(oraclePriceData, slot));
1339
- },
1340
- filterFcn
1341
- );
1342
- }
1343
-
1344
- *getRestingLimitBids<T extends MarketType>(
1345
- marketIndex: number,
1346
- slot: number,
1347
- marketType: T,
1348
- oraclePriceData: T extends { spot: unknown }
1349
- ? OraclePriceData
1350
- : MMOraclePriceData,
1351
- filterFcn?: DLOBFilterFcn
1352
- ): Generator<DLOBNode> {
1353
- if (isVariant(marketType, 'spot') && !oraclePriceData) {
1354
- throw new Error('Must provide OraclePriceData to get spot bids');
1355
- }
1356
-
1357
- this.updateRestingLimitOrders(slot);
1358
-
1359
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1360
- const nodeLists = this.tryGetMarketNodeLists(marketTypeStr, marketIndex);
1361
-
1362
- if (!nodeLists) {
1363
- return;
1364
- }
1365
-
1366
- const generatorList = [
1367
- nodeLists.restingLimit.bid.getGenerator(),
1368
- nodeLists.floatingLimit.bid.getGenerator(),
1369
- this.signedMsgGenerator(nodeLists.signedMsg.bid, (x: DLOBNode) =>
1370
- isRestingLimitOrder(getOrderOrThrow(x), slot)
1371
- ),
1372
- ];
1373
-
1374
- yield* this.getBestNode(
1375
- generatorList,
1376
- oraclePriceData,
1377
- slot,
1378
- (bestNode, currentNode, slot, oraclePriceData) => {
1379
- return bestNode
1380
- .getPriceOrThrow(oraclePriceData, slot)
1381
- .gt(currentNode.getPriceOrThrow(oraclePriceData, slot));
1382
- },
1383
- filterFcn
1384
- );
1385
- }
1386
-
1387
- /**
1388
- * This will look at both the taking and resting limit asks
1389
- * @param marketIndex
1390
- * @param fallbackAsk
1391
- * @param slot
1392
- * @param marketType
1393
- * @param oraclePriceData
1394
- * @param filterFcn
1395
- */
1396
- *getAsks<T extends MarketType>(
1397
- marketIndex: number,
1398
- _fallbackAsk: BN | undefined,
1399
- slot: number,
1400
- marketType: T,
1401
- oraclePriceData: T extends { spot: unknown }
1402
- ? OraclePriceData
1403
- : MMOraclePriceData,
1404
- filterFcn?: DLOBFilterFcn
1405
- ): Generator<DLOBNode> {
1406
- if (isVariant(marketType, 'spot') && !oraclePriceData) {
1407
- throw new Error('Must provide OraclePriceData to get spot asks');
1408
- }
1409
-
1410
- const generatorList = [
1411
- this.getTakingAsks(marketIndex, marketType, slot, oraclePriceData),
1412
- this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData),
1413
- ];
1414
-
1415
- yield* this.getBestNode(
1416
- generatorList,
1417
- oraclePriceData,
1418
- slot,
1419
- (bestNode, currentNode, slot, oraclePriceData) => {
1420
- const bestNodePrice = bestNode.getPrice(oraclePriceData, slot) ?? ZERO;
1421
- const currentNodePrice =
1422
- currentNode.getPrice(oraclePriceData, slot) ?? ZERO;
1423
-
1424
- if (bestNodePrice.eq(currentNodePrice)) {
1425
- return getOrderOrThrow(bestNode).slot.lt(
1426
- getOrderOrThrow(currentNode).slot
1427
- );
1428
- }
1429
-
1430
- return bestNodePrice.lt(currentNodePrice);
1431
- },
1432
- filterFcn
1433
- );
1434
- }
1435
-
1436
- /**
1437
- * This will look at both the taking and resting limit bids
1438
- * @param marketIndex
1439
- * @param fallbackBid
1440
- * @param slot
1441
- * @param marketType
1442
- * @param oraclePriceData
1443
- * @param filterFcn
1444
- */
1445
- *getBids<T extends MarketType>(
1446
- marketIndex: number,
1447
- _fallbackBid: BN | undefined,
1448
- slot: number,
1449
- marketType: T,
1450
- oraclePriceData: T extends { spot: unknown }
1451
- ? OraclePriceData
1452
- : MMOraclePriceData,
1453
- filterFcn?: DLOBFilterFcn
1454
- ): Generator<DLOBNode> {
1455
- if (isVariant(marketType, 'spot') && !oraclePriceData) {
1456
- throw new Error('Must provide OraclePriceData to get spot bids');
1457
- }
1458
-
1459
- const generatorList = [
1460
- this.getTakingBids(marketIndex, marketType, slot, oraclePriceData),
1461
- this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData),
1462
- ];
1463
-
1464
- yield* this.getBestNode(
1465
- generatorList,
1466
- oraclePriceData,
1467
- slot,
1468
- (bestNode, currentNode, slot, oraclePriceData) => {
1469
- const bestNodePrice =
1470
- bestNode.getPrice(oraclePriceData, slot) ?? BN_MAX;
1471
- const currentNodePrice =
1472
- currentNode.getPrice(oraclePriceData, slot) ?? BN_MAX;
1473
-
1474
- if (bestNodePrice.eq(currentNodePrice)) {
1475
- return getOrderOrThrow(bestNode).slot.lt(
1476
- getOrderOrThrow(currentNode).slot
1477
- );
1478
- }
1479
-
1480
- return bestNodePrice.gt(currentNodePrice);
1481
- },
1482
- filterFcn
1483
- );
1484
- }
1485
-
1486
- findCrossingRestingLimitOrders<T extends MarketType>(
1487
- marketIndex: number,
1488
- slot: number,
1489
- marketType: T,
1490
- oraclePriceData: T extends { spot: unknown }
1491
- ? OraclePriceData
1492
- : MMOraclePriceData
1493
- ): NodeToFill[] {
1494
- const nodesToFill = new Array<NodeToFill>();
1495
-
1496
- for (const askNode of this.getRestingLimitAsks(
1497
- marketIndex,
1498
- slot,
1499
- marketType,
1500
- oraclePriceData
1501
- )) {
1502
- const bidGenerator = this.getRestingLimitBids(
1503
- marketIndex,
1504
- slot,
1505
- marketType,
1506
- oraclePriceData
1507
- );
1508
-
1509
- for (const bidNode of bidGenerator) {
1510
- const bidPrice = bidNode.getPriceOrThrow(oraclePriceData, slot);
1511
- const askPrice = askNode.getPriceOrThrow(oraclePriceData, slot);
1512
-
1513
- // orders don't cross
1514
- if (bidPrice.lt(askPrice)) {
1515
- break;
1516
- }
1517
-
1518
- const bidOrder = getOrderOrThrow(bidNode);
1519
- const askOrder = getOrderOrThrow(askNode);
1520
-
1521
- // Can't match orders from the same user
1522
- const sameUser = bidNode.userAccount === askNode.userAccount;
1523
- if (sameUser) {
1524
- continue;
1525
- }
1526
-
1527
- const makerAndTaker = this.determineMakerAndTaker(askNode, bidNode);
1528
-
1529
- // unable to match maker and taker due to post only or slot
1530
- if (!makerAndTaker) {
1531
- continue;
1532
- }
1533
-
1534
- const { takerNode, makerNode } = makerAndTaker;
1535
-
1536
- const bidBaseRemaining = bidOrder.baseAssetAmount.sub(
1537
- bidOrder.baseAssetAmountFilled
1538
- );
1539
- const askBaseRemaining = askOrder.baseAssetAmount.sub(
1540
- askOrder.baseAssetAmountFilled
1541
- );
1542
-
1543
- const baseFilled = BN.min(bidBaseRemaining, askBaseRemaining);
1544
-
1545
- const newBidOrder = { ...bidOrder };
1546
- newBidOrder.baseAssetAmountFilled =
1547
- bidOrder.baseAssetAmountFilled.add(baseFilled);
1548
- this.getListForOnChainOrderOrThrow(newBidOrder, slot).update(
1549
- newBidOrder,
1550
- getUserAccountOrThrow(bidNode)
1551
- );
1552
-
1553
- // ask completely filled
1554
- const newAskOrder = { ...askOrder };
1555
- newAskOrder.baseAssetAmountFilled =
1556
- askOrder.baseAssetAmountFilled.add(baseFilled);
1557
- this.getListForOnChainOrderOrThrow(newAskOrder, slot).update(
1558
- newAskOrder,
1559
- getUserAccountOrThrow(askNode)
1560
- );
1561
-
1562
- nodesToFill.push({
1563
- node: takerNode,
1564
- makerNodes: [makerNode],
1565
- });
1566
-
1567
- if (newAskOrder.baseAssetAmount.eq(newAskOrder.baseAssetAmountFilled)) {
1568
- break;
1569
- }
1570
- }
1571
- }
1572
-
1573
- return nodesToFill;
1574
- }
1575
-
1576
- determineMakerAndTaker(
1577
- askNode: DLOBNode,
1578
- bidNode: DLOBNode
1579
- ): { takerNode: DLOBNode; makerNode: DLOBNode } | undefined {
1580
- const askOrder = getOrderOrThrow(askNode);
1581
- const bidOrder = getOrderOrThrow(bidNode);
1582
- const askSlot = askOrder.slot.add(new BN(askOrder.auctionDuration));
1583
- const bidSlot = bidOrder.slot.add(new BN(bidOrder.auctionDuration));
1584
-
1585
- if (bidOrder.postOnly && askOrder.postOnly) {
1586
- return undefined;
1587
- } else if (bidOrder.postOnly) {
1588
- return {
1589
- takerNode: askNode,
1590
- makerNode: bidNode,
1591
- };
1592
- } else if (askOrder.postOnly) {
1593
- return {
1594
- takerNode: bidNode,
1595
- makerNode: askNode,
1596
- };
1597
- } else if (askSlot.lte(bidSlot)) {
1598
- return {
1599
- takerNode: bidNode,
1600
- makerNode: askNode,
1601
- };
1602
- } else {
1603
- return {
1604
- takerNode: askNode,
1605
- makerNode: bidNode,
1606
- };
1607
- }
1608
- }
1609
-
1610
- public getBestAsk<T extends MarketType>(
1611
- marketIndex: number,
1612
- slot: number,
1613
- marketType: T,
1614
- oraclePriceData: T extends { spot: unknown }
1615
- ? OraclePriceData
1616
- : MMOraclePriceData
1617
- ): BN | undefined {
1618
- const bestAsk = this.getRestingLimitAsks(
1619
- marketIndex,
1620
- slot,
1621
- marketType,
1622
- oraclePriceData
1623
- ).next().value;
1624
-
1625
- if (bestAsk) {
1626
- return bestAsk.getPrice(oraclePriceData, slot);
1627
- }
1628
- return undefined;
1629
- }
1630
-
1631
- public getBestBid<T extends MarketType>(
1632
- marketIndex: number,
1633
- slot: number,
1634
- marketType: T,
1635
- oraclePriceData: T extends { spot: unknown }
1636
- ? OraclePriceData
1637
- : MMOraclePriceData
1638
- ): BN | undefined {
1639
- const bestBid = this.getRestingLimitBids(
1640
- marketIndex,
1641
- slot,
1642
- marketType,
1643
- oraclePriceData
1644
- ).next().value;
1645
-
1646
- if (bestBid) {
1647
- return bestBid.getPrice(oraclePriceData, slot);
1648
- }
1649
- return undefined;
1650
- }
1651
-
1652
- public *getStopLosses(
1653
- marketIndex: number,
1654
- marketType: MarketType,
1655
- direction: PositionDirection
1656
- ): Generator<DLOBNode> {
1657
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1658
- const marketNodeLists = this.getMarketNodeLists(marketTypeStr, marketIndex);
1659
-
1660
- if (isVariant(direction, 'long') && marketNodeLists.trigger.below) {
1661
- for (const node of marketNodeLists.trigger.below.getGenerator()) {
1662
- if (isVariant(getOrderOrThrow(node).direction, 'short')) {
1663
- yield node;
1664
- }
1665
- }
1666
- } else if (isVariant(direction, 'short') && marketNodeLists.trigger.above) {
1667
- for (const node of marketNodeLists.trigger.above.getGenerator()) {
1668
- if (isVariant(getOrderOrThrow(node).direction, 'long')) {
1669
- yield node;
1670
- }
1671
- }
1672
- }
1673
- }
1674
-
1675
- public *getStopLossMarkets(
1676
- marketIndex: number,
1677
- marketType: MarketType,
1678
- direction: PositionDirection
1679
- ): Generator<DLOBNode> {
1680
- for (const node of this.getStopLosses(marketIndex, marketType, direction)) {
1681
- if (isVariant(getOrderOrThrow(node).orderType, 'triggerMarket')) {
1682
- yield node;
1683
- }
1684
- }
1685
- }
1686
-
1687
- public *getStopLossLimits(
1688
- marketIndex: number,
1689
- marketType: MarketType,
1690
- direction: PositionDirection
1691
- ): Generator<DLOBNode> {
1692
- for (const node of this.getStopLosses(marketIndex, marketType, direction)) {
1693
- if (isVariant(getOrderOrThrow(node).orderType, 'triggerLimit')) {
1694
- yield node;
1695
- }
1696
- }
1697
- }
1698
-
1699
- public *getTakeProfits(
1700
- marketIndex: number,
1701
- marketType: MarketType,
1702
- direction: PositionDirection
1703
- ): Generator<DLOBNode> {
1704
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1705
- const marketNodeLists = this.getMarketNodeLists(marketTypeStr, marketIndex);
1706
-
1707
- if (isVariant(direction, 'long') && marketNodeLists.trigger.above) {
1708
- for (const node of marketNodeLists.trigger.above.getGenerator()) {
1709
- if (isVariant(getOrderOrThrow(node).direction, 'short')) {
1710
- yield node;
1711
- }
1712
- }
1713
- } else if (isVariant(direction, 'short') && marketNodeLists.trigger.below) {
1714
- for (const node of marketNodeLists.trigger.below.getGenerator()) {
1715
- if (isVariant(getOrderOrThrow(node).direction, 'long')) {
1716
- yield node;
1717
- }
1718
- }
1719
- }
1720
- }
1721
-
1722
- public *getTakeProfitMarkets(
1723
- marketIndex: number,
1724
- marketType: MarketType,
1725
- direction: PositionDirection
1726
- ): Generator<DLOBNode> {
1727
- for (const node of this.getTakeProfits(
1728
- marketIndex,
1729
- marketType,
1730
- direction
1731
- )) {
1732
- if (isVariant(getOrderOrThrow(node).orderType, 'triggerMarket')) {
1733
- yield node;
1734
- }
1735
- }
1736
- }
1737
-
1738
- public *getTakeProfitLimits(
1739
- marketIndex: number,
1740
- marketType: MarketType,
1741
- direction: PositionDirection
1742
- ): Generator<DLOBNode> {
1743
- for (const node of this.getTakeProfits(
1744
- marketIndex,
1745
- marketType,
1746
- direction
1747
- )) {
1748
- if (isVariant(getOrderOrThrow(node).orderType, 'triggerLimit')) {
1749
- yield node;
1750
- }
1751
- }
1752
- }
1753
-
1754
- public findNodesToTrigger(
1755
- marketIndex: number,
1756
- slot: number,
1757
- triggerPrice: BN,
1758
- marketType: MarketType,
1759
- stateAccount: StateAccount
1760
- ): NodeToTrigger[] {
1761
- if (exchangePaused(stateAccount)) {
1762
- return [];
1763
- }
1764
-
1765
- const nodesToTrigger: NodeToTrigger[] = [];
1766
- const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1767
- const marketNodeLists = this.orderLists
1768
- .get(marketTypeStr)
1769
- ?.get(marketIndex);
1770
-
1771
- const triggerAboveList = marketNodeLists
1772
- ? marketNodeLists.trigger.above
1773
- : undefined;
1774
- if (triggerAboveList) {
1775
- for (
1776
- let node = triggerAboveList.head;
1777
- node !== undefined;
1778
- node = node.next
1779
- ) {
1780
- if (triggerPrice.gt(node.order.triggerPrice)) {
1781
- nodesToTrigger.push({
1782
- node: node,
1783
- });
1784
- } else {
1785
- break;
1786
- }
1787
- }
1788
- }
1789
-
1790
- const triggerBelowList = marketNodeLists
1791
- ? marketNodeLists.trigger.below
1792
- : undefined;
1793
- if (triggerBelowList) {
1794
- for (
1795
- let node = triggerBelowList.head;
1796
- node !== undefined;
1797
- node = node.next
1798
- ) {
1799
- if (triggerPrice.lt(node.order.triggerPrice)) {
1800
- nodesToTrigger.push({
1801
- node: node,
1802
- });
1803
- } else {
1804
- break;
1805
- }
1806
- }
1807
- }
1808
-
1809
- return nodesToTrigger;
1810
- }
1811
-
1812
- public printTop(
1813
- velocityClient: VelocityClient,
1814
- slotSubscriber: SlotSubscriber,
1815
- marketIndex: number,
1816
- marketType: MarketType
1817
- ) {
1818
- if (isVariant(marketType, 'perp')) {
1819
- const slot = slotSubscriber.getSlot();
1820
- const oraclePriceData =
1821
- velocityClient.getMMOracleDataForPerpMarket(marketIndex);
1822
-
1823
- const bestAsk = this.getBestAsk(
1824
- marketIndex,
1825
- slot,
1826
- marketType,
1827
- oraclePriceData
1828
- );
1829
- const bestBid = this.getBestBid(
1830
- marketIndex,
1831
- slot,
1832
- marketType,
1833
- oraclePriceData
1834
- );
1835
- if (bestAsk === undefined || bestBid === undefined) {
1836
- throw new Error(
1837
- `printTop requires both a best ask and best bid for market ${marketIndex}`
1838
- );
1839
- }
1840
- const mid = bestAsk.add(bestBid).div(new BN(2));
1841
-
1842
- const bidSpread =
1843
- (convertToNumber(bestBid, PRICE_PRECISION) /
1844
- convertToNumber(oraclePriceData.price, PRICE_PRECISION) -
1845
- 1) *
1846
- 100.0;
1847
- const askSpread =
1848
- (convertToNumber(bestAsk, PRICE_PRECISION) /
1849
- convertToNumber(oraclePriceData.price, PRICE_PRECISION) -
1850
- 1) *
1851
- 100.0;
1852
-
1853
- const name = decodeName(
1854
- velocityClient.getPerpMarketAccountOrThrow(marketIndex).name
1855
- );
1856
- console.log(`Market ${name} Orders`);
1857
- console.log(
1858
- ` Ask`,
1859
- convertToNumber(bestAsk, PRICE_PRECISION).toFixed(3),
1860
- `(${askSpread.toFixed(4)}%)`
1861
- );
1862
- console.log(` Mid`, convertToNumber(mid, PRICE_PRECISION).toFixed(3));
1863
- console.log(
1864
- ` Bid`,
1865
- convertToNumber(bestBid, PRICE_PRECISION).toFixed(3),
1866
- `(${bidSpread.toFixed(4)}%)`
1867
- );
1868
- } else if (isVariant(marketType, 'spot')) {
1869
- const slot = slotSubscriber.getSlot();
1870
- const oraclePriceData =
1871
- velocityClient.getOracleDataForSpotMarket(marketIndex);
1872
-
1873
- const bestAsk = this.getBestAsk(
1874
- marketIndex,
1875
- slot,
1876
- MarketType.SPOT,
1877
- oraclePriceData
1878
- );
1879
- const bestBid = this.getBestBid(
1880
- marketIndex,
1881
- slot,
1882
- MarketType.SPOT,
1883
- oraclePriceData
1884
- );
1885
- if (bestAsk === undefined || bestBid === undefined) {
1886
- throw new Error(
1887
- `printTop requires both a best ask and best bid for market ${marketIndex}`
1888
- );
1889
- }
1890
- const mid = bestAsk.add(bestBid).div(new BN(2));
1891
-
1892
- const bidSpread =
1893
- (convertToNumber(bestBid, PRICE_PRECISION) /
1894
- convertToNumber(oraclePriceData.price, PRICE_PRECISION) -
1895
- 1) *
1896
- 100.0;
1897
- const askSpread =
1898
- (convertToNumber(bestAsk, PRICE_PRECISION) /
1899
- convertToNumber(oraclePriceData.price, PRICE_PRECISION) -
1900
- 1) *
1901
- 100.0;
1902
-
1903
- const name = decodeName(
1904
- velocityClient.getSpotMarketAccountOrThrow(marketIndex).name
1905
- );
1906
- console.log(`Market ${name} Orders`);
1907
- console.log(
1908
- ` Ask`,
1909
- convertToNumber(bestAsk, PRICE_PRECISION).toFixed(3),
1910
- `(${askSpread.toFixed(4)}%)`
1911
- );
1912
- console.log(` Mid`, convertToNumber(mid, PRICE_PRECISION).toFixed(3));
1913
- console.log(
1914
- ` Bid`,
1915
- convertToNumber(bestBid, PRICE_PRECISION).toFixed(3),
1916
- `(${bidSpread.toFixed(4)}%)`
1917
- );
1918
- }
1919
- }
1920
-
1921
- public getDLOBOrders(): DLOBOrders {
1922
- const dlobOrders: DLOBOrders = [];
1923
-
1924
- for (const nodeList of this.getNodeLists()) {
1925
- for (const node of nodeList.getGenerator()) {
1926
- dlobOrders.push({
1927
- user: new PublicKey(getUserAccountOrThrow(node)),
1928
- order: getOrderOrThrow(node),
1929
- });
1930
- }
1931
- }
1932
-
1933
- return dlobOrders;
1934
- }
1935
-
1936
- *getNodeLists(): Generator<NodeList<DLOBNodeType>> {
1937
- for (const [_, nodeLists] of this.getOrderListsForMarketType('perp')) {
1938
- yield nodeLists.restingLimit.bid;
1939
- yield nodeLists.restingLimit.ask;
1940
- yield nodeLists.takingLimit.bid;
1941
- yield nodeLists.takingLimit.ask;
1942
- yield nodeLists.market.bid;
1943
- yield nodeLists.market.ask;
1944
- yield nodeLists.floatingLimit.bid;
1945
- yield nodeLists.floatingLimit.ask;
1946
- yield nodeLists.trigger.above;
1947
- yield nodeLists.trigger.below;
1948
- }
1949
-
1950
- for (const [_, nodeLists] of this.getOrderListsForMarketType('spot')) {
1951
- yield nodeLists.restingLimit.bid;
1952
- yield nodeLists.restingLimit.ask;
1953
- yield nodeLists.takingLimit.bid;
1954
- yield nodeLists.takingLimit.ask;
1955
- yield nodeLists.market.bid;
1956
- yield nodeLists.market.ask;
1957
- yield nodeLists.floatingLimit.bid;
1958
- yield nodeLists.floatingLimit.ask;
1959
- yield nodeLists.trigger.above;
1960
- yield nodeLists.trigger.below;
1961
- }
1962
- }
1963
-
1964
- /**
1965
- * Get an L2 view of the order book for a given market.
1966
- *
1967
- * @param marketIndex
1968
- * @param marketType
1969
- * @param slot
1970
- * @param oraclePriceData
1971
- * @param depth how many levels of the order book to return
1972
- * @param fallbackL2Generators L2 generators for fallback liquidity e.g. vAMM {@link getVammL2Generator}
1973
- */
1974
- public getL2<T extends MarketType>({
1975
- marketIndex,
1976
- marketType,
1977
- slot,
1978
- oraclePriceData,
1979
- depth,
1980
- fallbackL2Generators = [],
1981
- }: {
1982
- marketIndex: number;
1983
- marketType: T;
1984
- slot: number;
1985
- oraclePriceData: T extends { spot: unknown }
1986
- ? OraclePriceData
1987
- : MMOraclePriceData;
1988
- depth: number;
1989
- fallbackL2Generators?: L2OrderBookGenerator[];
1990
- }): L2OrderBook {
1991
- const makerAskL2LevelGenerator = getL2GeneratorFromDLOBNodes(
1992
- this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData),
1993
- oraclePriceData,
1994
- slot
1995
- );
1996
-
1997
- const fallbackAskGenerators = fallbackL2Generators.map(
1998
- (fallbackL2Generator) => {
1999
- return fallbackL2Generator.getL2Asks();
2000
- }
2001
- );
2002
-
2003
- const askL2LevelGenerator = mergeL2LevelGenerators(
2004
- [makerAskL2LevelGenerator, ...fallbackAskGenerators],
2005
- (a, b) => {
2006
- return a.price.lt(b.price);
2007
- }
2008
- );
2009
-
2010
- const asks = createL2Levels(askL2LevelGenerator, depth);
2011
-
2012
- const makerBidGenerator = getL2GeneratorFromDLOBNodes(
2013
- this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData),
2014
- oraclePriceData,
2015
- slot
2016
- );
2017
-
2018
- const fallbackBidGenerators = fallbackL2Generators.map((fallbackOrders) => {
2019
- return fallbackOrders.getL2Bids();
2020
- });
2021
-
2022
- const bidL2LevelGenerator = mergeL2LevelGenerators(
2023
- [makerBidGenerator, ...fallbackBidGenerators],
2024
- (a, b) => {
2025
- return a.price.gt(b.price);
2026
- }
2027
- );
2028
-
2029
- const bids = createL2Levels(bidL2LevelGenerator, depth);
2030
-
2031
- return {
2032
- bids,
2033
- asks,
2034
- slot,
2035
- };
2036
- }
2037
-
2038
- /**
2039
- * Get an L3 view of the order book for a given market. Does not include fallback liquidity sources
2040
- *
2041
- * @param marketIndex
2042
- * @param marketType
2043
- * @param slot
2044
- * @param oraclePriceData
2045
- */
2046
- public getL3<T extends MarketType>({
2047
- marketIndex,
2048
- marketType,
2049
- slot,
2050
- oraclePriceData,
2051
- }: {
2052
- marketIndex: number;
2053
- marketType: T;
2054
- slot: number;
2055
- oraclePriceData: T extends { spot: unknown }
2056
- ? OraclePriceData
2057
- : MMOraclePriceData;
2058
- }): L3OrderBook {
2059
- const bids: L3Level[] = [];
2060
- const asks: L3Level[] = [];
2061
-
2062
- const restingAsks = this.getRestingLimitAsks(
2063
- marketIndex,
2064
- slot,
2065
- marketType,
2066
- oraclePriceData
2067
- );
2068
-
2069
- for (const ask of restingAsks) {
2070
- const askOrder = getOrderOrThrow(ask);
2071
- asks.push({
2072
- price: ask.getPriceOrThrow(oraclePriceData, slot),
2073
- size: askOrder.baseAssetAmount.sub(askOrder.baseAssetAmountFilled),
2074
- maker: new PublicKey(getUserAccountOrThrow(ask)),
2075
- orderId: askOrder.orderId,
2076
- });
2077
- }
2078
-
2079
- const restingBids = this.getRestingLimitBids(
2080
- marketIndex,
2081
- slot,
2082
- marketType,
2083
- oraclePriceData
2084
- );
2085
-
2086
- for (const bid of restingBids) {
2087
- const bidOrder = getOrderOrThrow(bid);
2088
- bids.push({
2089
- price: bid.getPriceOrThrow(oraclePriceData, slot),
2090
- size: bidOrder.baseAssetAmount.sub(bidOrder.baseAssetAmountFilled),
2091
- maker: new PublicKey(getUserAccountOrThrow(bid)),
2092
- orderId: bidOrder.orderId,
2093
- });
2094
- }
2095
-
2096
- return {
2097
- bids,
2098
- asks,
2099
- slot,
2100
- };
2101
- }
2102
-
2103
- private estimateFillExactBaseAmountInForSide(
2104
- baseAmountIn: BN,
2105
- oraclePriceData: OraclePriceData,
2106
- slot: number,
2107
- dlobSide: Generator<DLOBNode>
2108
- ): BN {
2109
- let runningSumQuote = ZERO;
2110
- let runningSumBase = ZERO;
2111
- for (const side of dlobSide) {
2112
- const price = side.getPriceOrThrow(oraclePriceData, slot); //side.order.quoteAssetAmount.div(side.order.baseAssetAmount);
2113
- const sideOrder = getOrderOrThrow(side);
2114
- const baseAmountRemaining = sideOrder.baseAssetAmount.sub(
2115
- sideOrder.baseAssetAmountFilled
2116
- );
2117
- if (runningSumBase.add(baseAmountRemaining).gt(baseAmountIn)) {
2118
- const remainingBase = baseAmountIn.sub(runningSumBase);
2119
- runningSumBase = runningSumBase.add(remainingBase);
2120
- runningSumQuote = runningSumQuote.add(remainingBase.mul(price));
2121
- break;
2122
- } else {
2123
- runningSumBase = runningSumBase.add(baseAmountRemaining);
2124
- runningSumQuote = runningSumQuote.add(baseAmountRemaining.mul(price));
2125
- }
2126
- }
2127
-
2128
- return runningSumQuote
2129
- .mul(QUOTE_PRECISION)
2130
- .div(BASE_PRECISION.mul(PRICE_PRECISION));
2131
- }
2132
-
2133
- /**
2134
- *
2135
- * @param param.marketIndex the index of the market
2136
- * @param param.marketType the type of the market
2137
- * @param param.baseAmount the base amount in to estimate
2138
- * @param param.orderDirection the direction of the trade
2139
- * @param param.slot current slot for estimating dlob node price
2140
- * @param param.oraclePriceData the oracle price data
2141
- * @returns the estimated quote amount filled: QUOTE_PRECISION
2142
- */
2143
- public estimateFillWithExactBaseAmount<T extends MarketType>({
2144
- marketIndex,
2145
- marketType,
2146
- baseAmount,
2147
- orderDirection,
2148
- slot,
2149
- oraclePriceData,
2150
- }: {
2151
- marketIndex: number;
2152
- marketType: T;
2153
- baseAmount: BN;
2154
- orderDirection: PositionDirection;
2155
- slot: number;
2156
- oraclePriceData: T extends { spot: unknown }
2157
- ? OraclePriceData
2158
- : MMOraclePriceData;
2159
- }): BN {
2160
- if (isVariant(orderDirection, 'long')) {
2161
- return this.estimateFillExactBaseAmountInForSide(
2162
- baseAmount,
2163
- oraclePriceData,
2164
- slot,
2165
- this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData)
2166
- );
2167
- } else if (isVariant(orderDirection, 'short')) {
2168
- return this.estimateFillExactBaseAmountInForSide(
2169
- baseAmount,
2170
- oraclePriceData,
2171
- slot,
2172
- this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData)
2173
- );
2174
- }
2175
- throw new Error(
2176
- `Invalid order direction ${getVariant(
2177
- orderDirection
2178
- )}: must be long or short`
2179
- );
2180
- }
2181
-
2182
- public getBestMakers<T extends MarketType>({
2183
- marketIndex,
2184
- marketType,
2185
- direction,
2186
- slot,
2187
- oraclePriceData,
2188
- numMakers,
2189
- }: {
2190
- marketIndex: number;
2191
- marketType: T;
2192
- direction: PositionDirection;
2193
- slot: number;
2194
- oraclePriceData: T extends { spot: unknown }
2195
- ? OraclePriceData
2196
- : MMOraclePriceData;
2197
- numMakers: number;
2198
- }): PublicKey[] {
2199
- const makers = new Map<string, PublicKey>();
2200
- const generator = isVariant(direction, 'long')
2201
- ? this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData)
2202
- : this.getRestingLimitAsks(
2203
- marketIndex,
2204
- slot,
2205
- marketType,
2206
- oraclePriceData
2207
- );
2208
-
2209
- for (const node of generator) {
2210
- const userAccount = getUserAccountOrThrow(node);
2211
- if (!makers.has(userAccount)) {
2212
- makers.set(userAccount, new PublicKey(userAccount));
2213
- }
2214
-
2215
- if (makers.size === numMakers) {
2216
- break;
2217
- }
2218
- }
2219
-
2220
- return Array.from(makers.values());
2221
- }
2222
- }