@velocity-exchange/sdk 0.3.0 → 0.4.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 (1046) hide show
  1. package/CHANGELOG.md +51 -0
  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 +2136 -4
  85. package/lib/browser/adminClient.js +2150 -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 +88 -21
  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 +22 -0
  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 +4 -4
  171. package/lib/browser/idl/velocity.json +4 -4
  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 +56 -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/slot/SlotSubscriber.d.ts +18 -0
  275. package/lib/browser/slot/SlotSubscriber.js +16 -0
  276. package/lib/browser/slot/SlothashSubscriber.d.ts +26 -0
  277. package/lib/browser/slot/SlothashSubscriber.js +25 -1
  278. package/lib/browser/tx/baseTxSender.d.ts +163 -0
  279. package/lib/browser/tx/baseTxSender.js +174 -6
  280. package/lib/browser/tx/blockhashFetcher/baseBlockhashFetcher.d.ts +12 -0
  281. package/lib/browser/tx/blockhashFetcher/baseBlockhashFetcher.js +12 -0
  282. package/lib/browser/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts +15 -0
  283. package/lib/browser/tx/blockhashFetcher/cachedBlockhashFetcher.js +15 -0
  284. package/lib/browser/tx/blockhashFetcher/types.d.ts +4 -0
  285. package/lib/browser/tx/fastSingleTxSender.d.ts +47 -0
  286. package/lib/browser/tx/fastSingleTxSender.js +48 -1
  287. package/lib/browser/tx/forwardOnlyTxSender.d.ts +48 -0
  288. package/lib/browser/tx/forwardOnlyTxSender.js +48 -0
  289. package/lib/browser/tx/reportTransactionError.d.ts +18 -6
  290. package/lib/browser/tx/reportTransactionError.js +18 -6
  291. package/lib/browser/tx/retryTxSender.d.ts +40 -0
  292. package/lib/browser/tx/retryTxSender.js +39 -0
  293. package/lib/browser/tx/txHandler.d.ts +146 -35
  294. package/lib/browser/tx/txHandler.js +132 -32
  295. package/lib/browser/tx/txParamProcessor.d.ts +47 -0
  296. package/lib/browser/tx/txParamProcessor.js +47 -0
  297. package/lib/browser/tx/types.d.ts +46 -0
  298. package/lib/browser/tx/types.js +5 -0
  299. package/lib/browser/tx/utils.d.ts +26 -0
  300. package/lib/browser/tx/utils.js +26 -0
  301. package/lib/browser/tx/whileValidTxSender.d.ts +82 -0
  302. package/lib/browser/tx/whileValidTxSender.js +81 -0
  303. package/lib/browser/types.d.ts +781 -13
  304. package/lib/browser/types.js +126 -9
  305. package/lib/browser/user.d.ts +645 -84
  306. package/lib/browser/user.js +779 -96
  307. package/lib/browser/userMap/PollingSubscription.d.ts +10 -0
  308. package/lib/browser/userMap/PollingSubscription.js +14 -2
  309. package/lib/browser/userMap/WebsocketSubscription.d.ts +9 -0
  310. package/lib/browser/userMap/WebsocketSubscription.js +9 -0
  311. package/lib/browser/userMap/grpcSubscription.d.ts +8 -0
  312. package/lib/browser/userMap/grpcSubscription.js +8 -0
  313. package/lib/browser/userMap/referrerMap.d.ts +72 -2
  314. package/lib/browser/userMap/referrerMap.js +84 -2
  315. package/lib/browser/userMap/revenueShareEscrowMap.d.ts +37 -10
  316. package/lib/browser/userMap/revenueShareEscrowMap.js +38 -11
  317. package/lib/browser/userMap/userMap.d.ts +81 -3
  318. package/lib/browser/userMap/userMap.js +81 -4
  319. package/lib/browser/userMap/userMapConfig.d.ts +23 -0
  320. package/lib/browser/userMap/userStatsMap.d.ts +22 -0
  321. package/lib/browser/userMap/userStatsMap.js +22 -0
  322. package/lib/browser/userName.d.ts +16 -0
  323. package/lib/browser/userName.js +16 -0
  324. package/lib/browser/userStats.d.ts +28 -1
  325. package/lib/browser/userStats.js +28 -1
  326. package/lib/browser/util/TransactionConfirmationManager.d.ts +33 -0
  327. package/lib/browser/util/TransactionConfirmationManager.js +33 -0
  328. package/lib/browser/util/chainClock.d.ts +24 -0
  329. package/lib/browser/util/chainClock.js +20 -0
  330. package/lib/browser/util/computeUnits.d.ts +32 -0
  331. package/lib/browser/util/computeUnits.js +32 -0
  332. package/lib/browser/util/digest.d.ts +17 -0
  333. package/lib/browser/util/digest.js +17 -0
  334. package/lib/browser/util/ed25519Utils.d.ts +12 -2
  335. package/lib/browser/util/ed25519Utils.js +12 -2
  336. package/lib/browser/util/promiseTimeout.d.ts +9 -0
  337. package/lib/browser/util/promiseTimeout.js +9 -0
  338. package/lib/browser/velocityClient.d.ts +3322 -215
  339. package/lib/browser/velocityClient.js +3412 -245
  340. package/lib/browser/wallet.d.ts +37 -0
  341. package/lib/browser/wallet.js +37 -0
  342. package/lib/node/accounts/basicUserAccountSubscriber.d.ts +18 -0
  343. package/lib/node/accounts/basicUserAccountSubscriber.d.ts.map +1 -1
  344. package/lib/node/accounts/basicUserAccountSubscriber.js +18 -0
  345. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts +18 -0
  346. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts.map +1 -1
  347. package/lib/node/accounts/basicUserStatsAccountSubscriber.js +18 -0
  348. package/lib/node/accounts/bulkAccountLoader.d.ts +52 -0
  349. package/lib/node/accounts/bulkAccountLoader.d.ts.map +1 -1
  350. package/lib/node/accounts/bulkAccountLoader.js +51 -0
  351. package/lib/node/accounts/bulkUserStatsSubscription.d.ts +9 -2
  352. package/lib/node/accounts/bulkUserStatsSubscription.d.ts.map +1 -1
  353. package/lib/node/accounts/bulkUserStatsSubscription.js +9 -2
  354. package/lib/node/accounts/bulkUserSubscription.d.ts +8 -2
  355. package/lib/node/accounts/bulkUserSubscription.d.ts.map +1 -1
  356. package/lib/node/accounts/bulkUserSubscription.js +8 -2
  357. package/lib/node/accounts/customizedCadenceBulkAccountLoader.d.ts +37 -0
  358. package/lib/node/accounts/customizedCadenceBulkAccountLoader.d.ts.map +1 -1
  359. package/lib/node/accounts/customizedCadenceBulkAccountLoader.js +37 -0
  360. package/lib/node/accounts/fetch.d.ts +59 -2
  361. package/lib/node/accounts/fetch.d.ts.map +1 -1
  362. package/lib/node/accounts/fetch.js +57 -2
  363. package/lib/node/accounts/grpcAccountSubscriber.d.ts +30 -0
  364. package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
  365. package/lib/node/accounts/grpcAccountSubscriber.js +30 -0
  366. package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +16 -0
  367. package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  368. package/lib/node/accounts/grpcInsuranceFundStakeAccountSubscriber.js +16 -0
  369. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +60 -1
  370. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
  371. package/lib/node/accounts/grpcMultiAccountSubscriber.js +118 -37
  372. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts +32 -0
  373. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts.map +1 -1
  374. package/lib/node/accounts/grpcMultiUserAccountSubscriber.js +40 -12
  375. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +33 -0
  376. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts.map +1 -1
  377. package/lib/node/accounts/grpcProgramAccountSubscriber.js +33 -0
  378. package/lib/node/accounts/grpcUserAccountSubscriber.d.ts +15 -0
  379. package/lib/node/accounts/grpcUserAccountSubscriber.d.ts.map +1 -1
  380. package/lib/node/accounts/grpcUserAccountSubscriber.js +15 -0
  381. package/lib/node/accounts/grpcUserStatsAccountSubscriber.d.ts +15 -0
  382. package/lib/node/accounts/grpcUserStatsAccountSubscriber.d.ts.map +1 -1
  383. package/lib/node/accounts/grpcUserStatsAccountSubscriber.js +15 -0
  384. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts +41 -0
  385. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts.map +1 -1
  386. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.js +41 -0
  387. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +116 -0
  388. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  389. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.js +152 -1
  390. package/lib/node/accounts/laserProgramAccountSubscriber.d.ts +34 -0
  391. package/lib/node/accounts/laserProgramAccountSubscriber.d.ts.map +1 -1
  392. package/lib/node/accounts/laserProgramAccountSubscriber.js +34 -0
  393. package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts +19 -0
  394. package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts.map +1 -1
  395. package/lib/node/accounts/oneShotUserAccountSubscriber.js +19 -0
  396. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts +19 -0
  397. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts.map +1 -1
  398. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.js +19 -0
  399. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +31 -0
  400. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  401. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.js +31 -0
  402. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts +19 -0
  403. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts.map +1 -1
  404. package/lib/node/accounts/pollingOracleAccountSubscriber.js +21 -2
  405. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts +22 -0
  406. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -1
  407. package/lib/node/accounts/pollingTokenAccountSubscriber.js +22 -0
  408. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts +32 -0
  409. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts.map +1 -1
  410. package/lib/node/accounts/pollingUserAccountSubscriber.js +32 -0
  411. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts +22 -0
  412. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts.map +1 -1
  413. package/lib/node/accounts/pollingUserStatsAccountSubscriber.js +22 -0
  414. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts +83 -0
  415. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts.map +1 -1
  416. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.js +84 -1
  417. package/lib/node/accounts/testBulkAccountLoader.d.ts +8 -0
  418. package/lib/node/accounts/testBulkAccountLoader.d.ts.map +1 -1
  419. package/lib/node/accounts/testBulkAccountLoader.js +8 -0
  420. package/lib/node/accounts/types.d.ts +149 -0
  421. package/lib/node/accounts/types.d.ts.map +1 -1
  422. package/lib/node/accounts/types.js +13 -0
  423. package/lib/node/accounts/utils.d.ts +18 -0
  424. package/lib/node/accounts/utils.d.ts.map +1 -1
  425. package/lib/node/accounts/utils.js +32 -16
  426. package/lib/node/accounts/webSocketAccountSubscriber.d.ts +41 -0
  427. package/lib/node/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
  428. package/lib/node/accounts/webSocketAccountSubscriber.js +41 -0
  429. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts +31 -0
  430. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts.map +1 -1
  431. package/lib/node/accounts/webSocketAccountSubscriberV2.js +31 -23
  432. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +27 -0
  433. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  434. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +27 -0
  435. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts +36 -0
  436. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts.map +1 -1
  437. package/lib/node/accounts/webSocketProgramAccountSubscriber.js +36 -0
  438. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +21 -0
  439. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -1
  440. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +25 -0
  441. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts +21 -0
  442. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts.map +1 -1
  443. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.js +21 -0
  444. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts +26 -0
  445. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -1
  446. package/lib/node/accounts/webSocketUserAccountSubscriber.js +26 -0
  447. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  448. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts.map +1 -1
  449. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.js +20 -0
  450. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts +121 -0
  451. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts.map +1 -1
  452. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.js +123 -2
  453. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +96 -0
  454. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  455. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.js +108 -0
  456. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts +28 -0
  457. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts.map +1 -1
  458. package/lib/node/accounts/websocketProgramUserAccountSubscriber.js +33 -0
  459. package/lib/node/addresses/marketAddresses.d.ts +10 -0
  460. package/lib/node/addresses/marketAddresses.d.ts.map +1 -1
  461. package/lib/node/addresses/marketAddresses.js +10 -0
  462. package/lib/node/addresses/pda.d.ts +214 -2
  463. package/lib/node/addresses/pda.d.ts.map +1 -1
  464. package/lib/node/addresses/pda.js +217 -5
  465. package/lib/node/adminClient.d.ts +2136 -4
  466. package/lib/node/adminClient.d.ts.map +1 -1
  467. package/lib/node/adminClient.js +2150 -4
  468. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +15 -0
  469. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
  470. package/lib/node/auctionSubscriber/auctionSubscriber.js +15 -0
  471. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +19 -0
  472. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
  473. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +19 -0
  474. package/lib/node/auctionSubscriber/index.d.ts +6 -0
  475. package/lib/node/auctionSubscriber/index.d.ts.map +1 -1
  476. package/lib/node/auctionSubscriber/index.js +6 -0
  477. package/lib/node/auctionSubscriber/types.d.ts +7 -0
  478. package/lib/node/auctionSubscriber/types.d.ts.map +1 -1
  479. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts +33 -0
  480. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts.map +1 -1
  481. package/lib/node/blockhashSubscriber/BlockhashSubscriber.js +33 -0
  482. package/lib/node/blockhashSubscriber/index.d.ts +5 -0
  483. package/lib/node/blockhashSubscriber/index.d.ts.map +1 -1
  484. package/lib/node/blockhashSubscriber/index.js +5 -0
  485. package/lib/node/blockhashSubscriber/types.d.ts +5 -0
  486. package/lib/node/blockhashSubscriber/types.d.ts.map +1 -1
  487. package/lib/node/clock/clockSubscriber.d.ts +24 -0
  488. package/lib/node/clock/clockSubscriber.d.ts.map +1 -1
  489. package/lib/node/clock/clockSubscriber.js +22 -0
  490. package/lib/node/config.d.ts +58 -4
  491. package/lib/node/config.d.ts.map +1 -1
  492. package/lib/node/config.js +88 -21
  493. package/lib/node/constants/numericConstants.d.ts +67 -0
  494. package/lib/node/constants/numericConstants.d.ts.map +1 -1
  495. package/lib/node/constants/numericConstants.js +68 -1
  496. package/lib/node/constants/perpMarkets.d.ts +10 -0
  497. package/lib/node/constants/perpMarkets.d.ts.map +1 -1
  498. package/lib/node/constants/perpMarkets.js +40 -908
  499. package/lib/node/constants/spotMarkets.d.ts +13 -0
  500. package/lib/node/constants/spotMarkets.d.ts.map +1 -1
  501. package/lib/node/constants/spotMarkets.js +16 -742
  502. package/lib/node/constants/txConstants.d.ts +1 -0
  503. package/lib/node/constants/txConstants.d.ts.map +1 -1
  504. package/lib/node/constants/txConstants.js +1 -0
  505. package/lib/node/core/VelocityCore.d.ts +285 -8
  506. package/lib/node/core/VelocityCore.d.ts.map +1 -1
  507. package/lib/node/core/VelocityCore.js +281 -8
  508. package/lib/node/core/instructions/deposit.d.ts +22 -0
  509. package/lib/node/core/instructions/deposit.d.ts.map +1 -1
  510. package/lib/node/core/instructions/deposit.js +22 -0
  511. package/lib/node/core/instructions/fill.d.ts +17 -0
  512. package/lib/node/core/instructions/fill.d.ts.map +1 -1
  513. package/lib/node/core/instructions/fill.js +17 -0
  514. package/lib/node/core/instructions/funding.d.ts +12 -0
  515. package/lib/node/core/instructions/funding.d.ts.map +1 -1
  516. package/lib/node/core/instructions/funding.js +12 -0
  517. package/lib/node/core/instructions/liquidation.d.ts +18 -0
  518. package/lib/node/core/instructions/liquidation.d.ts.map +1 -1
  519. package/lib/node/core/instructions/liquidation.js +18 -0
  520. package/lib/node/core/instructions/orders.d.ts +28 -0
  521. package/lib/node/core/instructions/orders.d.ts.map +1 -1
  522. package/lib/node/core/instructions/orders.js +28 -0
  523. package/lib/node/core/instructions/perpOrders.d.ts +106 -0
  524. package/lib/node/core/instructions/perpOrders.d.ts.map +1 -1
  525. package/lib/node/core/instructions/perpOrders.js +106 -0
  526. package/lib/node/core/instructions/settlement.d.ts +14 -0
  527. package/lib/node/core/instructions/settlement.d.ts.map +1 -1
  528. package/lib/node/core/instructions/settlement.js +14 -0
  529. package/lib/node/core/instructions/trigger.d.ts +15 -0
  530. package/lib/node/core/instructions/trigger.d.ts.map +1 -1
  531. package/lib/node/core/instructions/trigger.js +15 -0
  532. package/lib/node/core/instructions/withdraw.d.ts +21 -0
  533. package/lib/node/core/instructions/withdraw.d.ts.map +1 -1
  534. package/lib/node/core/instructions/withdraw.js +21 -0
  535. package/lib/node/core/remainingAccounts.d.ts +47 -3
  536. package/lib/node/core/remainingAccounts.d.ts.map +1 -1
  537. package/lib/node/core/remainingAccounts.js +20 -0
  538. package/lib/node/core/signedMsg.d.ts +38 -0
  539. package/lib/node/core/signedMsg.d.ts.map +1 -1
  540. package/lib/node/core/signedMsg.js +33 -0
  541. package/lib/node/decode/customCoder.d.ts +39 -3
  542. package/lib/node/decode/customCoder.d.ts.map +1 -1
  543. package/lib/node/decode/customCoder.js +45 -3
  544. package/lib/node/decode/user.d.ts +22 -0
  545. package/lib/node/decode/user.d.ts.map +1 -1
  546. package/lib/node/decode/user.js +22 -0
  547. package/lib/node/dlob/DLOB.d.ts +502 -53
  548. package/lib/node/dlob/DLOB.d.ts.map +1 -1
  549. package/lib/node/dlob/DLOB.js +540 -98
  550. package/lib/node/dlob/DLOBNode.d.ts +86 -4
  551. package/lib/node/dlob/DLOBNode.d.ts.map +1 -1
  552. package/lib/node/dlob/DLOBNode.js +37 -4
  553. package/lib/node/dlob/DLOBSubscriber.d.ts +39 -12
  554. package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
  555. package/lib/node/dlob/DLOBSubscriber.js +42 -12
  556. package/lib/node/dlob/NodeList.d.ts +69 -0
  557. package/lib/node/dlob/NodeList.d.ts.map +1 -1
  558. package/lib/node/dlob/NodeList.js +66 -0
  559. package/lib/node/dlob/orderBookLevels.d.ts +106 -13
  560. package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
  561. package/lib/node/dlob/orderBookLevels.js +84 -14
  562. package/lib/node/dlob/types.d.ts +15 -0
  563. package/lib/node/dlob/types.d.ts.map +1 -1
  564. package/lib/node/events/eventList.d.ts +14 -0
  565. package/lib/node/events/eventList.d.ts.map +1 -1
  566. package/lib/node/events/eventList.js +14 -0
  567. package/lib/node/events/eventSubscriber.d.ts +87 -10
  568. package/lib/node/events/eventSubscriber.d.ts.map +1 -1
  569. package/lib/node/events/eventSubscriber.js +93 -8
  570. package/lib/node/events/eventsServerLogProvider.d.ts +22 -0
  571. package/lib/node/events/eventsServerLogProvider.d.ts.map +1 -1
  572. package/lib/node/events/eventsServerLogProvider.js +27 -0
  573. package/lib/node/events/fetchLogs.d.ts +31 -0
  574. package/lib/node/events/fetchLogs.d.ts.map +1 -1
  575. package/lib/node/events/fetchLogs.js +46 -1
  576. package/lib/node/events/parse.d.ts +23 -0
  577. package/lib/node/events/parse.d.ts.map +1 -1
  578. package/lib/node/events/parse.js +23 -0
  579. package/lib/node/events/pollingLogProvider.d.ts +24 -0
  580. package/lib/node/events/pollingLogProvider.d.ts.map +1 -1
  581. package/lib/node/events/pollingLogProvider.js +24 -0
  582. package/lib/node/events/sort.d.ts +11 -0
  583. package/lib/node/events/sort.d.ts.map +1 -1
  584. package/lib/node/events/sort.js +12 -0
  585. package/lib/node/events/txEventCache.d.ts +13 -3
  586. package/lib/node/events/txEventCache.d.ts.map +1 -1
  587. package/lib/node/events/txEventCache.js +19 -7
  588. package/lib/node/events/types.d.ts +118 -2
  589. package/lib/node/events/types.d.ts.map +1 -1
  590. package/lib/node/events/types.js +11 -0
  591. package/lib/node/events/webSocketLogProvider.d.ts +23 -0
  592. package/lib/node/events/webSocketLogProvider.d.ts.map +1 -1
  593. package/lib/node/events/webSocketLogProvider.js +28 -0
  594. package/lib/node/factory/oracleClient.js +2 -2
  595. package/lib/node/idl/velocity.d.ts +4 -4
  596. package/lib/node/idl/velocity.d.ts.map +1 -1
  597. package/lib/node/idl/velocity.json +4 -4
  598. package/lib/node/keypair.d.ts +8 -0
  599. package/lib/node/keypair.d.ts.map +1 -1
  600. package/lib/node/keypair.js +8 -0
  601. package/lib/node/marginCalculation.d.ts +134 -2
  602. package/lib/node/marginCalculation.d.ts.map +1 -1
  603. package/lib/node/marginCalculation.js +121 -0
  604. package/lib/node/math/amm.d.ts +348 -29
  605. package/lib/node/math/amm.d.ts.map +1 -1
  606. package/lib/node/math/amm.js +310 -32
  607. package/lib/node/math/auction.d.ts +95 -19
  608. package/lib/node/math/auction.d.ts.map +1 -1
  609. package/lib/node/math/auction.js +118 -28
  610. package/lib/node/math/bankruptcy.d.ts +46 -0
  611. package/lib/node/math/bankruptcy.d.ts.map +1 -1
  612. package/lib/node/math/bankruptcy.js +89 -1
  613. package/lib/node/math/builder.d.ts +64 -8
  614. package/lib/node/math/builder.d.ts.map +1 -1
  615. package/lib/node/math/builder.js +71 -9
  616. package/lib/node/math/conversion.d.ts +21 -0
  617. package/lib/node/math/conversion.d.ts.map +1 -1
  618. package/lib/node/math/conversion.js +21 -0
  619. package/lib/node/math/exchangeStatus.d.ts +91 -0
  620. package/lib/node/math/exchangeStatus.d.ts.map +1 -1
  621. package/lib/node/math/exchangeStatus.js +106 -3
  622. package/lib/node/math/funding.d.ts +57 -20
  623. package/lib/node/math/funding.d.ts.map +1 -1
  624. package/lib/node/math/funding.js +63 -23
  625. package/lib/node/math/insurance.d.ts +62 -0
  626. package/lib/node/math/insurance.d.ts.map +1 -1
  627. package/lib/node/math/insurance.js +62 -0
  628. package/lib/node/math/liquidation.d.ts +127 -11
  629. package/lib/node/math/liquidation.d.ts.map +1 -1
  630. package/lib/node/math/liquidation.js +182 -19
  631. package/lib/node/math/margin.d.ts +79 -13
  632. package/lib/node/math/margin.d.ts.map +1 -1
  633. package/lib/node/math/margin.js +80 -14
  634. package/lib/node/math/market.d.ts +135 -15
  635. package/lib/node/math/market.d.ts.map +1 -1
  636. package/lib/node/math/market.js +145 -17
  637. package/lib/node/math/oracles.d.ts +113 -0
  638. package/lib/node/math/oracles.d.ts.map +1 -1
  639. package/lib/node/math/oracles.js +118 -1
  640. package/lib/node/math/orders.d.ts +115 -10
  641. package/lib/node/math/orders.d.ts.map +1 -1
  642. package/lib/node/math/orders.js +130 -19
  643. package/lib/node/math/position.d.ts +80 -33
  644. package/lib/node/math/position.d.ts.map +1 -1
  645. package/lib/node/math/position.js +80 -33
  646. package/lib/node/math/repeg.d.ts +48 -10
  647. package/lib/node/math/repeg.d.ts.map +1 -1
  648. package/lib/node/math/repeg.js +48 -10
  649. package/lib/node/math/spotBalance.d.ts +200 -5
  650. package/lib/node/math/spotBalance.d.ts.map +1 -1
  651. package/lib/node/math/spotBalance.js +239 -10
  652. package/lib/node/math/spotMarket.d.ts +36 -3
  653. package/lib/node/math/spotMarket.d.ts.map +1 -1
  654. package/lib/node/math/spotMarket.js +36 -3
  655. package/lib/node/math/spotPosition.d.ts +72 -0
  656. package/lib/node/math/spotPosition.d.ts.map +1 -1
  657. package/lib/node/math/spotPosition.js +62 -0
  658. package/lib/node/math/state.d.ts +31 -0
  659. package/lib/node/math/state.d.ts.map +1 -1
  660. package/lib/node/math/state.js +31 -0
  661. package/lib/node/math/superStake.d.ts +126 -2
  662. package/lib/node/math/superStake.d.ts.map +1 -1
  663. package/lib/node/math/superStake.js +123 -3
  664. package/lib/node/math/tiers.d.ts +29 -0
  665. package/lib/node/math/tiers.d.ts.map +1 -1
  666. package/lib/node/math/tiers.js +29 -0
  667. package/lib/node/math/trade.d.ts +102 -51
  668. package/lib/node/math/trade.d.ts.map +1 -1
  669. package/lib/node/math/trade.js +101 -55
  670. package/lib/node/math/utils.d.ts +71 -9
  671. package/lib/node/math/utils.d.ts.map +1 -1
  672. package/lib/node/math/utils.js +71 -9
  673. package/lib/node/memcmp.d.ts +94 -0
  674. package/lib/node/memcmp.d.ts.map +1 -1
  675. package/lib/node/memcmp.js +103 -2
  676. package/lib/node/oracles/oracleClientCache.d.ts +16 -0
  677. package/lib/node/oracles/oracleClientCache.d.ts.map +1 -1
  678. package/lib/node/oracles/oracleClientCache.js +16 -0
  679. package/lib/node/oracles/oracleId.d.ts +31 -0
  680. package/lib/node/oracles/oracleId.d.ts.map +1 -1
  681. package/lib/node/oracles/oracleId.js +39 -8
  682. package/lib/node/oracles/prelaunchOracleClient.d.ts +20 -0
  683. package/lib/node/oracles/prelaunchOracleClient.d.ts.map +1 -1
  684. package/lib/node/oracles/prelaunchOracleClient.js +20 -0
  685. package/lib/node/oracles/pythClient.d.ts +31 -0
  686. package/lib/node/oracles/pythClient.d.ts.map +1 -1
  687. package/lib/node/oracles/pythClient.js +32 -1
  688. package/lib/node/oracles/pythLazerClient.d.ts +34 -0
  689. package/lib/node/oracles/pythLazerClient.d.ts.map +1 -1
  690. package/lib/node/oracles/pythLazerClient.js +35 -1
  691. package/lib/node/oracles/quoteAssetOracleClient.d.ts +18 -0
  692. package/lib/node/oracles/quoteAssetOracleClient.d.ts.map +1 -1
  693. package/lib/node/oracles/quoteAssetOracleClient.js +18 -0
  694. package/lib/node/oracles/strictOraclePrice.d.ts +21 -0
  695. package/lib/node/oracles/strictOraclePrice.d.ts.map +1 -1
  696. package/lib/node/oracles/strictOraclePrice.js +21 -0
  697. package/lib/node/oracles/types.d.ts +45 -0
  698. package/lib/node/oracles/types.d.ts.map +1 -1
  699. package/lib/node/oracles/utils.d.ts +20 -0
  700. package/lib/node/oracles/utils.d.ts.map +1 -1
  701. package/lib/node/oracles/utils.js +20 -0
  702. package/lib/node/orderParams.d.ts +42 -4
  703. package/lib/node/orderParams.d.ts.map +1 -1
  704. package/lib/node/orderParams.js +42 -4
  705. package/lib/node/orderSubscriber/OrderSubscriber.d.ts +56 -0
  706. package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
  707. package/lib/node/orderSubscriber/OrderSubscriber.js +56 -0
  708. package/lib/node/orderSubscriber/PollingSubscription.d.ts +11 -0
  709. package/lib/node/orderSubscriber/PollingSubscription.d.ts.map +1 -1
  710. package/lib/node/orderSubscriber/PollingSubscription.js +11 -0
  711. package/lib/node/orderSubscriber/WebsocketSubscription.d.ts +18 -0
  712. package/lib/node/orderSubscriber/WebsocketSubscription.d.ts.map +1 -1
  713. package/lib/node/orderSubscriber/WebsocketSubscription.js +18 -0
  714. package/lib/node/orderSubscriber/grpcSubscription.d.ts +16 -0
  715. package/lib/node/orderSubscriber/grpcSubscription.d.ts.map +1 -1
  716. package/lib/node/orderSubscriber/grpcSubscription.js +16 -0
  717. package/lib/node/orderSubscriber/types.d.ts +9 -0
  718. package/lib/node/orderSubscriber/types.d.ts.map +1 -1
  719. package/lib/node/priorityFee/averageOverSlotsStrategy.d.ts +1 -0
  720. package/lib/node/priorityFee/averageOverSlotsStrategy.d.ts.map +1 -1
  721. package/lib/node/priorityFee/averageOverSlotsStrategy.js +1 -0
  722. package/lib/node/priorityFee/averageStrategy.d.ts +1 -0
  723. package/lib/node/priorityFee/averageStrategy.d.ts.map +1 -1
  724. package/lib/node/priorityFee/averageStrategy.js +1 -0
  725. package/lib/node/priorityFee/ewmaStrategy.d.ts +10 -0
  726. package/lib/node/priorityFee/ewmaStrategy.d.ts.map +1 -1
  727. package/lib/node/priorityFee/ewmaStrategy.js +10 -0
  728. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts +13 -0
  729. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts.map +1 -1
  730. package/lib/node/priorityFee/heliusPriorityFeeMethod.js +9 -2
  731. package/lib/node/priorityFee/maxOverSlotsStrategy.d.ts +1 -0
  732. package/lib/node/priorityFee/maxOverSlotsStrategy.d.ts.map +1 -1
  733. package/lib/node/priorityFee/maxOverSlotsStrategy.js +1 -0
  734. package/lib/node/priorityFee/maxStrategy.d.ts +1 -0
  735. package/lib/node/priorityFee/maxStrategy.d.ts.map +1 -1
  736. package/lib/node/priorityFee/maxStrategy.js +1 -0
  737. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +39 -0
  738. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts.map +1 -1
  739. package/lib/node/priorityFee/priorityFeeSubscriber.js +39 -0
  740. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts +20 -1
  741. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts.map +1 -1
  742. package/lib/node/priorityFee/priorityFeeSubscriberMap.js +21 -2
  743. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts +12 -0
  744. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts.map +1 -1
  745. package/lib/node/priorityFee/solanaPriorityFeeMethod.js +10 -0
  746. package/lib/node/priorityFee/types.d.ts +32 -0
  747. package/lib/node/priorityFee/types.d.ts.map +1 -1
  748. package/lib/node/priorityFee/types.js +5 -0
  749. package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts +11 -0
  750. package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts.map +1 -1
  751. package/lib/node/priorityFee/velocityPriorityFeeMethod.js +8 -0
  752. package/lib/node/slot/SlotSubscriber.d.ts +18 -0
  753. package/lib/node/slot/SlotSubscriber.d.ts.map +1 -1
  754. package/lib/node/slot/SlotSubscriber.js +16 -0
  755. package/lib/node/slot/SlothashSubscriber.d.ts +26 -0
  756. package/lib/node/slot/SlothashSubscriber.d.ts.map +1 -1
  757. package/lib/node/slot/SlothashSubscriber.js +25 -1
  758. package/lib/node/tx/baseTxSender.d.ts +163 -0
  759. package/lib/node/tx/baseTxSender.d.ts.map +1 -1
  760. package/lib/node/tx/baseTxSender.js +174 -6
  761. package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.d.ts +12 -0
  762. package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.d.ts.map +1 -1
  763. package/lib/node/tx/blockhashFetcher/baseBlockhashFetcher.js +12 -0
  764. package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts +15 -0
  765. package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.d.ts.map +1 -1
  766. package/lib/node/tx/blockhashFetcher/cachedBlockhashFetcher.js +15 -0
  767. package/lib/node/tx/blockhashFetcher/types.d.ts +4 -0
  768. package/lib/node/tx/blockhashFetcher/types.d.ts.map +1 -1
  769. package/lib/node/tx/fastSingleTxSender.d.ts +47 -0
  770. package/lib/node/tx/fastSingleTxSender.d.ts.map +1 -1
  771. package/lib/node/tx/fastSingleTxSender.js +48 -1
  772. package/lib/node/tx/forwardOnlyTxSender.d.ts +48 -0
  773. package/lib/node/tx/forwardOnlyTxSender.d.ts.map +1 -1
  774. package/lib/node/tx/forwardOnlyTxSender.js +48 -0
  775. package/lib/node/tx/reportTransactionError.d.ts +18 -6
  776. package/lib/node/tx/reportTransactionError.d.ts.map +1 -1
  777. package/lib/node/tx/reportTransactionError.js +18 -6
  778. package/lib/node/tx/retryTxSender.d.ts +40 -0
  779. package/lib/node/tx/retryTxSender.d.ts.map +1 -1
  780. package/lib/node/tx/retryTxSender.js +39 -0
  781. package/lib/node/tx/txHandler.d.ts +146 -35
  782. package/lib/node/tx/txHandler.d.ts.map +1 -1
  783. package/lib/node/tx/txHandler.js +132 -32
  784. package/lib/node/tx/txParamProcessor.d.ts +47 -0
  785. package/lib/node/tx/txParamProcessor.d.ts.map +1 -1
  786. package/lib/node/tx/txParamProcessor.js +47 -0
  787. package/lib/node/tx/types.d.ts +46 -0
  788. package/lib/node/tx/types.d.ts.map +1 -1
  789. package/lib/node/tx/types.js +5 -0
  790. package/lib/node/tx/utils.d.ts +26 -0
  791. package/lib/node/tx/utils.d.ts.map +1 -1
  792. package/lib/node/tx/utils.js +26 -0
  793. package/lib/node/tx/whileValidTxSender.d.ts +82 -0
  794. package/lib/node/tx/whileValidTxSender.d.ts.map +1 -1
  795. package/lib/node/tx/whileValidTxSender.js +81 -0
  796. package/lib/node/types.d.ts +781 -13
  797. package/lib/node/types.d.ts.map +1 -1
  798. package/lib/node/types.js +126 -9
  799. package/lib/node/user.d.ts +645 -84
  800. package/lib/node/user.d.ts.map +1 -1
  801. package/lib/node/user.js +779 -96
  802. package/lib/node/userMap/PollingSubscription.d.ts +10 -0
  803. package/lib/node/userMap/PollingSubscription.d.ts.map +1 -1
  804. package/lib/node/userMap/PollingSubscription.js +14 -2
  805. package/lib/node/userMap/WebsocketSubscription.d.ts +9 -0
  806. package/lib/node/userMap/WebsocketSubscription.d.ts.map +1 -1
  807. package/lib/node/userMap/WebsocketSubscription.js +9 -0
  808. package/lib/node/userMap/grpcSubscription.d.ts +8 -0
  809. package/lib/node/userMap/grpcSubscription.d.ts.map +1 -1
  810. package/lib/node/userMap/grpcSubscription.js +8 -0
  811. package/lib/node/userMap/referrerMap.d.ts +72 -2
  812. package/lib/node/userMap/referrerMap.d.ts.map +1 -1
  813. package/lib/node/userMap/referrerMap.js +84 -2
  814. package/lib/node/userMap/revenueShareEscrowMap.d.ts +37 -10
  815. package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
  816. package/lib/node/userMap/revenueShareEscrowMap.js +38 -11
  817. package/lib/node/userMap/userMap.d.ts +81 -3
  818. package/lib/node/userMap/userMap.d.ts.map +1 -1
  819. package/lib/node/userMap/userMap.js +81 -4
  820. package/lib/node/userMap/userMapConfig.d.ts +23 -0
  821. package/lib/node/userMap/userMapConfig.d.ts.map +1 -1
  822. package/lib/node/userMap/userStatsMap.d.ts +22 -0
  823. package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
  824. package/lib/node/userMap/userStatsMap.js +22 -0
  825. package/lib/node/userName.d.ts +16 -0
  826. package/lib/node/userName.d.ts.map +1 -1
  827. package/lib/node/userName.js +16 -0
  828. package/lib/node/userStats.d.ts +28 -1
  829. package/lib/node/userStats.d.ts.map +1 -1
  830. package/lib/node/userStats.js +28 -1
  831. package/lib/node/util/TransactionConfirmationManager.d.ts +33 -0
  832. package/lib/node/util/TransactionConfirmationManager.d.ts.map +1 -1
  833. package/lib/node/util/TransactionConfirmationManager.js +33 -0
  834. package/lib/node/util/chainClock.d.ts +24 -0
  835. package/lib/node/util/chainClock.d.ts.map +1 -1
  836. package/lib/node/util/chainClock.js +20 -0
  837. package/lib/node/util/computeUnits.d.ts +32 -0
  838. package/lib/node/util/computeUnits.d.ts.map +1 -1
  839. package/lib/node/util/computeUnits.js +32 -0
  840. package/lib/node/util/digest.d.ts +17 -0
  841. package/lib/node/util/digest.d.ts.map +1 -1
  842. package/lib/node/util/digest.js +17 -0
  843. package/lib/node/util/ed25519Utils.d.ts +12 -2
  844. package/lib/node/util/ed25519Utils.d.ts.map +1 -1
  845. package/lib/node/util/ed25519Utils.js +12 -2
  846. package/lib/node/util/promiseTimeout.d.ts +9 -0
  847. package/lib/node/util/promiseTimeout.d.ts.map +1 -1
  848. package/lib/node/util/promiseTimeout.js +9 -0
  849. package/lib/node/velocityClient.d.ts +3322 -215
  850. package/lib/node/velocityClient.d.ts.map +1 -1
  851. package/lib/node/velocityClient.js +3412 -245
  852. package/lib/node/wallet.d.ts +37 -0
  853. package/lib/node/wallet.d.ts.map +1 -1
  854. package/lib/node/wallet.js +37 -0
  855. package/package.json +2 -1
  856. package/src/accounts/basicUserAccountSubscriber.ts +18 -0
  857. package/src/accounts/basicUserStatsAccountSubscriber.ts +18 -0
  858. package/src/accounts/bulkAccountLoader.ts +52 -0
  859. package/src/accounts/bulkUserStatsSubscription.ts +9 -2
  860. package/src/accounts/bulkUserSubscription.ts +8 -2
  861. package/src/accounts/customizedCadenceBulkAccountLoader.ts +37 -0
  862. package/src/accounts/fetch.ts +59 -2
  863. package/src/accounts/grpcAccountSubscriber.ts +30 -0
  864. package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +16 -0
  865. package/src/accounts/grpcMultiAccountSubscriber.ts +121 -38
  866. package/src/accounts/grpcMultiUserAccountSubscriber.ts +49 -11
  867. package/src/accounts/grpcProgramAccountSubscriber.ts +33 -0
  868. package/src/accounts/grpcUserAccountSubscriber.ts +15 -0
  869. package/src/accounts/grpcUserStatsAccountSubscriber.ts +15 -0
  870. package/src/accounts/grpcVelocityClientAccountSubscriber.ts +41 -0
  871. package/src/accounts/grpcVelocityClientAccountSubscriberV2.ts +160 -1
  872. package/src/accounts/laserProgramAccountSubscriber.ts +34 -0
  873. package/src/accounts/oneShotUserAccountSubscriber.ts +19 -0
  874. package/src/accounts/oneShotUserStatsAccountSubscriber.ts +19 -0
  875. package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +31 -0
  876. package/src/accounts/pollingOracleAccountSubscriber.ts +21 -2
  877. package/src/accounts/pollingTokenAccountSubscriber.ts +22 -0
  878. package/src/accounts/pollingUserAccountSubscriber.ts +32 -0
  879. package/src/accounts/pollingUserStatsAccountSubscriber.ts +22 -0
  880. package/src/accounts/pollingVelocityClientAccountSubscriber.ts +84 -1
  881. package/src/accounts/testBulkAccountLoader.ts +8 -0
  882. package/src/accounts/types.ts +149 -0
  883. package/src/accounts/utils.ts +42 -24
  884. package/src/accounts/webSocketAccountSubscriber.ts +41 -0
  885. package/src/accounts/webSocketAccountSubscriberV2.ts +31 -23
  886. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +27 -0
  887. package/src/accounts/webSocketProgramAccountSubscriber.ts +36 -0
  888. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +26 -0
  889. package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +21 -0
  890. package/src/accounts/webSocketUserAccountSubscriber.ts +26 -0
  891. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +20 -0
  892. package/src/accounts/webSocketVelocityClientAccountSubscriber.ts +127 -2
  893. package/src/accounts/webSocketVelocityClientAccountSubscriberV2.ts +110 -0
  894. package/src/accounts/websocketProgramUserAccountSubscriber.ts +33 -0
  895. package/src/addresses/marketAddresses.ts +10 -0
  896. package/src/addresses/pda.ts +228 -12
  897. package/src/adminClient.ts +2156 -4
  898. package/src/auctionSubscriber/auctionSubscriber.ts +15 -0
  899. package/src/auctionSubscriber/auctionSubscriberGrpc.ts +19 -0
  900. package/src/auctionSubscriber/index.ts +6 -0
  901. package/src/auctionSubscriber/types.ts +7 -0
  902. package/src/blockhashSubscriber/BlockhashSubscriber.ts +33 -0
  903. package/src/blockhashSubscriber/index.ts +5 -0
  904. package/src/blockhashSubscriber/types.ts +5 -4
  905. package/src/clock/clockSubscriber.ts +24 -0
  906. package/src/config.ts +109 -22
  907. package/src/constants/numericConstants.ts +70 -0
  908. package/src/constants/perpMarkets.ts +45 -960
  909. package/src/constants/spotMarkets.ts +23 -793
  910. package/src/constants/txConstants.ts +1 -0
  911. package/src/core/VelocityCore.ts +286 -8
  912. package/src/core/instructions/deposit.ts +22 -0
  913. package/src/core/instructions/fill.ts +17 -0
  914. package/src/core/instructions/funding.ts +12 -0
  915. package/src/core/instructions/liquidation.ts +18 -0
  916. package/src/core/instructions/orders.ts +28 -0
  917. package/src/core/instructions/perpOrders.ts +106 -0
  918. package/src/core/instructions/settlement.ts +14 -0
  919. package/src/core/instructions/trigger.ts +15 -0
  920. package/src/core/instructions/withdraw.ts +21 -0
  921. package/src/core/remainingAccounts.ts +47 -3
  922. package/src/core/signedMsg.ts +38 -0
  923. package/src/decode/customCoder.ts +46 -3
  924. package/src/decode/user.ts +22 -0
  925. package/src/dlob/DLOB.ts +668 -97
  926. package/src/dlob/DLOBNode.ts +98 -6
  927. package/src/dlob/DLOBSubscriber.ts +49 -12
  928. package/src/dlob/NodeList.ts +69 -0
  929. package/src/dlob/orderBookLevels.ts +108 -14
  930. package/src/dlob/types.ts +15 -0
  931. package/src/events/eventList.ts +14 -0
  932. package/src/events/eventSubscriber.ts +100 -19
  933. package/src/events/eventsServerLogProvider.ts +28 -0
  934. package/src/events/fetchLogs.ts +52 -2
  935. package/src/events/parse.ts +23 -0
  936. package/src/events/pollingLogProvider.ts +24 -0
  937. package/src/events/sort.ts +12 -0
  938. package/src/events/txEventCache.ts +19 -7
  939. package/src/events/types.ts +131 -11
  940. package/src/events/webSocketLogProvider.ts +29 -0
  941. package/src/factory/oracleClient.ts +2 -2
  942. package/src/idl/velocity.json +4 -4
  943. package/src/idl/velocity.ts +4 -4
  944. package/src/keypair.ts +8 -0
  945. package/src/marginCalculation.ts +153 -2
  946. package/src/math/amm.ts +371 -36
  947. package/src/math/auction.ts +139 -30
  948. package/src/math/bankruptcy.ts +104 -1
  949. package/src/math/builder.ts +74 -8
  950. package/src/math/conversion.ts +21 -0
  951. package/src/math/exchangeStatus.ts +117 -2
  952. package/src/math/funding.ts +63 -23
  953. package/src/math/insurance.ts +62 -0
  954. package/src/math/liquidation.ts +227 -17
  955. package/src/math/margin.ts +80 -14
  956. package/src/math/market.ts +148 -17
  957. package/src/math/oracles.ts +124 -0
  958. package/src/math/orders.ts +141 -19
  959. package/src/math/position.ts +80 -33
  960. package/src/math/repeg.ts +48 -10
  961. package/src/math/spotBalance.ts +252 -12
  962. package/src/math/spotMarket.ts +36 -3
  963. package/src/math/spotPosition.ts +72 -0
  964. package/src/math/state.ts +31 -0
  965. package/src/math/superStake.ts +126 -3
  966. package/src/math/tiers.ts +29 -0
  967. package/src/math/trade.ts +125 -55
  968. package/src/math/utils.ts +71 -9
  969. package/src/memcmp.ts +104 -2
  970. package/src/oracles/oracleClientCache.ts +16 -0
  971. package/src/oracles/oracleId.ts +39 -6
  972. package/src/oracles/prelaunchOracleClient.ts +20 -0
  973. package/src/oracles/pythClient.ts +32 -1
  974. package/src/oracles/pythLazerClient.ts +35 -1
  975. package/src/oracles/quoteAssetOracleClient.ts +18 -0
  976. package/src/oracles/strictOraclePrice.ts +21 -0
  977. package/src/oracles/types.ts +45 -0
  978. package/src/oracles/utils.ts +20 -0
  979. package/src/orderParams.ts +42 -4
  980. package/src/orderSubscriber/OrderSubscriber.ts +56 -0
  981. package/src/orderSubscriber/PollingSubscription.ts +11 -0
  982. package/src/orderSubscriber/WebsocketSubscription.ts +18 -0
  983. package/src/orderSubscriber/grpcSubscription.ts +16 -0
  984. package/src/orderSubscriber/types.ts +9 -0
  985. package/src/priorityFee/averageOverSlotsStrategy.ts +1 -0
  986. package/src/priorityFee/averageStrategy.ts +1 -0
  987. package/src/priorityFee/ewmaStrategy.ts +10 -0
  988. package/src/priorityFee/heliusPriorityFeeMethod.ts +13 -2
  989. package/src/priorityFee/maxOverSlotsStrategy.ts +1 -0
  990. package/src/priorityFee/maxStrategy.ts +1 -0
  991. package/src/priorityFee/priorityFeeSubscriber.ts +39 -0
  992. package/src/priorityFee/priorityFeeSubscriberMap.ts +21 -2
  993. package/src/priorityFee/solanaPriorityFeeMethod.ts +12 -0
  994. package/src/priorityFee/types.ts +33 -15
  995. package/src/priorityFee/velocityPriorityFeeMethod.ts +11 -0
  996. package/src/slot/SlotSubscriber.ts +18 -0
  997. package/src/slot/SlothashSubscriber.ts +27 -1
  998. package/src/tx/baseTxSender.ts +185 -5
  999. package/src/tx/blockhashFetcher/baseBlockhashFetcher.ts +12 -0
  1000. package/src/tx/blockhashFetcher/cachedBlockhashFetcher.ts +15 -0
  1001. package/src/tx/blockhashFetcher/types.ts +4 -0
  1002. package/src/tx/fastSingleTxSender.ts +58 -4
  1003. package/src/tx/forwardOnlyTxSender.ts +48 -0
  1004. package/src/tx/reportTransactionError.ts +18 -6
  1005. package/src/tx/retryTxSender.ts +40 -0
  1006. package/src/tx/txHandler.ts +146 -35
  1007. package/src/tx/txParamProcessor.ts +47 -0
  1008. package/src/tx/types.ts +46 -0
  1009. package/src/tx/utils.ts +26 -0
  1010. package/src/tx/whileValidTxSender.ts +82 -0
  1011. package/src/types.ts +777 -24
  1012. package/src/user.ts +869 -110
  1013. package/src/userMap/PollingSubscription.ts +16 -2
  1014. package/src/userMap/WebsocketSubscription.ts +9 -0
  1015. package/src/userMap/grpcSubscription.ts +8 -0
  1016. package/src/userMap/referrerMap.ts +92 -3
  1017. package/src/userMap/revenueShareEscrowMap.ts +38 -11
  1018. package/src/userMap/userMap.ts +82 -4
  1019. package/src/userMap/userMapConfig.ts +23 -10
  1020. package/src/userMap/userStatsMap.ts +22 -0
  1021. package/src/userName.ts +16 -0
  1022. package/src/userStats.ts +28 -1
  1023. package/src/util/TransactionConfirmationManager.ts +33 -0
  1024. package/src/util/chainClock.ts +24 -0
  1025. package/src/util/computeUnits.ts +32 -0
  1026. package/src/util/digest.ts +17 -0
  1027. package/src/util/ed25519Utils.ts +12 -2
  1028. package/src/util/promiseTimeout.ts +9 -0
  1029. package/src/velocityClient.ts +3439 -249
  1030. package/src/wallet.ts +37 -0
  1031. package/tests/VelocityCore/builder_escrow.test.ts +121 -0
  1032. package/tests/amm/test.ts +102 -0
  1033. package/tests/builder/builderFee.test.ts +42 -0
  1034. package/tests/dlob/tickSizeStandardization.ts +545 -0
  1035. package/tests/exchangeStatus/test.ts +45 -0
  1036. package/tests/liquidation/test.ts +125 -0
  1037. package/tests/oracles/mmOracleGate.test.ts +379 -0
  1038. package/tests/oracles/pythPegSnap.test.ts +76 -0
  1039. package/tests/sdkParity/enumParity.test.ts +84 -0
  1040. package/tests/sdkParity/marginCategoryFill.test.ts +143 -0
  1041. package/tests/sdkParity/memcmpOffsets.test.ts +139 -0
  1042. package/tests/spot/test.ts +55 -0
  1043. package/tests/user/bankruptcy.ts +165 -0
  1044. package/tests/user/feeAndWithdrawLimits.ts +284 -0
  1045. package/tests/user/getMarginCalculation.ts +226 -1
  1046. package/tests/user/test.ts +8 -4
