@velocity-exchange/sdk 0.0.5 → 0.2.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 (815) hide show
  1. package/.prettierignore +1 -1
  2. package/CHANGELOG.md +35 -0
  3. package/README.md +30 -30
  4. package/bun.lock +24 -2
  5. package/lib/browser/accounts/basicUserAccountSubscriber.d.ts +5 -3
  6. package/lib/browser/accounts/basicUserAccountSubscriber.js +4 -3
  7. package/lib/browser/accounts/basicUserStatsAccountSubscriber.d.ts +5 -3
  8. package/lib/browser/accounts/basicUserStatsAccountSubscriber.js +4 -3
  9. package/lib/browser/accounts/bulkAccountLoader.d.ts +5 -5
  10. package/lib/browser/accounts/bulkAccountLoader.js +12 -1
  11. package/lib/browser/accounts/grpcAccountSubscriber.d.ts +2 -1
  12. package/lib/browser/accounts/grpcAccountSubscriber.js +8 -2
  13. package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +3 -2
  14. package/lib/browser/accounts/grpcMultiAccountSubscriber.js +13 -5
  15. package/lib/browser/accounts/grpcMultiUserAccountSubscriber.d.ts +2 -1
  16. package/lib/browser/accounts/grpcMultiUserAccountSubscriber.js +12 -8
  17. package/lib/browser/accounts/grpcProgramAccountSubscriber.d.ts +2 -1
  18. package/lib/browser/accounts/grpcProgramAccountSubscriber.js +9 -3
  19. package/lib/browser/accounts/grpcVelocityClientAccountSubscriber.d.ts +0 -4
  20. package/lib/browser/accounts/grpcVelocityClientAccountSubscriber.js +19 -6
  21. package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +1 -5
  22. package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.js +44 -23
  23. package/lib/browser/accounts/laserProgramAccountSubscriber.js +4 -1
  24. package/lib/browser/accounts/oneShotUserAccountSubscriber.js +4 -3
  25. package/lib/browser/accounts/oneShotUserStatsAccountSubscriber.js +7 -3
  26. package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  27. package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.js +3 -3
  28. package/lib/browser/accounts/pollingOracleAccountSubscriber.d.ts +0 -2
  29. package/lib/browser/accounts/pollingOracleAccountSubscriber.js +16 -5
  30. package/lib/browser/accounts/pollingTokenAccountSubscriber.d.ts +0 -2
  31. package/lib/browser/accounts/pollingTokenAccountSubscriber.js +16 -5
  32. package/lib/browser/accounts/pollingUserAccountSubscriber.d.ts +8 -4
  33. package/lib/browser/accounts/pollingUserAccountSubscriber.js +17 -11
  34. package/lib/browser/accounts/pollingUserStatsAccountSubscriber.d.ts +4 -2
  35. package/lib/browser/accounts/pollingUserStatsAccountSubscriber.js +6 -6
  36. package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.d.ts +7 -5
  37. package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.js +69 -35
  38. package/lib/browser/accounts/types.d.ts +6 -12
  39. package/lib/browser/accounts/utils.d.ts +1 -0
  40. package/lib/browser/accounts/utils.js +10 -3
  41. package/lib/browser/accounts/webSocketAccountSubscriber.d.ts +4 -2
  42. package/lib/browser/accounts/webSocketAccountSubscriber.js +17 -5
  43. package/lib/browser/accounts/webSocketAccountSubscriberV2.d.ts +4 -2
  44. package/lib/browser/accounts/webSocketAccountSubscriberV2.js +17 -6
  45. package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  46. package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +9 -0
  47. package/lib/browser/accounts/webSocketProgramAccountSubscriber.d.ts +3 -1
  48. package/lib/browser/accounts/webSocketProgramAccountSubscriber.js +14 -3
  49. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +3 -1
  50. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +15 -3
  51. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.d.ts +3 -1
  52. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.js +14 -4
  53. package/lib/browser/accounts/webSocketUserAccountSubscriber.d.ts +4 -2
  54. package/lib/browser/accounts/webSocketUserAccountSubscriber.js +9 -0
  55. package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.d.ts +4 -2
  56. package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.js +9 -0
  57. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.d.ts +4 -8
  58. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.js +52 -26
  59. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +7 -7
  60. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.js +49 -20
  61. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.d.ts +1 -1
  62. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.js +0 -3
  63. package/lib/browser/addresses/marketAddresses.js +3 -2
  64. package/lib/browser/addresses/pda.d.ts +1 -9
  65. package/lib/browser/addresses/pda.js +4 -23
  66. package/lib/browser/adminClient.d.ts +25 -29
  67. package/lib/browser/adminClient.js +168 -169
  68. package/lib/browser/auctionSubscriber/auctionSubscriber.d.ts +2 -4
  69. package/lib/browser/auctionSubscriber/auctionSubscriber.js +8 -10
  70. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.d.ts +2 -4
  71. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +11 -10
  72. package/lib/browser/auctionSubscriber/types.d.ts +3 -2
  73. package/lib/browser/bankrun/bankrunConnection.d.ts +6 -4
  74. package/lib/browser/bankrun/bankrunConnection.js +69 -31
  75. package/lib/browser/blockhashSubscriber/BlockhashSubscriber.d.ts +1 -1
  76. package/lib/browser/blockhashSubscriber/BlockhashSubscriber.js +3 -2
  77. package/lib/browser/clock/clockSubscriber.d.ts +6 -6
  78. package/lib/browser/clock/clockSubscriber.js +4 -3
  79. package/lib/browser/config.d.ts +11 -20
  80. package/lib/browser/config.js +13 -36
  81. package/lib/browser/constants/index.d.ts +1 -2
  82. package/lib/browser/constants/index.js +1 -2
  83. package/lib/browser/constants/numericConstants.d.ts +1 -3
  84. package/lib/browser/constants/numericConstants.js +3 -5
  85. package/lib/browser/constituentMap/constituentMap.d.ts +0 -2
  86. package/lib/browser/constituentMap/constituentMap.js +10 -6
  87. package/lib/browser/constituentMap/pollingConstituentAccountSubscriber.js +3 -2
  88. package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.d.ts +2 -3
  89. package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.js +10 -3
  90. package/lib/browser/core/VelocityCore.d.ts +9 -15
  91. package/lib/browser/core/VelocityCore.js +5 -7
  92. package/lib/browser/core/instructions/perpOrders.d.ts +14 -0
  93. package/lib/browser/core/instructions/perpOrders.js +14 -0
  94. package/lib/browser/core/instructions/withdraw.d.ts +1 -1
  95. package/lib/browser/core/instructions/withdraw.js +1 -1
  96. package/lib/browser/core/remainingAccounts.d.ts +2 -2
  97. package/lib/browser/core/remainingAccounts.js +5 -5
  98. package/lib/browser/decode/user.js +9 -1
  99. package/lib/browser/dlob/DLOB.d.ts +7 -2
  100. package/lib/browser/dlob/DLOB.js +205 -142
  101. package/lib/browser/dlob/DLOBNode.d.ts +5 -2
  102. package/lib/browser/dlob/DLOBNode.js +7 -0
  103. package/lib/browser/dlob/DLOBSubscriber.d.ts +0 -2
  104. package/lib/browser/dlob/DLOBSubscriber.js +39 -39
  105. package/lib/browser/dlob/NodeList.js +8 -7
  106. package/lib/browser/dlob/orderBookLevels.js +24 -17
  107. package/lib/browser/dlob/types.d.ts +0 -2
  108. package/lib/browser/events/eventList.js +4 -1
  109. package/lib/browser/events/eventSubscriber.d.ts +6 -3
  110. package/lib/browser/events/eventSubscriber.js +75 -26
  111. package/lib/browser/events/eventsServerLogProvider.d.ts +1 -1
  112. package/lib/browser/events/eventsServerLogProvider.js +17 -10
  113. package/lib/browser/events/fetchLogs.d.ts +1 -1
  114. package/lib/browser/events/fetchLogs.js +4 -2
  115. package/lib/browser/events/parse.js +14 -14
  116. package/lib/browser/events/pollingLogProvider.d.ts +1 -1
  117. package/lib/browser/events/pollingLogProvider.js +1 -0
  118. package/lib/browser/events/txEventCache.js +6 -2
  119. package/lib/browser/events/types.d.ts +3 -7
  120. package/lib/browser/events/types.js +1 -3
  121. package/lib/browser/events/webSocketLogProvider.d.ts +1 -1
  122. package/lib/browser/events/webSocketLogProvider.js +5 -2
  123. package/lib/browser/factory/bigNum.js +1 -1
  124. package/lib/{node/idl/drift.d.ts → browser/idl/velocity.d.ts} +2412 -2481
  125. package/lib/browser/idl/{drift.json → velocity.json} +2265 -2333
  126. package/lib/browser/index.d.ts +1 -3
  127. package/lib/browser/index.js +1 -4
  128. package/lib/browser/jupiter/jupiterClient.d.ts +1 -1
  129. package/lib/browser/jupiter/jupiterClient.js +18 -8
  130. package/lib/browser/marginCalculation.js +1 -0
  131. package/lib/browser/math/amm.d.ts +31 -13
  132. package/lib/browser/math/amm.js +87 -31
  133. package/lib/browser/math/auction.js +16 -12
  134. package/lib/browser/math/bankruptcy.js +1 -1
  135. package/lib/browser/math/builder.d.ts +18 -1
  136. package/lib/browser/math/builder.js +32 -1
  137. package/lib/browser/math/funding.js +30 -30
  138. package/lib/browser/math/insurance.d.ts +1 -1
  139. package/lib/browser/math/insurance.js +4 -5
  140. package/lib/browser/math/margin.d.ts +3 -11
  141. package/lib/browser/math/margin.js +10 -69
  142. package/lib/browser/math/market.d.ts +4 -12
  143. package/lib/browser/math/market.js +12 -46
  144. package/lib/browser/math/oracles.d.ts +4 -7
  145. package/lib/browser/math/oracles.js +24 -37
  146. package/lib/browser/math/orders.d.ts +2 -7
  147. package/lib/browser/math/orders.js +9 -81
  148. package/lib/browser/math/position.d.ts +0 -4
  149. package/lib/browser/math/position.js +6 -11
  150. package/lib/browser/math/repeg.d.ts +0 -1
  151. package/lib/browser/math/repeg.js +1 -23
  152. package/lib/browser/math/spotBalance.d.ts +4 -4
  153. package/lib/browser/math/spotBalance.js +6 -6
  154. package/lib/browser/math/spotMarket.d.ts +0 -6
  155. package/lib/browser/math/spotMarket.js +1 -16
  156. package/lib/browser/math/spotPosition.d.ts +4 -4
  157. package/lib/browser/math/state.d.ts +0 -2
  158. package/lib/browser/math/state.js +1 -9
  159. package/lib/browser/math/superStake.d.ts +11 -63
  160. package/lib/browser/math/superStake.js +29 -28
  161. package/lib/browser/math/trade.js +7 -7
  162. package/lib/browser/math/utils.d.ts +1 -1
  163. package/lib/browser/math/utils.js +1 -1
  164. package/lib/browser/memcmp.d.ts +0 -1
  165. package/lib/browser/memcmp.js +1 -10
  166. package/lib/browser/oracles/prelaunchOracleClient.js +3 -2
  167. package/lib/browser/oracles/pythClient.js +9 -3
  168. package/lib/browser/oracles/pythLazerClient.js +5 -4
  169. package/lib/browser/oracles/utils.d.ts +4 -0
  170. package/lib/browser/oracles/utils.js +9 -1
  171. package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +1 -3
  172. package/lib/browser/orderSubscriber/OrderSubscriber.js +16 -14
  173. package/lib/browser/orderSubscriber/WebsocketSubscription.js +1 -1
  174. package/lib/browser/orderSubscriber/grpcSubscription.js +2 -2
  175. package/lib/browser/orderSubscriber/types.d.ts +0 -2
  176. package/lib/browser/priorityFee/heliusPriorityFeeMethod.d.ts +1 -1
  177. package/lib/browser/priorityFee/priorityFeeSubscriber.d.ts +2 -6
  178. package/lib/browser/priorityFee/priorityFeeSubscriber.js +34 -18
  179. package/lib/browser/priorityFee/priorityFeeSubscriberMap.d.ts +0 -4
  180. package/lib/browser/priorityFee/priorityFeeSubscriberMap.js +3 -11
  181. package/lib/browser/priorityFee/solanaPriorityFeeMethod.d.ts +1 -1
  182. package/lib/browser/priorityFee/types.d.ts +3 -8
  183. package/lib/browser/priorityFee/velocityPriorityFeeMethod.d.ts +0 -8
  184. package/lib/browser/priorityFee/velocityPriorityFeeMethod.js +1 -3
  185. package/lib/browser/pyth/pythLazerSubscriber.d.ts +1 -1
  186. package/lib/browser/pyth/pythLazerSubscriber.js +1 -1
  187. package/lib/browser/pyth/types.d.ts +0 -1
  188. package/lib/browser/slot/SlotSubscriber.d.ts +1 -1
  189. package/lib/browser/slot/SlotSubscriber.js +2 -1
  190. package/lib/browser/slot/SlothashSubscriber.d.ts +3 -2
  191. package/lib/browser/slot/SlothashSubscriber.js +11 -4
  192. package/lib/browser/swap/UnifiedSwapClient.js +1 -1
  193. package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.d.ts +0 -3
  194. package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.js +5 -5
  195. package/lib/browser/swift/signedMsgUserAccountSubscriber.d.ts +7 -7
  196. package/lib/browser/swift/signedMsgUserAccountSubscriber.js +14 -10
  197. package/lib/browser/swift/swiftOrderSubscriber.d.ts +7 -6
  198. package/lib/browser/swift/swiftOrderSubscriber.js +15 -16
  199. package/lib/browser/testClient.js +3 -2
  200. package/lib/browser/tokenFaucet.js +16 -2
  201. package/lib/browser/tx/baseTxSender.d.ts +4 -4
  202. package/lib/browser/tx/baseTxSender.js +5 -4
  203. package/lib/browser/tx/fastSingleTxSender.d.ts +3 -3
  204. package/lib/browser/tx/fastSingleTxSender.js +9 -5
  205. package/lib/browser/tx/reportTransactionError.d.ts +2 -2
  206. package/lib/browser/tx/reportTransactionError.js +5 -1
  207. package/lib/browser/tx/retryTxSender.d.ts +1 -1
  208. package/lib/browser/tx/retryTxSender.js +4 -2
  209. package/lib/browser/tx/txHandler.d.ts +7 -1
  210. package/lib/browser/tx/txHandler.js +54 -40
  211. package/lib/browser/tx/txParamProcessor.d.ts +4 -1
  212. package/lib/browser/tx/txParamProcessor.js +6 -0
  213. package/lib/browser/tx/types.d.ts +1 -1
  214. package/lib/browser/tx/whileValidTxSender.d.ts +1 -6
  215. package/lib/browser/tx/whileValidTxSender.js +35 -13
  216. package/lib/browser/types.d.ts +102 -157
  217. package/lib/browser/types.js +6 -2
  218. package/lib/browser/user.d.ts +22 -10
  219. package/lib/browser/user.js +174 -154
  220. package/lib/browser/userConfig.d.ts +3 -2
  221. package/lib/browser/userMap/WebsocketSubscription.d.ts +1 -1
  222. package/lib/browser/userMap/WebsocketSubscription.js +1 -1
  223. package/lib/browser/userMap/grpcSubscription.d.ts +1 -1
  224. package/lib/browser/userMap/grpcSubscription.js +1 -1
  225. package/lib/browser/userMap/referrerMap.d.ts +0 -2
  226. package/lib/browser/userMap/referrerMap.js +10 -7
  227. package/lib/browser/userMap/revenueShareEscrowMap.d.ts +0 -2
  228. package/lib/browser/userMap/revenueShareEscrowMap.js +4 -4
  229. package/lib/browser/userMap/userMap.d.ts +3 -5
  230. package/lib/browser/userMap/userMap.js +39 -30
  231. package/lib/browser/userMap/userMapConfig.d.ts +3 -2
  232. package/lib/browser/userMap/userStatsMap.d.ts +1 -3
  233. package/lib/browser/userMap/userStatsMap.js +18 -17
  234. package/lib/browser/userStats.d.ts +7 -4
  235. package/lib/browser/userStats.js +28 -18
  236. package/lib/browser/userStatsConfig.d.ts +3 -2
  237. package/lib/browser/util/TransactionConfirmationManager.js +4 -2
  238. package/lib/browser/util/chainClock.d.ts +1 -1
  239. package/lib/browser/util/computeUnits.d.ts +1 -1
  240. package/lib/browser/util/computeUnits.js +6 -1
  241. package/lib/browser/util/ed25519Utils.d.ts +1 -1
  242. package/lib/browser/util/ed25519Utils.js +1 -1
  243. package/lib/browser/velocityClient.d.ts +144 -71
  244. package/lib/browser/velocityClient.js +653 -457
  245. package/lib/browser/velocityClientConfig.d.ts +0 -8
  246. package/lib/node/accounts/basicUserAccountSubscriber.d.ts +5 -3
  247. package/lib/node/accounts/basicUserAccountSubscriber.d.ts.map +1 -1
  248. package/lib/node/accounts/basicUserAccountSubscriber.js +4 -3
  249. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts +5 -3
  250. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts.map +1 -1
  251. package/lib/node/accounts/basicUserStatsAccountSubscriber.js +4 -3
  252. package/lib/node/accounts/bulkAccountLoader.d.ts +5 -5
  253. package/lib/node/accounts/bulkAccountLoader.d.ts.map +1 -1
  254. package/lib/node/accounts/bulkAccountLoader.js +12 -1
  255. package/lib/node/accounts/grpcAccountSubscriber.d.ts +2 -1
  256. package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
  257. package/lib/node/accounts/grpcAccountSubscriber.js +8 -2
  258. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +3 -2
  259. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
  260. package/lib/node/accounts/grpcMultiAccountSubscriber.js +13 -5
  261. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts +2 -1
  262. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts.map +1 -1
  263. package/lib/node/accounts/grpcMultiUserAccountSubscriber.js +12 -8
  264. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +2 -1
  265. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts.map +1 -1
  266. package/lib/node/accounts/grpcProgramAccountSubscriber.js +9 -3
  267. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts +0 -4
  268. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts.map +1 -1
  269. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.js +19 -6
  270. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +1 -5
  271. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  272. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.js +44 -23
  273. package/lib/node/accounts/laserProgramAccountSubscriber.d.ts.map +1 -1
  274. package/lib/node/accounts/laserProgramAccountSubscriber.js +4 -1
  275. package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts.map +1 -1
  276. package/lib/node/accounts/oneShotUserAccountSubscriber.js +4 -3
  277. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts.map +1 -1
  278. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.js +7 -3
  279. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  280. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  281. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.js +3 -3
  282. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts +0 -2
  283. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts.map +1 -1
  284. package/lib/node/accounts/pollingOracleAccountSubscriber.js +16 -5
  285. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts +0 -2
  286. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -1
  287. package/lib/node/accounts/pollingTokenAccountSubscriber.js +16 -5
  288. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts +8 -4
  289. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts.map +1 -1
  290. package/lib/node/accounts/pollingUserAccountSubscriber.js +17 -11
  291. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts +4 -2
  292. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts.map +1 -1
  293. package/lib/node/accounts/pollingUserStatsAccountSubscriber.js +6 -6
  294. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts +7 -5
  295. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts.map +1 -1
  296. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.js +69 -35
  297. package/lib/node/accounts/types.d.ts +6 -12
  298. package/lib/node/accounts/types.d.ts.map +1 -1
  299. package/lib/node/accounts/utils.d.ts +1 -0
  300. package/lib/node/accounts/utils.d.ts.map +1 -1
  301. package/lib/node/accounts/utils.js +10 -3
  302. package/lib/node/accounts/webSocketAccountSubscriber.d.ts +4 -2
  303. package/lib/node/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
  304. package/lib/node/accounts/webSocketAccountSubscriber.js +17 -5
  305. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts +4 -2
  306. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts.map +1 -1
  307. package/lib/node/accounts/webSocketAccountSubscriberV2.js +17 -6
  308. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  309. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  310. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +9 -0
  311. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts +3 -1
  312. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts.map +1 -1
  313. package/lib/node/accounts/webSocketProgramAccountSubscriber.js +14 -3
  314. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +3 -1
  315. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -1
  316. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +15 -3
  317. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts +3 -1
  318. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts.map +1 -1
  319. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.js +14 -4
  320. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts +4 -2
  321. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -1
  322. package/lib/node/accounts/webSocketUserAccountSubscriber.js +9 -0
  323. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts +4 -2
  324. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts.map +1 -1
  325. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.js +9 -0
  326. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts +4 -8
  327. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts.map +1 -1
  328. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.js +52 -26
  329. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +7 -7
  330. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  331. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.js +49 -20
  332. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts +1 -1
  333. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts.map +1 -1
  334. package/lib/node/accounts/websocketProgramUserAccountSubscriber.js +0 -3
  335. package/lib/node/addresses/marketAddresses.d.ts.map +1 -1
  336. package/lib/node/addresses/marketAddresses.js +3 -2
  337. package/lib/node/addresses/pda.d.ts +1 -9
  338. package/lib/node/addresses/pda.d.ts.map +1 -1
  339. package/lib/node/addresses/pda.js +4 -23
  340. package/lib/node/adminClient.d.ts +25 -29
  341. package/lib/node/adminClient.d.ts.map +1 -1
  342. package/lib/node/adminClient.js +168 -169
  343. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +2 -4
  344. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
  345. package/lib/node/auctionSubscriber/auctionSubscriber.js +8 -10
  346. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +2 -4
  347. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
  348. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +11 -10
  349. package/lib/node/auctionSubscriber/types.d.ts +3 -2
  350. package/lib/node/auctionSubscriber/types.d.ts.map +1 -1
  351. package/lib/node/bankrun/bankrunConnection.d.ts +6 -4
  352. package/lib/node/bankrun/bankrunConnection.d.ts.map +1 -1
  353. package/lib/node/bankrun/bankrunConnection.js +69 -31
  354. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts +1 -1
  355. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts.map +1 -1
  356. package/lib/node/blockhashSubscriber/BlockhashSubscriber.js +3 -2
  357. package/lib/node/clock/clockSubscriber.d.ts +6 -6
  358. package/lib/node/clock/clockSubscriber.d.ts.map +1 -1
  359. package/lib/node/clock/clockSubscriber.js +4 -3
  360. package/lib/node/config.d.ts +11 -20
  361. package/lib/node/config.d.ts.map +1 -1
  362. package/lib/node/config.js +13 -36
  363. package/lib/node/constants/index.d.ts +1 -2
  364. package/lib/node/constants/index.d.ts.map +1 -1
  365. package/lib/node/constants/index.js +1 -2
  366. package/lib/node/constants/numericConstants.d.ts +1 -3
  367. package/lib/node/constants/numericConstants.d.ts.map +1 -1
  368. package/lib/node/constants/numericConstants.js +3 -5
  369. package/lib/node/constituentMap/constituentMap.d.ts +0 -2
  370. package/lib/node/constituentMap/constituentMap.d.ts.map +1 -1
  371. package/lib/node/constituentMap/constituentMap.js +10 -6
  372. package/lib/node/constituentMap/pollingConstituentAccountSubscriber.d.ts.map +1 -1
  373. package/lib/node/constituentMap/pollingConstituentAccountSubscriber.js +3 -2
  374. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts +2 -3
  375. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts.map +1 -1
  376. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.js +10 -3
  377. package/lib/node/core/VelocityCore.d.ts +9 -15
  378. package/lib/node/core/VelocityCore.d.ts.map +1 -1
  379. package/lib/node/core/VelocityCore.js +5 -7
  380. package/lib/node/core/instructions/perpOrders.d.ts +14 -0
  381. package/lib/node/core/instructions/perpOrders.d.ts.map +1 -1
  382. package/lib/node/core/instructions/perpOrders.js +14 -0
  383. package/lib/node/core/instructions/withdraw.d.ts +1 -1
  384. package/lib/node/core/instructions/withdraw.d.ts.map +1 -1
  385. package/lib/node/core/instructions/withdraw.js +1 -1
  386. package/lib/node/core/remainingAccounts.d.ts +2 -2
  387. package/lib/node/core/remainingAccounts.d.ts.map +1 -1
  388. package/lib/node/core/remainingAccounts.js +5 -5
  389. package/lib/node/decode/user.d.ts.map +1 -1
  390. package/lib/node/decode/user.js +9 -1
  391. package/lib/node/dlob/DLOB.d.ts +7 -2
  392. package/lib/node/dlob/DLOB.d.ts.map +1 -1
  393. package/lib/node/dlob/DLOB.js +205 -142
  394. package/lib/node/dlob/DLOBNode.d.ts +5 -2
  395. package/lib/node/dlob/DLOBNode.d.ts.map +1 -1
  396. package/lib/node/dlob/DLOBNode.js +7 -0
  397. package/lib/node/dlob/DLOBSubscriber.d.ts +0 -2
  398. package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
  399. package/lib/node/dlob/DLOBSubscriber.js +39 -39
  400. package/lib/node/dlob/NodeList.d.ts.map +1 -1
  401. package/lib/node/dlob/NodeList.js +8 -7
  402. package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
  403. package/lib/node/dlob/orderBookLevels.js +24 -17
  404. package/lib/node/dlob/types.d.ts +0 -2
  405. package/lib/node/dlob/types.d.ts.map +1 -1
  406. package/lib/node/events/eventList.d.ts +3 -3
  407. package/lib/node/events/eventList.d.ts.map +1 -1
  408. package/lib/node/events/eventList.js +4 -1
  409. package/lib/node/events/eventSubscriber.d.ts +6 -3
  410. package/lib/node/events/eventSubscriber.d.ts.map +1 -1
  411. package/lib/node/events/eventSubscriber.js +75 -26
  412. package/lib/node/events/eventsServerLogProvider.d.ts +2 -2
  413. package/lib/node/events/eventsServerLogProvider.d.ts.map +1 -1
  414. package/lib/node/events/eventsServerLogProvider.js +17 -10
  415. package/lib/node/events/fetchLogs.d.ts +1 -1
  416. package/lib/node/events/fetchLogs.d.ts.map +1 -1
  417. package/lib/node/events/fetchLogs.js +4 -2
  418. package/lib/node/events/parse.d.ts.map +1 -1
  419. package/lib/node/events/parse.js +14 -14
  420. package/lib/node/events/pollingLogProvider.d.ts +2 -2
  421. package/lib/node/events/pollingLogProvider.d.ts.map +1 -1
  422. package/lib/node/events/pollingLogProvider.js +1 -0
  423. package/lib/node/events/txEventCache.d.ts +3 -3
  424. package/lib/node/events/txEventCache.d.ts.map +1 -1
  425. package/lib/node/events/txEventCache.js +6 -2
  426. package/lib/node/events/types.d.ts +3 -7
  427. package/lib/node/events/types.d.ts.map +1 -1
  428. package/lib/node/events/types.js +1 -3
  429. package/lib/node/events/webSocketLogProvider.d.ts +2 -2
  430. package/lib/node/events/webSocketLogProvider.d.ts.map +1 -1
  431. package/lib/node/events/webSocketLogProvider.js +5 -2
  432. package/lib/node/factory/bigNum.js +1 -1
  433. package/lib/{browser/idl/drift.d.ts → node/idl/velocity.d.ts} +2413 -2480
  434. package/lib/node/idl/velocity.d.ts.map +1 -0
  435. package/lib/node/idl/{drift.json → velocity.json} +2265 -2333
  436. package/lib/node/index.d.ts +1 -3
  437. package/lib/node/index.d.ts.map +1 -1
  438. package/lib/node/index.js +1 -4
  439. package/lib/node/jupiter/jupiterClient.d.ts +1 -1
  440. package/lib/node/jupiter/jupiterClient.d.ts.map +1 -1
  441. package/lib/node/jupiter/jupiterClient.js +18 -8
  442. package/lib/node/marginCalculation.d.ts.map +1 -1
  443. package/lib/node/marginCalculation.js +1 -0
  444. package/lib/node/math/amm.d.ts +31 -13
  445. package/lib/node/math/amm.d.ts.map +1 -1
  446. package/lib/node/math/amm.js +87 -31
  447. package/lib/node/math/auction.d.ts.map +1 -1
  448. package/lib/node/math/auction.js +16 -12
  449. package/lib/node/math/bankruptcy.js +1 -1
  450. package/lib/node/math/builder.d.ts +18 -1
  451. package/lib/node/math/builder.d.ts.map +1 -1
  452. package/lib/node/math/builder.js +32 -1
  453. package/lib/node/math/funding.d.ts.map +1 -1
  454. package/lib/node/math/funding.js +30 -30
  455. package/lib/node/math/insurance.d.ts +1 -1
  456. package/lib/node/math/insurance.d.ts.map +1 -1
  457. package/lib/node/math/insurance.js +4 -5
  458. package/lib/node/math/margin.d.ts +3 -11
  459. package/lib/node/math/margin.d.ts.map +1 -1
  460. package/lib/node/math/margin.js +10 -69
  461. package/lib/node/math/market.d.ts +4 -12
  462. package/lib/node/math/market.d.ts.map +1 -1
  463. package/lib/node/math/market.js +12 -46
  464. package/lib/node/math/oracles.d.ts +4 -7
  465. package/lib/node/math/oracles.d.ts.map +1 -1
  466. package/lib/node/math/oracles.js +24 -37
  467. package/lib/node/math/orders.d.ts +2 -7
  468. package/lib/node/math/orders.d.ts.map +1 -1
  469. package/lib/node/math/orders.js +9 -81
  470. package/lib/node/math/position.d.ts +1 -5
  471. package/lib/node/math/position.d.ts.map +1 -1
  472. package/lib/node/math/position.js +6 -11
  473. package/lib/node/math/repeg.d.ts +0 -1
  474. package/lib/node/math/repeg.d.ts.map +1 -1
  475. package/lib/node/math/repeg.js +1 -23
  476. package/lib/node/math/spotBalance.d.ts +4 -4
  477. package/lib/node/math/spotBalance.d.ts.map +1 -1
  478. package/lib/node/math/spotBalance.js +6 -6
  479. package/lib/node/math/spotMarket.d.ts +0 -6
  480. package/lib/node/math/spotMarket.d.ts.map +1 -1
  481. package/lib/node/math/spotMarket.js +1 -16
  482. package/lib/node/math/spotPosition.d.ts +4 -4
  483. package/lib/node/math/spotPosition.d.ts.map +1 -1
  484. package/lib/node/math/state.d.ts +0 -2
  485. package/lib/node/math/state.d.ts.map +1 -1
  486. package/lib/node/math/state.js +1 -9
  487. package/lib/node/math/superStake.d.ts +11 -63
  488. package/lib/node/math/superStake.d.ts.map +1 -1
  489. package/lib/node/math/superStake.js +29 -28
  490. package/lib/node/math/trade.d.ts +2 -2
  491. package/lib/node/math/trade.d.ts.map +1 -1
  492. package/lib/node/math/trade.js +7 -7
  493. package/lib/node/math/utils.d.ts +1 -1
  494. package/lib/node/math/utils.js +1 -1
  495. package/lib/node/memcmp.d.ts +0 -1
  496. package/lib/node/memcmp.d.ts.map +1 -1
  497. package/lib/node/memcmp.js +1 -10
  498. package/lib/node/oracles/oracleClientCache.d.ts +1 -1
  499. package/lib/node/oracles/prelaunchOracleClient.d.ts.map +1 -1
  500. package/lib/node/oracles/prelaunchOracleClient.js +3 -2
  501. package/lib/node/oracles/pythClient.d.ts.map +1 -1
  502. package/lib/node/oracles/pythClient.js +9 -3
  503. package/lib/node/oracles/pythLazerClient.d.ts.map +1 -1
  504. package/lib/node/oracles/pythLazerClient.js +5 -4
  505. package/lib/node/oracles/utils.d.ts +4 -0
  506. package/lib/node/oracles/utils.d.ts.map +1 -1
  507. package/lib/node/oracles/utils.js +9 -1
  508. package/lib/node/orderSubscriber/OrderSubscriber.d.ts +1 -3
  509. package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
  510. package/lib/node/orderSubscriber/OrderSubscriber.js +16 -14
  511. package/lib/node/orderSubscriber/WebsocketSubscription.js +1 -1
  512. package/lib/node/orderSubscriber/grpcSubscription.js +2 -2
  513. package/lib/node/orderSubscriber/types.d.ts +0 -2
  514. package/lib/node/orderSubscriber/types.d.ts.map +1 -1
  515. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts +1 -1
  516. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts.map +1 -1
  517. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +2 -6
  518. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts.map +1 -1
  519. package/lib/node/priorityFee/priorityFeeSubscriber.js +34 -18
  520. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts +0 -4
  521. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts.map +1 -1
  522. package/lib/node/priorityFee/priorityFeeSubscriberMap.js +3 -11
  523. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts +1 -1
  524. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts.map +1 -1
  525. package/lib/node/priorityFee/types.d.ts +3 -8
  526. package/lib/node/priorityFee/types.d.ts.map +1 -1
  527. package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts +0 -8
  528. package/lib/node/priorityFee/velocityPriorityFeeMethod.d.ts.map +1 -1
  529. package/lib/node/priorityFee/velocityPriorityFeeMethod.js +1 -3
  530. package/lib/node/pyth/pythLazerSubscriber.d.ts +1 -1
  531. package/lib/node/pyth/pythLazerSubscriber.js +1 -1
  532. package/lib/node/pyth/types.d.ts +0 -1
  533. package/lib/node/pyth/types.d.ts.map +1 -1
  534. package/lib/node/slot/SlotSubscriber.d.ts +1 -1
  535. package/lib/node/slot/SlotSubscriber.d.ts.map +1 -1
  536. package/lib/node/slot/SlotSubscriber.js +2 -1
  537. package/lib/node/slot/SlothashSubscriber.d.ts +3 -2
  538. package/lib/node/slot/SlothashSubscriber.d.ts.map +1 -1
  539. package/lib/node/slot/SlothashSubscriber.js +11 -4
  540. package/lib/node/swap/UnifiedSwapClient.js +1 -1
  541. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts +0 -3
  542. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts.map +1 -1
  543. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.js +5 -5
  544. package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts +7 -7
  545. package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts.map +1 -1
  546. package/lib/node/swift/signedMsgUserAccountSubscriber.js +14 -10
  547. package/lib/node/swift/swiftOrderSubscriber.d.ts +7 -6
  548. package/lib/node/swift/swiftOrderSubscriber.d.ts.map +1 -1
  549. package/lib/node/swift/swiftOrderSubscriber.js +15 -16
  550. package/lib/node/testClient.js +3 -2
  551. package/lib/node/tokenFaucet.d.ts.map +1 -1
  552. package/lib/node/tokenFaucet.js +16 -2
  553. package/lib/node/tx/baseTxSender.d.ts +4 -4
  554. package/lib/node/tx/baseTxSender.d.ts.map +1 -1
  555. package/lib/node/tx/baseTxSender.js +5 -4
  556. package/lib/node/tx/fastSingleTxSender.d.ts +3 -3
  557. package/lib/node/tx/fastSingleTxSender.d.ts.map +1 -1
  558. package/lib/node/tx/fastSingleTxSender.js +9 -5
  559. package/lib/node/tx/reportTransactionError.d.ts +2 -2
  560. package/lib/node/tx/reportTransactionError.d.ts.map +1 -1
  561. package/lib/node/tx/reportTransactionError.js +5 -1
  562. package/lib/node/tx/retryTxSender.d.ts +1 -1
  563. package/lib/node/tx/retryTxSender.d.ts.map +1 -1
  564. package/lib/node/tx/retryTxSender.js +4 -2
  565. package/lib/node/tx/txHandler.d.ts +9 -3
  566. package/lib/node/tx/txHandler.d.ts.map +1 -1
  567. package/lib/node/tx/txHandler.js +54 -40
  568. package/lib/node/tx/txParamProcessor.d.ts +4 -1
  569. package/lib/node/tx/txParamProcessor.d.ts.map +1 -1
  570. package/lib/node/tx/txParamProcessor.js +6 -0
  571. package/lib/node/tx/types.d.ts +1 -1
  572. package/lib/node/tx/types.d.ts.map +1 -1
  573. package/lib/node/tx/whileValidTxSender.d.ts +1 -6
  574. package/lib/node/tx/whileValidTxSender.d.ts.map +1 -1
  575. package/lib/node/tx/whileValidTxSender.js +35 -13
  576. package/lib/node/types.d.ts +100 -155
  577. package/lib/node/types.d.ts.map +1 -1
  578. package/lib/node/types.js +6 -2
  579. package/lib/node/user.d.ts +22 -10
  580. package/lib/node/user.d.ts.map +1 -1
  581. package/lib/node/user.js +174 -154
  582. package/lib/node/userConfig.d.ts +3 -2
  583. package/lib/node/userConfig.d.ts.map +1 -1
  584. package/lib/node/userMap/WebsocketSubscription.d.ts +1 -1
  585. package/lib/node/userMap/WebsocketSubscription.d.ts.map +1 -1
  586. package/lib/node/userMap/WebsocketSubscription.js +1 -1
  587. package/lib/node/userMap/grpcSubscription.d.ts +1 -1
  588. package/lib/node/userMap/grpcSubscription.d.ts.map +1 -1
  589. package/lib/node/userMap/grpcSubscription.js +1 -1
  590. package/lib/node/userMap/referrerMap.d.ts +0 -2
  591. package/lib/node/userMap/referrerMap.d.ts.map +1 -1
  592. package/lib/node/userMap/referrerMap.js +10 -7
  593. package/lib/node/userMap/revenueShareEscrowMap.d.ts +0 -2
  594. package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
  595. package/lib/node/userMap/revenueShareEscrowMap.js +4 -4
  596. package/lib/node/userMap/userMap.d.ts +3 -5
  597. package/lib/node/userMap/userMap.d.ts.map +1 -1
  598. package/lib/node/userMap/userMap.js +39 -30
  599. package/lib/node/userMap/userMapConfig.d.ts +3 -2
  600. package/lib/node/userMap/userMapConfig.d.ts.map +1 -1
  601. package/lib/node/userMap/userStatsMap.d.ts +1 -3
  602. package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
  603. package/lib/node/userMap/userStatsMap.js +18 -17
  604. package/lib/node/userStats.d.ts +7 -4
  605. package/lib/node/userStats.d.ts.map +1 -1
  606. package/lib/node/userStats.js +28 -18
  607. package/lib/node/userStatsConfig.d.ts +3 -2
  608. package/lib/node/userStatsConfig.d.ts.map +1 -1
  609. package/lib/node/util/TransactionConfirmationManager.d.ts.map +1 -1
  610. package/lib/node/util/TransactionConfirmationManager.js +4 -2
  611. package/lib/node/util/chainClock.d.ts +1 -1
  612. package/lib/node/util/chainClock.d.ts.map +1 -1
  613. package/lib/node/util/computeUnits.d.ts +1 -1
  614. package/lib/node/util/computeUnits.d.ts.map +1 -1
  615. package/lib/node/util/computeUnits.js +6 -1
  616. package/lib/node/util/ed25519Utils.d.ts +1 -1
  617. package/lib/node/util/ed25519Utils.js +1 -1
  618. package/lib/node/velocityClient.d.ts +145 -72
  619. package/lib/node/velocityClient.d.ts.map +1 -1
  620. package/lib/node/velocityClient.js +653 -457
  621. package/lib/node/velocityClientConfig.d.ts +0 -8
  622. package/lib/node/velocityClientConfig.d.ts.map +1 -1
  623. package/package.json +10 -9
  624. package/scripts/deposit-isolated-positions.ts +2 -2
  625. package/scripts/find-flagged-users.ts +1 -1
  626. package/scripts/grpc-client-test-comparison.ts +4 -4
  627. package/scripts/grpc-multiuser-client-test-comparison.ts +2 -2
  628. package/scripts/single-grpc-client-test.ts +3 -3
  629. package/scripts/updateVersion.js +4 -4
  630. package/scripts/withdraw-isolated-positions.ts +2 -2
  631. package/src/accounts/basicUserAccountSubscriber.ts +7 -5
  632. package/src/accounts/basicUserStatsAccountSubscriber.ts +9 -5
  633. package/src/accounts/bulkAccountLoader.ts +20 -7
  634. package/src/accounts/grpcAccountSubscriber.ts +11 -3
  635. package/src/accounts/grpcMultiAccountSubscriber.ts +24 -9
  636. package/src/accounts/grpcMultiUserAccountSubscriber.ts +16 -12
  637. package/src/accounts/grpcProgramAccountSubscriber.ts +12 -4
  638. package/src/accounts/grpcVelocityClientAccountSubscriber.ts +17 -14
  639. package/src/accounts/grpcVelocityClientAccountSubscriberV2.ts +69 -49
  640. package/src/accounts/laserProgramAccountSubscriber.ts +4 -1
  641. package/src/accounts/oneShotUserAccountSubscriber.ts +5 -3
  642. package/src/accounts/oneShotUserStatsAccountSubscriber.ts +8 -3
  643. package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +11 -5
  644. package/src/accounts/pollingOracleAccountSubscriber.ts +17 -10
  645. package/src/accounts/pollingTokenAccountSubscriber.ts +17 -10
  646. package/src/accounts/pollingUserAccountSubscriber.ts +26 -20
  647. package/src/accounts/pollingUserStatsAccountSubscriber.ts +12 -10
  648. package/src/accounts/pollingVelocityClientAccountSubscriber.ts +80 -41
  649. package/src/accounts/types.ts +8 -13
  650. package/src/accounts/utils.ts +12 -2
  651. package/src/accounts/webSocketAccountSubscriber.ts +23 -10
  652. package/src/accounts/webSocketAccountSubscriberV2.ts +23 -8
  653. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +17 -2
  654. package/src/accounts/webSocketProgramAccountSubscriber.ts +29 -4
  655. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +30 -4
  656. package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +29 -4
  657. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -2
  658. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +15 -2
  659. package/src/accounts/webSocketVelocityClientAccountSubscriber.ts +79 -58
  660. package/src/accounts/webSocketVelocityClientAccountSubscriberV2.ts +77 -45
  661. package/src/accounts/websocketProgramUserAccountSubscriber.ts +1 -6
  662. package/src/addresses/marketAddresses.ts +3 -2
  663. package/src/addresses/pda.ts +3 -38
  664. package/src/adminClient.ts +285 -315
  665. package/src/auctionSubscriber/auctionSubscriber.ts +8 -11
  666. package/src/auctionSubscriber/auctionSubscriberGrpc.ts +13 -11
  667. package/src/auctionSubscriber/types.ts +3 -3
  668. package/src/bankrun/bankrunConnection.ts +83 -39
  669. package/src/blockhashSubscriber/BlockhashSubscriber.ts +5 -4
  670. package/src/clock/clockSubscriber.ts +10 -9
  671. package/src/config.ts +16 -70
  672. package/src/constants/index.ts +1 -2
  673. package/src/constants/numericConstants.ts +3 -3
  674. package/src/constituentMap/constituentMap.ts +10 -7
  675. package/src/constituentMap/pollingConstituentAccountSubscriber.ts +4 -2
  676. package/src/constituentMap/webSocketConstituentAccountSubscriber.ts +13 -4
  677. package/src/core/VelocityCore.ts +11 -20
  678. package/src/core/instructions/perpOrders.ts +14 -0
  679. package/src/core/instructions/withdraw.ts +2 -2
  680. package/src/core/remainingAccounts.ts +7 -7
  681. package/src/decode/user.ts +8 -1
  682. package/src/dlob/DLOB.ts +300 -176
  683. package/src/dlob/DLOBNode.ts +23 -3
  684. package/src/dlob/DLOBSubscriber.ts +53 -42
  685. package/src/dlob/NodeList.ts +8 -7
  686. package/src/dlob/orderBookLevels.ts +51 -25
  687. package/src/dlob/types.ts +0 -2
  688. package/src/events/eventList.ts +4 -1
  689. package/src/events/eventSubscriber.ts +108 -50
  690. package/src/events/eventsServerLogProvider.ts +24 -15
  691. package/src/events/fetchLogs.ts +3 -3
  692. package/src/events/parse.ts +14 -14
  693. package/src/events/pollingLogProvider.ts +2 -2
  694. package/src/events/txEventCache.ts +8 -2
  695. package/src/events/types.ts +4 -15
  696. package/src/events/webSocketLogProvider.ts +5 -3
  697. package/src/factory/bigNum.ts +2 -2
  698. package/src/idl/{drift.json → velocity.json} +2265 -2333
  699. package/src/idl/{drift.ts → velocity.ts} +2267 -2335
  700. package/src/index.ts +1 -6
  701. package/src/jupiter/jupiterClient.ts +30 -10
  702. package/src/margin/README.md +2 -2
  703. package/src/marginCalculation.ts +1 -0
  704. package/src/math/amm.ts +217 -40
  705. package/src/math/auction.ts +16 -12
  706. package/src/math/bankruptcy.ts +1 -1
  707. package/src/math/builder.ts +45 -1
  708. package/src/math/funding.ts +51 -47
  709. package/src/math/insurance.ts +6 -8
  710. package/src/math/margin.ts +12 -119
  711. package/src/math/market.ts +16 -98
  712. package/src/math/oracles.ts +29 -43
  713. package/src/math/orders.ts +13 -109
  714. package/src/math/position.ts +6 -12
  715. package/src/math/repeg.ts +0 -29
  716. package/src/math/spotBalance.ts +14 -10
  717. package/src/math/spotMarket.ts +2 -28
  718. package/src/math/spotPosition.ts +4 -4
  719. package/src/math/state.ts +0 -8
  720. package/src/math/superStake.ts +36 -85
  721. package/src/math/trade.ts +7 -3
  722. package/src/math/utils.ts +1 -1
  723. package/src/memcmp.ts +0 -9
  724. package/src/oracles/prelaunchOracleClient.ts +7 -2
  725. package/src/oracles/pythClient.ts +12 -3
  726. package/src/oracles/pythLazerClient.ts +10 -5
  727. package/src/oracles/utils.ts +15 -0
  728. package/src/orderSubscriber/OrderSubscriber.ts +12 -12
  729. package/src/orderSubscriber/WebsocketSubscription.ts +1 -1
  730. package/src/orderSubscriber/grpcSubscription.ts +2 -2
  731. package/src/orderSubscriber/types.ts +0 -2
  732. package/src/priorityFee/heliusPriorityFeeMethod.ts +1 -1
  733. package/src/priorityFee/priorityFeeSubscriber.ts +50 -21
  734. package/src/priorityFee/priorityFeeSubscriberMap.ts +2 -10
  735. package/src/priorityFee/solanaPriorityFeeMethod.ts +1 -1
  736. package/src/priorityFee/types.ts +3 -13
  737. package/src/priorityFee/velocityPriorityFeeMethod.ts +0 -12
  738. package/src/pyth/pythLazerSubscriber.ts +1 -1
  739. package/src/pyth/types.ts +0 -2
  740. package/src/slot/SlotSubscriber.ts +3 -3
  741. package/src/slot/SlothashSubscriber.ts +17 -6
  742. package/src/swap/UnifiedSwapClient.ts +1 -1
  743. package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +5 -6
  744. package/src/swift/signedMsgUserAccountSubscriber.ts +18 -16
  745. package/src/swift/swiftOrderSubscriber.ts +17 -21
  746. package/src/testClient.ts +2 -2
  747. package/src/tokenFaucet.ts +22 -3
  748. package/src/tx/baseTxSender.ts +10 -10
  749. package/src/tx/fastSingleTxSender.ts +12 -8
  750. package/src/tx/reportTransactionError.ts +12 -7
  751. package/src/tx/retryTxSender.ts +6 -4
  752. package/src/tx/txHandler.ts +88 -43
  753. package/src/tx/txParamProcessor.ts +18 -3
  754. package/src/tx/types.ts +1 -1
  755. package/src/tx/whileValidTxSender.ts +51 -22
  756. package/src/types.ts +119 -173
  757. package/src/user.ts +208 -165
  758. package/src/userConfig.ts +1 -3
  759. package/src/userMap/WebsocketSubscription.ts +2 -2
  760. package/src/userMap/grpcSubscription.ts +2 -2
  761. package/src/userMap/referrerMap.ts +13 -13
  762. package/src/userMap/revenueShareEscrowMap.ts +6 -5
  763. package/src/userMap/userMap.ts +46 -32
  764. package/src/userMap/userMapConfig.ts +3 -3
  765. package/src/userMap/userStatsMap.ts +32 -23
  766. package/src/userStats.ts +22 -13
  767. package/src/userStatsConfig.ts +3 -3
  768. package/src/util/TransactionConfirmationManager.ts +10 -2
  769. package/src/util/chainClock.ts +1 -1
  770. package/src/util/computeUnits.ts +7 -3
  771. package/src/util/ed25519Utils.ts +1 -1
  772. package/src/velocityClient.ts +867 -600
  773. package/src/velocityClientConfig.ts +0 -10
  774. package/tests/{DriftCore → VelocityCore}/decode.test.ts +1 -1
  775. package/tests/{DriftCore → VelocityCore}/fill_trigger.test.ts +1 -1
  776. package/tests/{DriftCore → VelocityCore}/instructions.test.ts +1 -1
  777. package/tests/{DriftCore → VelocityCore}/pdas.test.ts +4 -2
  778. package/tests/amm/test.ts +138 -0
  779. package/tests/ci/verifyConstants.ts +24 -27
  780. package/tests/decode/test.ts +2 -2
  781. package/tests/dlob/helpers.ts +11 -10
  782. package/tests/events/parseLogsForCuUsage.ts +15 -15
  783. package/tests/user/helpers.ts +9 -9
  784. package/tests/user/test.ts +13 -10
  785. package/tsconfig.json +1 -0
  786. package/lib/browser/constants/insuranceFund.d.ts +0 -5
  787. package/lib/browser/constants/insuranceFund.js +0 -9
  788. package/lib/browser/tx/forwardOnlyTxSender.d.ts +0 -37
  789. package/lib/browser/tx/forwardOnlyTxSender.js +0 -92
  790. package/lib/browser/util/deprecatedAlias.d.ts +0 -26
  791. package/lib/browser/util/deprecatedAlias.js +0 -10
  792. package/lib/browser/util/tps.d.ts +0 -2
  793. package/lib/browser/util/tps.js +0 -16
  794. package/lib/node/constants/insuranceFund.d.ts +0 -6
  795. package/lib/node/constants/insuranceFund.d.ts.map +0 -1
  796. package/lib/node/constants/insuranceFund.js +0 -9
  797. package/lib/node/idl/drift.d.ts.map +0 -1
  798. package/lib/node/tx/forwardOnlyTxSender.d.ts +0 -38
  799. package/lib/node/tx/forwardOnlyTxSender.d.ts.map +0 -1
  800. package/lib/node/tx/forwardOnlyTxSender.js +0 -92
  801. package/lib/node/util/deprecatedAlias.d.ts +0 -27
  802. package/lib/node/util/deprecatedAlias.d.ts.map +0 -1
  803. package/lib/node/util/deprecatedAlias.js +0 -10
  804. package/lib/node/util/tps.d.ts +0 -3
  805. package/lib/node/util/tps.d.ts.map +0 -1
  806. package/lib/node/util/tps.js +0 -16
  807. package/src/constants/insuranceFund.ts +0 -8
  808. package/src/tx/forwardOnlyTxSender.ts +0 -145
  809. package/src/util/deprecatedAlias.ts +0 -21
  810. package/src/util/tps.ts +0 -27
  811. /package/lib/browser/idl/{drift.js → velocity.js} +0 -0
  812. /package/lib/node/idl/{drift.js → velocity.js} +0 -0
  813. /package/tests/{DriftCore → VelocityCore}/perp_orders.test.ts +0 -0
  814. /package/tests/{DriftCore → VelocityCore}/remainingAccounts.test.ts +0 -0
  815. /package/tests/{DriftCore → VelocityCore}/settlement_liquidation.test.ts +0 -0