@@ -9,9 +9,12 @@ import { StrictOraclePrice } from '../oracles/strictOraclePrice';
9
9
  * @param {BN} tokenAmount - the amount of tokens
10
10
  * @param {SpotMarketAccount} spotMarket - the spot market account
11
11
  * @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
12
+ * @param {boolean} [roundUp] - override the default rounding direction (program's `round_up`);
13
+ * defaults to rounding up for borrows only. Callers reducing a deposit balance while the
14
+ * funds are leaving Velocity (e.g. a withdrawal) should pass `true` to match `is_leaving_velocity`.
12
15
  * @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
13
16
  */
14
- export declare function getBalance(tokenAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
17
+ export declare function getBalance(tokenAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType, roundUp?: boolean): BN;
15
18
  /**
16
19
  * Calculates the spot token amount including any accumulated interest.
17
20
  *
@@ -47,31 +50,189 @@ export declare function getStrictTokenValue(tokenAmount: BN, spotDecimals: numbe
47
50
  * @return {BN} The value of the token based on the oracle, scaled by `PRICE_PRECISION`
48
51
  */
49
52
  export declare function getTokenValue(tokenAmount: BN, spotDecimals: number, oraclePriceData: Pick<OraclePriceData, 'price'>): BN;
53
+ /**
54
+ * Calculates the collateral (asset) weight applied to a spot deposit balance, mirroring
55
+ * `SpotMarket::get_asset_weight`'s `Initial`/`Maintenance` branches (there is no SDK
56
+ * equivalent of the on-chain `Fill` branch, which averages initial and maintenance).
57
+ * Size is first rescaled into `AMM_RESERVE_PRECISION` before the IMF size-discount is applied,
58
+ * so larger positions receive a lower (more conservative) weight.
59
+ *
60
+ * @param {BN} balanceAmount - The deposit token amount, scaled by the spot market's token decimals
61
+ * @param {BN} oraclePrice - The oracle price, PRICE_PRECISION (1e6); only used for the `Initial`
62
+ * scaled-weight lookup (`calculateScaledInitialAssetWeight`)
63
+ * @param {SpotMarketAccount} spotMarket - The spot market account
64
+ * @param {MarginCategory | undefined} marginCategory - `'Initial'`, `'Maintenance'`, `'Fill'`
65
+ * (the integer-averaged midpoint of scaled-initial and maintenance weights), or `undefined`
66
+ * (defaults to the scaled initial weight, used for e.g. UI display outside a margin check)
67
+ * @return {BN} The asset weight, scaled by `SPOT_MARKET_WEIGHT_PRECISION` (1e4, i.e. 10000 = 100%)
68
+ */
50
69
  export declare function calculateAssetWeight(balanceAmount: BN, oraclePrice: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory | undefined): BN;
70
+ /**
71
+ * Calculates the initial asset weight after applying the market's optional deposit-value
72
+ * scaling, mirroring `SpotMarket::get_scaled_initial_asset_weight`. When
73
+ * `scaleInitialAssetWeightStart` is set and total deposit value exceeds it, the weight is
74
+ * scaled down proportionally (`initialAssetWeight * scaleInitialAssetWeightStart / depositsValue`)
75
+ * so the market's collateral usefulness degrades as its deposits grow past the configured cap.
76
+ *
77
+ * @param {SpotMarketAccount} spotMarket - The spot market account
78
+ * @param {BN} oraclePrice - The oracle price, PRICE_PRECISION (1e6), used to value total deposits
79
+ * @return {BN} The (possibly scaled) initial asset weight, `SPOT_MARKET_WEIGHT_PRECISION` (1e4)
80
+ */
51
81
  export declare function calculateScaledInitialAssetWeight(spotMarket: SpotMarketAccount, oraclePrice: BN): BN;
82
+ /**
83
+ * Calculates the liability (borrow) weight applied to a spot borrow balance, mirroring
84
+ * `SpotMarket::get_liability_weight`'s `Initial`/`Maintenance` branches. Size is rescaled into
85
+ * `AMM_RESERVE_PRECISION` before the IMF size-premium is applied, so larger borrows receive a
86
+ * higher (more conservative) weight.
87
+ *
88
+ * @param {BN} size - The borrow token amount, scaled by the spot market's token decimals
89
+ * @param {SpotMarketAccount} spotMarket - The spot market account
90
+ * @param {MarginCategory | undefined} marginCategory - `'Initial'`, `'Maintenance'`, `'Fill'`
91
+ * (the integer-averaged midpoint of initial and maintenance liability weights), or
92
+ * `undefined` (defaults to `initialLiabilityWeight` with no size premium applied)
93
+ * @return {BN} The liability weight, scaled by `SPOT_MARKET_WEIGHT_PRECISION` (1e4, i.e. 10000 = 100%)
94
+ */
52
95
  export declare function calculateLiabilityWeight(size: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory | undefined): BN;
96
+ /**
97
+ * Calculates a spot market's utilization (borrows / deposits), mirroring
98
+ * `calculate_utilization`. Returns `SPOT_MARKET_UTILIZATION_PRECISION` (100% utilization) if
99
+ * there are borrows but no deposits, and zero if both are zero.
100
+ *
101
+ * @param {SpotMarketAccount} bank - The spot market account
102
+ * @param {BN} [delta] - Optional hypothetical change in token amount, scaled by the market's
103
+ * token decimals: a positive delta is added to deposits, a negative delta (its absolute
104
+ * value) is added to borrows. Defaults to zero (current on-chain utilization).
105
+ * @return {BN} Utilization, scaled by `SPOT_MARKET_UTILIZATION_PRECISION` (1e6, i.e. 1e6 = 100%)
106
+ */
53
107
  export declare function calculateUtilization(bank: SpotMarketAccount, delta?: BN): BN;
54
108
  /**
55
- * calculates max borrow amount where rate would stay below targetBorrowRate
56
- * @param spotMarketAccount
57
- * @param targetBorrowRate
58
- * @returns : Precision: TOKEN DECIMALS
109
+ * SDK-only helper (no direct on-chain counterpart) that inverts `calculateInterestRate`'s
110
+ * utilization curve to find how much more can be borrowed before the borrow rate would reach
111
+ * `targetBorrowRate`. Useful for UI "available to borrow at rate X" displays.
112
+ *
113
+ * @param {SpotMarketAccount} spotMarketAccount - The spot market account
114
+ * @param {BN} targetBorrowRate - The target annualized borrow rate, `SPOT_MARKET_RATE_PRECISION` (1e6)
115
+ * @returns {{ totalCapacity: BN; remainingCapacity: BN }} Both scaled by the market's token
116
+ * decimals. `totalCapacity` is the total borrow amount implied by the target utilization;
117
+ * `remainingCapacity` is `totalCapacity` minus current borrows (zero if the market's current
118
+ * borrow rate already meets or exceeds the target), additionally capped by
119
+ * `maxTokenBorrowsFraction` of `maxTokenDeposits` when that cap is configured (>0)
59
120
  */
60
121
  export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: SpotMarketAccount, targetBorrowRate: BN): {
61
122
  totalCapacity: BN;
62
123
  remainingCapacity: BN;
63
124
  };
125
+ /**
126
+ * Calculates the annualized borrow interest rate for a spot market, mirroring
127
+ * `calculate_borrow_rate` / the underlying utilization curve. Below `optimalUtilization` the
128
+ * rate ramps linearly from 0 to `optimalBorrowRate`; above it, the rate ramps through a fixed
129
+ * piecewise schedule (85/90/95/99/99.5/100% utilization breakpoints) from `optimalBorrowRate`
130
+ * up to `maxBorrowRate`. The result is floored at `minBorrowRate / 200` (i.e. `minBorrowRate`
131
+ * is in units of half-percentage-points of `PERCENTAGE_PRECISION`).
132
+ *
133
+ * @param {SpotMarketAccount} bank - The spot market account
134
+ * @param {BN} [delta] - Optional hypothetical change in token amount passed through to
135
+ * `calculateUtilization` (ignored if `currentUtilization` is provided)
136
+ * @param {BN} [currentUtilization] - Precomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION`
137
+ * (1e6); if omitted it is derived from `bank` and `delta`
138
+ * @return {BN} Annualized borrow rate, scaled by `SPOT_MARKET_RATE_PRECISION` (1e6)
139
+ */
64
140
  export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: BN, currentUtilization?: BN): BN;
141
+ /**
142
+ * Calculates the annualized deposit interest rate for a spot market, mirroring
143
+ * `calculate_deposit_rate` (velocity-rs). Lenders receive the borrow rate net of the insurance
144
+ * fund and protocol fee carveouts (`ifFeeFactor` + `protocolFeeFactor`, both `PERCENTAGE_PRECISION`),
145
+ * scaled down by utilization since only borrowed deposits earn interest.
146
+ *
147
+ * @param {SpotMarketAccount} bank - The spot market account
148
+ * @param {BN} [delta] - Optional hypothetical change in token amount; positive adds to deposits,
149
+ * negative adds to borrows (see `calculateUtilization`)
150
+ * @param {BN} [currentUtilization] - Precomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION`
151
+ * (1e6); if omitted it is derived from `bank` and `delta`
152
+ * @return {BN} Annualized deposit rate, scaled by `SPOT_MARKET_RATE_PRECISION` (1e6)
153
+ */
65
154
  export declare function calculateDepositRate(bank: SpotMarketAccount, delta?: BN, currentUtilization?: BN): BN;
155
+ /**
156
+ * Alias for `calculateInterestRate` (annualized borrow rate).
157
+ *
158
+ * @param {SpotMarketAccount} bank - The spot market account
159
+ * @param {BN} [delta] - Optional hypothetical change in token amount (see `calculateUtilization`)
160
+ * @param {BN} [currentUtilization] - Precomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION` (1e6)
161
+ * @return {BN} Annualized borrow rate, scaled by `SPOT_MARKET_RATE_PRECISION` (1e6)
162
+ */
66
163
  export declare function calculateBorrowRate(bank: SpotMarketAccount, delta?: BN, currentUtilization?: BN): BN;