@@ -4,13 +4,14 @@ exports.User = void 0;
4
4
  const types_1 = require("./types");
5
5
  const position_1 = require("./math/position");
6
6
  const numericConstants_1 = require("./constants/numericConstants");
7
+ const utils_1 = require("./accounts/utils");
7
8
  const bigNum_1 = require("./factory/bigNum");
8
9
  const anchor_1 = require("./isomorphic/anchor");
9
10
  const position_2 = require("./math/position");
10
11
  const market_1 = require("./math/market");
11
12
  const margin_1 = require("./math/margin");
12
13
  const spotMarket_1 = require("./math/spotMarket");
13
- const utils_1 = require("./math/utils");
14
+ const utils_2 = require("./math/utils");
14
15
  const spotBalance_1 = require("./math/spotBalance");
15
16
  const trade_1 = require("./math/trade");
16
17
  const types_2 = require("./types");
@@ -27,10 +28,6 @@ const strictOraclePrice_1 = require("./oracles/strictOraclePrice");
27
28
  const grpcUserAccountSubscriber_1 = require("./accounts/grpcUserAccountSubscriber");
28
29
  const marginCalculation_1 = require("./marginCalculation");
29
30
  class User {
30
- /** @deprecated Use `velocityClient` instead. `driftClient` will be removed in a future major. */
31
- get driftClient() {
32
- return this.velocityClient;
33
- }
34
31
  get isSubscribed() {
35
32
  return this._isSubscribed && this.accountSubscriber.isSubscribed;
36
33
  }
@@ -38,40 +35,40 @@ class User {
38
35
  this._isSubscribed = val;
39
36
  }
40
37
  constructor(config) {
41
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
38
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
42
39
  this._isSubscribed = false;
43
40
  // Type-system guarantees at least one of the two is supplied.
44
- const velocityClient = ((_a = config.velocityClient) !== null && _a !== void 0 ? _a : config.driftClient);
41
+ const velocityClient = config.velocityClient;
45
42
  this.velocityClient = velocityClient;
46
43
  this.userAccountPublicKey = config.userAccountPublicKey;
47
- if (((_b = config.accountSubscription) === null || _b === void 0 ? void 0 : _b.type) === 'polling') {
44
+ if (((_a = config.accountSubscription) === null || _a === void 0 ? void 0 : _a.type) === 'polling') {
48
45
  this.accountSubscriber = new pollingUserAccountSubscriber_1.PollingUserAccountSubscriber(velocityClient.connection, config.userAccountPublicKey, config.accountSubscription.accountLoader, this.velocityClient.program.account.user.coder.accounts.decodeUnchecked.bind(this.velocityClient.program.account.user.coder.accounts));
49
46
  }
50
- else if (((_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.type) === 'custom') {
47
+ else if (((_b = config.accountSubscription) === null || _b === void 0 ? void 0 : _b.type) === 'custom') {
51
48
  this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
52
49
  }
53
- else if (((_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.type) === 'grpc') {
50
+ else if (((_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.type) === 'grpc') {
54
51
  if (config.accountSubscription.grpcMultiUserAccountSubscriber) {
55
52
  this.accountSubscriber =
56
53
  config.accountSubscription.grpcMultiUserAccountSubscriber.forUser(config.userAccountPublicKey);
57
54
  }
58
55
  else {
59
56
  this.accountSubscriber = new grpcUserAccountSubscriber_1.grpcUserAccountSubscriber(config.accountSubscription.grpcConfigs, velocityClient.program, config.userAccountPublicKey, {
60
- resubTimeoutMs: (_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.resubTimeoutMs,
61
- logResubMessages: (_f = config.accountSubscription) === null || _f === void 0 ? void 0 : _f.logResubMessages,
57
+ resubTimeoutMs: (_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.resubTimeoutMs,
58
+ logResubMessages: (_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.logResubMessages,
62
59
  });
63
60
  }
64
61
  }
65
62
  else {
66
- if (((_g = config.accountSubscription) === null || _g === void 0 ? void 0 : _g.type) === 'websocket' &&
67
- ((_h = config.accountSubscription) === null || _h === void 0 ? void 0 : _h.programUserAccountSubscriber)) {
63
+ if (((_f = config.accountSubscription) === null || _f === void 0 ? void 0 : _f.type) === 'websocket' &&
64
+ ((_g = config.accountSubscription) === null || _g === void 0 ? void 0 : _g.programUserAccountSubscriber)) {
68
65
  this.accountSubscriber = new websocketProgramUserAccountSubscriber_1.WebSocketProgramUserAccountSubscriber(velocityClient.program, config.userAccountPublicKey, config.accountSubscription.programUserAccountSubscriber);
69
66
  }
70
67
  else {
71
68
  this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(velocityClient.program, config.userAccountPublicKey, {
72
- resubTimeoutMs: (_j = config.accountSubscription) === null || _j === void 0 ? void 0 : _j.resubTimeoutMs,
73
- logResubMessages: (_k = config.accountSubscription) === null || _k === void 0 ? void 0 : _k.logResubMessages,
74
- }, (_l = config.accountSubscription) === null || _l === void 0 ? void 0 : _l.commitment);
69
+ resubTimeoutMs: (_h = config.accountSubscription) === null || _h === void 0 ? void 0 : _h.resubTimeoutMs,
70
+ logResubMessages: (_j = config.accountSubscription) === null || _j === void 0 ? void 0 : _j.logResubMessages,
71
+ }, (_k = config.accountSubscription) === null || _k === void 0 ? void 0 : _k.commitment);
75
72
  }
76
73
  }
77
74
  this.eventEmitter = this.accountSubscriber.eventEmitter;
@@ -96,15 +93,37 @@ class User {
96
93
  this.isSubscribed = false;
97
94
  }
98
95
  getUserAccount() {
99
- return this.accountSubscriber.getUserAccountAndSlot().data;
96
+ var _a;
97
+ return (_a = this.accountSubscriber.getUserAccountAndSlot()) === null || _a === void 0 ? void 0 : _a.data;
98
+ }
99
+ /**
100
+ * Like {@link getUserAccount} but throws a named error instead of returning
101
+ * `undefined` when the account has not been loaded yet. Use at call sites
102
+ * that structurally require a loaded account.
103
+ */
104
+ getUserAccountOrThrow() {
105
+ const userAccount = this.getUserAccount();
106
+ if (!userAccount) {
107
+ throw new Error(`User account not loaded: ${this.getUserAccountPublicKey().toString()}`);
108
+ }
109
+ return userAccount;
100
110
  }
101
111
  async forceGetUserAccount() {
102
112
  await this.fetchAccounts();
103
- return this.accountSubscriber.getUserAccountAndSlot().data;
113
+ const account = this.accountSubscriber.getUserAccountAndSlot();
114
+ return account === null || account === void 0 ? void 0 : account.data;
104
115
  }
105
116
  getUserAccountAndSlot() {
106
117
  return this.accountSubscriber.getUserAccountAndSlot();
107
118
  }
119
+ /**
120
+ * Like {@link getUserAccountAndSlot} but throws a named error instead of
121
+ * returning `undefined` when the account has not been loaded yet. Use at
122
+ * call sites that structurally require a loaded account.
123
+ */
124
+ getUserAccountAndSlotOrThrow() {
125
+ return (0, utils_1.assertDataAndSlot)(this.accountSubscriber.getUserAccountAndSlot(), `User account not loaded: ${this.getUserAccountPublicKey().toString()}`);
126
+ }
108
127
  getPerpPositionForUserAccount(userAccount, marketIndex) {
109
128
  return this.getActivePerpPositionsForUserAccount(userAccount).find((position) => position.marketIndex === marketIndex);
110
129
  }
@@ -114,16 +133,23 @@ class User {
114
133
  * @returns userPerpPosition
115
134
  */
116
135
  getPerpPosition(marketIndex) {
117
- const userAccount = this.getUserAccount();
136
+ const userAccount = this.getUserAccountOrThrow();
118
137
  return this.getPerpPositionForUserAccount(userAccount, marketIndex);
119
138
  }
120
139
  getPerpPositionOrEmpty(marketIndex) {
121
140
  var _a;
122
- const userAccount = this.getUserAccount();
141
+ const userAccount = this.getUserAccountOrThrow();
123
142
  return ((_a = this.getPerpPositionForUserAccount(userAccount, marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptyPosition(marketIndex));
124
143
  }
144
+ getPerpPositionOrThrow(marketIndex) {
145
+ const position = this.getPerpPosition(marketIndex);
146
+ if (!position) {
147
+ throw new Error(`No perp position found for market ${marketIndex}`);
148
+ }
149
+ return position;
150
+ }
125
151
  getPerpPositionAndSlot(marketIndex) {
126
- const userAccount = this.getUserAccountAndSlot();
152
+ const userAccount = this.getUserAccountAndSlotOrThrow();
127
153
  const perpPosition = this.getPerpPositionForUserAccount(userAccount.data, marketIndex);
128
154
  return {
129
155
  data: perpPosition,
@@ -139,11 +165,11 @@ class User {
139
165
  * @returns userSpotPosition
140
166
  */
141
167
  getSpotPosition(marketIndex) {
142
- const userAccount = this.getUserAccount();
168
+ const userAccount = this.getUserAccountOrThrow();
143
169
  return this.getSpotPositionForUserAccount(userAccount, marketIndex);
144
170
  }
145
171
  getSpotPositionAndSlot(marketIndex) {
146
- const userAccount = this.getUserAccountAndSlot();
172
+ const userAccount = this.getUserAccountAndSlotOrThrow();
147
173
  const spotPosition = this.getSpotPositionForUserAccount(userAccount.data, marketIndex);
148
174
  return {
149
175
  data: spotPosition,
@@ -172,7 +198,7 @@ class User {
172
198
  if (spotPosition === undefined) {
173
199
  return numericConstants_1.ZERO;
174
200
  }
175
- const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
201
+ const spotMarket = this.velocityClient.getSpotMarketAccountOrThrow(marketIndex);
176
202
  return (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_2.getTokenAmount)(spotPosition.scaledBalance, spotMarket, spotPosition.balanceType), spotPosition.balanceType);
177
203
  }
178
204
  getEmptyPosition(marketIndex) {
@@ -201,8 +227,8 @@ class User {
201
227
  const perpPosition = this.getPerpPosition(perpMarketIndex);
202
228
  if (!perpPosition)
203
229
  return numericConstants_1.ZERO;
204
- const perpMarket = this.velocityClient.getPerpMarketAccount(perpMarketIndex);
205
- const spotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
230
+ const perpMarket = this.velocityClient.getPerpMarketAccountOrThrow(perpMarketIndex);
231
+ const spotMarket = this.velocityClient.getSpotMarketAccountOrThrow(perpMarket.quoteSpotMarketIndex);
206
232
  if (perpPosition === undefined) {
207
233
  return numericConstants_1.ZERO;
208
234
  }
@@ -218,8 +244,8 @@ class User {
218
244
  if (!((_a = perpPosition.isolatedPositionScaledBalance) === null || _a === void 0 ? void 0 : _a.gt(numericConstants_1.ZERO))) {
219
245
  return total;
220
246
  }
221
- const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
222
- const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
247
+ const perpMarket = this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex);
248
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccountOrThrow(perpMarket.quoteSpotMarketIndex);
223
249
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(perpMarket.quoteSpotMarketIndex);
224
250
  const strictOracle = new strictOraclePrice_1.StrictOraclePrice(quoteOraclePriceData.price, quoteOraclePriceData.twap);
225
251
  const tokenAmount = (0, spotBalance_2.getTokenAmount)(perpPosition.isolatedPositionScaledBalance, quoteSpotMarket, types_2.SpotBalanceType.DEPOSIT);
@@ -238,11 +264,11 @@ class User {
238
264
  * @returns Order
239
265
  */
240
266
  getOrder(orderId) {
241
- const userAccount = this.getUserAccount();
267
+ const userAccount = this.getUserAccountOrThrow();
242
268
  return this.getOrderForUserAccount(userAccount, orderId);
243
269
  }
244
270
  getOrderAndSlot(orderId) {
245
- const userAccount = this.getUserAccountAndSlot();
271
+ const userAccount = this.getUserAccountAndSlotOrThrow();
246
272
  const order = this.getOrderForUserAccount(userAccount.data, orderId);
247
273
  return {
248
274
  data: order,
@@ -257,11 +283,11 @@ class User {
257
283
  * @returns Order
258
284
  */
259
285
  getOrderByUserOrderId(userOrderId) {
260
- const userAccount = this.getUserAccount();
286
+ const userAccount = this.getUserAccountOrThrow();
261
287
  return this.getOrderByUserIdForUserAccount(userAccount, userOrderId);
262
288
  }
263
289
  getOrderByUserOrderIdAndSlot(userOrderId) {
264
- const userAccount = this.getUserAccountAndSlot();
290
+ const userAccount = this.getUserAccountAndSlotOrThrow();
265
291
  const order = this.getOrderByUserIdForUserAccount(userAccount.data, userOrderId);
266
292
  return {
267
293
  data: order,
@@ -272,12 +298,14 @@ class User {
272
298
  return userAccount === null || userAccount === void 0 ? void 0 : userAccount.orders.filter((order) => (0, types_1.isVariant)(order.status, 'open'));
273
299
  }
274
300
  getOpenOrders() {
301
+ var _a;
275
302
  const userAccount = this.getUserAccount();
276
- return this.getOpenOrdersForUserAccount(userAccount);
303
+ return (_a = this.getOpenOrdersForUserAccount(userAccount)) !== null && _a !== void 0 ? _a : [];
277
304
  }
278
305
  getOpenOrdersAndSlot() {
279
- const userAccount = this.getUserAccountAndSlot();
280
- const openOrders = this.getOpenOrdersForUserAccount(userAccount.data);
306
+ var _a;
307
+ const userAccount = this.getUserAccountAndSlotOrThrow();
308
+ const openOrders = (_a = this.getOpenOrdersForUserAccount(userAccount.data)) !== null && _a !== void 0 ? _a : [];
281
309
  return {
282
310
  data: openOrders,
283
311
  slot: userAccount.slot,
@@ -296,7 +324,7 @@ class User {
296
324
  * @returns : open asks
297
325
  */
298
326
  getPerpBidAsks(marketIndex) {
299
- const position = this.getPerpPosition(marketIndex);
327
+ const position = this.getPerpPositionOrThrow(marketIndex);
300
328
  const totalOpenBids = position.openBids;
301
329
  const totalOpenAsks = position.openAsks;
302
330
  return [totalOpenBids, totalOpenAsks];
@@ -307,7 +335,7 @@ class User {
307
335
  */
308
336
  getPerpBuyingPower(marketIndex, collateralBuffer = numericConstants_1.ZERO, maxMarginRatio = undefined, positionType = 'cross') {
309
337
  const perpPosition = this.getPerpPositionOrEmpty(marketIndex);
310
- const perpMarket = this.velocityClient.getPerpMarketAccount(marketIndex);
338
+ const perpMarket = this.velocityClient.getPerpMarketAccountOrThrow(marketIndex);
311
339
  const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
312
340
  const worstCaseBaseAssetAmount = perpPosition
313
341
  ? (0, margin_2.calculateWorstCaseBaseAssetAmount)(perpPosition, perpMarket, oraclePriceData.price)
@@ -326,9 +354,14 @@ class User {
326
354
  }
327
355
  return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, worstCaseBaseAssetAmount, maxMarginRatio || perpPosition.maxMarginRatio);
328
356
  }
357
+ resolveMaxMarginRatio(perpMarketMaxMarginRatio) {
358
+ // 0 means "no custom margin ratio override", so Math.max returns
359
+ // userAccount.maxMarginRatio unchanged — the expected semantic.
360
+ return Math.max(perpMarketMaxMarginRatio !== null && perpMarketMaxMarginRatio !== void 0 ? perpMarketMaxMarginRatio : 0, this.getUserAccountOrThrow().maxMarginRatio);
361
+ }
329
362
  getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(marketIndex, freeCollateral, baseAssetAmount, perpMarketMaxMarginRatio = undefined) {
330
- const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
331
- const marginRatio = (0, market_1.calculateMarketMarginRatio)(this.velocityClient.getPerpMarketAccount(marketIndex), baseAssetAmount, 'Initial', maxMarginRatio);
363
+ const maxMarginRatio = this.resolveMaxMarginRatio(perpMarketMaxMarginRatio);
364
+ const marginRatio = (0, market_1.calculateMarketMarginRatio)(this.velocityClient.getPerpMarketAccountOrThrow(marketIndex), baseAssetAmount, 'Initial', maxMarginRatio);
332
365
  return freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new anchor_1.BN(marginRatio));
333
366
  }
334
367
  /**
@@ -406,11 +439,11 @@ class User {
406
439
  });
407
440
  }
408
441
  getActivePerpPositions() {
409
- const userAccount = this.getUserAccount();
442
+ const userAccount = this.getUserAccountOrThrow();
410
443
  return this.getActivePerpPositionsForUserAccount(userAccount);
411
444
  }
412
445
  getActivePerpPositionsAndSlot() {
413
- const userAccount = this.getUserAccountAndSlot();
446
+ const userAccount = this.getUserAccountAndSlotOrThrow();
414
447
  const positions = this.getActivePerpPositionsForUserAccount(userAccount.data);
415
448
  return {
416
449
  data: positions,
@@ -421,11 +454,11 @@ class User {
421
454
  return userAccount.spotPositions.filter((pos) => !(0, spotPosition_1.isSpotPositionAvailable)(pos));
422
455
  }
423
456
  getActiveSpotPositions() {
424
- const userAccount = this.getUserAccount();
457
+ const userAccount = this.getUserAccountOrThrow();
425
458
  return this.getActiveSpotPositionsForUserAccount(userAccount);
426
459
  }
427
460
  getActiveSpotPositionsAndSlot() {
428
- const userAccount = this.getUserAccountAndSlot();
461
+ const userAccount = this.getUserAccountAndSlotOrThrow();
429
462
  const positions = this.getActiveSpotPositionsForUserAccount(userAccount.data);
430
463
  return {
431
464
  data: positions,
@@ -440,9 +473,9 @@ class User {
440
473
  return this.getActivePerpPositions()
441
474
  .filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
442
475
  .reduce((unrealizedPnl, perpPosition) => {
443
- const market = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
476
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex);
444
477
  const oraclePriceData = this.getMMOracleDataForPerpMarket(market.marketIndex);
445
- const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
478
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccountOrThrow(market.quoteSpotMarketIndex);
446
479
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
447
480
  let positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(market, perpPosition, withFunding, oraclePriceData);
448
481
  let quotePrice;
@@ -476,10 +509,10 @@ class User {
476
509
  * @returns : Precision QUOTE_PRECISION
477
510
  */
478
511
  getUnrealizedFundingPNL(marketIndex) {
479
- return this.getUserAccount()
512
+ return this.getUserAccountOrThrow()
480
513
  .perpPositions.filter((pos) => marketIndex !== undefined ? pos.marketIndex === marketIndex : true)
481
514
  .reduce((pnl, perpPosition) => {
482
- const market = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
515
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex);
483
516
  return pnl.add((0, position_1.calculateUnsettledFundingPnl)(market, perpPosition));
484
517
  }, numericConstants_1.ZERO);
485
518
  }
@@ -488,7 +521,7 @@ class User {
488
521
  let netQuoteValue = numericConstants_1.ZERO;
489
522
  let totalAssetValue = numericConstants_1.ZERO;
490
523
  let totalLiabilityValue = numericConstants_1.ZERO;
491
- for (const spotPosition of this.getUserAccount().spotPositions) {
524
+ for (const spotPosition of this.getUserAccountOrThrow().spotPositions) {
492
525
  const countForBase = marketIndex === undefined || spotPosition.marketIndex === marketIndex;
493
526
  const countForQuote = marketIndex === undefined ||
494
527
  marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX ||
@@ -497,7 +530,7 @@ class User {
497
530
  (!countForBase && !countForQuote)) {
498
531
  continue;
499
532
  }
500
- const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
533
+ const spotMarketAccount = this.velocityClient.getSpotMarketAccountOrThrow(spotPosition.marketIndex);
501
534
  const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
502
535
  let twap5min;
503
536
  if (strict) {
@@ -532,7 +565,7 @@ class User {
532
565
  continue;
533
566
  }
534
567
  }
535
- const { tokenAmount: worstCaseTokenAmount, ordersValue: worstCaseQuoteTokenAmount, } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, marginCategory, this.getUserAccount().maxMarginRatio);
568
+ const { tokenAmount: worstCaseTokenAmount, ordersValue: worstCaseQuoteTokenAmount, } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, marginCategory, this.getUserAccountOrThrow().maxMarginRatio);
536
569
  if (worstCaseTokenAmount.gt(numericConstants_1.ZERO) && countForBase) {
537
570
  const baseAssetValue = this.getSpotAssetValue(worstCaseTokenAmount, strictOraclePrice, spotMarketAccount, marginCategory);
538
571
  totalAssetValue = totalAssetValue.add(baseAssetValue);
@@ -547,7 +580,7 @@ class User {
547
580
  if (worstCaseQuoteTokenAmount.lt(numericConstants_1.ZERO) && countForQuote) {
548
581
  let weight = numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION;
549
582
  if (marginCategory === 'Initial') {
550
- weight = anchor_1.BN.max(weight, new anchor_1.BN(this.getUserAccount().maxMarginRatio));
583
+ weight = anchor_1.BN.max(weight, new anchor_1.BN(this.getUserAccountOrThrow().maxMarginRatio));
551
584
  }
552
585
  const weightedTokenValue = worstCaseQuoteTokenAmount
553
586
  .abs()
@@ -572,14 +605,14 @@ class User {
572
605
  return totalLiabilityValue;
573
606
  }
574
607
  getSpotLiabilityValue(tokenAmount, strictOraclePrice, spotMarketAccount, marginCategory, liquidationBuffer) {
575
- return (0, spotBalance_2.getSpotLiabilityValue)(tokenAmount, strictOraclePrice, spotMarketAccount, this.getUserAccount().maxMarginRatio, marginCategory, liquidationBuffer);
608
+ return (0, spotBalance_2.getSpotLiabilityValue)(tokenAmount, strictOraclePrice, spotMarketAccount, this.getUserAccountOrThrow().maxMarginRatio, marginCategory, liquidationBuffer);
576
609
  }
577
610
  getSpotMarketAssetValue(marketIndex, marginCategory, includeOpenOrders, strict = false, now) {
578
611
  const { totalAssetValue } = this.getSpotMarketAssetAndLiabilityValue(marketIndex, marginCategory, undefined, includeOpenOrders, strict, now);
579
612
  return totalAssetValue;
580
613
  }
581
614
  getSpotAssetValue(tokenAmount, strictOraclePrice, spotMarketAccount, marginCategory) {
582
- return (0, spotBalance_2.getSpotAssetValue)(tokenAmount, strictOraclePrice, spotMarketAccount, this.getUserAccount().maxMarginRatio, marginCategory);
615
+ return (0, spotBalance_2.getSpotAssetValue)(tokenAmount, strictOraclePrice, spotMarketAccount, this.getUserAccountOrThrow().maxMarginRatio, marginCategory);
583
616
  }
584
617
  getSpotPositionValue(marketIndex, marginCategory, includeOpenOrders, strict = false, now) {
585
618
  const { totalAssetValue, totalLiabilityValue } = this.getSpotMarketAssetAndLiabilityValue(marketIndex, marginCategory, undefined, includeOpenOrders, strict, now);
@@ -609,7 +642,11 @@ class User {
609
642
  liquidationBufferMap,
610
643
  });
611
644
  if (perpMarketIndex !== undefined) {
612
- const { totalCollateral, totalCollateralBuffer } = marginCalc.isolatedMarginCalculations.get(perpMarketIndex);
645
+ const isolatedMarginCalculation = marginCalc.isolatedMarginCalculations.get(perpMarketIndex);
646
+ if (!isolatedMarginCalculation) {
647
+ throw new Error(`No isolated margin calculation for perp market ${perpMarketIndex}`);
648
+ }
649
+ const { totalCollateral, totalCollateralBuffer } = isolatedMarginCalculation;
613
650
  if (liquidationBuffer === null || liquidationBuffer === void 0 ? void 0 : liquidationBuffer.gt(numericConstants_1.ZERO)) {
614
651
  return totalCollateralBuffer;
615
652
  }
@@ -673,7 +710,7 @@ class User {
673
710
  return health;
674
711
  }
675
712
  calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
676
- const market = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
713
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex);
677
714
  let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
678
715
  if ((0, types_1.isVariant)(market.status, 'settlement')) {
679
716
  valuationPrice = market.expiryPrice;
@@ -690,7 +727,7 @@ class User {
690
727
  liabilityValue = (0, margin_1.calculatePerpLiabilityValue)(baseAssetAmount, valuationPrice);
691
728
  }
692
729
  if (marginCategory) {
693
- const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
730
+ const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccountOrThrow().maxMarginRatio);
694
731
  let marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, userCustomMargin));
695
732
  if (liquidationBuffer !== undefined) {
696
733
  marginRatio = marginRatio.add(liquidationBuffer);
@@ -698,7 +735,7 @@ class User {
698
735
  if ((0, types_1.isVariant)(market.status, 'settlement')) {
699
736
  marginRatio = numericConstants_1.ZERO;
700
737
  }
701
- const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
738
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccountOrThrow(market.quoteSpotMarketIndex);
702
739
  const quoteOraclePriceData = this.velocityClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
703
740
  let quotePrice;
704
741
  if (strict) {
@@ -723,7 +760,7 @@ class User {
723
760
  * @returns : Precision QUOTE_PRECISION
724
761
  */
725
762
  getPerpMarketLiabilityValue(marketIndex, marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
726
- const perpPosition = this.getPerpPosition(marketIndex);
763
+ const perpPosition = this.getPerpPositionOrThrow(marketIndex);
727
764
  return this.calculateWeightedPerpPositionLiability(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict);
728
765
  }
729
766
  /**
@@ -742,7 +779,7 @@ class User {
742
779
  */
743
780
  getPerpPositionValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
744
781
  const userPosition = this.getPerpPositionOrEmpty(marketIndex);
745
- const market = this.velocityClient.getPerpMarketAccount(userPosition.marketIndex);
782
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(userPosition.marketIndex);
746
783
  return (0, margin_2.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData, includeOpenOrders);
747
784
  }
748
785
  /**
@@ -751,7 +788,7 @@ class User {
751
788
  */
752
789
  getPerpLiabilityValue(marketIndex, oraclePriceData, includeOpenOrders = false) {
753
790
  const userPosition = this.getPerpPositionOrEmpty(marketIndex);
754
- const market = this.velocityClient.getPerpMarketAccount(userPosition.marketIndex);
791
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(userPosition.marketIndex);
755
792
  if (includeOpenOrders) {
756
793
  return (0, margin_1.calculateWorstCasePerpLiabilityValue)(userPosition, market, oraclePriceData.price).worstCaseLiabilityValue;
757
794
  }
@@ -775,7 +812,7 @@ class User {
775
812
  * @returns : Precision PRICE_PRECISION
776
813
  */
777
814
  getPositionEstimatedExitPriceAndPnl(position, amountToClose, useAMMClose = false) {
778
- const market = this.velocityClient.getPerpMarketAccount(position.marketIndex);
815
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(position.marketIndex);
779
816
  const entryPrice = (0, position_1.calculateEntryPrice)(position);
780
817
  const oraclePriceData = this.getMMOracleDataForPerpMarket(position.marketIndex);
781
818
  if (amountToClose) {
@@ -831,9 +868,9 @@ class User {
831
868
  if (perpMarketIndex) {
832
869
  const perpPosition = this.getPerpPositionOrEmpty(perpMarketIndex);
833
870
  const perpLiability = this.calculateWeightedPerpPositionLiability(perpPosition, marginCategory, undefined, includeOpenOrders);
834
- const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
871
+ const perpMarket = this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex);
835
872
  const oraclePriceData = this.getOracleDataForPerpMarket(perpPosition.marketIndex);
836
- const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
873
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccountOrThrow(perpMarket.quoteSpotMarketIndex);
837
874
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(perpMarket.quoteSpotMarketIndex);
838
875
  const strictOracle = new strictOraclePrice_1.StrictOraclePrice(quoteOraclePriceData.price, quoteOraclePriceData.twap);
839
876
  const positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(perpMarket, perpPosition, true, oraclePriceData);
@@ -862,6 +899,9 @@ class User {
862
899
  isDustDepositPosition(spotMarketAccount) {
863
900
  const marketIndex = spotMarketAccount.marketIndex;
864
901
  const spotPosition = this.getSpotPosition(spotMarketAccount.marketIndex);
902
+ if (!spotPosition) {
903
+ throw new Error(`No spot position found for market ${spotMarketAccount.marketIndex}`);
904
+ }
865
905
  if ((0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
866
906
  return false;
867
907
  }
@@ -911,8 +951,8 @@ class User {
911
951
  */
912
952
  getTotalAllTimePnl() {
913
953
  const netUsdValue = this.getNetUsdValue();
914
- const totalDeposits = this.getUserAccount().totalDeposits;
915
- const totalWithdraws = this.getUserAccount().totalWithdraws;
954
+ const totalDeposits = this.getUserAccountOrThrow().totalDeposits;
955
+ const totalWithdraws = this.getUserAccountOrThrow().totalWithdraws;
916
956
  const totalPnl = netUsdValue.add(totalWithdraws).sub(totalDeposits);
917
957
  return totalPnl;
918
958
  }
@@ -1052,7 +1092,7 @@ class User {
1052
1092
  this.hasIsolatedPositionBeingLiquidated());
1053
1093
  }
1054
1094
  isCrossMarginBeingLiquidated() {
1055
- return ((this.getUserAccount().status &
1095
+ return ((this.getUserAccountOrThrow().status &
1056
1096
  (types_1.UserStatus.BEING_LIQUIDATED | types_1.UserStatus.BANKRUPT)) >
1057
1097
  0);
1058
1098
  }
@@ -1067,8 +1107,9 @@ class User {
1067
1107
  0);
1068
1108
  }
1069
1109
  isIsolatedPositionBeingLiquidated(perpMarketIndex) {
1110
+ var _a;
1070
1111
  const position = this.getActivePerpPositions().find((position) => position.marketIndex === perpMarketIndex);
1071
- return (((position === null || position === void 0 ? void 0 : position.positionFlag) &
1112
+ return ((((_a = position === null || position === void 0 ? void 0 : position.positionFlag) !== null && _a !== void 0 ? _a : 0) &
1072
1113
  (types_2.PositionFlag.BeingLiquidated | types_2.PositionFlag.Bankruptcy)) >
1073
1114
  0);
1074
1115
  }
@@ -1087,23 +1128,23 @@ class User {
1087
1128
  return isolatedMarginCalculation.totalCollateral.lt(isolatedMarginCalculation.marginRequirement);
1088
1129
  }
1089
1130
  hasStatus(status) {
1090
- return (this.getUserAccount().status & status) > 0;
1131
+ return (this.getUserAccountOrThrow().status & status) > 0;
1091
1132
  }
1092
1133
  isBankrupt() {
1093
- return (this.getUserAccount().status & types_1.UserStatus.BANKRUPT) > 0;
1134
+ return (this.getUserAccountOrThrow().status & types_1.UserStatus.BANKRUPT) > 0;
1094
1135
  }
1095
1136
  /**
1096
1137
  * Checks if any user position cumulative funding differs from respective market cumulative funding
1097
1138
  * @returns
1098
1139
  */
1099
1140
  needsToSettleFundingPayment() {
1100
- for (const userPosition of this.getUserAccount().perpPositions) {
1141
+ for (const userPosition of this.getUserAccountOrThrow().perpPositions) {
1101
1142
  if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
1102
1143
  continue;
1103
1144
  }
1104
- const market = this.velocityClient.getPerpMarketAccount(userPosition.marketIndex);
1105
- if (market.amm.cumulativeFundingRateLong.eq(userPosition.lastCumulativeFundingRate) ||
1106
- market.amm.cumulativeFundingRateShort.eq(userPosition.lastCumulativeFundingRate)) {
1145
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(userPosition.marketIndex);
1146
+ if (market.cumulativeFundingRateLong.eq(userPosition.lastCumulativeFundingRate) ||
1147
+ market.cumulativeFundingRateShort.eq(userPosition.lastCumulativeFundingRate)) {
1107
1148
  continue;
1108
1149
  }
1109
1150
  return true;
@@ -1123,7 +1164,7 @@ class User {
1123
1164
  const totalCollateral = this.getTotalCollateral('Maintenance');
1124
1165
  const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
1125
1166
  const freeCollateral = anchor_1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(maintenanceMarginRequirement));
1126
- const market = this.velocityClient.getSpotMarketAccount(marketIndex);
1167
+ const market = this.velocityClient.getSpotMarketAccountOrThrow(marketIndex);
1127
1168
  let signedTokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_2.getTokenAmount)(currentSpotPosition.scaledBalance, market, currentSpotPosition.balanceType), currentSpotPosition.balanceType);
1128
1169
  signedTokenAmount = signedTokenAmount.add(positionBaseSizeChange);
1129
1170
  if (signedTokenAmount.eq(numericConstants_1.ZERO)) {
@@ -1133,14 +1174,14 @@ class User {
1133
1174
  const oracle = market.oracle;
1134
1175
  const perpMarketWithSameOracle = this.velocityClient
1135
1176
  .getPerpMarketAccounts()
1136
- .find((market) => market.amm.oracle.equals(oracle));
1177
+ .find((market) => market.oracle.equals(oracle));
1137
1178
  const oraclePrice = this.velocityClient.getOracleDataForSpotMarket(marketIndex).price;
1138
1179
  if (perpMarketWithSameOracle) {
1139
1180
  const perpPosition = this.getPerpPositionOrEmpty(perpMarketWithSameOracle.marketIndex);
1140
1181
  if (perpPosition) {
1141
1182
  let freeCollateralDeltaForPerp = this.calculateFreeCollateralDeltaForPerp(perpMarketWithSameOracle, perpPosition, numericConstants_1.ZERO, oraclePrice);
1142
1183
  if (freeCollateralDeltaForPerp) {
1143
- const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.oracleSource, perpMarketWithSameOracle.amm.oracleSource);
1184
+ const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.oracleSource, perpMarketWithSameOracle.oracleSource);
1144
1185
  freeCollateralDeltaForPerp = freeCollateralDeltaForPerp
1145
1186
  .mul(numerator)
1146
1187
  .div(denominator);
@@ -1171,8 +1212,8 @@ class User {
1171
1212
  * @returns Precision : PRICE_PRECISION
1172
1213
  */
1173
1214
  liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance', includeOpenOrders = false, offsetCollateral = numericConstants_1.ZERO, marginType) {
1174
- const market = this.velocityClient.getPerpMarketAccount(marketIndex);
1175
- const oracle = this.velocityClient.getPerpMarketAccount(marketIndex).amm.oracle;
1215
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(marketIndex);
1216
+ const oracle = market.oracle;
1176
1217
  const oraclePrice = this.velocityClient.getOracleDataForPerpMarket(marketIndex).price;
1177
1218
  const currentPerpPosition = this.getPerpPositionOrEmpty(marketIndex);
1178
1219
  if (marginType === 'Isolated') {
@@ -1201,7 +1242,7 @@ class User {
1201
1242
  const totalCollateral = this.getTotalCollateral(marginCategory, false, includeOpenOrders);
1202
1243
  const marginRequirement = this.getMarginRequirement(marginCategory, undefined, false, includeOpenOrders);
1203
1244
  let freeCollateral = anchor_1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(marginRequirement)).add(offsetCollateral);
1204
- positionBaseSizeChange = (0, orders_1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.amm.orderStepSize);
1245
+ positionBaseSizeChange = (0, orders_1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.orderStepSize);
1205
1246
  const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders);
1206
1247
  freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
1207
1248
  let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, oraclePrice, marginCategory, includeOpenOrders);
@@ -1217,7 +1258,7 @@ class User {
1217
1258
  const signedTokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_2.getTokenAmount)(spotPosition.scaledBalance, spotMarketWithSameOracle, spotPosition.balanceType), spotPosition.balanceType);
1218
1259
  let spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount, marginCategory);
1219
1260
  if (spotFreeCollateralDelta) {
1220
- const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.amm.oracleSource, spotMarketWithSameOracle.oracleSource);
1261
+ const { numerator, denominator } = (0, oracles_1.getMultipleBetweenOracleSources)(market.oracleSource, spotMarketWithSameOracle.oracleSource);
1221
1262
  spotFreeCollateralDelta = spotFreeCollateralDelta
1222
1263
  .mul(numerator)
1223
1264
  .div(denominator);
@@ -1274,7 +1315,7 @@ class User {
1274
1315
  baseAssetAmount = perpPosition.baseAssetAmount;
1275
1316
  liabilityValue = (0, margin_1.calculatePerpLiabilityValue)(baseAssetAmount, oraclePrice);
1276
1317
  }
1277
- const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
1318
+ const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccountOrThrow().maxMarginRatio);
1278
1319
  const marginRatio = (0, market_1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory, userCustomMargin);
1279
1320
  return liabilityValue.mul(new anchor_1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION);
1280
1321
  };
@@ -1291,7 +1332,7 @@ class User {
1291
1332
  // zero if include orders == false
1292
1333
  const orderBaseAssetAmount = baseAssetAmount.sub(perpPosition.baseAssetAmount);
1293
1334
  const proposedBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
1294
- const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
1335
+ const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccountOrThrow().maxMarginRatio);
1295
1336
  const marginRatio = (0, market_1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory, userCustomMargin);
1296
1337
  const marginRatioQuotePrecision = new anchor_1.BN(marginRatio)
1297
1338
  .mul(numericConstants_1.QUOTE_PRECISION)
@@ -1355,11 +1396,11 @@ class User {
1355
1396
  return this.liquidationPrice(positionMarketIndex, closeBaseAmount, estimatedEntryPrice);
1356
1397
  }
1357
1398
  getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize, estEntryPrice, perpMarketMaxMarginRatio) {
1358
- const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
1399
+ const maxMarginRatio = this.resolveMaxMarginRatio(perpMarketMaxMarginRatio);
1359
1400
  return (0, margin_2.calculateMarginUSDCRequiredForTrade)(this.velocityClient, targetMarketIndex, baseSize, maxMarginRatio, estEntryPrice);
1360
1401
  }
1361
1402
  getCollateralDepositRequiredForTrade(targetMarketIndex, baseSize, collateralIndex, perpMarketMaxMarginRatio) {
1362
- const maxMarginRatio = Math.max(perpMarketMaxMarginRatio, this.getUserAccount().maxMarginRatio);
1403
+ const maxMarginRatio = this.resolveMaxMarginRatio(perpMarketMaxMarginRatio);
1363
1404
  return (0, margin_2.calculateCollateralDepositRequiredForTrade)(this.velocityClient, targetMarketIndex, baseSize, collateralIndex, maxMarginRatio);
1364
1405
  }
1365
1406
  /**
@@ -1382,7 +1423,7 @@ class User {
1382
1423
  ? true
1383
1424
  : targetSide === currentPositionSide;
1384
1425
  const oracleData = this.getMMOracleDataForPerpMarket(targetMarketIndex);
1385
- const marketAccount = this.velocityClient.getPerpMarketAccount(targetMarketIndex);
1426
+ const marketAccount = this.velocityClient.getPerpMarketAccountOrThrow(targetMarketIndex);
1386
1427
  // add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
1387
1428
  const oppositeSizeLiabilityValue = targetingSameSide
1388
1429
  ? numericConstants_1.ZERO
@@ -1406,7 +1447,7 @@ class User {
1406
1447
  const perpLiabilityValue = (0, margin_1.calculatePerpLiabilityValue)(currentPosition.baseAssetAmount, oracleData.price);
1407
1448
  const totalCollateral = this.getTotalCollateral();
1408
1449
  const marginRequirement = this.getInitialMarginRequirement();
1409
- const marginRatio = Math.max(currentPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
1450
+ const marginRatio = Math.max(currentPosition.maxMarginRatio, this.getUserAccountOrThrow().maxMarginRatio);
1410
1451
  const marginFreedByClosing = perpLiabilityValue
1411
1452
  .mul(new anchor_1.BN(marginRatio))
1412
1453
  .div(numericConstants_1.MARGIN_PRECISION);
@@ -1453,7 +1494,7 @@ class User {
1453
1494
  * @returns tradeSizeAllowed : Precision QUOTE_PRECISION
1454
1495
  */
1455
1496
  getMaxTradeSizeUSDCForSpot(targetMarketIndex, direction, currentQuoteAssetValue, currentSpotMarketNetValue) {
1456
- const market = this.velocityClient.getSpotMarketAccount(targetMarketIndex);
1497
+ const market = this.velocityClient.getSpotMarketAccountOrThrow(targetMarketIndex);
1457
1498
  const oraclePrice = this.velocityClient.getOracleDataForSpotMarket(targetMarketIndex).price;
1458
1499
  currentQuoteAssetValue = this.getSpotMarketAssetValue(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
1459
1500
  currentSpotMarketNetValue =
@@ -1461,19 +1502,19 @@ class User {
1461
1502
  let freeCollateral = this.getFreeCollateral();
1462
1503
  const marginRatio = (0, spotMarket_1.calculateSpotMarketMarginRatio)(market, oraclePrice, 'Initial', numericConstants_1.ZERO, (0, types_1.isVariant)(direction, 'long')
1463
1504
  ? types_2.SpotBalanceType.DEPOSIT
1464
- : types_2.SpotBalanceType.BORROW, this.getUserAccount().maxMarginRatio);
1505
+ : types_2.SpotBalanceType.BORROW, this.getUserAccountOrThrow().maxMarginRatio);
1465
1506
  let tradeAmount = numericConstants_1.ZERO;
1466
- if (this.getUserAccount().isMarginTradingEnabled) {
1507
+ if (this.getUserAccountOrThrow().isMarginTradingEnabled) {
1467
1508
  // if the user is buying/selling and already short/long, need to account for closing out short/long
1468
1509
  if ((0, types_1.isVariant)(direction, 'long') && currentSpotMarketNetValue.lt(numericConstants_1.ZERO)) {
1469
1510
  tradeAmount = currentSpotMarketNetValue.abs();
1470
- const marginRatio = (0, spotMarket_1.calculateSpotMarketMarginRatio)(market, oraclePrice, 'Initial', this.getTokenAmount(targetMarketIndex).abs(), types_2.SpotBalanceType.BORROW, this.getUserAccount().maxMarginRatio);
1511
+ const marginRatio = (0, spotMarket_1.calculateSpotMarketMarginRatio)(market, oraclePrice, 'Initial', this.getTokenAmount(targetMarketIndex).abs(), types_2.SpotBalanceType.BORROW, this.getUserAccountOrThrow().maxMarginRatio);
1471
1512
  freeCollateral = freeCollateral.add(tradeAmount.mul(new anchor_1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
1472
1513
  }
1473
1514
  else if ((0, types_1.isVariant)(direction, 'short') &&
1474
1515
  currentSpotMarketNetValue.gt(numericConstants_1.ZERO)) {
1475
1516
  tradeAmount = currentSpotMarketNetValue;
1476
- const marginRatio = (0, spotMarket_1.calculateSpotMarketMarginRatio)(market, oraclePrice, 'Initial', this.getTokenAmount(targetMarketIndex), types_2.SpotBalanceType.DEPOSIT, this.getUserAccount().maxMarginRatio);
1517
+ const marginRatio = (0, spotMarket_1.calculateSpotMarketMarginRatio)(market, oraclePrice, 'Initial', this.getTokenAmount(targetMarketIndex), types_2.SpotBalanceType.DEPOSIT, this.getUserAccountOrThrow().maxMarginRatio);
1477
1518
  freeCollateral = freeCollateral.add(tradeAmount.mul(new anchor_1.BN(marginRatio)).div(numericConstants_1.MARGIN_PRECISION));
1478
1519
  }
1479
1520
  tradeAmount = tradeAmount.add(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION).div(new anchor_1.BN(marginRatio)));
@@ -1496,8 +1537,8 @@ class User {
1496
1537
  * @param iterationLimit how long to run appromixation before erroring out
1497
1538
  */
1498
1539
  getMaxSwapAmount({ inMarketIndex, outMarketIndex, calculateSwap, iterationLimit = 1000, }) {
1499
- const inMarket = this.velocityClient.getSpotMarketAccount(inMarketIndex);
1500
- const outMarket = this.velocityClient.getSpotMarketAccount(outMarketIndex);
1540
+ const inMarket = this.velocityClient.getSpotMarketAccountOrThrow(inMarketIndex);
1541
+ const outMarket = this.velocityClient.getSpotMarketAccountOrThrow(outMarketIndex);
1501
1542
  const inOraclePriceData = this.getOracleDataForSpotMarket(inMarketIndex);
1502
1543
  const inOraclePrice = inOraclePriceData.price;
1503
1544
  const outOraclePriceData = this.getOracleDataForSpotMarket(outMarketIndex);
@@ -1603,7 +1644,7 @@ class User {
1603
1644
  return clonedPosition;
1604
1645
  }
1605
1646
  const preTokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_2.getTokenAmount)(position.scaledBalance, market, position.balanceType), position.balanceType);
1606
- if ((0, utils_1.sigNum)(preTokenAmount).eq((0, utils_1.sigNum)(tokenAmount))) {
1647
+ if ((0, utils_2.sigNum)(preTokenAmount).eq((0, utils_2.sigNum)(tokenAmount))) {
1607
1648
  const scaledBalanceDelta = (0, spotBalance_1.getBalance)(tokenAmount.abs(), market, position.balanceType);
1608
1649
  clonedPosition.scaledBalance =
1609
1650
  clonedPosition.scaledBalance.add(scaledBalanceDelta);
@@ -1625,15 +1666,15 @@ class User {
1625
1666
  }
1626
1667
  calculateSpotPositionFreeCollateralContribution(spotPosition, strictOraclePrice) {
1627
1668
  const marginCategory = 'Initial';
1628
- const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
1629
- const { freeCollateralContribution } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, marginCategory, this.getUserAccount().maxMarginRatio);
1669
+ const spotMarketAccount = this.velocityClient.getSpotMarketAccountOrThrow(spotPosition.marketIndex);
1670
+ const { freeCollateralContribution } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, marginCategory, this.getUserAccountOrThrow().maxMarginRatio);
1630
1671
  return freeCollateralContribution;
1631
1672
  }
1632
1673
  calculateSpotPositionLeverageContribution(spotPosition, strictOraclePrice) {
1633
1674
  let totalAssetValue = numericConstants_1.ZERO;
1634
1675
  let totalLiabilityValue = numericConstants_1.ZERO;
1635
- const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
1636
- const { tokenValue, ordersValue } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, 'Initial', this.getUserAccount().maxMarginRatio);
1676
+ const spotMarketAccount = this.velocityClient.getSpotMarketAccountOrThrow(spotPosition.marketIndex);
1677
+ const { tokenValue, ordersValue } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, 'Initial', this.getUserAccountOrThrow().maxMarginRatio);
1637
1678
  if (tokenValue.gte(numericConstants_1.ZERO)) {
1638
1679
  totalAssetValue = tokenValue;
1639
1680
  }
@@ -1659,8 +1700,8 @@ class User {
1659
1700
  * @param outAmount
1660
1701
  */
1661
1702
  accountLeverageAfterSwap({ inMarketIndex, outMarketIndex, inAmount, outAmount, }) {
1662
- const inMarket = this.velocityClient.getSpotMarketAccount(inMarketIndex);
1663
- const outMarket = this.velocityClient.getSpotMarketAccount(outMarketIndex);
1703
+ const inMarket = this.velocityClient.getSpotMarketAccountOrThrow(inMarketIndex);
1704
+ const outMarket = this.velocityClient.getSpotMarketAccountOrThrow(outMarketIndex);
1664
1705
  const inOraclePriceData = this.getOracleDataForSpotMarket(inMarketIndex);
1665
1706
  const inOraclePrice = inOraclePriceData.price;
1666
1707
  const outOraclePriceData = this.getOracleDataForSpotMarket(outMarketIndex);
@@ -1747,7 +1788,7 @@ class User {
1747
1788
  return newLeverage;
1748
1789
  }
1749
1790
  const currentPosition = this.getPerpPositionOrEmpty(targetMarketIndex);
1750
- const perpMarket = this.velocityClient.getPerpMarketAccount(targetMarketIndex);
1791
+ const perpMarket = this.velocityClient.getPerpMarketAccountOrThrow(targetMarketIndex);
1751
1792
  const oracleData = this.getOracleDataForPerpMarket(targetMarketIndex);
1752
1793
  let {
1753
1794
  // eslint-disable-next-line prefer-const
@@ -1782,13 +1823,11 @@ class User {
1782
1823
  }
1783
1824
  getUserFeeTier(marketType, now) {
1784
1825
  const state = this.velocityClient.getStateAccount();
1785
- const feeTierIndex = 0;
1786
1826
  if ((0, types_1.isVariant)(marketType, 'perp')) {
1787
1827
  const userStatsAccount = this.velocityClient
1788
- .getUserStats()
1789
- .getAccount();
1828
+ .getUserStatsOrThrow()
1829
+ .getAccountOrThrow();
1790
1830
  const total30dVolume = (0, trade_1.getUser30dRollingVolumeEstimate)(userStatsAccount, now);
1791
- const stakedGovAssetAmount = userStatsAccount.ifStakedGovTokenAmount;
1792
1831
  const volumeThresholds = [
1793
1832
  new anchor_1.BN(2000000).mul(numericConstants_1.QUOTE_PRECISION),
1794
1833
  new anchor_1.BN(10000000).mul(numericConstants_1.QUOTE_PRECISION),
@@ -1796,14 +1835,6 @@ class User {
1796
1835
  new anchor_1.BN(80000000).mul(numericConstants_1.QUOTE_PRECISION),
1797
1836
  new anchor_1.BN(200000000).mul(numericConstants_1.QUOTE_PRECISION),
1798
1837
  ];
1799
- const stakeThresholds = [
1800
- new anchor_1.BN(1000 - 1).mul(numericConstants_1.QUOTE_PRECISION),
1801
- new anchor_1.BN(10000 - 1).mul(numericConstants_1.QUOTE_PRECISION),
1802
- new anchor_1.BN(50000 - 1).mul(numericConstants_1.QUOTE_PRECISION),
1803
- new anchor_1.BN(100000 - 1).mul(numericConstants_1.QUOTE_PRECISION),
1804
- new anchor_1.BN(250000 - 5).mul(numericConstants_1.QUOTE_PRECISION),
1805
- ];
1806
- const stakeBenefitFrac = [0, 5, 10, 20, 30, 40];
1807
1838
  let feeTierIndex = 5;
1808
1839
  for (let i = 0; i < volumeThresholds.length; i++) {
1809
1840
  if (total30dVolume.lt(volumeThresholds[i])) {
@@ -1811,23 +1842,9 @@ class User {
1811
1842
  break;
1812
1843
  }
1813
1844
  }
1814
- let stakeBenefitIndex = 5;
1815
- for (let i = 0; i < stakeThresholds.length; i++) {
1816
- if (stakedGovAssetAmount.lt(stakeThresholds[i])) {
1817
- stakeBenefitIndex = i;
1818
- break;
1819
- }
1820
- }
1821
- const stakeBenefit = stakeBenefitFrac[stakeBenefitIndex];
1822
- const tier = { ...state.perpFeeStructure.feeTiers[feeTierIndex] };
1823
- if (stakeBenefit > 0) {
1824
- tier.feeNumerator = (tier.feeNumerator * (100 - stakeBenefit)) / 100;
1825
- tier.makerRebateNumerator =
1826
- (tier.makerRebateNumerator * (100 + stakeBenefit)) / 100;
1827
- }
1828
- return tier;
1845
+ return state.perpFeeStructure.feeTiers[feeTierIndex];
1829
1846
  }
1830
- return state.spotFeeStructure.feeTiers[feeTierIndex];
1847
+ return state.spotFeeStructure.feeTiers[0];
1831
1848
  }
1832
1849
  /**
1833
1850
  * Calculates how much perp fee will be taken for a given sized trade
@@ -1856,7 +1873,7 @@ class User {
1856
1873
  */
1857
1874
  getWithdrawalLimit(marketIndex, reduceOnly) {
1858
1875
  const nowTs = new anchor_1.BN(Math.floor(Date.now() / 1000));
1859
- const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
1876
+ const spotMarket = this.velocityClient.getSpotMarketAccountOrThrow(marketIndex);
1860
1877
  // eslint-disable-next-line prefer-const
1861
1878
  let { borrowLimit, withdrawLimit } = (0, spotBalance_2.calculateWithdrawLimit)(spotMarket, nowTs);
1862
1879
  const freeCollateral = this.getFreeCollateral();
@@ -1884,7 +1901,7 @@ class User {
1884
1901
  amountWithdrawable = userDepositAmount;
1885
1902
  }
1886
1903
  else {
1887
- amountWithdrawable = (0, utils_1.divCeil)((0, utils_1.divCeil)(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION), assetWeight).mul(numericConstants_1.PRICE_PRECISION), oracleData.price)
1904
+ amountWithdrawable = (0, utils_2.divCeil)((0, utils_2.divCeil)(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION), assetWeight).mul(numericConstants_1.PRICE_PRECISION), oracleData.price)
1888
1905
  .mul(numeratorScale)
1889
1906
  .div(denominatorScale);
1890
1907
  }
@@ -1909,10 +1926,10 @@ class User {
1909
1926
  }
1910
1927
  }
1911
1928
  canBypassWithdrawLimits(marketIndex) {
1912
- const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
1929
+ const spotMarket = this.velocityClient.getSpotMarketAccountOrThrow(marketIndex);
1913
1930
  const maxDepositAmount = spotMarket.withdrawGuardThreshold.div(new anchor_1.BN(10));
1914
1931
  const position = this.getSpotPosition(marketIndex);
1915
- const netDeposits = this.getUserAccount().totalDeposits.sub(this.getUserAccount().totalWithdraws);
1932
+ const netDeposits = this.getUserAccountOrThrow().totalDeposits.sub(this.getUserAccountOrThrow().totalWithdraws);
1916
1933
  if (!position) {
1917
1934
  return {
1918
1935
  canBypass: false,
@@ -1946,7 +1963,7 @@ class User {
1946
1963
  };
1947
1964
  }
1948
1965
  canMakeIdle(slot) {
1949
- const userAccount = this.getUserAccount();
1966
+ const userAccount = this.getUserAccountOrThrow();
1950
1967
  if (userAccount.idle) {
1951
1968
  return false;
1952
1969
  }
@@ -1989,12 +2006,13 @@ class User {
1989
2006
  return true;
1990
2007
  }
1991
2008
  canBeDeleted(userStatsAccount, now) {
1992
- const userAccount = this.getUserAccount();
1993
- const userStatsAccountToUse = userStatsAccount || this.velocityClient.getUserStats().getAccount();
2009
+ const userAccount = this.getUserAccountOrThrow();
2010
+ const userStatsAccountToUse = userStatsAccount !== null && userStatsAccount !== void 0 ? userStatsAccount : this.velocityClient.getUserStatsOrThrow().getAccount();
1994
2011
  const nowInSeconds = now || new anchor_1.BN(Math.floor(Date.now() / 1000));
1995
2012
  const stateAccount = this.velocityClient.getStateAccount();
1996
2013
  // Referrer cannot delete sub_account_id 0
1997
- const isReferrer = (userStatsAccountToUse.referrerStatus & types_1.ReferrerStatus.IsReferrer) > 0;
2014
+ const isReferrer = userStatsAccountToUse !== undefined &&
2015
+ (userStatsAccountToUse.referrerStatus & types_1.ReferrerStatus.IsReferrer) > 0;
1998
2016
  if (isReferrer && userAccount.subAccountId === 0) {
1999
2017
  return { canDelete: false, reason: 'is-subaccount-0-referrer' };
2000
2018
  }
@@ -2023,7 +2041,7 @@ class User {
2023
2041
  }
2024
2042
  }
2025
2043
  // Fresh account (< 13 days) with init fee must be idle
2026
- if (stateAccount.maxInitializeUserFee > 0) {
2044
+ if (stateAccount.maxInitializeUserFee > 0 && userStatsAccountToUse) {
2027
2045
  const minActionTs = anchor_1.BN.min(userStatsAccountToUse.lastFillerVolume30DTs, anchor_1.BN.min(userStatsAccountToUse.lastMakerVolume30DTs, userStatsAccountToUse.lastTakerVolume30DTs));
2028
2046
  const estimatedAge = anchor_1.BN.max(nowInSeconds.sub(minActionTs), numericConstants_1.ZERO);
2029
2047
  if (estimatedAge.lt(new anchor_1.BN(numericConstants_1.ACCOUNT_AGE_DELETION_CUTOFF_SECONDS))) {
@@ -2041,13 +2059,13 @@ class User {
2041
2059
  let safestPerpTier = 4;
2042
2060
  let safestSpotTier = 4;
2043
2061
  for (const perpPosition of this.getActivePerpPositions()) {
2044
- safestPerpTier = Math.min(safestPerpTier, (0, tiers_1.getPerpMarketTierNumber)(this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex)));
2062
+ safestPerpTier = Math.min(safestPerpTier, (0, tiers_1.getPerpMarketTierNumber)(this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex)));
2045
2063
  }
2046
2064
  for (const spotPosition of this.getActiveSpotPositions()) {
2047
2065
  if ((0, types_1.isVariant)(spotPosition.balanceType, 'deposit')) {
2048
2066
  continue;
2049
2067
  }
2050
- safestSpotTier = Math.min(safestSpotTier, (0, tiers_1.getSpotMarketTierNumber)(this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex)));
2068
+ safestSpotTier = Math.min(safestSpotTier, (0, tiers_1.getSpotMarketTierNumber)(this.velocityClient.getSpotMarketAccountOrThrow(spotPosition.marketIndex)));
2051
2069
  }
2052
2070
  return {
2053
2071
  perpTier: safestPerpTier,
@@ -2055,7 +2073,7 @@ class User {
2055
2073
  };
2056
2074
  }
2057
2075
  getPerpPositionHealth({ marginCategory, perpPosition, oraclePriceData, quoteOraclePriceData, includeOpenOrders = true, }) {
2058
- const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
2076
+ const perpMarket = this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex);
2059
2077
  const _oraclePriceData = oraclePriceData ||
2060
2078
  this.velocityClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
2061
2079
  const oraclePrice = _oraclePriceData.price;
@@ -2070,7 +2088,7 @@ class User {
2070
2088
  worstCaseBaseAmount = perpPosition.baseAssetAmount;
2071
2089
  worstCaseLiabilityValue = (0, margin_1.calculatePerpLiabilityValue)(perpPosition.baseAssetAmount, oraclePrice);
2072
2090
  }
2073
- const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccount().maxMarginRatio);
2091
+ const userCustomMargin = Math.max(perpPosition.maxMarginRatio, this.getUserAccountOrThrow().maxMarginRatio);
2074
2092
  const marginRatio = new anchor_1.BN((0, market_1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, userCustomMargin));
2075
2093
  const _quoteOraclePriceData = quoteOraclePriceData ||
2076
2094
  this.velocityClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
@@ -2096,7 +2114,7 @@ class User {
2096
2114
  perpPnl: [],
2097
2115
  };
2098
2116
  for (const perpPosition of this.getActivePerpPositions()) {
2099
- const perpMarket = this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex);
2117
+ const perpMarket = this.velocityClient.getPerpMarketAccountOrThrow(perpPosition.marketIndex);
2100
2118
  const oraclePriceData = this.velocityClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
2101
2119
  const quoteOraclePriceData = this.velocityClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
2102
2120
  healthComponents.perpPositions.push(this.getPerpPositionHealth({
@@ -2105,7 +2123,7 @@ class User {
2105
2123
  oraclePriceData,
2106
2124
  quoteOraclePriceData,
2107
2125
  }));
2108
- const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
2126
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccountOrThrow(perpMarket.quoteSpotMarketIndex);
2109
2127
  const positionUnrealizedPnl = (0, position_2.calculatePositionPNL)(perpMarket, perpPosition, true, oraclePriceData);
2110
2128
  let pnlWeight;
2111
2129
  if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
@@ -2130,7 +2148,7 @@ class User {
2130
2148
  }
2131
2149
  let netQuoteValue = numericConstants_1.ZERO;
2132
2150
  for (const spotPosition of this.getActiveSpotPositions()) {
2133
- const spotMarketAccount = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
2151
+ const spotMarketAccount = this.velocityClient.getSpotMarketAccountOrThrow(spotPosition.marketIndex);
2134
2152
  const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
2135
2153
  const strictOraclePrice = new strictOraclePrice_1.StrictOraclePrice(oraclePriceData.price);
2136
2154
  if (spotPosition.marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX) {
@@ -2138,7 +2156,7 @@ class User {
2138
2156
  netQuoteValue = netQuoteValue.add(tokenAmount);
2139
2157
  continue;
2140
2158
  }
2141
- const { tokenAmount: worstCaseTokenAmount, tokenValue: tokenValue, weight, weightedTokenValue: weightedTokenValue, ordersValue: ordersValue, } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, marginCategory, this.getUserAccount().maxMarginRatio);
2159
+ const { tokenAmount: worstCaseTokenAmount, tokenValue: tokenValue, weight, weightedTokenValue: weightedTokenValue, ordersValue: ordersValue, } = (0, spotPosition_1.getWorstCaseTokenAmounts)(spotPosition, spotMarketAccount, strictOraclePrice, marginCategory, this.getUserAccountOrThrow().maxMarginRatio);
2142
2160
  netQuoteValue = netQuoteValue.add(ordersValue);
2143
2161
  const baseAssetValue = tokenValue.abs();
2144
2162
  const weightedValue = weightedTokenValue.abs();
@@ -2165,7 +2183,7 @@ class User {
2165
2183
  const spotMarketAccount = this.velocityClient.getQuoteSpotMarketAccount();
2166
2184
  const oraclePriceData = this.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
2167
2185
  const baseAssetValue = (0, spotBalance_1.getTokenValue)(netQuoteValue, spotMarketAccount.decimals, oraclePriceData);
2168
- const { weight, weightedTokenValue } = (0, spotPosition_1.calculateWeightedTokenValue)(netQuoteValue, baseAssetValue, oraclePriceData.price, spotMarketAccount, marginCategory, this.getUserAccount().maxMarginRatio);
2186
+ const { weight, weightedTokenValue } = (0, spotPosition_1.calculateWeightedTokenValue)(netQuoteValue, baseAssetValue, oraclePriceData.price, spotMarketAccount, marginCategory, this.getUserAccountOrThrow().maxMarginRatio);
2169
2187
  if (netQuoteValue.lt(numericConstants_1.ZERO)) {
2170
2188
  healthComponents.borrows.push({
2171
2189
  marketIndex: spotMarketAccount.marketIndex,
@@ -2232,7 +2250,9 @@ class User {
2232
2250
  const liquidationBufferMap = (_b = opts === null || opts === void 0 ? void 0 : opts.liquidationBufferMap) !== null && _b !== void 0 ? _b : new Map();
2233
2251
  const includeOpenOrders = (_c = opts === null || opts === void 0 ? void 0 : opts.includeOpenOrders) !== null && _c !== void 0 ? _c : true;
2234
2252
  // Equivalent to on-chain user_custom_margin_ratio
2235
- const userCustomMarginRatio = marginCategory === 'Initial' ? this.getUserAccount().maxMarginRatio : 0;
2253
+ const userCustomMarginRatio = marginCategory === 'Initial'
2254
+ ? this.getUserAccountOrThrow().maxMarginRatio
2255
+ : 0;
2236
2256
  // Initialize calc via JS mirror of Rust/on-chain MarginCalculation
2237
2257
  const isolatedMarginBuffers = new Map();
2238
2258
  for (const [marketIndex, isolatedMarginBuffer,] of (_d = opts === null || opts === void 0 ? void 0 : opts.liquidationBufferMap) !== null && _d !== void 0 ? _d : new Map()) {
@@ -2246,11 +2266,11 @@ class User {
2246
2266
  .setIsolatedMarginBuffers(isolatedMarginBuffers);
2247
2267
  const calc = new marginCalculation_1.MarginCalculation(ctx);
2248
2268
  // SPOT POSITIONS
2249
- for (const spotPosition of this.getUserAccount().spotPositions) {
2269
+ for (const spotPosition of this.getUserAccountOrThrow().spotPositions) {
2250
2270
  if ((0, spotPosition_1.isSpotPositionAvailable)(spotPosition))
2251
2271
  continue;
2252
2272
  const isQuote = spotPosition.marketIndex === numericConstants_1.QUOTE_SPOT_MARKET_INDEX;
2253
- const spotMarket = this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
2273
+ const spotMarket = this.velocityClient.getSpotMarketAccountOrThrow(spotPosition.marketIndex);
2254
2274
  const oraclePriceData = this.getOracleDataForSpotMarket(spotPosition.marketIndex);
2255
2275
  const twap5 = strict
2256
2276
  ? (0, oracles_1.calculateLiveOracleTwap)(spotMarket.historicalOracleData, oraclePriceData, new anchor_1.BN(Math.floor(Date.now() / 1000)), numericConstants_1.FIVE_MINUTE)
@@ -2299,8 +2319,8 @@ class User {
2299
2319
  }
2300
2320
  // PERP POSITIONS
2301
2321
  for (const marketPosition of this.getActivePerpPositions()) {
2302
- const market = this.velocityClient.getPerpMarketAccount(marketPosition.marketIndex);
2303
- const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
2322
+ const market = this.velocityClient.getPerpMarketAccountOrThrow(marketPosition.marketIndex);
2323
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccountOrThrow(market.quoteSpotMarketIndex);
2304
2324
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
2305
2325
  const oraclePriceData = this.getMMOracleDataForPerpMarket(market.marketIndex);
2306
2326
  const nonMmmOraclePriceData = this.getOracleDataForPerpMarket(market.marketIndex);
@@ -2353,7 +2373,7 @@ class User {
2353
2373
  // derive isolated quote deposit value, mirroring on-chain logic
2354
2374
  let depositValue = numericConstants_1.ZERO;
2355
2375
  if ((_h = marketPosition.isolatedPositionScaledBalance) === null || _h === void 0 ? void 0 : _h.gt(numericConstants_1.ZERO)) {
2356
- const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
2376
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccountOrThrow(market.quoteSpotMarketIndex);
2357
2377
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(market.quoteSpotMarketIndex);
2358
2378
  const strictQuote = new strictOraclePrice_1.StrictOraclePrice(quoteOraclePriceData.price, strict
2359
2379
  ? quoteSpotMarket.historicalOracleData.lastOraclePriceTwap5Min