164
+ /**
165
+ * Projects the cumulative interest multipliers that would accrue between `spotMarket.lastInterestTs`
166
+ * and `now` at the market's current interest rate, mirroring the gross amounts computed by
167
+ * `calculate_accumulated_interest`. This is a point-in-time estimate for display purposes only —
168
+ * the actual on-chain update (`update_spot_market_cumulative_interest`) re-derives the rate from
169
+ * utilization at settlement time (same as this function calling `calculateInterestRate(bank)` with
170
+ * no delta), and only runs at all if `deposit_interest > 0 && borrow_interest > 1`. Borrow interest
171
+ * is always rounded up by 1 (added unconditionally), matching the program's lender-favoring
172
+ * rounding, and is credited to `cumulativeBorrowInterest` in full. **`depositInterest` here is the
173
+ * gross pre-carveout amount** — on-chain, `insuranceFund.ifFeeFactor` and `protocolFeeFactor`
174
+ * (both `IF_FACTOR_PRECISION`) are each cut from it first (to `revenuePool` and `protocolFeePool`
175
+ * respectively) and only the remainder is what actually gets added to `cumulativeDepositInterest`;
176
+ * this function does not replicate that split, so it overstates the deposit-side increment
177
+ * whenever either factor is non-zero.
178
+ *
179
+ * @param {SpotMarketAccount} bank - The spot market account
180
+ * @param {BN} now - The timestamp (unix seconds) to project interest up to
181
+ * @return {{ borrowInterest: BN; depositInterest: BN }} `borrowInterest` is the exact amount added
182
+ * to `cumulativeBorrowInterest`; `depositInterest` is the gross pre-carveout amount, not
183
+ * necessarily what's added to `cumulativeDepositInterest` (see above). Both in the same
184
+ * fixed-point units as those cumulative fields (`SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION`)
185
+ */
67
186
  export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
68
187
  borrowInterest: BN;
69
188
  depositInterest: BN;
70
189
  };
190
+ /**
191
+ * Calculates the minimum deposit / maximum borrow token amounts that keep the market's
192
+ * utilization from exceeding a "max withdraw utilization" ceiling, mirroring
193
+ * `calculate_token_utilization_limits`. The ceiling is `max(optimalUtilization,
194
+ * utilizationTwap + (100% - utilizationTwap) / 2)` — i.e. it allows utilization to rise, but
195
+ * only up to halfway from the TWAP to 100%. Deposit sizes already below
196
+ * `withdrawGuardThreshold` are never blocked (the min-deposit result is capped so it can't
197
+ * exceed `depositTokenAmount - withdrawGuardThreshold`), and borrows below the guard threshold
198
+ * are never blocked either (the max-borrow result is floored at `withdrawGuardThreshold`).
199
+ *
200
+ * @param {BN} depositTokenAmount - Current total deposit token amount, market's token decimals
201
+ * @param {BN} borrowTokenAmount - Current total borrow token amount, market's token decimals
202
+ * @param {SpotMarketAccount} spotMarket - The spot market account
203
+ * @return {{ minDepositTokensForUtilization: BN; maxBorrowTokensForUtilization: BN }} Both
204
+ * scaled by the market's token decimals
205
+ */
71
206
  export declare function calculateTokenUtilizationLimits(depositTokenAmount: BN, borrowTokenAmount: BN, spotMarket: SpotMarketAccount): {
72
207
  minDepositTokensForUtilization: BN;
73
208
  maxBorrowTokensForUtilization: BN;
74
209
  };
210
+ /**
211
+ * Estimates the current immediate withdraw/borrow limits for a spot market, mirroring the
212
+ * on-chain `check_withdraw_limits` / `get_max_withdraw_for_market_with_token_amount` guard
213
+ * (combining `calculate_min_deposit_token_amount`, `calculate_max_borrow_token_amount`, and
214
+ * `calculateTokenUtilizationLimits`). Because the SDK cannot force an on-chain TWAP update
215
+ * before reading it, this projects a "live" 24h deposit/borrow TWAP by weighting the stored
216
+ * TWAP and the current amount by `sinceStart`/`sinceLast` (the same weighted-average shape as
217
+ * `update_spot_market_twap_stats`, without its rounding bias term) before deriving limits, so
218
+ * the result approximates what the on-chain TWAP would be if updated at `now`.
219
+ *
220
+ * Deposit/borrow TWAP friction bands differ by pool: the main pool (`poolId === 0`) targets
221
+ * ~30-92.5% utilization (borrow ceiling is `lesserDepositAmount` clamped between 1/3 and
222
+ * 13/14 of itself, floored around the live borrow TWAP + 1/5), isolated pools (`poolId !== 0`)
223
+ * target ~50-95% (clamped between 1/2 and 19/20, floored around the live borrow TWAP + 1/3).
224
+ * `lesserDepositAmount` is `min(currentDepositAmount, live deposit TWAP)` — using the smaller of
225
+ * the two keeps the borrow ceiling conservative whether deposits are rising or falling.
226
+ * `borrowLimit` is additionally zeroed for `assetTier === 'protected'` markets, and both limits
227
+ * are clamped by `maxTokenBorrowsFraction` of `maxTokenDeposits` when that cap is configured.
228
+ *
229
+ * @param {SpotMarketAccount} spotMarket - The spot market account
230
+ * @param {BN} now - The timestamp (unix seconds) to project the live TWAP up to
231
+ * @return {{ borrowLimit: BN; withdrawLimit: BN; minDepositAmount: BN; maxBorrowAmount: BN;
232
+ * currentDepositAmount: BN; currentBorrowAmount: BN }} All values scaled by the market's token
233
+ * decimals. `withdrawLimit`/`borrowLimit` are floored at zero (a market already past its
234
+ * min-deposit/max-borrow bound reports zero remaining room rather than negative)
235
+ */
75
236
  export declare function calculateWithdrawLimit(spotMarket: SpotMarketAccount, now: BN): {
76
237
  borrowLimit: BN;
77
238
  withdrawLimit: BN;
@@ -80,5 +241,39 @@ export declare function calculateWithdrawLimit(spotMarket: SpotMarketAccount, no
80
241
  currentDepositAmount: BN;
81
242
  currentBorrowAmount: BN;
82
243
  };
244
+ /**
245
+ * Calculates the margin-weighted value of a spot deposit, mirroring the asset-side of the
246
+ * program's collateral valuation (`get_strict_token_value` + `get_asset_weight`). Uses the
247
+ * worst of the oracle's live price and its 5min TWAP (via `strictOraclePrice`) so a favorable
248
+ * price spike can't be used to over-value collateral.
249
+ *
250
+ * @param {BN} tokenAmount - The deposit token amount, scaled by `spotMarketAccount.decimals`
251
+ * @param {StrictOraclePrice} strictOraclePrice - Live oracle price + 5min TWAP, PRICE_PRECISION (1e6)
252
+ * @param {SpotMarketAccount} spotMarketAccount - The spot market account
253
+ * @param {number} maxMarginRatio - The user's custom max margin ratio (0 if unset), in
254
+ * `SPOT_MARKET_WEIGHT_PRECISION` (1e4) units; only applied when `marginCategory === 'Initial'`
255
+ * and the market isn't the quote spot market, capping the weight at
256
+ * `SPOT_MARKET_WEIGHT_PRECISION - maxMarginRatio`
257
+ * @param {MarginCategory} [marginCategory] - When omitted, returns the unweighted (100%) value
258
+ * @return {BN} The (optionally weighted) asset value, scaled by `PRICE_PRECISION` (1e6)
259
+ */
83
260
  export declare function getSpotAssetValue(tokenAmount: BN, strictOraclePrice: StrictOraclePrice, spotMarketAccount: SpotMarketAccount, maxMarginRatio: number, marginCategory?: MarginCategory): BN;
261
+ /**
262
+ * Calculates the margin-weighted value of a spot borrow, mirroring the liability-side of the
263
+ * program's collateral valuation (`get_strict_token_value` + `get_liability_weight`). Uses the
264
+ * worst of the oracle's live price and its 5min TWAP (via `strictOraclePrice`) so a favorable
265
+ * price dip can't be used to under-value a liability.
266
+ *
267
+ * @param {BN} tokenAmount - The borrow token amount (positive), scaled by `spotMarketAccount.decimals`
268
+ * @param {StrictOraclePrice} strictOraclePrice - Live oracle price + 5min TWAP, PRICE_PRECISION (1e6)
269
+ * @param {SpotMarketAccount} spotMarketAccount - The spot market account
270
+ * @param {number} maxMarginRatio - The user's custom max margin ratio (0 if unset),
271
+ * `SPOT_MARKET_WEIGHT_PRECISION` (1e4) units; only applied when `marginCategory === 'Initial'`
272
+ * and the market isn't the quote spot market, flooring the weight at
273
+ * `SPOT_MARKET_WEIGHT_PRECISION + maxMarginRatio`
274
+ * @param {MarginCategory} [marginCategory] - When omitted, returns the unweighted (100%) value
275
+ * @param {BN} [liquidationBuffer] - Extra weight added on top (`SPOT_MARKET_WEIGHT_PRECISION`
276
+ * units) to make maintenance margin checks stricter during liquidation eligibility checks
277
+ * @return {BN} The (optionally weighted) liability value, scaled by `PRICE_PRECISION` (1e6)
278
+ */
84
279
  export declare function getSpotLiabilityValue(tokenAmount: BN, strictOraclePrice: StrictOraclePrice, spotMarketAccount: SpotMarketAccount, maxMarginRatio: number, marginCategory?: MarginCategory, liquidationBuffer?: BN): BN;
@@ -7,6 +7,17 @@ const numericConstants_1 = require("../constants/numericConstants");
7
7
  const margin_1 = require("./margin");
8
8
  const numericConstants_2 = require("../constants/numericConstants");
9
9
  const utils_1 = require("./utils");
10
+ // BN's `.div()` truncates toward zero; the program uses `safe_div_floor` when
11
+ // the numerator is negative (get_token_value / get_strict_token_value), so a
12
+ // negative dividend must round toward -infinity here to match.
13
+ function divFloor(a, b) {
14
+ const quotient = a.div(b);
15
+ const remainder = a.mod(b);
16
+ if (!remainder.isZero() && a.isNeg() !== b.isNeg()) {
17
+ return quotient.sub(numericConstants_1.ONE);
18
+ }
19
+ return quotient;
20
+ }
10
21
  /**
11
22
  * Calculates the balance of a given token amount including any accumulated interest. This
12
23
  * is the same as `SpotPosition.scaledBalance`.
@@ -14,15 +25,19 @@ const utils_1 = require("./utils");
14
25
  * @param {BN} tokenAmount - the amount of tokens
15
26
  * @param {SpotMarketAccount} spotMarket - the spot market account
16
27
  * @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
28
+ * @param {boolean} [roundUp] - override the default rounding direction (program's `round_up`);
29
+ * defaults to rounding up for borrows only. Callers reducing a deposit balance while the
30
+ * funds are leaving Velocity (e.g. a withdrawal) should pass `true` to match `is_leaving_velocity`.
17
31
  * @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
18
32
  */
19
- function getBalance(tokenAmount, spotMarket, balanceType) {
33
+ function getBalance(tokenAmount, spotMarket, balanceType, roundUp) {
20
34
  const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
21
35
  const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
22
36
  ? spotMarket.cumulativeDepositInterest
23
37
  : spotMarket.cumulativeBorrowInterest;
24
38
  let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
25
- if (!balance.eq(numericConstants_1.ZERO) && (0, types_1.isVariant)(balanceType, 'borrow')) {
39
+ const shouldRoundUp = roundUp !== null && roundUp !== void 0 ? roundUp : (0, types_1.isVariant)(balanceType, 'borrow');
40
+ if (!balance.eq(numericConstants_1.ZERO) && shouldRoundUp) {
26
41
  balance = balance.add(numericConstants_1.ONE);
27
42
  }
28
43
  return balance;
@@ -84,7 +99,11 @@ function getStrictTokenValue(tokenAmount, spotDecimals, strictOraclePrice) {
84
99
  price = strictOraclePrice.max();
85
100
  }
86
101
  const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(spotDecimals));
87
- return tokenAmount.mul(price).div(precisionDecrease);
102
+ const tokenWithPrice = tokenAmount.mul(price);
103
+ if (tokenWithPrice.isNeg()) {
104
+ return divFloor(tokenWithPrice, precisionDecrease);
105
+ }
106
+ return tokenWithPrice.div(precisionDecrease);
88
107
  }
89
108
  exports.getStrictTokenValue = getStrictTokenValue;
90
109
  /**
@@ -100,9 +119,29 @@ function getTokenValue(tokenAmount, spotDecimals, oraclePriceData) {
100
119
  return numericConstants_1.ZERO;
101
120
  }
102
121
  const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(spotDecimals));
103
- return tokenAmount.mul(oraclePriceData.price).div(precisionDecrease);
122
+ const tokenWithOraclePrice = tokenAmount.mul(oraclePriceData.price);
123
+ if (tokenWithOraclePrice.isNeg()) {
124
+ return divFloor(tokenWithOraclePrice, precisionDecrease);
125
+ }
126
+ return tokenWithOraclePrice.div(precisionDecrease);
104
127
  }
105
128
  exports.getTokenValue = getTokenValue;
129
+ /**
130
+ * Calculates the collateral (asset) weight applied to a spot deposit balance, mirroring
131
+ * `SpotMarket::get_asset_weight`'s `Initial`/`Maintenance` branches (there is no SDK
132
+ * equivalent of the on-chain `Fill` branch, which averages initial and maintenance).
133
+ * Size is first rescaled into `AMM_RESERVE_PRECISION` before the IMF size-discount is applied,
134
+ * so larger positions receive a lower (more conservative) weight.
135
+ *
136
+ * @param {BN} balanceAmount - The deposit token amount, scaled by the spot market's token decimals
137
+ * @param {BN} oraclePrice - The oracle price, PRICE_PRECISION (1e6); only used for the `Initial`
138
+ * scaled-weight lookup (`calculateScaledInitialAssetWeight`)
139
+ * @param {SpotMarketAccount} spotMarket - The spot market account
140
+ * @param {MarginCategory | undefined} marginCategory - `'Initial'`, `'Maintenance'`, `'Fill'`
141
+ * (the integer-averaged midpoint of scaled-initial and maintenance weights), or `undefined`
142
+ * (defaults to the scaled initial weight, used for e.g. UI display outside a margin check)
143
+ * @return {BN} The asset weight, scaled by `SPOT_MARKET_WEIGHT_PRECISION` (1e4, i.e. 10000 = 100%)
144
+ */
106
145
  function calculateAssetWeight(balanceAmount, oraclePrice, spotMarket, marginCategory) {
107
146
  const sizePrecision = numericConstants_1.TEN.pow(new anchor_1.BN(spotMarket.decimals));
108
147
  let sizeInAmmReservePrecision;
@@ -119,6 +158,13 @@ function calculateAssetWeight(balanceAmount, oraclePrice, spotMarket, marginCate
119
158
  case 'Initial':
120
159
  assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), calculateScaledInitialAssetWeight(spotMarket, oraclePrice));
121
160
  break;
161
+ case 'Fill':
162
+ // mirrors SpotMarket::get_asset_weight's Fill branch:
163
+ // (scaled_initial_asset_weight + maintenance_asset_weight) / 2 (integer division)
164
+ assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), calculateScaledInitialAssetWeight(spotMarket, oraclePrice)
165
+ .add(new anchor_1.BN(spotMarket.maintenanceAssetWeight))
166
+ .divn(2));
167
+ break;
122
168
  case 'Maintenance':
123
169
  assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceAssetWeight));
124
170
  break;
@@ -129,6 +175,17 @@ function calculateAssetWeight(balanceAmount, oraclePrice, spotMarket, marginCate
129
175
  return assetWeight;
130
176
  }
131
177
  exports.calculateAssetWeight = calculateAssetWeight;
178
+ /**
179
+ * Calculates the initial asset weight after applying the market's optional deposit-value
180
+ * scaling, mirroring `SpotMarket::get_scaled_initial_asset_weight`. When
181
+ * `scaleInitialAssetWeightStart` is set and total deposit value exceeds it, the weight is
182
+ * scaled down proportionally (`initialAssetWeight * scaleInitialAssetWeightStart / depositsValue`)
183
+ * so the market's collateral usefulness degrades as its deposits grow past the configured cap.
184
+ *
185
+ * @param {SpotMarketAccount} spotMarket - The spot market account
186
+ * @param {BN} oraclePrice - The oracle price, PRICE_PRECISION (1e6), used to value total deposits
187
+ * @return {BN} The (possibly scaled) initial asset weight, `SPOT_MARKET_WEIGHT_PRECISION` (1e4)
188
+ */
132
189
  function calculateScaledInitialAssetWeight(spotMarket, oraclePrice) {
133
190
  if (spotMarket.scaleInitialAssetWeightStart.eq(numericConstants_1.ZERO)) {
134
191
  return new anchor_1.BN(spotMarket.initialAssetWeight);
@@ -147,6 +204,19 @@ function calculateScaledInitialAssetWeight(spotMarket, oraclePrice) {
147
204
  }
148
205
  }
149
206
  exports.calculateScaledInitialAssetWeight = calculateScaledInitialAssetWeight;
207
+ /**
208
+ * Calculates the liability (borrow) weight applied to a spot borrow balance, mirroring
209
+ * `SpotMarket::get_liability_weight`'s `Initial`/`Maintenance` branches. Size is rescaled into
210
+ * `AMM_RESERVE_PRECISION` before the IMF size-premium is applied, so larger borrows receive a
211
+ * higher (more conservative) weight.
212
+ *
213
+ * @param {BN} size - The borrow token amount, scaled by the spot market's token decimals
214
+ * @param {SpotMarketAccount} spotMarket - The spot market account
215
+ * @param {MarginCategory | undefined} marginCategory - `'Initial'`, `'Maintenance'`, `'Fill'`
216
+ * (the integer-averaged midpoint of initial and maintenance liability weights), or
217
+ * `undefined` (defaults to `initialLiabilityWeight` with no size premium applied)
218
+ * @return {BN} The liability weight, scaled by `SPOT_MARKET_WEIGHT_PRECISION` (1e4, i.e. 10000 = 100%)
219
+ */
150
220
  function calculateLiabilityWeight(size, spotMarket, marginCategory) {
151
221
  const sizePrecision = numericConstants_1.TEN.pow(new anchor_1.BN(spotMarket.decimals));
152
222
  let sizeInAmmReservePrecision;
@@ -163,6 +233,13 @@ function calculateLiabilityWeight(size, spotMarket, marginCategory) {
163
233
  case 'Initial':
164
234
  liabilityWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
165
235
  break;
236
+ case 'Fill':
237
+ // mirrors SpotMarket::get_liability_weight's Fill branch:
238
+ // (initial_liability_weight + maintenance_liability_weight) / 2 (integer division)
239
+ liabilityWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight)
240
+ .add(new anchor_1.BN(spotMarket.maintenanceLiabilityWeight))
241
+ .divn(2), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
242
+ break;
166
243
  case 'Maintenance':
167
244
  liabilityWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
168
245
  break;
@@ -173,6 +250,17 @@ function calculateLiabilityWeight(size, spotMarket, marginCategory) {
173
250
  return liabilityWeight;
174
251
  }
175
252
  exports.calculateLiabilityWeight = calculateLiabilityWeight;
253
+ /**
254
+ * Calculates a spot market's utilization (borrows / deposits), mirroring
255
+ * `calculate_utilization`. Returns `SPOT_MARKET_UTILIZATION_PRECISION` (100% utilization) if
256
+ * there are borrows but no deposits, and zero if both are zero.
257
+ *
258
+ * @param {SpotMarketAccount} bank - The spot market account
259
+ * @param {BN} [delta] - Optional hypothetical change in token amount, scaled by the market's
260
+ * token decimals: a positive delta is added to deposits, a negative delta (its absolute
261
+ * value) is added to borrows. Defaults to zero (current on-chain utilization).
262
+ * @return {BN} Utilization, scaled by `SPOT_MARKET_UTILIZATION_PRECISION` (1e6, i.e. 1e6 = 100%)
263
+ */
176
264
  function calculateUtilization(bank, delta = numericConstants_1.ZERO) {
177
265
  let tokenDepositAmount = getTokenAmount(bank.depositBalance, bank, types_1.SpotBalanceType.DEPOSIT);
178
266
  let tokenBorrowAmount = getTokenAmount(bank.borrowBalance, bank, types_1.SpotBalanceType.BORROW);
@@ -198,10 +286,17 @@ function calculateUtilization(bank, delta = numericConstants_1.ZERO) {
198
286
  }
199
287
  exports.calculateUtilization = calculateUtilization;
200
288
  /**
201
- * calculates max borrow amount where rate would stay below targetBorrowRate
202
- * @param spotMarketAccount
203
- * @param targetBorrowRate
204
- * @returns : Precision: TOKEN DECIMALS
289
+ * SDK-only helper (no direct on-chain counterpart) that inverts `calculateInterestRate`'s
290
+ * utilization curve to find how much more can be borrowed before the borrow rate would reach
291
+ * `targetBorrowRate`. Useful for UI "available to borrow at rate X" displays.
292
+ *
293
+ * @param {SpotMarketAccount} spotMarketAccount - The spot market account
294
+ * @param {BN} targetBorrowRate - The target annualized borrow rate, `SPOT_MARKET_RATE_PRECISION` (1e6)
295
+ * @returns {{ totalCapacity: BN; remainingCapacity: BN }} Both scaled by the market's token
296
+ * decimals. `totalCapacity` is the total borrow amount implied by the target utilization;
297
+ * `remainingCapacity` is `totalCapacity` minus current borrows (zero if the market's current
298
+ * borrow rate already meets or exceeds the target), additionally capped by
299
+ * `maxTokenBorrowsFraction` of `maxTokenDeposits` when that cap is configured (>0)
205
300
  */
206
301
  function calculateSpotMarketBorrowCapacity(spotMarketAccount, targetBorrowRate) {
207
302
  const currentBorrowRate = calculateBorrowRate(spotMarketAccount);
@@ -246,6 +341,21 @@ function calculateSpotMarketBorrowCapacity(spotMarketAccount, targetBorrowRate)
246
341
  return { totalCapacity, remainingCapacity };
247
342
  }
248
343
  exports.calculateSpotMarketBorrowCapacity = calculateSpotMarketBorrowCapacity;
344
+ /**
345
+ * Calculates the annualized borrow interest rate for a spot market, mirroring
346
+ * `calculate_borrow_rate` / the underlying utilization curve. Below `optimalUtilization` the
347
+ * rate ramps linearly from 0 to `optimalBorrowRate`; above it, the rate ramps through a fixed
348
+ * piecewise schedule (85/90/95/99/99.5/100% utilization breakpoints) from `optimalBorrowRate`
349
+ * up to `maxBorrowRate`. The result is floored at `minBorrowRate / 200` (i.e. `minBorrowRate`
350
+ * is in units of half-percentage-points of `PERCENTAGE_PRECISION`).
351
+ *
352
+ * @param {SpotMarketAccount} bank - The spot market account
353
+ * @param {BN} [delta] - Optional hypothetical change in token amount passed through to
354
+ * `calculateUtilization` (ignored if `currentUtilization` is provided)
355
+ * @param {BN} [currentUtilization] - Precomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION`
356
+ * (1e6); if omitted it is derived from `bank` and `delta`
357
+ * @return {BN} Annualized borrow rate, scaled by `SPOT_MARKET_RATE_PRECISION` (1e6)
358
+ */
249
359
  function calculateInterestRate(bank, delta = numericConstants_1.ZERO, currentUtilization) {
250
360
  // todo: ensure both a delta and current util aren't pass?
251
361
  const utilization = currentUtilization !== null && currentUtilization !== void 0 ? currentUtilization : calculateUtilization(bank, delta);
@@ -296,6 +406,19 @@ function calculateInterestRate(bank, delta = numericConstants_1.ZERO, currentUti
296
406
  return anchor_1.BN.max(minRate, rate);
297
407
  }
298
408
  exports.calculateInterestRate = calculateInterestRate;
409
+ /**
410
+ * Calculates the annualized deposit interest rate for a spot market, mirroring
411
+ * `calculate_deposit_rate` (velocity-rs). Lenders receive the borrow rate net of the insurance
412
+ * fund and protocol fee carveouts (`ifFeeFactor` + `protocolFeeFactor`, both `PERCENTAGE_PRECISION`),
413
+ * scaled down by utilization since only borrowed deposits earn interest.
414
+ *
415
+ * @param {SpotMarketAccount} bank - The spot market account
416
+ * @param {BN} [delta] - Optional hypothetical change in token amount; positive adds to deposits,
417
+ * negative adds to borrows (see `calculateUtilization`)
418
+ * @param {BN} [currentUtilization] - Precomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION`
419
+ * (1e6); if omitted it is derived from `bank` and `delta`
420
+ * @return {BN} Annualized deposit rate, scaled by `SPOT_MARKET_RATE_PRECISION` (1e6)
421
+ */
299
422
  function calculateDepositRate(bank, delta = numericConstants_1.ZERO, currentUtilization) {
300
423
  // positive delta => adding to deposit
301
424
  // negative delta => adding to borrow
@@ -309,10 +432,40 @@ function calculateDepositRate(bank, delta = numericConstants_1.ZERO, currentUtil
309
432
  return depositRate;
310
433
  }
311
434
  exports.calculateDepositRate = calculateDepositRate;
435
+ /**
436
+ * Alias for `calculateInterestRate` (annualized borrow rate).
437
+ *
438
+ * @param {SpotMarketAccount} bank - The spot market account
439
+ * @param {BN} [delta] - Optional hypothetical change in token amount (see `calculateUtilization`)
440
+ * @param {BN} [currentUtilization] - Precomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION` (1e6)
441
+ * @return {BN} Annualized borrow rate, scaled by `SPOT_MARKET_RATE_PRECISION` (1e6)
442
+ */
312
443
  function calculateBorrowRate(bank, delta = numericConstants_1.ZERO, currentUtilization) {
313
444
  return calculateInterestRate(bank, delta, currentUtilization);
314
445
  }
315
446
  exports.calculateBorrowRate = calculateBorrowRate;
447
+ /**
448
+ * Projects the cumulative interest multipliers that would accrue between `spotMarket.lastInterestTs`
449
+ * and `now` at the market's current interest rate, mirroring the gross amounts computed by
450
+ * `calculate_accumulated_interest`. This is a point-in-time estimate for display purposes only —
451
+ * the actual on-chain update (`update_spot_market_cumulative_interest`) re-derives the rate from
452
+ * utilization at settlement time (same as this function calling `calculateInterestRate(bank)` with
453
+ * no delta), and only runs at all if `deposit_interest > 0 && borrow_interest > 1`. Borrow interest
454
+ * is always rounded up by 1 (added unconditionally), matching the program's lender-favoring
455
+ * rounding, and is credited to `cumulativeBorrowInterest` in full. **`depositInterest` here is the
456
+ * gross pre-carveout amount** — on-chain, `insuranceFund.ifFeeFactor` and `protocolFeeFactor`
457
+ * (both `IF_FACTOR_PRECISION`) are each cut from it first (to `revenuePool` and `protocolFeePool`
458
+ * respectively) and only the remainder is what actually gets added to `cumulativeDepositInterest`;
459
+ * this function does not replicate that split, so it overstates the deposit-side increment
460
+ * whenever either factor is non-zero.
461
+ *
462
+ * @param {SpotMarketAccount} bank - The spot market account
463
+ * @param {BN} now - The timestamp (unix seconds) to project interest up to
464
+ * @return {{ borrowInterest: BN; depositInterest: BN }} `borrowInterest` is the exact amount added
465
+ * to `cumulativeBorrowInterest`; `depositInterest` is the gross pre-carveout amount, not
466
+ * necessarily what's added to `cumulativeDepositInterest` (see above). Both in the same
467
+ * fixed-point units as those cumulative fields (`SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION`)
468
+ */
316
469
  function calculateInterestAccumulated(bank, now) {
317
470
  const interestRate = calculateInterestRate(bank);
318
471
  const timeSinceLastUpdate = now.sub(bank.lastInterestTs);
@@ -333,6 +486,22 @@ function calculateInterestAccumulated(bank, now) {
333
486
  return { borrowInterest, depositInterest };
334
487
  }
335
488
  exports.calculateInterestAccumulated = calculateInterestAccumulated;
489
+ /**
490
+ * Calculates the minimum deposit / maximum borrow token amounts that keep the market's
491
+ * utilization from exceeding a "max withdraw utilization" ceiling, mirroring
492
+ * `calculate_token_utilization_limits`. The ceiling is `max(optimalUtilization,
493
+ * utilizationTwap + (100% - utilizationTwap) / 2)` — i.e. it allows utilization to rise, but
494
+ * only up to halfway from the TWAP to 100%. Deposit sizes already below
495
+ * `withdrawGuardThreshold` are never blocked (the min-deposit result is capped so it can't
496
+ * exceed `depositTokenAmount - withdrawGuardThreshold`), and borrows below the guard threshold
497
+ * are never blocked either (the max-borrow result is floored at `withdrawGuardThreshold`).
498
+ *
499
+ * @param {BN} depositTokenAmount - Current total deposit token amount, market's token decimals
500
+ * @param {BN} borrowTokenAmount - Current total borrow token amount, market's token decimals
501
+ * @param {SpotMarketAccount} spotMarket - The spot market account
502
+ * @return {{ minDepositTokensForUtilization: BN; maxBorrowTokensForUtilization: BN }} Both
503
+ * scaled by the market's token decimals
504
+ */
336
505
  function calculateTokenUtilizationLimits(depositTokenAmount, borrowTokenAmount, spotMarket) {
337
506
  // Calculates the allowable minimum deposit and maximum borrow amounts for immediate withdrawal based on market utilization.
338
507
  // First, it determines a maximum withdrawal utilization from the market's target and historic utilization.
@@ -354,6 +523,32 @@ function calculateTokenUtilizationLimits(depositTokenAmount, borrowTokenAmount,
354
523
  };
355
524
  }
356
525
  exports.calculateTokenUtilizationLimits = calculateTokenUtilizationLimits;
526
+ /**
527
+ * Estimates the current immediate withdraw/borrow limits for a spot market, mirroring the
528
+ * on-chain `check_withdraw_limits` / `get_max_withdraw_for_market_with_token_amount` guard
529
+ * (combining `calculate_min_deposit_token_amount`, `calculate_max_borrow_token_amount`, and
530
+ * `calculateTokenUtilizationLimits`). Because the SDK cannot force an on-chain TWAP update
531
+ * before reading it, this projects a "live" 24h deposit/borrow TWAP by weighting the stored
532
+ * TWAP and the current amount by `sinceStart`/`sinceLast` (the same weighted-average shape as
533
+ * `update_spot_market_twap_stats`, without its rounding bias term) before deriving limits, so
534
+ * the result approximates what the on-chain TWAP would be if updated at `now`.
535
+ *
536
+ * Deposit/borrow TWAP friction bands differ by pool: the main pool (`poolId === 0`) targets
537
+ * ~30-92.5% utilization (borrow ceiling is `lesserDepositAmount` clamped between 1/3 and
538
+ * 13/14 of itself, floored around the live borrow TWAP + 1/5), isolated pools (`poolId !== 0`)
539
+ * target ~50-95% (clamped between 1/2 and 19/20, floored around the live borrow TWAP + 1/3).
540
+ * `lesserDepositAmount` is `min(currentDepositAmount, live deposit TWAP)` — using the smaller of
541
+ * the two keeps the borrow ceiling conservative whether deposits are rising or falling.
542
+ * `borrowLimit` is additionally zeroed for `assetTier === 'protected'` markets, and both limits
543
+ * are clamped by `maxTokenBorrowsFraction` of `maxTokenDeposits` when that cap is configured.
544
+ *
545
+ * @param {SpotMarketAccount} spotMarket - The spot market account
546
+ * @param {BN} now - The timestamp (unix seconds) to project the live TWAP up to
547
+ * @return {{ borrowLimit: BN; withdrawLimit: BN; minDepositAmount: BN; maxBorrowAmount: BN;
548
+ * currentDepositAmount: BN; currentBorrowAmount: BN }} All values scaled by the market's token
549
+ * decimals. `withdrawLimit`/`borrowLimit` are floored at zero (a market already past its
550
+ * min-deposit/max-borrow bound reports zero remaining room rather than negative)
551
+ */
357
552
  function calculateWithdrawLimit(spotMarket, now) {
358
553
  const marketDepositTokenAmount = getTokenAmount(spotMarket.depositBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
359
554
  const marketBorrowTokenAmount = getTokenAmount(spotMarket.borrowBalance, spotMarket, types_1.SpotBalanceType.BORROW);
@@ -371,10 +566,10 @@ function calculateWithdrawLimit(spotMarket, now) {
371
566
  const lesserDepositAmount = anchor_1.BN.min(marketDepositTokenAmount, depositTokenTwapLive);
372
567
  let maxBorrowTokensTwap;
373
568
  if (spotMarket.poolId == 0) {
374
- maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(3)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(7)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(8))))); // main pool between ~30-92.5% utilization with friction on twap in 20% increments
569
+ maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(lesserDepositAmount.div(new anchor_1.BN(3)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(5)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(14))))); // main pool between ~30-92.5% utilization with friction on twap in 20% increments
375
570
  }
376
571
  else {
377
- maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(2)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(3)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(20))))); // isolated pools between 50-95% utilization with friction on twap in 33% increments
572
+ maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(lesserDepositAmount.div(new anchor_1.BN(2)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(3)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(20))))); // isolated pools between 50-95% utilization with friction on twap in 33% increments
378
573
  }
379
574
  const minDepositTokensTwap = depositTokenTwapLive.sub(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(4)), anchor_1.BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)));
380
575
  const { minDepositTokensForUtilization, maxBorrowTokensForUtilization } = calculateTokenUtilizationLimits(marketDepositTokenAmount, marketBorrowTokenAmount, spotMarket);
@@ -404,6 +599,22 @@ function calculateWithdrawLimit(spotMarket, now) {
404
599
  };
405
600
  }
406
601
  exports.calculateWithdrawLimit = calculateWithdrawLimit;
602
+ /**
603
+ * Calculates the margin-weighted value of a spot deposit, mirroring the asset-side of the
604
+ * program's collateral valuation (`get_strict_token_value` + `get_asset_weight`). Uses the
605
+ * worst of the oracle's live price and its 5min TWAP (via `strictOraclePrice`) so a favorable
606
+ * price spike can't be used to over-value collateral.
607
+ *
608
+ * @param {BN} tokenAmount - The deposit token amount, scaled by `spotMarketAccount.decimals`
609
+ * @param {StrictOraclePrice} strictOraclePrice - Live oracle price + 5min TWAP, PRICE_PRECISION (1e6)
610
+ * @param {SpotMarketAccount} spotMarketAccount - The spot market account
611
+ * @param {number} maxMarginRatio - The user's custom max margin ratio (0 if unset), in
612
+ * `SPOT_MARKET_WEIGHT_PRECISION` (1e4) units; only applied when `marginCategory === 'Initial'`
613
+ * and the market isn't the quote spot market, capping the weight at
614
+ * `SPOT_MARKET_WEIGHT_PRECISION - maxMarginRatio`
615
+ * @param {MarginCategory} [marginCategory] - When omitted, returns the unweighted (100%) value
616
+ * @return {BN} The (optionally weighted) asset value, scaled by `PRICE_PRECISION` (1e6)
617
+ */
407
618
  function getSpotAssetValue(tokenAmount, strictOraclePrice, spotMarketAccount, maxMarginRatio, marginCategory) {
408
619
  let assetValue = getStrictTokenValue(tokenAmount, spotMarketAccount.decimals, strictOraclePrice);
409
620
  if (marginCategory !== undefined) {
@@ -418,6 +629,24 @@ function getSpotAssetValue(tokenAmount, strictOraclePrice, spotMarketAccount, ma
418
629
  return assetValue;
419
630
  }
420
631
  exports.getSpotAssetValue = getSpotAssetValue;
632
+ /**
633
+ * Calculates the margin-weighted value of a spot borrow, mirroring the liability-side of the
634
+ * program's collateral valuation (`get_strict_token_value` + `get_liability_weight`). Uses the
635
+ * worst of the oracle's live price and its 5min TWAP (via `strictOraclePrice`) so a favorable
636
+ * price dip can't be used to under-value a liability.
637
+ *
638
+ * @param {BN} tokenAmount - The borrow token amount (positive), scaled by `spotMarketAccount.decimals`
639
+ * @param {StrictOraclePrice} strictOraclePrice - Live oracle price + 5min TWAP, PRICE_PRECISION (1e6)
640
+ * @param {SpotMarketAccount} spotMarketAccount - The spot market account
641
+ * @param {number} maxMarginRatio - The user's custom max margin ratio (0 if unset),
642
+ * `SPOT_MARKET_WEIGHT_PRECISION` (1e4) units; only applied when `marginCategory === 'Initial'`
643
+ * and the market isn't the quote spot market, flooring the weight at
644
+ * `SPOT_MARKET_WEIGHT_PRECISION + maxMarginRatio`
645
+ * @param {MarginCategory} [marginCategory] - When omitted, returns the unweighted (100%) value
646
+ * @param {BN} [liquidationBuffer] - Extra weight added on top (`SPOT_MARKET_WEIGHT_PRECISION`
647
+ * units) to make maintenance margin checks stricter during liquidation eligibility checks
648
+ * @return {BN} The (optionally weighted) liability value, scaled by `PRICE_PRECISION` (1e6)
649
+ */
421
650
  function getSpotLiabilityValue(tokenAmount, strictOraclePrice, spotMarketAccount, maxMarginRatio, marginCategory, liquidationBuffer) {
422
651
  let liabilityValue = getStrictTokenValue(tokenAmount, spotMarketAccount.decimals, strictOraclePrice);
423
652
  if (marginCategory !== undefined) {