@velocity-exchange/sdk 0.1.0 → 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 (676) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/bun.lock +23 -1
  3. package/lib/browser/accounts/basicUserAccountSubscriber.d.ts +5 -3
  4. package/lib/browser/accounts/basicUserAccountSubscriber.js +4 -3
  5. package/lib/browser/accounts/basicUserStatsAccountSubscriber.d.ts +5 -3
  6. package/lib/browser/accounts/basicUserStatsAccountSubscriber.js +4 -3
  7. package/lib/browser/accounts/bulkAccountLoader.d.ts +5 -5
  8. package/lib/browser/accounts/bulkAccountLoader.js +12 -1
  9. package/lib/browser/accounts/grpcAccountSubscriber.d.ts +2 -1
  10. package/lib/browser/accounts/grpcAccountSubscriber.js +8 -2
  11. package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +3 -2
  12. package/lib/browser/accounts/grpcMultiAccountSubscriber.js +13 -5
  13. package/lib/browser/accounts/grpcMultiUserAccountSubscriber.d.ts +2 -1
  14. package/lib/browser/accounts/grpcMultiUserAccountSubscriber.js +12 -8
  15. package/lib/browser/accounts/grpcProgramAccountSubscriber.d.ts +2 -1
  16. package/lib/browser/accounts/grpcProgramAccountSubscriber.js +8 -2
  17. package/lib/browser/accounts/grpcVelocityClientAccountSubscriber.js +18 -3
  18. package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +1 -1
  19. package/lib/browser/accounts/grpcVelocityClientAccountSubscriberV2.js +40 -17
  20. package/lib/browser/accounts/laserProgramAccountSubscriber.js +3 -0
  21. package/lib/browser/accounts/oneShotUserAccountSubscriber.js +4 -3
  22. package/lib/browser/accounts/oneShotUserStatsAccountSubscriber.js +7 -3
  23. package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  24. package/lib/browser/accounts/pollingInsuranceFundStakeAccountSubscriber.js +3 -3
  25. package/lib/browser/accounts/pollingOracleAccountSubscriber.d.ts +0 -2
  26. package/lib/browser/accounts/pollingOracleAccountSubscriber.js +16 -5
  27. package/lib/browser/accounts/pollingTokenAccountSubscriber.d.ts +0 -2
  28. package/lib/browser/accounts/pollingTokenAccountSubscriber.js +16 -5
  29. package/lib/browser/accounts/pollingUserAccountSubscriber.d.ts +8 -4
  30. package/lib/browser/accounts/pollingUserAccountSubscriber.js +17 -11
  31. package/lib/browser/accounts/pollingUserStatsAccountSubscriber.d.ts +4 -2
  32. package/lib/browser/accounts/pollingUserStatsAccountSubscriber.js +6 -6
  33. package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.d.ts +7 -1
  34. package/lib/browser/accounts/pollingVelocityClientAccountSubscriber.js +64 -28
  35. package/lib/browser/accounts/types.d.ts +4 -4
  36. package/lib/browser/accounts/utils.d.ts +1 -0
  37. package/lib/browser/accounts/utils.js +8 -1
  38. package/lib/browser/accounts/webSocketAccountSubscriber.d.ts +4 -2
  39. package/lib/browser/accounts/webSocketAccountSubscriber.js +17 -5
  40. package/lib/browser/accounts/webSocketAccountSubscriberV2.d.ts +4 -2
  41. package/lib/browser/accounts/webSocketAccountSubscriberV2.js +17 -6
  42. package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  43. package/lib/browser/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +9 -0
  44. package/lib/browser/accounts/webSocketProgramAccountSubscriber.d.ts +3 -1
  45. package/lib/browser/accounts/webSocketProgramAccountSubscriber.js +14 -3
  46. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +3 -1
  47. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +15 -3
  48. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.d.ts +3 -1
  49. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.js +14 -4
  50. package/lib/browser/accounts/webSocketUserAccountSubscriber.d.ts +4 -2
  51. package/lib/browser/accounts/webSocketUserAccountSubscriber.js +9 -0
  52. package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.d.ts +4 -2
  53. package/lib/browser/accounts/webSocketUserStatsAccountSubsriber.js +9 -0
  54. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.d.ts +4 -4
  55. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriber.js +48 -20
  56. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +7 -3
  57. package/lib/browser/accounts/webSocketVelocityClientAccountSubscriberV2.js +45 -14
  58. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.d.ts +1 -1
  59. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.js +0 -3
  60. package/lib/browser/addresses/marketAddresses.js +3 -2
  61. package/lib/browser/addresses/pda.d.ts +0 -2
  62. package/lib/browser/addresses/pda.js +1 -14
  63. package/lib/browser/adminClient.d.ts +24 -28
  64. package/lib/browser/adminClient.js +156 -157
  65. package/lib/browser/auctionSubscriber/auctionSubscriber.d.ts +1 -1
  66. package/lib/browser/auctionSubscriber/auctionSubscriber.js +6 -4
  67. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.d.ts +1 -1
  68. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +9 -4
  69. package/lib/browser/bankrun/bankrunConnection.d.ts +6 -4
  70. package/lib/browser/bankrun/bankrunConnection.js +69 -31
  71. package/lib/browser/blockhashSubscriber/BlockhashSubscriber.d.ts +1 -1
  72. package/lib/browser/blockhashSubscriber/BlockhashSubscriber.js +3 -2
  73. package/lib/browser/clock/clockSubscriber.d.ts +6 -6
  74. package/lib/browser/clock/clockSubscriber.js +4 -3
  75. package/lib/browser/config.d.ts +1 -1
  76. package/lib/browser/config.js +8 -4
  77. package/lib/browser/constants/index.d.ts +1 -2
  78. package/lib/browser/constants/index.js +1 -2
  79. package/lib/browser/constants/numericConstants.d.ts +1 -3
  80. package/lib/browser/constants/numericConstants.js +3 -5
  81. package/lib/browser/constituentMap/constituentMap.js +5 -1
  82. package/lib/browser/constituentMap/pollingConstituentAccountSubscriber.js +3 -2
  83. package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.d.ts +2 -3
  84. package/lib/browser/constituentMap/webSocketConstituentAccountSubscriber.js +10 -3
  85. package/lib/browser/core/instructions/perpOrders.d.ts +14 -0
  86. package/lib/browser/core/instructions/perpOrders.js +14 -0
  87. package/lib/browser/core/remainingAccounts.d.ts +2 -2
  88. package/lib/browser/core/remainingAccounts.js +2 -2
  89. package/lib/browser/decode/user.js +9 -1
  90. package/lib/browser/dlob/DLOB.d.ts +6 -1
  91. package/lib/browser/dlob/DLOB.js +201 -138
  92. package/lib/browser/dlob/DLOBNode.d.ts +5 -2
  93. package/lib/browser/dlob/DLOBNode.js +7 -0
  94. package/lib/browser/dlob/DLOBSubscriber.js +37 -32
  95. package/lib/browser/dlob/NodeList.js +8 -7
  96. package/lib/browser/dlob/orderBookLevels.js +19 -12
  97. package/lib/browser/events/eventList.js +4 -1
  98. package/lib/browser/events/eventSubscriber.d.ts +5 -2
  99. package/lib/browser/events/eventSubscriber.js +75 -26
  100. package/lib/browser/events/eventsServerLogProvider.d.ts +1 -1
  101. package/lib/browser/events/eventsServerLogProvider.js +15 -8
  102. package/lib/browser/events/fetchLogs.d.ts +1 -1
  103. package/lib/browser/events/fetchLogs.js +4 -2
  104. package/lib/browser/events/pollingLogProvider.d.ts +1 -1
  105. package/lib/browser/events/pollingLogProvider.js +1 -0
  106. package/lib/browser/events/txEventCache.js +6 -2
  107. package/lib/browser/events/types.d.ts +2 -4
  108. package/lib/browser/events/types.js +0 -2
  109. package/lib/browser/events/webSocketLogProvider.d.ts +1 -1
  110. package/lib/browser/events/webSocketLogProvider.js +5 -2
  111. package/lib/browser/factory/bigNum.js +1 -1
  112. package/lib/browser/idl/velocity.d.ts +1243 -1586
  113. package/lib/browser/idl/velocity.json +1234 -1577
  114. package/lib/browser/index.d.ts +0 -2
  115. package/lib/browser/index.js +0 -2
  116. package/lib/browser/jupiter/jupiterClient.d.ts +1 -1
  117. package/lib/browser/jupiter/jupiterClient.js +18 -8
  118. package/lib/browser/marginCalculation.js +1 -0
  119. package/lib/browser/math/amm.d.ts +31 -13
  120. package/lib/browser/math/amm.js +77 -22
  121. package/lib/browser/math/bankruptcy.js +1 -1
  122. package/lib/browser/math/funding.js +6 -6
  123. package/lib/browser/math/insurance.d.ts +1 -1
  124. package/lib/browser/math/insurance.js +4 -5
  125. package/lib/browser/math/margin.d.ts +1 -9
  126. package/lib/browser/math/margin.js +3 -62
  127. package/lib/browser/math/market.d.ts +4 -12
  128. package/lib/browser/math/market.js +6 -40
  129. package/lib/browser/math/oracles.d.ts +0 -3
  130. package/lib/browser/math/oracles.js +1 -14
  131. package/lib/browser/math/orders.d.ts +0 -5
  132. package/lib/browser/math/orders.js +2 -74
  133. package/lib/browser/math/position.d.ts +0 -4
  134. package/lib/browser/math/position.js +5 -10
  135. package/lib/browser/math/repeg.d.ts +0 -1
  136. package/lib/browser/math/repeg.js +1 -23
  137. package/lib/browser/math/spotBalance.d.ts +4 -4
  138. package/lib/browser/math/spotBalance.js +6 -6
  139. package/lib/browser/math/spotMarket.d.ts +0 -6
  140. package/lib/browser/math/spotMarket.js +1 -16
  141. package/lib/browser/math/spotPosition.d.ts +4 -4
  142. package/lib/browser/math/state.d.ts +0 -2
  143. package/lib/browser/math/state.js +1 -9
  144. package/lib/browser/math/superStake.d.ts +3 -53
  145. package/lib/browser/math/superStake.js +16 -13
  146. package/lib/browser/math/trade.js +6 -6
  147. package/lib/browser/memcmp.d.ts +0 -1
  148. package/lib/browser/memcmp.js +1 -10
  149. package/lib/browser/oracles/prelaunchOracleClient.js +3 -2
  150. package/lib/browser/oracles/pythClient.js +9 -3
  151. package/lib/browser/oracles/pythLazerClient.js +3 -2
  152. package/lib/browser/oracles/utils.d.ts +4 -0
  153. package/lib/browser/oracles/utils.js +9 -1
  154. package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +1 -1
  155. package/lib/browser/orderSubscriber/OrderSubscriber.js +8 -2
  156. package/lib/browser/priorityFee/heliusPriorityFeeMethod.d.ts +1 -1
  157. package/lib/browser/priorityFee/priorityFeeSubscriber.d.ts +1 -1
  158. package/lib/browser/priorityFee/priorityFeeSubscriber.js +32 -8
  159. package/lib/browser/priorityFee/solanaPriorityFeeMethod.d.ts +1 -1
  160. package/lib/browser/pyth/types.d.ts +0 -1
  161. package/lib/browser/slot/SlotSubscriber.d.ts +1 -1
  162. package/lib/browser/slot/SlotSubscriber.js +2 -1
  163. package/lib/browser/slot/SlothashSubscriber.d.ts +3 -2
  164. package/lib/browser/slot/SlothashSubscriber.js +11 -4
  165. package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.d.ts +0 -3
  166. package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.js +5 -5
  167. package/lib/browser/swift/signedMsgUserAccountSubscriber.d.ts +3 -2
  168. package/lib/browser/swift/signedMsgUserAccountSubscriber.js +13 -5
  169. package/lib/browser/swift/swiftOrderSubscriber.d.ts +1 -1
  170. package/lib/browser/swift/swiftOrderSubscriber.js +10 -5
  171. package/lib/browser/testClient.js +3 -2
  172. package/lib/browser/tokenFaucet.js +16 -2
  173. package/lib/browser/tx/baseTxSender.d.ts +4 -4
  174. package/lib/browser/tx/baseTxSender.js +5 -4
  175. package/lib/browser/tx/fastSingleTxSender.d.ts +3 -3
  176. package/lib/browser/tx/fastSingleTxSender.js +9 -5
  177. package/lib/browser/tx/reportTransactionError.d.ts +2 -2
  178. package/lib/browser/tx/reportTransactionError.js +5 -1
  179. package/lib/browser/tx/retryTxSender.d.ts +1 -1
  180. package/lib/browser/tx/retryTxSender.js +4 -2
  181. package/lib/browser/tx/txHandler.d.ts +7 -1
  182. package/lib/browser/tx/txHandler.js +54 -40
  183. package/lib/browser/tx/txParamProcessor.d.ts +4 -1
  184. package/lib/browser/tx/txParamProcessor.js +6 -0
  185. package/lib/browser/tx/types.d.ts +1 -1
  186. package/lib/browser/tx/whileValidTxSender.d.ts +1 -6
  187. package/lib/browser/tx/whileValidTxSender.js +35 -13
  188. package/lib/browser/types.d.ts +27 -70
  189. package/lib/browser/types.js +2 -2
  190. package/lib/browser/user.d.ts +21 -7
  191. package/lib/browser/user.js +156 -132
  192. package/lib/browser/userMap/WebsocketSubscription.d.ts +1 -1
  193. package/lib/browser/userMap/grpcSubscription.d.ts +1 -1
  194. package/lib/browser/userMap/referrerMap.js +10 -3
  195. package/lib/browser/userMap/revenueShareEscrowMap.js +4 -0
  196. package/lib/browser/userMap/userMap.d.ts +1 -1
  197. package/lib/browser/userMap/userMap.js +36 -23
  198. package/lib/browser/userMap/userStatsMap.d.ts +1 -1
  199. package/lib/browser/userMap/userStatsMap.js +18 -13
  200. package/lib/browser/userStats.d.ts +7 -2
  201. package/lib/browser/userStats.js +18 -4
  202. package/lib/browser/util/TransactionConfirmationManager.js +4 -2
  203. package/lib/browser/util/chainClock.d.ts +1 -1
  204. package/lib/browser/util/computeUnits.d.ts +1 -1
  205. package/lib/browser/util/computeUnits.js +6 -1
  206. package/lib/browser/velocityClient.d.ts +92 -34
  207. package/lib/browser/velocityClient.js +414 -280
  208. package/lib/node/accounts/basicUserAccountSubscriber.d.ts +5 -3
  209. package/lib/node/accounts/basicUserAccountSubscriber.d.ts.map +1 -1
  210. package/lib/node/accounts/basicUserAccountSubscriber.js +4 -3
  211. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts +5 -3
  212. package/lib/node/accounts/basicUserStatsAccountSubscriber.d.ts.map +1 -1
  213. package/lib/node/accounts/basicUserStatsAccountSubscriber.js +4 -3
  214. package/lib/node/accounts/bulkAccountLoader.d.ts +5 -5
  215. package/lib/node/accounts/bulkAccountLoader.d.ts.map +1 -1
  216. package/lib/node/accounts/bulkAccountLoader.js +12 -1
  217. package/lib/node/accounts/grpcAccountSubscriber.d.ts +2 -1
  218. package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
  219. package/lib/node/accounts/grpcAccountSubscriber.js +8 -2
  220. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +3 -2
  221. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
  222. package/lib/node/accounts/grpcMultiAccountSubscriber.js +13 -5
  223. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts +2 -1
  224. package/lib/node/accounts/grpcMultiUserAccountSubscriber.d.ts.map +1 -1
  225. package/lib/node/accounts/grpcMultiUserAccountSubscriber.js +12 -8
  226. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +2 -1
  227. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts.map +1 -1
  228. package/lib/node/accounts/grpcProgramAccountSubscriber.js +8 -2
  229. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.d.ts.map +1 -1
  230. package/lib/node/accounts/grpcVelocityClientAccountSubscriber.js +18 -3
  231. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts +1 -1
  232. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  233. package/lib/node/accounts/grpcVelocityClientAccountSubscriberV2.js +40 -17
  234. package/lib/node/accounts/laserProgramAccountSubscriber.d.ts.map +1 -1
  235. package/lib/node/accounts/laserProgramAccountSubscriber.js +3 -0
  236. package/lib/node/accounts/oneShotUserAccountSubscriber.d.ts.map +1 -1
  237. package/lib/node/accounts/oneShotUserAccountSubscriber.js +4 -3
  238. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.d.ts.map +1 -1
  239. package/lib/node/accounts/oneShotUserStatsAccountSubscriber.js +7 -3
  240. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  241. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  242. package/lib/node/accounts/pollingInsuranceFundStakeAccountSubscriber.js +3 -3
  243. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts +0 -2
  244. package/lib/node/accounts/pollingOracleAccountSubscriber.d.ts.map +1 -1
  245. package/lib/node/accounts/pollingOracleAccountSubscriber.js +16 -5
  246. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts +0 -2
  247. package/lib/node/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -1
  248. package/lib/node/accounts/pollingTokenAccountSubscriber.js +16 -5
  249. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts +8 -4
  250. package/lib/node/accounts/pollingUserAccountSubscriber.d.ts.map +1 -1
  251. package/lib/node/accounts/pollingUserAccountSubscriber.js +17 -11
  252. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts +4 -2
  253. package/lib/node/accounts/pollingUserStatsAccountSubscriber.d.ts.map +1 -1
  254. package/lib/node/accounts/pollingUserStatsAccountSubscriber.js +6 -6
  255. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts +7 -1
  256. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.d.ts.map +1 -1
  257. package/lib/node/accounts/pollingVelocityClientAccountSubscriber.js +64 -28
  258. package/lib/node/accounts/types.d.ts +4 -4
  259. package/lib/node/accounts/types.d.ts.map +1 -1
  260. package/lib/node/accounts/utils.d.ts +1 -0
  261. package/lib/node/accounts/utils.d.ts.map +1 -1
  262. package/lib/node/accounts/utils.js +8 -1
  263. package/lib/node/accounts/webSocketAccountSubscriber.d.ts +4 -2
  264. package/lib/node/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
  265. package/lib/node/accounts/webSocketAccountSubscriber.js +17 -5
  266. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts +4 -2
  267. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts.map +1 -1
  268. package/lib/node/accounts/webSocketAccountSubscriberV2.js +17 -6
  269. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +4 -2
  270. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts.map +1 -1
  271. package/lib/node/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +9 -0
  272. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts +3 -1
  273. package/lib/node/accounts/webSocketProgramAccountSubscriber.d.ts.map +1 -1
  274. package/lib/node/accounts/webSocketProgramAccountSubscriber.js +14 -3
  275. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +3 -1
  276. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +1 -1
  277. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +15 -3
  278. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts +3 -1
  279. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts.map +1 -1
  280. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.js +14 -4
  281. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts +4 -2
  282. package/lib/node/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -1
  283. package/lib/node/accounts/webSocketUserAccountSubscriber.js +9 -0
  284. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts +4 -2
  285. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.d.ts.map +1 -1
  286. package/lib/node/accounts/webSocketUserStatsAccountSubsriber.js +9 -0
  287. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts +4 -4
  288. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.d.ts.map +1 -1
  289. package/lib/node/accounts/webSocketVelocityClientAccountSubscriber.js +48 -20
  290. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts +7 -3
  291. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.d.ts.map +1 -1
  292. package/lib/node/accounts/webSocketVelocityClientAccountSubscriberV2.js +45 -14
  293. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts +1 -1
  294. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts.map +1 -1
  295. package/lib/node/accounts/websocketProgramUserAccountSubscriber.js +0 -3
  296. package/lib/node/addresses/marketAddresses.d.ts.map +1 -1
  297. package/lib/node/addresses/marketAddresses.js +3 -2
  298. package/lib/node/addresses/pda.d.ts +0 -2
  299. package/lib/node/addresses/pda.d.ts.map +1 -1
  300. package/lib/node/addresses/pda.js +1 -14
  301. package/lib/node/adminClient.d.ts +24 -28
  302. package/lib/node/adminClient.d.ts.map +1 -1
  303. package/lib/node/adminClient.js +156 -157
  304. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +1 -1
  305. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
  306. package/lib/node/auctionSubscriber/auctionSubscriber.js +6 -4
  307. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +1 -1
  308. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
  309. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +9 -4
  310. package/lib/node/bankrun/bankrunConnection.d.ts +6 -4
  311. package/lib/node/bankrun/bankrunConnection.d.ts.map +1 -1
  312. package/lib/node/bankrun/bankrunConnection.js +69 -31
  313. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts +1 -1
  314. package/lib/node/blockhashSubscriber/BlockhashSubscriber.d.ts.map +1 -1
  315. package/lib/node/blockhashSubscriber/BlockhashSubscriber.js +3 -2
  316. package/lib/node/clock/clockSubscriber.d.ts +6 -6
  317. package/lib/node/clock/clockSubscriber.d.ts.map +1 -1
  318. package/lib/node/clock/clockSubscriber.js +4 -3
  319. package/lib/node/config.d.ts +1 -1
  320. package/lib/node/config.d.ts.map +1 -1
  321. package/lib/node/config.js +8 -4
  322. package/lib/node/constants/index.d.ts +1 -2
  323. package/lib/node/constants/index.d.ts.map +1 -1
  324. package/lib/node/constants/index.js +1 -2
  325. package/lib/node/constants/numericConstants.d.ts +1 -3
  326. package/lib/node/constants/numericConstants.d.ts.map +1 -1
  327. package/lib/node/constants/numericConstants.js +3 -5
  328. package/lib/node/constituentMap/constituentMap.d.ts.map +1 -1
  329. package/lib/node/constituentMap/constituentMap.js +5 -1
  330. package/lib/node/constituentMap/pollingConstituentAccountSubscriber.d.ts.map +1 -1
  331. package/lib/node/constituentMap/pollingConstituentAccountSubscriber.js +3 -2
  332. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts +2 -3
  333. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.d.ts.map +1 -1
  334. package/lib/node/constituentMap/webSocketConstituentAccountSubscriber.js +10 -3
  335. package/lib/node/core/instructions/perpOrders.d.ts +14 -0
  336. package/lib/node/core/instructions/perpOrders.d.ts.map +1 -1
  337. package/lib/node/core/instructions/perpOrders.js +14 -0
  338. package/lib/node/core/remainingAccounts.d.ts +2 -2
  339. package/lib/node/core/remainingAccounts.d.ts.map +1 -1
  340. package/lib/node/core/remainingAccounts.js +2 -2
  341. package/lib/node/decode/user.d.ts.map +1 -1
  342. package/lib/node/decode/user.js +9 -1
  343. package/lib/node/dlob/DLOB.d.ts +6 -1
  344. package/lib/node/dlob/DLOB.d.ts.map +1 -1
  345. package/lib/node/dlob/DLOB.js +201 -138
  346. package/lib/node/dlob/DLOBNode.d.ts +5 -2
  347. package/lib/node/dlob/DLOBNode.d.ts.map +1 -1
  348. package/lib/node/dlob/DLOBNode.js +7 -0
  349. package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
  350. package/lib/node/dlob/DLOBSubscriber.js +37 -32
  351. package/lib/node/dlob/NodeList.d.ts.map +1 -1
  352. package/lib/node/dlob/NodeList.js +8 -7
  353. package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
  354. package/lib/node/dlob/orderBookLevels.js +19 -12
  355. package/lib/node/events/eventList.d.ts +3 -3
  356. package/lib/node/events/eventList.d.ts.map +1 -1
  357. package/lib/node/events/eventList.js +4 -1
  358. package/lib/node/events/eventSubscriber.d.ts +5 -2
  359. package/lib/node/events/eventSubscriber.d.ts.map +1 -1
  360. package/lib/node/events/eventSubscriber.js +75 -26
  361. package/lib/node/events/eventsServerLogProvider.d.ts +2 -2
  362. package/lib/node/events/eventsServerLogProvider.d.ts.map +1 -1
  363. package/lib/node/events/eventsServerLogProvider.js +15 -8
  364. package/lib/node/events/fetchLogs.d.ts +1 -1
  365. package/lib/node/events/fetchLogs.d.ts.map +1 -1
  366. package/lib/node/events/fetchLogs.js +4 -2
  367. package/lib/node/events/pollingLogProvider.d.ts +2 -2
  368. package/lib/node/events/pollingLogProvider.d.ts.map +1 -1
  369. package/lib/node/events/pollingLogProvider.js +1 -0
  370. package/lib/node/events/txEventCache.d.ts +3 -3
  371. package/lib/node/events/txEventCache.d.ts.map +1 -1
  372. package/lib/node/events/txEventCache.js +6 -2
  373. package/lib/node/events/types.d.ts +2 -4
  374. package/lib/node/events/types.d.ts.map +1 -1
  375. package/lib/node/events/types.js +0 -2
  376. package/lib/node/events/webSocketLogProvider.d.ts +2 -2
  377. package/lib/node/events/webSocketLogProvider.d.ts.map +1 -1
  378. package/lib/node/events/webSocketLogProvider.js +5 -2
  379. package/lib/node/factory/bigNum.js +1 -1
  380. package/lib/node/idl/velocity.d.ts +1243 -1586
  381. package/lib/node/idl/velocity.d.ts.map +1 -1
  382. package/lib/node/idl/velocity.json +1234 -1577
  383. package/lib/node/index.d.ts +0 -2
  384. package/lib/node/index.d.ts.map +1 -1
  385. package/lib/node/index.js +0 -2
  386. package/lib/node/jupiter/jupiterClient.d.ts +1 -1
  387. package/lib/node/jupiter/jupiterClient.d.ts.map +1 -1
  388. package/lib/node/jupiter/jupiterClient.js +18 -8
  389. package/lib/node/marginCalculation.d.ts.map +1 -1
  390. package/lib/node/marginCalculation.js +1 -0
  391. package/lib/node/math/amm.d.ts +31 -13
  392. package/lib/node/math/amm.d.ts.map +1 -1
  393. package/lib/node/math/amm.js +77 -22
  394. package/lib/node/math/bankruptcy.js +1 -1
  395. package/lib/node/math/funding.d.ts.map +1 -1
  396. package/lib/node/math/funding.js +6 -6
  397. package/lib/node/math/insurance.d.ts +1 -1
  398. package/lib/node/math/insurance.d.ts.map +1 -1
  399. package/lib/node/math/insurance.js +4 -5
  400. package/lib/node/math/margin.d.ts +1 -9
  401. package/lib/node/math/margin.d.ts.map +1 -1
  402. package/lib/node/math/margin.js +3 -62
  403. package/lib/node/math/market.d.ts +4 -12
  404. package/lib/node/math/market.d.ts.map +1 -1
  405. package/lib/node/math/market.js +6 -40
  406. package/lib/node/math/oracles.d.ts +0 -3
  407. package/lib/node/math/oracles.d.ts.map +1 -1
  408. package/lib/node/math/oracles.js +1 -14
  409. package/lib/node/math/orders.d.ts +0 -5
  410. package/lib/node/math/orders.d.ts.map +1 -1
  411. package/lib/node/math/orders.js +2 -74
  412. package/lib/node/math/position.d.ts +1 -5
  413. package/lib/node/math/position.d.ts.map +1 -1
  414. package/lib/node/math/position.js +5 -10
  415. package/lib/node/math/repeg.d.ts +0 -1
  416. package/lib/node/math/repeg.d.ts.map +1 -1
  417. package/lib/node/math/repeg.js +1 -23
  418. package/lib/node/math/spotBalance.d.ts +4 -4
  419. package/lib/node/math/spotBalance.d.ts.map +1 -1
  420. package/lib/node/math/spotBalance.js +6 -6
  421. package/lib/node/math/spotMarket.d.ts +0 -6
  422. package/lib/node/math/spotMarket.d.ts.map +1 -1
  423. package/lib/node/math/spotMarket.js +1 -16
  424. package/lib/node/math/spotPosition.d.ts +4 -4
  425. package/lib/node/math/spotPosition.d.ts.map +1 -1
  426. package/lib/node/math/state.d.ts +0 -2
  427. package/lib/node/math/state.d.ts.map +1 -1
  428. package/lib/node/math/state.js +1 -9
  429. package/lib/node/math/superStake.d.ts +3 -53
  430. package/lib/node/math/superStake.d.ts.map +1 -1
  431. package/lib/node/math/superStake.js +16 -13
  432. package/lib/node/math/trade.d.ts +2 -2
  433. package/lib/node/math/trade.d.ts.map +1 -1
  434. package/lib/node/math/trade.js +6 -6
  435. package/lib/node/memcmp.d.ts +0 -1
  436. package/lib/node/memcmp.d.ts.map +1 -1
  437. package/lib/node/memcmp.js +1 -10
  438. package/lib/node/oracles/oracleClientCache.d.ts +1 -1
  439. package/lib/node/oracles/prelaunchOracleClient.d.ts.map +1 -1
  440. package/lib/node/oracles/prelaunchOracleClient.js +3 -2
  441. package/lib/node/oracles/pythClient.d.ts.map +1 -1
  442. package/lib/node/oracles/pythClient.js +9 -3
  443. package/lib/node/oracles/pythLazerClient.d.ts.map +1 -1
  444. package/lib/node/oracles/pythLazerClient.js +3 -2
  445. package/lib/node/oracles/utils.d.ts +4 -0
  446. package/lib/node/oracles/utils.d.ts.map +1 -1
  447. package/lib/node/oracles/utils.js +9 -1
  448. package/lib/node/orderSubscriber/OrderSubscriber.d.ts +1 -1
  449. package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
  450. package/lib/node/orderSubscriber/OrderSubscriber.js +8 -2
  451. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts +1 -1
  452. package/lib/node/priorityFee/heliusPriorityFeeMethod.d.ts.map +1 -1
  453. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +1 -1
  454. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts.map +1 -1
  455. package/lib/node/priorityFee/priorityFeeSubscriber.js +32 -8
  456. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts +1 -1
  457. package/lib/node/priorityFee/solanaPriorityFeeMethod.d.ts.map +1 -1
  458. package/lib/node/pyth/types.d.ts +0 -1
  459. package/lib/node/pyth/types.d.ts.map +1 -1
  460. package/lib/node/slot/SlotSubscriber.d.ts +1 -1
  461. package/lib/node/slot/SlotSubscriber.d.ts.map +1 -1
  462. package/lib/node/slot/SlotSubscriber.js +2 -1
  463. package/lib/node/slot/SlothashSubscriber.d.ts +3 -2
  464. package/lib/node/slot/SlothashSubscriber.d.ts.map +1 -1
  465. package/lib/node/slot/SlothashSubscriber.js +11 -4
  466. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts +0 -3
  467. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts.map +1 -1
  468. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.js +5 -5
  469. package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts +3 -2
  470. package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts.map +1 -1
  471. package/lib/node/swift/signedMsgUserAccountSubscriber.js +13 -5
  472. package/lib/node/swift/swiftOrderSubscriber.d.ts +1 -1
  473. package/lib/node/swift/swiftOrderSubscriber.d.ts.map +1 -1
  474. package/lib/node/swift/swiftOrderSubscriber.js +10 -5
  475. package/lib/node/testClient.js +3 -2
  476. package/lib/node/tokenFaucet.d.ts.map +1 -1
  477. package/lib/node/tokenFaucet.js +16 -2
  478. package/lib/node/tx/baseTxSender.d.ts +4 -4
  479. package/lib/node/tx/baseTxSender.d.ts.map +1 -1
  480. package/lib/node/tx/baseTxSender.js +5 -4
  481. package/lib/node/tx/fastSingleTxSender.d.ts +3 -3
  482. package/lib/node/tx/fastSingleTxSender.d.ts.map +1 -1
  483. package/lib/node/tx/fastSingleTxSender.js +9 -5
  484. package/lib/node/tx/reportTransactionError.d.ts +2 -2
  485. package/lib/node/tx/reportTransactionError.d.ts.map +1 -1
  486. package/lib/node/tx/reportTransactionError.js +5 -1
  487. package/lib/node/tx/retryTxSender.d.ts +1 -1
  488. package/lib/node/tx/retryTxSender.d.ts.map +1 -1
  489. package/lib/node/tx/retryTxSender.js +4 -2
  490. package/lib/node/tx/txHandler.d.ts +9 -3
  491. package/lib/node/tx/txHandler.d.ts.map +1 -1
  492. package/lib/node/tx/txHandler.js +54 -40
  493. package/lib/node/tx/txParamProcessor.d.ts +4 -1
  494. package/lib/node/tx/txParamProcessor.d.ts.map +1 -1
  495. package/lib/node/tx/txParamProcessor.js +6 -0
  496. package/lib/node/tx/types.d.ts +1 -1
  497. package/lib/node/tx/types.d.ts.map +1 -1
  498. package/lib/node/tx/whileValidTxSender.d.ts +1 -6
  499. package/lib/node/tx/whileValidTxSender.d.ts.map +1 -1
  500. package/lib/node/tx/whileValidTxSender.js +35 -13
  501. package/lib/node/types.d.ts +25 -68
  502. package/lib/node/types.d.ts.map +1 -1
  503. package/lib/node/types.js +2 -2
  504. package/lib/node/user.d.ts +21 -7
  505. package/lib/node/user.d.ts.map +1 -1
  506. package/lib/node/user.js +156 -132
  507. package/lib/node/userMap/WebsocketSubscription.d.ts +1 -1
  508. package/lib/node/userMap/WebsocketSubscription.d.ts.map +1 -1
  509. package/lib/node/userMap/grpcSubscription.d.ts +1 -1
  510. package/lib/node/userMap/grpcSubscription.d.ts.map +1 -1
  511. package/lib/node/userMap/referrerMap.d.ts.map +1 -1
  512. package/lib/node/userMap/referrerMap.js +10 -3
  513. package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
  514. package/lib/node/userMap/revenueShareEscrowMap.js +4 -0
  515. package/lib/node/userMap/userMap.d.ts +1 -1
  516. package/lib/node/userMap/userMap.d.ts.map +1 -1
  517. package/lib/node/userMap/userMap.js +36 -23
  518. package/lib/node/userMap/userStatsMap.d.ts +1 -1
  519. package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
  520. package/lib/node/userMap/userStatsMap.js +18 -13
  521. package/lib/node/userStats.d.ts +7 -2
  522. package/lib/node/userStats.d.ts.map +1 -1
  523. package/lib/node/userStats.js +18 -4
  524. package/lib/node/util/TransactionConfirmationManager.d.ts.map +1 -1
  525. package/lib/node/util/TransactionConfirmationManager.js +4 -2
  526. package/lib/node/util/chainClock.d.ts +1 -1
  527. package/lib/node/util/chainClock.d.ts.map +1 -1
  528. package/lib/node/util/computeUnits.d.ts +1 -1
  529. package/lib/node/util/computeUnits.d.ts.map +1 -1
  530. package/lib/node/util/computeUnits.js +6 -1
  531. package/lib/node/velocityClient.d.ts +93 -35
  532. package/lib/node/velocityClient.d.ts.map +1 -1
  533. package/lib/node/velocityClient.js +414 -280
  534. package/package.json +5 -3
  535. package/src/accounts/basicUserAccountSubscriber.ts +7 -5
  536. package/src/accounts/basicUserStatsAccountSubscriber.ts +9 -5
  537. package/src/accounts/bulkAccountLoader.ts +20 -7
  538. package/src/accounts/grpcAccountSubscriber.ts +11 -3
  539. package/src/accounts/grpcMultiAccountSubscriber.ts +24 -9
  540. package/src/accounts/grpcMultiUserAccountSubscriber.ts +16 -12
  541. package/src/accounts/grpcProgramAccountSubscriber.ts +11 -3
  542. package/src/accounts/grpcVelocityClientAccountSubscriber.ts +17 -7
  543. package/src/accounts/grpcVelocityClientAccountSubscriberV2.ts +66 -39
  544. package/src/accounts/laserProgramAccountSubscriber.ts +3 -0
  545. package/src/accounts/oneShotUserAccountSubscriber.ts +5 -3
  546. package/src/accounts/oneShotUserStatsAccountSubscriber.ts +8 -3
  547. package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +11 -5
  548. package/src/accounts/pollingOracleAccountSubscriber.ts +17 -10
  549. package/src/accounts/pollingTokenAccountSubscriber.ts +17 -10
  550. package/src/accounts/pollingUserAccountSubscriber.ts +26 -20
  551. package/src/accounts/pollingUserStatsAccountSubscriber.ts +12 -10
  552. package/src/accounts/pollingVelocityClientAccountSubscriber.ts +76 -30
  553. package/src/accounts/types.ts +6 -4
  554. package/src/accounts/utils.ts +10 -0
  555. package/src/accounts/webSocketAccountSubscriber.ts +23 -10
  556. package/src/accounts/webSocketAccountSubscriberV2.ts +23 -8
  557. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +17 -2
  558. package/src/accounts/webSocketProgramAccountSubscriber.ts +29 -4
  559. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +30 -4
  560. package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +29 -4
  561. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -2
  562. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +15 -2
  563. package/src/accounts/webSocketVelocityClientAccountSubscriber.ts +76 -48
  564. package/src/accounts/webSocketVelocityClientAccountSubscriberV2.ts +74 -35
  565. package/src/accounts/websocketProgramUserAccountSubscriber.ts +1 -6
  566. package/src/addresses/marketAddresses.ts +3 -2
  567. package/src/addresses/pda.ts +0 -25
  568. package/src/adminClient.ts +273 -303
  569. package/src/auctionSubscriber/auctionSubscriber.ts +7 -5
  570. package/src/auctionSubscriber/auctionSubscriberGrpc.ts +12 -5
  571. package/src/bankrun/bankrunConnection.ts +83 -39
  572. package/src/blockhashSubscriber/BlockhashSubscriber.ts +5 -4
  573. package/src/clock/clockSubscriber.ts +10 -9
  574. package/src/config.ts +8 -3
  575. package/src/constants/index.ts +1 -2
  576. package/src/constants/numericConstants.ts +3 -3
  577. package/src/constituentMap/constituentMap.ts +8 -1
  578. package/src/constituentMap/pollingConstituentAccountSubscriber.ts +4 -2
  579. package/src/constituentMap/webSocketConstituentAccountSubscriber.ts +13 -4
  580. package/src/core/instructions/perpOrders.ts +14 -0
  581. package/src/core/remainingAccounts.ts +4 -4
  582. package/src/decode/user.ts +8 -1
  583. package/src/dlob/DLOB.ts +296 -172
  584. package/src/dlob/DLOBNode.ts +23 -3
  585. package/src/dlob/DLOBSubscriber.ts +51 -34
  586. package/src/dlob/NodeList.ts +8 -7
  587. package/src/dlob/orderBookLevels.ts +45 -25
  588. package/src/events/eventList.ts +4 -1
  589. package/src/events/eventSubscriber.ts +107 -49
  590. package/src/events/eventsServerLogProvider.ts +22 -13
  591. package/src/events/fetchLogs.ts +3 -3
  592. package/src/events/pollingLogProvider.ts +2 -2
  593. package/src/events/txEventCache.ts +8 -2
  594. package/src/events/types.ts +0 -8
  595. package/src/events/webSocketLogProvider.ts +5 -3
  596. package/src/factory/bigNum.ts +2 -2
  597. package/src/idl/velocity.json +1234 -1577
  598. package/src/idl/velocity.ts +1234 -1577
  599. package/src/index.ts +0 -2
  600. package/src/jupiter/jupiterClient.ts +30 -10
  601. package/src/marginCalculation.ts +1 -0
  602. package/src/math/amm.ts +188 -33
  603. package/src/math/bankruptcy.ts +1 -1
  604. package/src/math/funding.ts +7 -6
  605. package/src/math/insurance.ts +6 -8
  606. package/src/math/margin.ts +5 -112
  607. package/src/math/market.ts +6 -96
  608. package/src/math/oracles.ts +0 -17
  609. package/src/math/orders.ts +1 -110
  610. package/src/math/position.ts +4 -16
  611. package/src/math/repeg.ts +0 -29
  612. package/src/math/spotBalance.ts +14 -10
  613. package/src/math/spotMarket.ts +2 -28
  614. package/src/math/spotPosition.ts +4 -4
  615. package/src/math/state.ts +0 -8
  616. package/src/math/superStake.ts +19 -65
  617. package/src/math/trade.ts +2 -6
  618. package/src/memcmp.ts +0 -9
  619. package/src/oracles/prelaunchOracleClient.ts +7 -2
  620. package/src/oracles/pythClient.ts +12 -3
  621. package/src/oracles/pythLazerClient.ts +7 -2
  622. package/src/oracles/utils.ts +15 -0
  623. package/src/orderSubscriber/OrderSubscriber.ts +10 -4
  624. package/src/priorityFee/heliusPriorityFeeMethod.ts +1 -1
  625. package/src/priorityFee/priorityFeeSubscriber.ts +45 -9
  626. package/src/priorityFee/solanaPriorityFeeMethod.ts +1 -1
  627. package/src/pyth/types.ts +0 -2
  628. package/src/slot/SlotSubscriber.ts +3 -3
  629. package/src/slot/SlothashSubscriber.ts +17 -6
  630. package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +5 -6
  631. package/src/swift/signedMsgUserAccountSubscriber.ts +16 -8
  632. package/src/swift/swiftOrderSubscriber.ts +10 -9
  633. package/src/testClient.ts +2 -2
  634. package/src/tokenFaucet.ts +22 -3
  635. package/src/tx/baseTxSender.ts +10 -10
  636. package/src/tx/fastSingleTxSender.ts +12 -8
  637. package/src/tx/reportTransactionError.ts +12 -7
  638. package/src/tx/retryTxSender.ts +6 -4
  639. package/src/tx/txHandler.ts +88 -43
  640. package/src/tx/txParamProcessor.ts +18 -3
  641. package/src/tx/types.ts +1 -1
  642. package/src/tx/whileValidTxSender.ts +51 -22
  643. package/src/types.ts +39 -75
  644. package/src/user.ts +200 -152
  645. package/src/userMap/WebsocketSubscription.ts +1 -1
  646. package/src/userMap/grpcSubscription.ts +1 -1
  647. package/src/userMap/referrerMap.ts +13 -9
  648. package/src/userMap/revenueShareEscrowMap.ts +6 -1
  649. package/src/userMap/userMap.ts +43 -25
  650. package/src/userMap/userStatsMap.ts +32 -19
  651. package/src/userStats.ts +21 -8
  652. package/src/util/TransactionConfirmationManager.ts +10 -2
  653. package/src/util/chainClock.ts +1 -1
  654. package/src/util/computeUnits.ts +7 -3
  655. package/src/velocityClient.ts +536 -383
  656. package/tests/amm/test.ts +138 -0
  657. package/tests/dlob/helpers.ts +6 -1
  658. package/tsconfig.json +1 -0
  659. package/lib/browser/constants/insuranceFund.d.ts +0 -5
  660. package/lib/browser/constants/insuranceFund.js +0 -9
  661. package/lib/browser/tx/forwardOnlyTxSender.d.ts +0 -37
  662. package/lib/browser/tx/forwardOnlyTxSender.js +0 -92
  663. package/lib/browser/util/tps.d.ts +0 -2
  664. package/lib/browser/util/tps.js +0 -16
  665. package/lib/node/constants/insuranceFund.d.ts +0 -6
  666. package/lib/node/constants/insuranceFund.d.ts.map +0 -1
  667. package/lib/node/constants/insuranceFund.js +0 -9
  668. package/lib/node/tx/forwardOnlyTxSender.d.ts +0 -38
  669. package/lib/node/tx/forwardOnlyTxSender.d.ts.map +0 -1
  670. package/lib/node/tx/forwardOnlyTxSender.js +0 -92
  671. package/lib/node/util/tps.d.ts +0 -3
  672. package/lib/node/util/tps.d.ts.map +0 -1
  673. package/lib/node/util/tps.js +0 -16
  674. package/src/constants/insuranceFund.ts +0 -8
  675. package/src/tx/forwardOnlyTxSender.ts +0 -145
  676. package/src/util/tps.ts +0 -27
@@ -90,7 +90,7 @@ class VelocityClient {
90
90
  this._isSubscribed = val;
91
91
  }
92
92
  constructor(config) {
93
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25;
93
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26;
94
94
  this.users = new Map();
95
95
  this._isSubscribed = false;
96
96
  this.perpMarketLastSlotCache = new Map();
@@ -197,34 +197,28 @@ class VelocityClient {
197
197
  accountSubscription: this.userAccountSubscriptionConfig,
198
198
  });
199
199
  }
200
- this.marketLookupTable = config.marketLookupTable;
201
- if (!this.marketLookupTable) {
202
- this.marketLookupTable = new web3_js_1.PublicKey(config_1.configs[this.env].MARKET_LOOKUP_TABLE);
203
- }
204
- this.marketLookupTables = config.marketLookupTables;
205
- if (!this.marketLookupTables) {
206
- this.marketLookupTables = config_1.configs[this.env].MARKET_LOOKUP_TABLES.map((tableAddr) => new web3_js_1.PublicKey(tableAddr));
207
- }
200
+ this.marketLookupTables =
201
+ (_5 = config.marketLookupTables) !== null && _5 !== void 0 ? _5 : config_1.configs[this.env].MARKET_LOOKUP_TABLES.map((tableAddr) => new web3_js_1.PublicKey(tableAddr));
208
202
  const delistedMarketSetting = config.delistedMarketSetting || types_2.DelistedMarketSetting.Unsubscribe;
209
203
  const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
210
204
  config.spotMarketIndexes === undefined &&
211
205
  config.oracleInfos === undefined;
212
- if (((_5 = config.accountSubscription) === null || _5 === void 0 ? void 0 : _5.type) === 'polling') {
213
- this.accountSubscriber = new pollingVelocityClientAccountSubscriber_1.PollingVelocityClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_6 = config.perpMarketIndexes) !== null && _6 !== void 0 ? _6 : [], (_7 = config.spotMarketIndexes) !== null && _7 !== void 0 ? _7 : [], (_8 = config.oracleInfos) !== null && _8 !== void 0 ? _8 : [], noMarketsAndOraclesSpecified, delistedMarketSetting);
214
- }
215
- else if (((_9 = config.accountSubscription) === null || _9 === void 0 ? void 0 : _9.type) === 'grpc') {
216
- const accountSubscriberClass = (_11 = (_10 = config.accountSubscription) === null || _10 === void 0 ? void 0 : _10.velocityClientAccountSubscriber) !== null && _11 !== void 0 ? _11 : grpcVelocityClientAccountSubscriber_1.grpcVelocityClientAccountSubscriber;
217
- this.accountSubscriber = new accountSubscriberClass(config.accountSubscription.grpcConfigs, this.program, (_12 = config.perpMarketIndexes) !== null && _12 !== void 0 ? _12 : [], (_13 = config.spotMarketIndexes) !== null && _13 !== void 0 ? _13 : [], (_14 = config.oracleInfos) !== null && _14 !== void 0 ? _14 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
218
- resubTimeoutMs: (_15 = config.accountSubscription) === null || _15 === void 0 ? void 0 : _15.resubTimeoutMs,
219
- logResubMessages: (_16 = config.accountSubscription) === null || _16 === void 0 ? void 0 : _16.logResubMessages,
206
+ if (((_6 = config.accountSubscription) === null || _6 === void 0 ? void 0 : _6.type) === 'polling') {
207
+ this.accountSubscriber = new pollingVelocityClientAccountSubscriber_1.PollingVelocityClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_7 = config.perpMarketIndexes) !== null && _7 !== void 0 ? _7 : [], (_8 = config.spotMarketIndexes) !== null && _8 !== void 0 ? _8 : [], (_9 = config.oracleInfos) !== null && _9 !== void 0 ? _9 : [], noMarketsAndOraclesSpecified, delistedMarketSetting);
208
+ }
209
+ else if (((_10 = config.accountSubscription) === null || _10 === void 0 ? void 0 : _10.type) === 'grpc') {
210
+ const accountSubscriberClass = (_12 = (_11 = config.accountSubscription) === null || _11 === void 0 ? void 0 : _11.velocityClientAccountSubscriber) !== null && _12 !== void 0 ? _12 : grpcVelocityClientAccountSubscriber_1.grpcVelocityClientAccountSubscriber;
211
+ this.accountSubscriber = new accountSubscriberClass(config.accountSubscription.grpcConfigs, this.program, (_13 = config.perpMarketIndexes) !== null && _13 !== void 0 ? _13 : [], (_14 = config.spotMarketIndexes) !== null && _14 !== void 0 ? _14 : [], (_15 = config.oracleInfos) !== null && _15 !== void 0 ? _15 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
212
+ resubTimeoutMs: (_16 = config.accountSubscription) === null || _16 === void 0 ? void 0 : _16.resubTimeoutMs,
213
+ logResubMessages: (_17 = config.accountSubscription) === null || _17 === void 0 ? void 0 : _17.logResubMessages,
220
214
  });
221
215
  }
222
216
  else {
223
- const accountSubscriberClass = (_18 = (_17 = config.accountSubscription) === null || _17 === void 0 ? void 0 : _17.velocityClientAccountSubscriber) !== null && _18 !== void 0 ? _18 : webSocketVelocityClientAccountSubscriber_1.WebSocketVelocityClientAccountSubscriber;
224
- this.accountSubscriber = new accountSubscriberClass(this.program, (_19 = config.perpMarketIndexes) !== null && _19 !== void 0 ? _19 : [], (_20 = config.spotMarketIndexes) !== null && _20 !== void 0 ? _20 : [], (_21 = config.oracleInfos) !== null && _21 !== void 0 ? _21 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
225
- resubTimeoutMs: (_22 = config.accountSubscription) === null || _22 === void 0 ? void 0 : _22.resubTimeoutMs,
226
- logResubMessages: (_23 = config.accountSubscription) === null || _23 === void 0 ? void 0 : _23.logResubMessages,
227
- }, (_24 = config.accountSubscription) === null || _24 === void 0 ? void 0 : _24.commitment);
217
+ const accountSubscriberClass = (_19 = (_18 = config.accountSubscription) === null || _18 === void 0 ? void 0 : _18.velocityClientAccountSubscriber) !== null && _19 !== void 0 ? _19 : webSocketVelocityClientAccountSubscriber_1.WebSocketVelocityClientAccountSubscriber;
218
+ this.accountSubscriber = new accountSubscriberClass(this.program, (_20 = config.perpMarketIndexes) !== null && _20 !== void 0 ? _20 : [], (_21 = config.spotMarketIndexes) !== null && _21 !== void 0 ? _21 : [], (_22 = config.oracleInfos) !== null && _22 !== void 0 ? _22 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
219
+ resubTimeoutMs: (_23 = config.accountSubscription) === null || _23 === void 0 ? void 0 : _23.resubTimeoutMs,
220
+ logResubMessages: (_24 = config.accountSubscription) === null || _24 === void 0 ? void 0 : _24.logResubMessages,
221
+ }, (_25 = config.accountSubscription) === null || _25 === void 0 ? void 0 : _25.commitment);
228
222
  }
229
223
  this.eventEmitter = this.accountSubscriber.eventEmitter;
230
224
  this.metricsEventEmitter = new events_1.EventEmitter();
@@ -232,7 +226,7 @@ class VelocityClient {
232
226
  this.enableMetricsEvents = true;
233
227
  }
234
228
  this.txSender =
235
- (_25 = config.txSender) !== null && _25 !== void 0 ? _25 : new retryTxSender_1.RetryTxSender({
229
+ (_26 = config.txSender) !== null && _26 !== void 0 ? _26 : new retryTxSender_1.RetryTxSender({
236
230
  connection: this.connection,
237
231
  wallet: this.wallet,
238
232
  opts: this.opts,
@@ -312,6 +306,17 @@ class VelocityClient {
312
306
  var _a;
313
307
  return (_a = this.accountSubscriber.getMarketAccountAndSlot(marketIndex)) === null || _a === void 0 ? void 0 : _a.data;
314
308
  }
309
+ /**
310
+ * Like {@link getPerpMarketAccount} but throws if the market is not loaded,
311
+ * for call sites that require a guaranteed account.
312
+ */
313
+ getPerpMarketAccountOrThrow(marketIndex) {
314
+ const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
315
+ if (!perpMarketAccount) {
316
+ throw new Error(`Perp market ${marketIndex} not found`);
317
+ }
318
+ return perpMarketAccount;
319
+ }
315
320
  /**
316
321
  * Forces a fetch to rpc before returning accounts. Useful for anchor tests.
317
322
  * @param marketIndex
@@ -335,15 +340,28 @@ class VelocityClient {
335
340
  .map((value) => value.data);
336
341
  }
337
342
  getSpotMarketAccount(marketIndex) {
338
- return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
343
+ var _a;
344
+ return (_a = this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)) === null || _a === void 0 ? void 0 : _a.data;
345
+ }
346
+ /**
347
+ * Like {@link getSpotMarketAccount} but throws if the market is not loaded,
348
+ * for call sites that require a guaranteed account.
349
+ */
350
+ getSpotMarketAccountOrThrow(marketIndex) {
351
+ const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
352
+ if (!spotMarketAccount) {
353
+ throw new Error(`Spot market ${marketIndex} not found`);
354
+ }
355
+ return spotMarketAccount;
339
356
  }
340
357
  /**
341
358
  * Forces a fetch to rpc before returning accounts. Useful for anchor tests.
342
359
  * @param marketIndex
343
360
  */
344
361
  async forceGetSpotMarketAccount(marketIndex) {
362
+ var _a;
345
363
  await this.accountSubscriber.fetch();
346
- return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
364
+ return (_a = this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)) === null || _a === void 0 ? void 0 : _a.data;
347
365
  }
348
366
  getSpotMarketAccounts() {
349
367
  return this.accountSubscriber
@@ -352,23 +370,11 @@ class VelocityClient {
352
370
  .map((value) => value.data);
353
371
  }
354
372
  getQuoteSpotMarketAccount() {
355
- return this.accountSubscriber.getSpotMarketAccountAndSlot(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).data;
373
+ return this.getSpotMarketAccountOrThrow(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
356
374
  }
357
375
  getOraclePriceDataAndSlot(oraclePublicKey, oracleSource) {
358
376
  return this.accountSubscriber.getOraclePriceDataAndSlot((0, oracleId_1.getOracleId)(oraclePublicKey, oracleSource));
359
377
  }
360
- /** @deprecated use fetchAllLookupTableAccounts() */
361
- async fetchMarketLookupTableAccount() {
362
- if (this.lookupTableAccount)
363
- return this.lookupTableAccount;
364
- if (!this.marketLookupTable) {
365
- console.log('Market lookup table address not set');
366
- return;
367
- }
368
- const lookupTableAccount = (await this.connection.getAddressLookupTable(this.marketLookupTable)).value;
369
- this.lookupTableAccount = lookupTableAccount;
370
- return lookupTableAccount;
371
- }
372
378
  async fetchAllLookupTableAccounts() {
373
379
  if (this.lookupTableAccounts)
374
380
  return this.lookupTableAccounts;
@@ -493,7 +499,8 @@ class VelocityClient {
493
499
  const authorityChanged = authority && !((_a = this.authority) === null || _a === void 0 ? void 0 : _a.equals(authority));
494
500
  this.activeSubAccountId = subAccountId;
495
501
  this.authority = authority !== null && authority !== void 0 ? authority : this.authority;
496
- this.userStatsAccountPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.authority);
502
+ const userStatsAccountPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.authority);
503
+ this.userStatsAccountPublicKey = userStatsAccountPublicKey;
497
504
  /* If changing the user authority ie switching from delegate to non-delegate account, need to re-subscribe to the user stats account */
498
505
  if (authorityChanged && this.userStats) {
499
506
  if (this.userStats.isSubscribed) {
@@ -501,16 +508,17 @@ class VelocityClient {
501
508
  }
502
509
  this.userStats = new userStats_1.UserStats({
503
510
  velocityClient: this,
504
- userStatsAccountPublicKey: this.userStatsAccountPublicKey,
511
+ userStatsAccountPublicKey: userStatsAccountPublicKey,
505
512
  accountSubscription: this.userStatsAccountSubscriptionConfig,
506
513
  });
507
514
  this.userStats.subscribe();
508
515
  }
509
516
  }
510
517
  async addUser(subAccountId, authority, userAccount) {
518
+ var _a;
511
519
  authority = authority !== null && authority !== void 0 ? authority : this.authority;
512
520
  const userKey = this.getUserMapKey(subAccountId, authority);
513
- if (this.users.has(userKey) && this.users.get(userKey).isSubscribed) {
521
+ if (this.users.has(userKey) && ((_a = this.users.get(userKey)) === null || _a === void 0 ? void 0 : _a.isSubscribed)) {
514
522
  return true;
515
523
  }
516
524
  const user = this.createUser(subAccountId, this.userAccountSubscriptionConfig, authority);
@@ -887,7 +895,16 @@ class VelocityClient {
887
895
  userStatsAccount = await (0, fetch_1.fetchUserStatsAccount)(this.connection, this.program, this.wallet.publicKey);
888
896
  }
889
897
  else {
890
- userStatsAccount = userStats.getAccount();
898
+ const account = userStats.getAccount();
899
+ if (!account) {
900
+ userStatsAccount = await (0, fetch_1.fetchUserStatsAccount)(this.connection, this.program, this.wallet.publicKey);
901
+ }
902
+ else {
903
+ userStatsAccount = account;
904
+ }
905
+ }
906
+ if (!userStatsAccount) {
907
+ throw new Error('UserStats account does not exist');
891
908
  }
892
909
  return userStatsAccount.numberOfSubAccountsCreated;
893
910
  }
@@ -969,7 +986,7 @@ class VelocityClient {
969
986
  let remainingAccounts;
970
987
  try {
971
988
  remainingAccounts = this.getRemainingAccounts({
972
- userAccounts: [this.getUserAccount(subAccountId)],
989
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
973
990
  });
974
991
  }
975
992
  catch (err) {
@@ -1201,7 +1218,7 @@ class VelocityClient {
1201
1218
  if ((0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
1202
1219
  continue;
1203
1220
  }
1204
- const spotMarket = this.getSpotMarketAccount(spotPosition.marketIndex);
1221
+ const spotMarket = this.getSpotMarketAccountOrThrow(spotPosition.marketIndex);
1205
1222
  remainingAccounts.push({
1206
1223
  isSigner: false,
1207
1224
  isWritable: true,
@@ -1287,27 +1304,44 @@ class VelocityClient {
1287
1304
  getUser(subAccountId, authority) {
1288
1305
  subAccountId = subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId;
1289
1306
  authority = authority !== null && authority !== void 0 ? authority : this.authority;
1307
+ if (subAccountId === undefined || authority === undefined) {
1308
+ throw new Error('Subaccount ID and authority are required');
1309
+ }
1290
1310
  const userMapKey = this.getUserMapKey(subAccountId, authority);
1291
- if (!this.users.has(userMapKey)) {
1311
+ const user = this.users.get(userMapKey);
1312
+ if (!user) {
1292
1313
  throw new Error(`VelocityClient has no user for user id ${userMapKey}`);
1293
1314
  }
1294
- return this.users.get(userMapKey);
1315
+ return user;
1295
1316
  }
1296
1317
  hasUser(subAccountId, authority) {
1297
1318
  subAccountId = subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId;
1298
1319
  authority = authority !== null && authority !== void 0 ? authority : this.authority;
1320
+ if (subAccountId === undefined || authority === undefined) {
1321
+ throw new Error('Subaccount ID and authority are required');
1322
+ }
1299
1323
  const userMapKey = this.getUserMapKey(subAccountId, authority);
1300
1324
  return this.users.has(userMapKey);
1301
1325
  }
1302
1326
  getUsers() {
1303
1327
  // delegate users get added to the end
1304
1328
  return [...this.users.values()]
1305
- .filter((acct) => acct.getUserAccount().authority.equals(this.wallet.publicKey))
1306
- .concat([...this.users.values()].filter((acct) => !acct.getUserAccount().authority.equals(this.wallet.publicKey)));
1329
+ .filter((acct) => { var _a; return (_a = acct.getUserAccount()) === null || _a === void 0 ? void 0 : _a.authority.equals(this.wallet.publicKey); })
1330
+ .concat([...this.users.values()].filter((acct) => { var _a; return !((_a = acct.getUserAccount()) === null || _a === void 0 ? void 0 : _a.authority.equals(this.wallet.publicKey)); }));
1307
1331
  }
1308
1332
  getUserStats() {
1309
1333
  return this.userStats;
1310
1334
  }
1335
+ /**
1336
+ * Like {@link getUserStats} but throws if there is no UserStats
1337
+ * subscription, for call sites that require a guaranteed account.
1338
+ */
1339
+ getUserStatsOrThrow() {
1340
+ if (!this.userStats) {
1341
+ throw new Error('VelocityClient has no UserStats subscription');
1342
+ }
1343
+ return this.userStats;
1344
+ }
1311
1345
  async fetchReferrerNameAccount(name) {
1312
1346
  const nameBuffer = (0, userName_1.encodeName)(name);
1313
1347
  const referrerNameAccountPublicKey = (0, pda_1.getReferrerNamePublicKeySync)(this.program.programId, nameBuffer);
@@ -1326,6 +1360,14 @@ class VelocityClient {
1326
1360
  getUserAccount(subAccountId, authority) {
1327
1361
  return this.getUser(subAccountId, authority).getUserAccount();
1328
1362
  }
1363
+ /**
1364
+ * Like {@link getUserAccount} but throws a named error instead of returning
1365
+ * `undefined` when the account has not been loaded yet. Use at call sites
1366
+ * that structurally require a loaded account.
1367
+ */
1368
+ getUserAccountOrThrow(subAccountId, authority) {
1369
+ return this.getUser(subAccountId, authority).getUserAccountOrThrow();
1370
+ }
1329
1371
  /**
1330
1372
  * Forces a fetch to rpc before returning accounts. Useful for anchor tests.
1331
1373
  * @param subAccountId
@@ -1338,7 +1380,7 @@ class VelocityClient {
1338
1380
  return this.getUser(subAccountId, authority).getUserAccountAndSlot();
1339
1381
  }
1340
1382
  getSpotPosition(marketIndex, subAccountId) {
1341
- return this.getUserAccount(subAccountId).spotPositions.find((spotPosition) => spotPosition.marketIndex === marketIndex);
1383
+ return this.getUserAccountOrThrow(subAccountId).spotPositions.find((spotPosition) => spotPosition.marketIndex === marketIndex);
1342
1384
  }
1343
1385
  getQuoteAssetTokenAmount() {
1344
1386
  return this.getTokenAmount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
@@ -1356,7 +1398,7 @@ class VelocityClient {
1356
1398
  if (spotPosition === undefined) {
1357
1399
  return numericConstants_1.ZERO;
1358
1400
  }
1359
- const spotMarket = this.getSpotMarketAccount(marketIndex);
1401
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
1360
1402
  return (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarket, spotPosition.balanceType), spotPosition.balanceType);
1361
1403
  }
1362
1404
  /**
@@ -1365,7 +1407,7 @@ class VelocityClient {
1365
1407
  * @param amount
1366
1408
  */
1367
1409
  convertToSpotPrecision(marketIndex, amount) {
1368
- const spotMarket = this.getSpotMarketAccount(marketIndex);
1410
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
1369
1411
  return (0, spotMarket_1.castNumberToSpotPrecision)(amount, spotMarket);
1370
1412
  }
1371
1413
  /**
@@ -1407,11 +1449,41 @@ class VelocityClient {
1407
1449
  this.mustIncludeSpotMarketIndexes.add(spotMarketIndex);
1408
1450
  });
1409
1451
  }
1452
+ cachePerpMarketSlot(slot, ...marketIndexes) {
1453
+ for (const marketIndex of marketIndexes) {
1454
+ if (slot !== undefined) {
1455
+ this.perpMarketLastSlotCache.set(marketIndex, slot);
1456
+ }
1457
+ else {
1458
+ this.perpMarketLastSlotCache.delete(marketIndex);
1459
+ }
1460
+ }
1461
+ }
1462
+ cacheSpotMarketSlot(slot, ...marketIndexes) {
1463
+ for (const marketIndex of marketIndexes) {
1464
+ if (slot !== undefined) {
1465
+ this.spotMarketLastSlotCache.set(marketIndex, slot);
1466
+ }
1467
+ else {
1468
+ this.spotMarketLastSlotCache.delete(marketIndex);
1469
+ }
1470
+ }
1471
+ }
1410
1472
  getRemainingAccounts(params) {
1411
- return VelocityCore_1.VelocityCore.remainingAccounts.getRemainingAccounts(this, params);
1473
+ return VelocityCore_1.VelocityCore.remainingAccounts.getRemainingAccounts({
1474
+ getPerpMarketAccount: (marketIndex) => this.getPerpMarketAccountOrThrow(marketIndex),
1475
+ getSpotMarketAccount: (marketIndex) => this.getSpotMarketAccountOrThrow(marketIndex),
1476
+ getUserAccountAndSlot: (subAccountId, authority) => this.getUserAccountAndSlot(subAccountId, authority),
1477
+ activeSubAccountId: this.activeSubAccountId,
1478
+ authority: this.authority,
1479
+ perpMarketLastSlotCache: this.perpMarketLastSlotCache,
1480
+ spotMarketLastSlotCache: this.spotMarketLastSlotCache,
1481
+ mustIncludePerpMarketIndexes: this.mustIncludePerpMarketIndexes,
1482
+ mustIncludeSpotMarketIndexes: this.mustIncludeSpotMarketIndexes,
1483
+ }, params);
1412
1484
  }
1413
1485
  addPerpMarketToRemainingAccountMaps(marketIndex, writable, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap) {
1414
- const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
1486
+ const perpMarketAccount = this.getPerpMarketAccountOrThrow(marketIndex);
1415
1487
  perpMarketAccountMap.set(marketIndex, {
1416
1488
  pubkey: perpMarketAccount.pubkey,
1417
1489
  isSigner: false,
@@ -1426,7 +1498,7 @@ class VelocityClient {
1426
1498
  this.addSpotMarketToRemainingAccountMaps(perpMarketAccount.quoteSpotMarketIndex, false, oracleAccountMap, spotMarketAccountMap);
1427
1499
  }
1428
1500
  addSpotMarketToRemainingAccountMaps(marketIndex, writable, oracleAccountMap, spotMarketAccountMap) {
1429
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1501
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
1430
1502
  spotMarketAccountMap.set(spotMarketAccount.marketIndex, {
1431
1503
  pubkey: spotMarketAccount.pubkey,
1432
1504
  isSigner: false,
@@ -1491,10 +1563,32 @@ class VelocityClient {
1491
1563
  perpMarketAccountMap,
1492
1564
  };
1493
1565
  }
1566
+ /**
1567
+ * Look up an open order by its program-assigned order ID from the cached user account.
1568
+ *
1569
+ * `orderId` is the monotonically incrementing u32 counter that the program assigns at
1570
+ * placement time — it is not known until the place instruction executes on-chain. Use
1571
+ * {@link getOrderByUserId} when you need to look up an order by the caller-supplied
1572
+ * `userOrderId` instead.
1573
+ *
1574
+ * Returns `undefined` when the order is not found (already filled, cancelled, or the
1575
+ * account cache is stale).
1576
+ */
1494
1577
  getOrder(orderId, subAccountId) {
1495
1578
  var _a;
1496
1579
  return (_a = this.getUserAccount(subAccountId)) === null || _a === void 0 ? void 0 : _a.orders.find((order) => order.orderId === orderId);
1497
1580
  }
1581
+ /**
1582
+ * Look up an open order by the caller-supplied `userOrderId` from the cached user account.
1583
+ *
1584
+ * `userOrderId` is a 1-255 slot chosen by the caller in {@link OrderParams} and is stable
1585
+ * across the life of the order — useful when you need to reference an order before the
1586
+ * program-assigned {@link Order.orderId} is known (e.g. immediately after placing without
1587
+ * waiting for confirmation). Use {@link getOrder} when you have the program-assigned ID.
1588
+ *
1589
+ * Returns `undefined` when the order is not found (already filled, cancelled, or the
1590
+ * account cache is stale).
1591
+ */
1498
1592
  getOrderByUserId(userOrderId, subAccountId) {
1499
1593
  var _a;
1500
1594
  return (_a = this.getUserAccount(subAccountId)) === null || _a === void 0 ? void 0 : _a.orders.find((order) => order.userOrderId === userOrderId);
@@ -1506,7 +1600,7 @@ class VelocityClient {
1506
1600
  * @param tokenProgram
1507
1601
  */
1508
1602
  async getAssociatedTokenAccount(marketIndex, useNative = true, tokenProgram = spl_token_1.TOKEN_PROGRAM_ID, authority = this.wallet.publicKey, allowOwnerOffCurve = false) {
1509
- const spotMarket = this.getSpotMarketAccount(marketIndex);
1603
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
1510
1604
  if (useNative && spotMarket.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT)) {
1511
1605
  return authority;
1512
1606
  }
@@ -1533,7 +1627,7 @@ class VelocityClient {
1533
1627
  }
1534
1628
  async getDepositTxnIx(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly = false, overrides) {
1535
1629
  var _a;
1536
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1630
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
1537
1631
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
1538
1632
  const signer = (_a = overrides === null || overrides === void 0 ? void 0 : overrides.authority) !== null && _a !== void 0 ? _a : this.wallet.publicKey;
1539
1633
  const createWSOLTokenAccount = isSolMarket && associatedTokenAccount.equals(signer);
@@ -1594,7 +1688,7 @@ class VelocityClient {
1594
1688
  async deposit(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly = false, txParams, initSwiftAccount = false, overrides) {
1595
1689
  const tx = await this.createDepositTxn(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly, txParams, initSwiftAccount, overrides);
1596
1690
  const { txSig, slot } = await this.sendTransaction(tx, [], this.opts);
1597
- this.spotMarketLastSlotCache.set(marketIndex, slot);
1691
+ this.cacheSpotMarketSlot(slot, marketIndex);
1598
1692
  return txSig;
1599
1693
  }
1600
1694
  async getDepositInstruction(amount, marketIndex, userTokenAccount, subAccountId, reduceOnly = false, userInitialized = true, overrides) {
@@ -1602,8 +1696,12 @@ class VelocityClient {
1602
1696
  const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId);
1603
1697
  let remainingAccounts = [];
1604
1698
  if (userInitialized) {
1699
+ const userAccount = await this.forceGetUserAccount(subAccountId);
1700
+ if (!userAccount) {
1701
+ throw new Error('User account not loaded after force fetch');
1702
+ }
1605
1703
  remainingAccounts = this.getRemainingAccounts({
1606
- userAccounts: [await this.forceGetUserAccount(subAccountId)],
1704
+ userAccounts: [userAccount],
1607
1705
  useMarketLastSlotCache: true,
1608
1706
  writableSpotMarketIndexes: [marketIndex],
1609
1707
  });
@@ -1614,7 +1712,7 @@ class VelocityClient {
1614
1712
  writableSpotMarketIndexes: [marketIndex],
1615
1713
  });
1616
1714
  }
1617
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1715
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
1618
1716
  this.addTokenMintToRemainingAccounts(spotMarketAccount, remainingAccounts);
1619
1717
  if (this.isTransferHook(spotMarketAccount)) {
1620
1718
  await this.addExtraAccountMetasToRemainingAccounts(spotMarketAccount.mint, remainingAccounts);
@@ -1703,7 +1801,11 @@ class VelocityClient {
1703
1801
  return;
1704
1802
  }
1705
1803
  const extraAccountMetasAddress = (0, spl_token_1.getExtraAccountMetaAddress)(mint, hookAccount.programId);
1706
- const extraAccountMetas = (0, spl_token_1.getExtraAccountMetas)(await this.connection.getAccountInfo(extraAccountMetasAddress));
1804
+ const extraAccountMetasAccount = await this.connection.getAccountInfo(extraAccountMetasAddress);
1805
+ if (!extraAccountMetasAccount) {
1806
+ throw new Error('Extra account metas account not found for transfer hook');
1807
+ }
1808
+ const extraAccountMetas = (0, spl_token_1.getExtraAccountMetas)(extraAccountMetasAccount);
1707
1809
  for (const acc of extraAccountMetas) {
1708
1810
  // assuming it's an extra account meta that does not rely on ix data
1709
1811
  const resolvedAcc = await (0, spl_token_1.resolveExtraAccountMeta)(this.connection, acc, remainingAccounts, Buffer.from([]), hookAccount.programId);
@@ -1733,7 +1835,7 @@ class VelocityClient {
1733
1835
  const [, initializeSignedMsgUserOrdersAccountIx] = await this.getInitializeSignedMsgUserOrdersAccountIx(this.authority, 8, overrides);
1734
1836
  ixs.push(initializeSignedMsgUserOrdersAccountIx);
1735
1837
  }
1736
- const spotMarket = this.getSpotMarketAccount(marketIndex);
1838
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
1737
1839
  const isSolMarket = spotMarket.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
1738
1840
  // Use external wallet for deposit source if provided, otherwise use the wallet
1739
1841
  const depositSource = (_a = overrides === null || overrides === void 0 ? void 0 : overrides.externalWallet) !== null && _a !== void 0 ? _a : this.wallet.publicKey;
@@ -1784,6 +1886,9 @@ class VelocityClient {
1784
1886
  }
1785
1887
  ixs.push(depositCollateralIx);
1786
1888
  if (!donateAmount.eq(numericConstants_1.ZERO)) {
1889
+ if (!wsolTokenAccount) {
1890
+ throw new Error('wsolTokenAccount is required to donate to rev pool');
1891
+ }
1787
1892
  const donateIx = await this.getDepositIntoSpotMarketRevenuePoolIx(1, donateAmount, wsolTokenAccount);
1788
1893
  ixs.push(donateIx);
1789
1894
  }
@@ -1795,6 +1900,9 @@ class VelocityClient {
1795
1900
  // Close the wrapped sol account at the end of the transaction
1796
1901
  // Return funds to the deposit source (external wallet if provided)
1797
1902
  if (createWSOLTokenAccount) {
1903
+ if (!wsolTokenAccount) {
1904
+ throw new Error('wsolTokenAccount was not created');
1905
+ }
1798
1906
  ixs.push((0, spl_token_1.createCloseAccountInstruction)(wsolTokenAccount, depositSource, depositSource, []));
1799
1907
  }
1800
1908
  return {
@@ -1827,7 +1935,7 @@ class VelocityClient {
1827
1935
  const [tx, userAccountPublicKey] = await this.createInitializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex, subAccountId, name, fromSubAccountId, referrerInfo, donateAmount, txParams, customMaxMarginRatio, poolId, overrides);
1828
1936
  const additionalSigners = [];
1829
1937
  const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
1830
- this.spotMarketLastSlotCache.set(marketIndex, slot);
1938
+ this.cacheSpotMarketSlot(slot, marketIndex);
1831
1939
  await this.addUser(subAccountId);
1832
1940
  return [txSig, userAccountPublicKey];
1833
1941
  }
@@ -1850,7 +1958,7 @@ class VelocityClient {
1850
1958
  }
1851
1959
  async getWithdrawalIxs(amount, marketIndex, associatedTokenAddress, reduceOnly = false, subAccountId) {
1852
1960
  const withdrawIxs = [];
1853
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1961
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
1854
1962
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
1855
1963
  const authority = this.wallet.publicKey;
1856
1964
  const createWSOLTokenAccount = isSolMarket && associatedTokenAddress.equals(authority);
@@ -1886,7 +1994,7 @@ class VelocityClient {
1886
1994
  const withdrawIxs = await this.getWithdrawalIxs(amount, marketIndex, associatedTokenAddress, reduceOnly, subAccountId);
1887
1995
  const tx = await this.buildTransaction(withdrawIxs, txParams !== null && txParams !== void 0 ? txParams : this.txParams);
1888
1996
  const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
1889
- this.spotMarketLastSlotCache.set(marketIndex, slot);
1997
+ this.cacheSpotMarketSlot(slot, marketIndex);
1890
1998
  return txSig;
1891
1999
  }
1892
2000
  async withdrawAllDustPositions(subAccountId, txParams, opts) {
@@ -1915,12 +2023,12 @@ class VelocityClient {
1915
2023
  async getWithdrawIx(amount, marketIndex, userTokenAccount, reduceOnly = false, subAccountId) {
1916
2024
  const user = await this.getUserAccountPublicKey(subAccountId);
1917
2025
  const remainingAccounts = this.getRemainingAccounts({
1918
- userAccounts: [this.getUserAccount(subAccountId)],
2026
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
1919
2027
  useMarketLastSlotCache: true,
1920
2028
  writableSpotMarketIndexes: [marketIndex],
1921
2029
  readableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
1922
2030
  });
1923
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
2031
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
1924
2032
  this.addTokenMintToRemainingAccounts(spotMarketAccount, remainingAccounts);
1925
2033
  if (this.isTransferHook(spotMarketAccount)) {
1926
2034
  await this.addExtraAccountMetasToRemainingAccounts(spotMarketAccount.mint, remainingAccounts);
@@ -1955,7 +2063,7 @@ class VelocityClient {
1955
2063
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getTransferDepositIx(amount, marketIndex, fromSubAccountId, toSubAccountId), txParams), [], this.opts);
1956
2064
  if (fromSubAccountId === this.activeSubAccountId ||
1957
2065
  toSubAccountId === this.activeSubAccountId) {
1958
- this.spotMarketLastSlotCache.set(marketIndex, slot);
2066
+ this.cacheSpotMarketSlot(slot, marketIndex);
1959
2067
  }
1960
2068
  return txSig;
1961
2069
  }
@@ -1964,9 +2072,10 @@ class VelocityClient {
1964
2072
  const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, toSubAccountId);
1965
2073
  let remainingAccounts;
1966
2074
  const userMapKey = this.getUserMapKey(fromSubAccountId, this.wallet.publicKey);
1967
- if (this.users.has(userMapKey)) {
2075
+ const mapUser = this.users.get(userMapKey);
2076
+ if (mapUser) {
1968
2077
  remainingAccounts = this.getRemainingAccounts({
1969
- userAccounts: [this.users.get(userMapKey).getUserAccount()],
2078
+ userAccounts: [mapUser.getUserAccountOrThrow()],
1970
2079
  useMarketLastSlotCache: true,
1971
2080
  writableSpotMarketIndexes: [marketIndex],
1972
2081
  });
@@ -1987,7 +2096,7 @@ class VelocityClient {
1987
2096
  toUser,
1988
2097
  userStats: this.getUserStatsAccountPublicKey(),
1989
2098
  state: await this.getStatePublicKey(),
1990
- spotMarketVault: this.getSpotMarketAccount(marketIndex).vault,
2099
+ spotMarketVault: this.getSpotMarketAccountOrThrow(marketIndex).vault,
1991
2100
  },
1992
2101
  remainingAccounts,
1993
2102
  });
@@ -1996,7 +2105,7 @@ class VelocityClient {
1996
2105
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getTransferDepositByDelegateIx(amount, marketIndex, fromSubAccountId, toSubAccountId), txParams), [], this.opts);
1997
2106
  if (fromSubAccountId === this.activeSubAccountId ||
1998
2107
  toSubAccountId === this.activeSubAccountId) {
1999
- this.spotMarketLastSlotCache.set(marketIndex, slot);
2108
+ this.cacheSpotMarketSlot(slot, marketIndex);
2000
2109
  }
2001
2110
  return txSig;
2002
2111
  }
@@ -2005,9 +2114,10 @@ class VelocityClient {
2005
2114
  const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, toSubAccountId);
2006
2115
  let remainingAccounts;
2007
2116
  const userMapKey = this.getUserMapKey(fromSubAccountId, this.authority);
2008
- if (this.users.has(userMapKey)) {
2117
+ const mapUser = this.users.get(userMapKey);
2118
+ if (mapUser) {
2009
2119
  remainingAccounts = this.getRemainingAccounts({
2010
- userAccounts: [this.users.get(userMapKey).getUserAccount()],
2120
+ userAccounts: [mapUser.getUserAccountOrThrow()],
2011
2121
  useMarketLastSlotCache: true,
2012
2122
  writableSpotMarketIndexes: [marketIndex],
2013
2123
  });
@@ -2027,7 +2137,7 @@ class VelocityClient {
2027
2137
  toUser,
2028
2138
  userStats: this.getUserStatsAccountPublicKey(),
2029
2139
  state: await this.getStatePublicKey(),
2030
- spotMarketVault: this.getSpotMarketAccount(marketIndex).vault,
2140
+ spotMarketVault: this.getSpotMarketAccountOrThrow(marketIndex).vault,
2031
2141
  },
2032
2142
  remainingAccounts,
2033
2143
  });
@@ -2036,19 +2146,16 @@ class VelocityClient {
2036
2146
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getTransferPoolsIx(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId), txParams), [], this.opts);
2037
2147
  if (fromSubAccountId === this.activeSubAccountId ||
2038
2148
  toSubAccountId === this.activeSubAccountId) {
2039
- this.spotMarketLastSlotCache.set(depositFromMarketIndex, slot);
2040
- this.spotMarketLastSlotCache.set(depositToMarketIndex, slot);
2041
- this.spotMarketLastSlotCache.set(borrowFromMarketIndex, slot);
2042
- this.spotMarketLastSlotCache.set(borrowToMarketIndex, slot);
2149
+ this.cacheSpotMarketSlot(slot, depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex);
2043
2150
  }
2044
2151
  return txSig;
2045
2152
  }
2046
2153
  async getTransferPoolsIx(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId, isToNewSubAccount) {
2047
2154
  const fromUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, fromSubAccountId);
2048
2155
  const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, toSubAccountId);
2049
- const userAccounts = [this.getUserAccount(fromSubAccountId)];
2156
+ const userAccounts = [this.getUserAccountOrThrow(fromSubAccountId)];
2050
2157
  if (!isToNewSubAccount) {
2051
- userAccounts.push(this.getUserAccount(toSubAccountId));
2158
+ userAccounts.push(this.getUserAccountOrThrow(toSubAccountId));
2052
2159
  }
2053
2160
  const remainingAccounts = this.getRemainingAccounts({
2054
2161
  userAccounts,
@@ -2061,8 +2168,8 @@ class VelocityClient {
2061
2168
  ],
2062
2169
  });
2063
2170
  const tokenPrograms = new Set();
2064
- const depositFromSpotMarket = this.getSpotMarketAccount(depositFromMarketIndex);
2065
- const borrowFromSpotMarket = this.getSpotMarketAccount(borrowFromMarketIndex);
2171
+ const depositFromSpotMarket = this.getSpotMarketAccountOrThrow(depositFromMarketIndex);
2172
+ const borrowFromSpotMarket = this.getSpotMarketAccountOrThrow(borrowFromMarketIndex);
2066
2173
  tokenPrograms.add(this.getTokenProgramForSpotMarket(depositFromSpotMarket).toBase58());
2067
2174
  tokenPrograms.add(this.getTokenProgramForSpotMarket(borrowFromSpotMarket).toBase58());
2068
2175
  for (const tokenProgram of tokenPrograms) {
@@ -2079,10 +2186,10 @@ class VelocityClient {
2079
2186
  toUser,
2080
2187
  userStats: this.getUserStatsAccountPublicKey(),
2081
2188
  state: await this.getStatePublicKey(),
2082
- depositFromSpotMarketVault: this.getSpotMarketAccount(depositFromMarketIndex).vault,
2083
- depositToSpotMarketVault: this.getSpotMarketAccount(depositToMarketIndex).vault,
2084
- borrowFromSpotMarketVault: this.getSpotMarketAccount(borrowFromMarketIndex).vault,
2085
- borrowToSpotMarketVault: this.getSpotMarketAccount(borrowToMarketIndex).vault,
2189
+ depositFromSpotMarketVault: this.getSpotMarketAccountOrThrow(depositFromMarketIndex).vault,
2190
+ depositToSpotMarketVault: this.getSpotMarketAccountOrThrow(depositToMarketIndex).vault,
2191
+ borrowFromSpotMarketVault: this.getSpotMarketAccountOrThrow(borrowFromMarketIndex).vault,
2192
+ borrowToSpotMarketVault: this.getSpotMarketAccountOrThrow(borrowToMarketIndex).vault,
2086
2193
  velocitySigner: this.getSignerPublicKey(),
2087
2194
  },
2088
2195
  remainingAccounts,
@@ -2097,8 +2204,8 @@ class VelocityClient {
2097
2204
  const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, toSubAccountId);
2098
2205
  const remainingAccounts = this.getRemainingAccounts({
2099
2206
  userAccounts: [
2100
- this.getUserAccount(fromSubAccountId),
2101
- this.getUserAccount(toSubAccountId),
2207
+ this.getUserAccountOrThrow(fromSubAccountId),
2208
+ this.getUserAccountOrThrow(toSubAccountId),
2102
2209
  ],
2103
2210
  useMarketLastSlotCache: true,
2104
2211
  writablePerpMarketIndexes: [marketIndex],
@@ -2140,9 +2247,9 @@ class VelocityClient {
2140
2247
  }
2141
2248
  async getDepositIntoIsolatedPerpPositionIx(amount, perpMarketIndex, userTokenAccount, subAccountId) {
2142
2249
  const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId);
2143
- const perpMarketAccount = this.getPerpMarketAccount(perpMarketIndex);
2250
+ const perpMarketAccount = this.getPerpMarketAccountOrThrow(perpMarketIndex);
2144
2251
  const spotMarketIndex = perpMarketAccount.quoteSpotMarketIndex;
2145
- const spotMarketAccount = this.getSpotMarketAccount(spotMarketIndex);
2252
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(spotMarketIndex);
2146
2253
  const remainingAccounts = this.getRemainingAccounts({
2147
2254
  userAccounts: [],
2148
2255
  writableSpotMarketIndexes: [spotMarketIndex],
@@ -2168,7 +2275,7 @@ class VelocityClient {
2168
2275
  const transferIx = await this.getTransferIsolatedPerpPositionDepositIx(amount, perpMarketIndex, subAccountId, noBuffer);
2169
2276
  const needsToSettle = amount.lt(tokenAmountDeposited.neg()) || amount.eq(numericConstants_1.MIN_I64) || trySettle;
2170
2277
  if (needsToSettle) {
2171
- const settleIx = await this.settleMultiplePNLsIx(await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId), this.getUserAccount(subAccountId), [perpMarketIndex], types_1.SettlePnlMode.TRY_SETTLE);
2278
+ const settleIx = await this.settleMultiplePNLsIx(await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId), this.getUserAccountOrThrow(subAccountId), [perpMarketIndex], types_1.SettlePnlMode.TRY_SETTLE);
2172
2279
  ixs.push(settleIx);
2173
2280
  }
2174
2281
  ixs.push(transferIx);
@@ -2181,10 +2288,10 @@ class VelocityClient {
2181
2288
  }
2182
2289
  async getTransferIsolatedPerpPositionDepositIx(amount, perpMarketIndex, subAccountId, noAmountBuffer, signingAuthority) {
2183
2290
  const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId);
2184
- const perpMarketAccount = this.getPerpMarketAccount(perpMarketIndex);
2291
+ const perpMarketAccount = this.getPerpMarketAccountOrThrow(perpMarketIndex);
2185
2292
  const spotMarketIndex = perpMarketAccount.quoteSpotMarketIndex;
2186
- const spotMarketAccount = this.getSpotMarketAccount(spotMarketIndex);
2187
- const user = await this.getUserAccount(subAccountId);
2293
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(spotMarketIndex);
2294
+ const user = await this.getUserAccountOrThrow(subAccountId);
2188
2295
  const remainingAccounts = this.getRemainingAccounts({
2189
2296
  userAccounts: [user],
2190
2297
  writableSpotMarketIndexes: [spotMarketIndex],
@@ -2211,16 +2318,20 @@ class VelocityClient {
2211
2318
  }
2212
2319
  async getWithdrawFromIsolatedPerpPositionIxsBundle(amount, perpMarketIndex, subAccountId, userTokenAccount) {
2213
2320
  const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId);
2214
- const userAccount = this.getUserAccount(subAccountId);
2321
+ const userAccount = this.getUserAccountOrThrow(subAccountId);
2215
2322
  const tokenAmountDeposited = this.getIsolatedPerpPositionTokenAmount(perpMarketIndex);
2216
- const isolatedPositionUnrealizedPnl = (0, position_1.calculateClaimablePnl)(this.getPerpMarketAccount(perpMarketIndex), this.getSpotMarketAccount(this.getPerpMarketAccount(perpMarketIndex).quoteSpotMarketIndex), userAccount.perpPositions.find((p) => p.marketIndex === perpMarketIndex), this.getOracleDataForSpotMarket(this.getPerpMarketAccount(perpMarketIndex).quoteSpotMarketIndex));
2323
+ const isolatedPerpPosition = userAccount.perpPositions.find((p) => p.marketIndex === perpMarketIndex);
2324
+ if (!isolatedPerpPosition) {
2325
+ throw new Error(`No perp position found for market index ${perpMarketIndex}`);
2326
+ }
2327
+ const isolatedPositionUnrealizedPnl = (0, position_1.calculateClaimablePnl)(this.getPerpMarketAccountOrThrow(perpMarketIndex), this.getSpotMarketAccountOrThrow(this.getPerpMarketAccountOrThrow(perpMarketIndex).quoteSpotMarketIndex), isolatedPerpPosition, this.getOracleDataForSpotMarket(this.getPerpMarketAccountOrThrow(perpMarketIndex).quoteSpotMarketIndex));
2217
2328
  const depositAmountPlusUnrealizedPnl = tokenAmountDeposited.add(isolatedPositionUnrealizedPnl);
2218
2329
  const amountToWithdraw = amount.gt(depositAmountPlusUnrealizedPnl)
2219
2330
  ? numericConstants_1.MIN_I64 // min i64
2220
2331
  : amount;
2221
2332
  let associatedTokenAccount = userTokenAccount;
2222
2333
  if (!associatedTokenAccount) {
2223
- const perpMarketAccount = this.getPerpMarketAccount(perpMarketIndex);
2334
+ const perpMarketAccount = this.getPerpMarketAccountOrThrow(perpMarketIndex);
2224
2335
  const quoteSpotMarketIndex = perpMarketAccount.quoteSpotMarketIndex;
2225
2336
  associatedTokenAccount = await this.getAssociatedTokenAccount(quoteSpotMarketIndex);
2226
2337
  }
@@ -2236,11 +2347,11 @@ class VelocityClient {
2236
2347
  }
2237
2348
  async getWithdrawFromIsolatedPerpPositionIx(amount, perpMarketIndex, userTokenAccount, subAccountId) {
2238
2349
  const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId);
2239
- const perpMarketAccount = this.getPerpMarketAccount(perpMarketIndex);
2350
+ const perpMarketAccount = this.getPerpMarketAccountOrThrow(perpMarketIndex);
2240
2351
  const spotMarketIndex = perpMarketAccount.quoteSpotMarketIndex;
2241
- const spotMarketAccount = this.getSpotMarketAccount(spotMarketIndex);
2352
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(spotMarketIndex);
2242
2353
  const remainingAccounts = this.getRemainingAccounts({
2243
- userAccounts: [this.getUserAccount(subAccountId)],
2354
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
2244
2355
  writableSpotMarketIndexes: [spotMarketIndex],
2245
2356
  readablePerpMarketIndex: [perpMarketIndex],
2246
2357
  });
@@ -2263,7 +2374,7 @@ class VelocityClient {
2263
2374
  return txSig;
2264
2375
  }
2265
2376
  async updateSpotMarketCumulativeInterestIx(marketIndex) {
2266
- const spotMarket = this.getSpotMarketAccount(marketIndex);
2377
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
2267
2378
  return await this.program.instruction.updateSpotMarketCumulativeInterest({
2268
2379
  accounts: {
2269
2380
  state: await this.getStatePublicKey(),
@@ -2348,7 +2459,7 @@ class VelocityClient {
2348
2459
  const preppedTxs = await this.prepareMarketOrderTxs(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams, bracketOrdersParams, cancelExistingOrders, settlePnl);
2349
2460
  const signedTxs = (await this.txHandler.getSignedTransactionMap(preppedTxs, this.wallet)).signedTxMap;
2350
2461
  const { txSig, slot } = await this.sendTransaction(signedTxs.marketOrderTx, [], this.opts, true);
2351
- this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2462
+ this.cachePerpMarketSlot(slot, orderParams.marketIndex);
2352
2463
  return {
2353
2464
  txSig,
2354
2465
  signedFillTx: signedTxs.fillTx,
@@ -2364,7 +2475,7 @@ class VelocityClient {
2364
2475
  preIxs.push(await this.getTransferIsolatedPerpPositionDepositIx(isolatedPositionDepositAmount, orderParams.marketIndex, subAccountId));
2365
2476
  }
2366
2477
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlacePerpOrderIx(orderParams, subAccountId), txParams, undefined, undefined, undefined, undefined, preIxs), [], this.opts);
2367
- this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2478
+ this.cachePerpMarketSlot(slot, orderParams.marketIndex);
2368
2479
  return txSig;
2369
2480
  }
2370
2481
  /**
@@ -2418,7 +2529,7 @@ class VelocityClient {
2418
2529
  const remainingAccounts = this.getRemainingAccounts({
2419
2530
  userAccounts: (depositToTradeArgs === null || depositToTradeArgs === void 0 ? void 0 : depositToTradeArgs.isMakingNewAccount)
2420
2531
  ? []
2421
- : [this.getUserAccount(subAccountId)],
2532
+ : [this.getUserAccountOrThrow(subAccountId)],
2422
2533
  useMarketLastSlotCache: false,
2423
2534
  readablePerpMarketIndex: orderParams.marketIndex,
2424
2535
  readableSpotMarketIndexes: isDepositToTradeTx
@@ -2447,7 +2558,7 @@ class VelocityClient {
2447
2558
  const marketAccountInfos = [];
2448
2559
  const oracleAccountInfos = [];
2449
2560
  for (const marketIndex of marketIndexes) {
2450
- const market = this.getPerpMarketAccount(marketIndex);
2561
+ const market = this.getPerpMarketAccountOrThrow(marketIndex);
2451
2562
  marketAccountInfos.push({
2452
2563
  pubkey: market.pubkey,
2453
2564
  isWritable: true,
@@ -2508,6 +2619,20 @@ class VelocityClient {
2508
2619
  },
2509
2620
  });
2510
2621
  }
2622
+ /**
2623
+ * Cancel an open order and broadcast the transaction.
2624
+ *
2625
+ * When `orderId` is `undefined` (omitted or passed explicitly), the instruction is
2626
+ * sent with a `null` order ID and the program cancels the most recently placed order
2627
+ * on-chain via `get_last_order_id`. This is safe to use in a composed transaction
2628
+ * where a place instruction runs first and the assigned order ID is not yet known.
2629
+ *
2630
+ * Note: when `orderId` is `undefined` and `overrides.withdrawIsolatedDepositAmount`
2631
+ * is also provided, `getOrder` will return `undefined` (the ID is unknown client-side),
2632
+ * causing the withdraw path to throw — supply an explicit `orderId` in that case.
2633
+ *
2634
+ * @see {@link getCancelOrderIx} to obtain the instruction without sending.
2635
+ */
2511
2636
  async cancelOrder(orderId, txParams, subAccountId, overrides) {
2512
2637
  const cancelIx = await this.getCancelOrderIx(orderId, subAccountId);
2513
2638
  const instructions = [cancelIx];
@@ -2516,6 +2641,9 @@ class VelocityClient {
2516
2641
  const perpMarketIndex = order === null || order === void 0 ? void 0 : order.marketIndex;
2517
2642
  const withdrawAmount = overrides.withdrawIsolatedDepositAmount;
2518
2643
  if (withdrawAmount.gt(numericConstants_1.ZERO)) {
2644
+ if (perpMarketIndex === undefined) {
2645
+ throw new Error(`Order ${orderId} not found when withdrawing isolated deposit`);
2646
+ }
2519
2647
  const withdrawIxs = await this.getWithdrawFromIsolatedPerpPositionIxsBundle(withdrawAmount, perpMarketIndex, subAccountId);
2520
2648
  instructions.push(...withdrawIxs);
2521
2649
  }
@@ -2523,10 +2651,25 @@ class VelocityClient {
2523
2651
  const { txSig } = await this.sendTransaction(await this.buildTransaction(instructions, txParams), [], this.opts);
2524
2652
  return txSig;
2525
2653
  }
2654
+ /**
2655
+ * Build a `cancelOrder` instruction for the given order.
2656
+ *
2657
+ * When `orderId` is `undefined` (omitted or passed explicitly), the instruction is
2658
+ * built with a `null` order ID (`orderId ?? null`). The program interprets a `null`
2659
+ * ID as "cancel the user's most recently placed order" (via `get_last_order_id`
2660
+ * on-chain). This is useful when composing a multi-instruction transaction where a
2661
+ * place instruction precedes the cancel and the program-assigned order ID is not yet
2662
+ * known at build time.
2663
+ *
2664
+ * When `orderId` is supplied, only that specific order is cancelled.
2665
+ *
2666
+ * @see {@link cancelOrder} to send the transaction directly.
2667
+ * @see {@link getCancelOrderByUserIdIx} to cancel by the caller-supplied `userOrderId`.
2668
+ */
2526
2669
  async getCancelOrderIx(orderId, subAccountId) {
2527
2670
  const user = await this.getUserAccountPublicKey(subAccountId);
2528
2671
  const remainingAccounts = this.getRemainingAccounts({
2529
- userAccounts: [this.getUserAccount(subAccountId)],
2672
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
2530
2673
  useMarketLastSlotCache: true,
2531
2674
  });
2532
2675
  return await VelocityCore_1.VelocityCore.buildCancelOrderInstruction({
@@ -2544,10 +2687,13 @@ class VelocityClient {
2544
2687
  }
2545
2688
  async getCancelOrderByUserIdIx(userOrderId, subAccountId) {
2546
2689
  const user = await this.getUserAccountPublicKey(subAccountId);
2547
- const order = this.getOrderByUserId(userOrderId);
2548
- const oracle = this.getPerpMarketAccount(order.marketIndex).oracle;
2690
+ const order = this.getOrderByUserId(userOrderId, subAccountId);
2691
+ if (!order) {
2692
+ throw new Error(`Order with user order id ${userOrderId} not found`);
2693
+ }
2694
+ const oracle = this.getPerpMarketAccountOrThrow(order.marketIndex).oracle;
2549
2695
  const remainingAccounts = this.getRemainingAccounts({
2550
- userAccounts: [this.getUserAccount(subAccountId)],
2696
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
2551
2697
  useMarketLastSlotCache: true,
2552
2698
  });
2553
2699
  return await VelocityCore_1.VelocityCore.buildCancelOrderByUserIdInstruction({
@@ -2584,7 +2730,7 @@ class VelocityClient {
2584
2730
  async getCancelOrdersByIdsIx(orderIds, subAccountId, user, overrides) {
2585
2731
  var _a, _b, _c;
2586
2732
  const userAccountPubKey = (_a = user === null || user === void 0 ? void 0 : user.userAccountPublicKey) !== null && _a !== void 0 ? _a : (await this.getUserAccountPublicKey(subAccountId));
2587
- const userAccount = (_b = user === null || user === void 0 ? void 0 : user.getUserAccount()) !== null && _b !== void 0 ? _b : this.getUserAccount(subAccountId);
2733
+ const userAccount = (_b = user === null || user === void 0 ? void 0 : user.getUserAccount()) !== null && _b !== void 0 ? _b : this.getUserAccountOrThrow(subAccountId);
2588
2734
  const remainingAccounts = this.getRemainingAccounts({
2589
2735
  userAccounts: [userAccount],
2590
2736
  useMarketLastSlotCache: true,
@@ -2616,7 +2762,7 @@ class VelocityClient {
2616
2762
  }
2617
2763
  }
2618
2764
  const remainingAccounts = this.getRemainingAccounts({
2619
- userAccounts: [this.getUserAccount(subAccountId)],
2765
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
2620
2766
  readablePerpMarketIndex,
2621
2767
  readableSpotMarketIndexes,
2622
2768
  useMarketLastSlotCache: true,
@@ -2680,7 +2826,7 @@ class VelocityClient {
2680
2826
  }
2681
2827
  }
2682
2828
  const remainingAccounts = this.getRemainingAccounts({
2683
- userAccounts: [this.getUserAccount(subAccountId)],
2829
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
2684
2830
  readablePerpMarketIndex,
2685
2831
  readableSpotMarketIndexes,
2686
2832
  useMarketLastSlotCache: true,
@@ -2722,7 +2868,7 @@ class VelocityClient {
2722
2868
  }
2723
2869
  }
2724
2870
  const remainingAccounts = this.getRemainingAccounts({
2725
- userAccounts: [this.getUserAccount(subAccountId)],
2871
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
2726
2872
  readablePerpMarketIndex,
2727
2873
  readableSpotMarketIndexes,
2728
2874
  useMarketLastSlotCache: true,
@@ -2765,7 +2911,7 @@ class VelocityClient {
2765
2911
  const user = await this.getUserAccountPublicKey(subAccountId);
2766
2912
  const isPerp = (0, types_1.isVariant)(params.marketType, 'perp');
2767
2913
  const remainingAccounts = this.getRemainingAccounts({
2768
- userAccounts: [this.getUserAccount(subAccountId)],
2914
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
2769
2915
  readablePerpMarketIndex: isPerp ? [params.marketIndex] : [],
2770
2916
  readableSpotMarketIndexes: isPerp ? [] : [params.marketIndex],
2771
2917
  useMarketLastSlotCache: true,
@@ -2804,6 +2950,7 @@ class VelocityClient {
2804
2950
  // is detected from the order bitflags; pass the taker's decoded escrow (e.g.
2805
2951
  // from a RevenueShareEscrowMap) so referred takers also get it attached.
2806
2952
  takerEscrow) {
2953
+ var _a;
2807
2954
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
2808
2955
  let filler;
2809
2956
  if (fillerAuthority) {
@@ -2821,7 +2968,10 @@ class VelocityClient {
2821
2968
  }
2822
2969
  const marketIndex = order
2823
2970
  ? order.marketIndex
2824
- : userAccount.orders.find((order) => order.orderId === userAccount.nextOrderId - 1).marketIndex;
2971
+ : (_a = userAccount.orders.find((order) => order.orderId === userAccount.nextOrderId - 1)) === null || _a === void 0 ? void 0 : _a.marketIndex;
2972
+ if (marketIndex === undefined) {
2973
+ throw new Error('No order found to fill');
2974
+ }
2825
2975
  makerInfo = Array.isArray(makerInfo)
2826
2976
  ? makerInfo
2827
2977
  : makerInfo
@@ -2869,7 +3019,13 @@ class VelocityClient {
2869
3019
  if (takerEscrowMeta) {
2870
3020
  remainingAccounts.push(takerEscrowMeta);
2871
3021
  }
2872
- const orderId = isSignedMsg ? null : order.orderId;
3022
+ let orderId = null;
3023
+ if (!isSignedMsg) {
3024
+ if (!order) {
3025
+ throw new Error('order is required to fill a non-signedMsg order');
3026
+ }
3027
+ orderId = order.orderId;
3028
+ }
2873
3029
  return await VelocityCore_1.VelocityCore.buildFillPerpOrderInstruction({
2874
3030
  program: this.program,
2875
3031
  orderId,
@@ -2987,13 +3143,12 @@ class VelocityClient {
2987
3143
  const lookupTables = res.lookupTables;
2988
3144
  const tx = (await this.buildTransaction(ixs, txParams, 0, lookupTables));
2989
3145
  const { txSig, slot } = await this.sendTransaction(tx);
2990
- this.spotMarketLastSlotCache.set(outMarketIndex, slot);
2991
- this.spotMarketLastSlotCache.set(inMarketIndex, slot);
3146
+ this.cacheSpotMarketSlot(slot, outMarketIndex, inMarketIndex);
2992
3147
  return txSig;
2993
3148
  }
2994
3149
  async getTitanSwapIx({ titanClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, onlyDirectRoutes, reduceOnly, userAccountPublicKey, }) {
2995
- const outMarket = this.getSpotMarketAccount(outMarketIndex);
2996
- const inMarket = this.getSpotMarketAccount(inMarketIndex);
3150
+ const outMarket = this.getSpotMarketAccountOrThrow(outMarketIndex);
3151
+ const inMarket = this.getSpotMarketAccountOrThrow(inMarketIndex);
2997
3152
  const isExactOut = swapMode === 'ExactOut';
2998
3153
  const exactOutBufferedAmountIn = amount.muln(1001).divn(1000); // Add 10bp buffer
2999
3154
  const preInstructions = [];
@@ -3046,8 +3201,8 @@ class VelocityClient {
3046
3201
  return { ixs, lookupTables };
3047
3202
  }
3048
3203
  async getJupiterSwapIxV6({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, onlyDirectRoutes, quote, reduceOnly, userAccountPublicKey, }) {
3049
- const outMarket = this.getSpotMarketAccount(outMarketIndex);
3050
- const inMarket = this.getSpotMarketAccount(inMarketIndex);
3204
+ const outMarket = this.getSpotMarketAccountOrThrow(outMarketIndex);
3205
+ const inMarket = this.getSpotMarketAccountOrThrow(inMarketIndex);
3051
3206
  if (!quote) {
3052
3207
  const fetchedQuote = await jupiterClient.getQuote({
3053
3208
  inputMint: inMarket.mint,
@@ -3128,8 +3283,11 @@ class VelocityClient {
3128
3283
  const userAccountPublicKeyToUse = userAccountPublicKey || (await this.getUserAccountPublicKey());
3129
3284
  const userAccounts = [];
3130
3285
  try {
3131
- if (this.hasUser() && this.getUser().getUserAccountAndSlot()) {
3132
- userAccounts.push(this.getUser().getUserAccountAndSlot().data);
3286
+ const userAccount = this.hasUser()
3287
+ ? this.getUser().getUserAccount()
3288
+ : undefined;
3289
+ if (userAccount) {
3290
+ userAccounts.push(userAccount);
3133
3291
  }
3134
3292
  }
3135
3293
  catch (err) {
@@ -3140,8 +3298,8 @@ class VelocityClient {
3140
3298
  writableSpotMarketIndexes: [outMarketIndex, inMarketIndex],
3141
3299
  readableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
3142
3300
  });
3143
- const outSpotMarket = this.getSpotMarketAccount(outMarketIndex);
3144
- const inSpotMarket = this.getSpotMarketAccount(inMarketIndex);
3301
+ const outSpotMarket = this.getSpotMarketAccountOrThrow(outMarketIndex);
3302
+ const inSpotMarket = this.getSpotMarketAccountOrThrow(inMarketIndex);
3145
3303
  const outTokenProgram = this.getTokenProgramForSpotMarket(outSpotMarket);
3146
3304
  const inTokenProgram = this.getTokenProgramForSpotMarket(inSpotMarket);
3147
3305
  if (!outTokenProgram.equals(inTokenProgram)) {
@@ -3205,8 +3363,8 @@ class VelocityClient {
3205
3363
  }
3206
3364
  async getSwapIxV2({ swapClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, onlyDirectRoutes, reduceOnly, quote, v6, userAccountPublicKey, }) {
3207
3365
  // Get market accounts to determine mints
3208
- const outMarket = this.getSpotMarketAccount(outMarketIndex);
3209
- const inMarket = this.getSpotMarketAccount(inMarketIndex);
3366
+ const outMarket = this.getSpotMarketAccountOrThrow(outMarketIndex);
3367
+ const inMarket = this.getSpotMarketAccountOrThrow(inMarketIndex);
3210
3368
  const isExactOut = swapMode === 'ExactOut';
3211
3369
  const preInstructions = [];
3212
3370
  // Handle token accounts if not provided
@@ -3280,7 +3438,7 @@ class VelocityClient {
3280
3438
  return this.sendTransaction(tx);
3281
3439
  }
3282
3440
  async getStakeForMSOLIx({ amount, userAccountPublicKey, }) {
3283
- const wSOLMint = this.getSpotMarketAccount(1).mint;
3441
+ const wSOLMint = this.getSpotMarketAccountOrThrow(1).mint;
3284
3442
  const mSOLAccount = await this.getAssociatedTokenAccount(2);
3285
3443
  const wSOLAccount = await this.getAssociatedTokenAccount(1, false);
3286
3444
  const wSOLAccountExists = await this.checkIfAccountExists(wSOLAccount);
@@ -3431,7 +3589,7 @@ class VelocityClient {
3431
3589
  }
3432
3590
  async placeAndTakePerpOrder(orderParams, makerInfo, successCondition, auctionDurationPercentage, txParams, subAccountId, takerEscrow) {
3433
3591
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, successCondition, auctionDurationPercentage, subAccountId, undefined, takerEscrow), txParams), [], this.opts);
3434
- this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
3592
+ this.cachePerpMarketSlot(slot, orderParams.marketIndex);
3435
3593
  return txSig;
3436
3594
  }
3437
3595
  async preparePlaceAndTakePerpOrderWithAdditionalOrders(orderParams, makerInfo, bracketOrdersParams = new Array(), txParams, subAccountId, cancelExistingOrders, settlePnl, exitEarlyIfSimFails, auctionDurationPercentage, optionalIxs, isolatedPositionDepositAmount) {
@@ -3469,6 +3627,9 @@ class VelocityClient {
3469
3627
  useSimulatedComputeUnits: false,
3470
3628
  };
3471
3629
  if (shouldUseSimulationComputeUnits || shouldExitIfSimulationFails) {
3630
+ if (!txParams) {
3631
+ throw new Error('txParams is required when simulating compute units or exiting early on failed simulation');
3632
+ }
3472
3633
  const placeAndTakeTxToSim = (await this.buildTransaction(placeAndTakeIxs, txParams, undefined, lookupTableAccounts, true, recentBlockHash, optionalIxs));
3473
3634
  const simulationResult = await txParamProcessor_1.TransactionParamProcessor.getTxSimComputeUnits(placeAndTakeTxToSim, this.connection, (_b = txParams.computeUnitsBufferMultiplier) !== null && _b !== void 0 ? _b : 1.2, txParams.lowerBoundCu);
3474
3635
  if (shouldExitIfSimulationFails && !simulationResult.success) {
@@ -3495,7 +3656,7 @@ class VelocityClient {
3495
3656
  const prepSettlePnlTx = async () => {
3496
3657
  if (settlePnl && (0, types_1.isVariant)(orderParams.marketType, 'perp')) {
3497
3658
  const userAccountPublicKey = await this.getUserAccountPublicKey(subAccountId);
3498
- const settlePnlIx = await this.settlePNLIx(userAccountPublicKey, this.getUserAccount(subAccountId), orderParams.marketIndex);
3659
+ const settlePnlIx = await this.settlePNLIx(userAccountPublicKey, this.getUserAccountOrThrow(subAccountId), orderParams.marketIndex);
3499
3660
  txsToSign.settlePnlTx = await this.buildTransaction([settlePnlIx], txParams, this.txVersion, lookupTableAccounts, undefined, recentBlockHash, optionalIxs);
3500
3661
  }
3501
3662
  return;
@@ -3518,8 +3679,11 @@ class VelocityClient {
3518
3679
  const signedTxs = (await this.txHandler.getSignedTransactionMap(txsToSign,
3519
3680
  // @ts-ignore
3520
3681
  this.provider.wallet)).signedTxMap;
3682
+ if (!signedTxs.placeAndTakeTx) {
3683
+ throw new Error('placeAndTakeTx was not built');
3684
+ }
3521
3685
  const { txSig, slot } = await this.sendTransaction(signedTxs.placeAndTakeTx, [], this.opts, true);
3522
- this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
3686
+ this.cachePerpMarketSlot(slot, orderParams.marketIndex);
3523
3687
  return {
3524
3688
  txSig,
3525
3689
  signedCancelExistingOrdersTx: signedTxs.cancelExistingOrdersTx,
@@ -3541,7 +3705,7 @@ class VelocityClient {
3541
3705
  : makerInfo
3542
3706
  ? [makerInfo]
3543
3707
  : [];
3544
- const userAccounts = [this.getUserAccount(subAccountId)];
3708
+ const userAccounts = [this.getUserAccountOrThrow(subAccountId)];
3545
3709
  for (const maker of makerInfo) {
3546
3710
  userAccounts.push(maker.makerUserAccount);
3547
3711
  }
@@ -3585,7 +3749,7 @@ class VelocityClient {
3585
3749
  }
3586
3750
  async placeAndMakePerpOrder(orderParams, takerInfo, txParams, subAccountId, takerEscrow) {
3587
3751
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndMakePerpOrderIx(orderParams, takerInfo, subAccountId, takerEscrow), txParams), [], this.opts);
3588
- this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
3752
+ this.cachePerpMarketSlot(slot, orderParams.marketIndex);
3589
3753
  return txSig;
3590
3754
  }
3591
3755
  async getPlaceAndMakePerpOrderIx(orderParams, takerInfo, subAccountId,
@@ -3599,7 +3763,7 @@ class VelocityClient {
3599
3763
  const user = await this.getUserAccountPublicKey(subAccountId);
3600
3764
  const remainingAccounts = this.getRemainingAccounts({
3601
3765
  userAccounts: [
3602
- this.getUserAccount(subAccountId),
3766
+ this.getUserAccountOrThrow(subAccountId),
3603
3767
  takerInfo.takerUserAccount,
3604
3768
  ],
3605
3769
  useMarketLastSlotCache: true,
@@ -3674,6 +3838,9 @@ class VelocityClient {
3674
3838
  });
3675
3839
  }
3676
3840
  signMessage(message, keypair = this.wallet.payer) {
3841
+ if (!keypair) {
3842
+ throw new Error('No keypair available to sign message');
3843
+ }
3677
3844
  return Buffer.from(tweetnacl_1.default.sign.detached(message, keypair.secretKey));
3678
3845
  }
3679
3846
  async placeSignedMsgTakerOrder(signedSignedMsgOrderParams, marketIndex, takerInfo, precedingIxs = [], overrideCustomIxIndex, txParams) {
@@ -3727,7 +3894,7 @@ class VelocityClient {
3727
3894
  async placeAndMakeSignedMsgPerpOrder(signedSignedMsgOrderParams, signedMsgOrderUuid, takerInfo, orderParams, txParams, subAccountId, precedingIxs = [], overrideCustomIxIndex, takerEscrow) {
3728
3895
  const ixs = await this.getPlaceAndMakeSignedMsgPerpOrderIxs(signedSignedMsgOrderParams, signedMsgOrderUuid, takerInfo, orderParams, subAccountId, precedingIxs, overrideCustomIxIndex, takerEscrow);
3729
3896
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(ixs, txParams), [], this.opts);
3730
- this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
3897
+ this.cachePerpMarketSlot(slot, orderParams.marketIndex);
3731
3898
  return txSig;
3732
3899
  }
3733
3900
  async getPlaceAndMakeSignedMsgPerpOrderIxs(signedSignedMsgOrderParams, signedMsgOrderUuid, takerInfo, orderParams, subAccountId, precedingIxs = [], overrideCustomIxIndex,
@@ -3741,7 +3908,7 @@ class VelocityClient {
3741
3908
  const user = await this.getUserAccountPublicKey(subAccountId);
3742
3909
  const remainingAccounts = this.getRemainingAccounts({
3743
3910
  userAccounts: [
3744
- this.getUserAccount(subAccountId),
3911
+ this.getUserAccountOrThrow(subAccountId),
3745
3912
  takerInfo.takerUserAccount,
3746
3913
  ],
3747
3914
  useMarketLastSlotCache: false,
@@ -3867,9 +4034,13 @@ class VelocityClient {
3867
4034
  * @returns
3868
4035
  */
3869
4036
  async getModifyOrderIx({ orderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, bitFlags, maxTs, policy, }, subAccountId, overrides) {
3870
- var _a, _b, _c, _d, _e, _f, _g;
4037
+ var _a, _b, _c, _d, _e;
3871
4038
  const userPubKey = (_b = (_a = overrides === null || overrides === void 0 ? void 0 : overrides.user) === null || _a === void 0 ? void 0 : _a.getUserAccountPublicKey()) !== null && _b !== void 0 ? _b : (await this.getUserAccountPublicKey(subAccountId));
3872
- const userAccount = (_d = (_c = overrides === null || overrides === void 0 ? void 0 : overrides.user) === null || _c === void 0 ? void 0 : _c.getUserAccount()) !== null && _d !== void 0 ? _d : this.getUserAccount(subAccountId);
4039
+ const overrideUserAccount = (_c = overrides === null || overrides === void 0 ? void 0 : overrides.user) === null || _c === void 0 ? void 0 : _c.getUserAccount();
4040
+ if ((overrides === null || overrides === void 0 ? void 0 : overrides.user) && !overrideUserAccount) {
4041
+ throw new Error('modifyOrder: override user account is not loaded');
4042
+ }
4043
+ const userAccount = overrideUserAccount !== null && overrideUserAccount !== void 0 ? overrideUserAccount : this.getUserAccountOrThrow(subAccountId);
3873
4044
  const remainingAccounts = this.getRemainingAccounts({
3874
4045
  userAccounts: [userAccount],
3875
4046
  useMarketLastSlotCache: true,
@@ -3890,7 +4061,7 @@ class VelocityClient {
3890
4061
  policy: policy || null,
3891
4062
  maxTs: maxTs || null,
3892
4063
  };
3893
- const authority = (_g = (_e = overrides === null || overrides === void 0 ? void 0 : overrides.authority) !== null && _e !== void 0 ? _e : (_f = overrides === null || overrides === void 0 ? void 0 : overrides.user) === null || _f === void 0 ? void 0 : _f.getUserAccount().authority) !== null && _g !== void 0 ? _g : this.wallet.publicKey;
4064
+ const authority = (_e = (_d = overrides === null || overrides === void 0 ? void 0 : overrides.authority) !== null && _d !== void 0 ? _d : overrideUserAccount === null || overrideUserAccount === void 0 ? void 0 : overrideUserAccount.authority) !== null && _e !== void 0 ? _e : this.wallet.publicKey;
3894
4065
  return await VelocityCore_1.VelocityCore.buildModifyOrderInstruction({
3895
4066
  program: this.program,
3896
4067
  orderId,
@@ -3927,7 +4098,7 @@ class VelocityClient {
3927
4098
  async getModifyOrderByUserIdIx({ userOrderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, bitFlags, maxTs, policy, }, subAccountId) {
3928
4099
  const user = await this.getUserAccountPublicKey(subAccountId);
3929
4100
  const remainingAccounts = this.getRemainingAccounts({
3930
- userAccounts: [this.getUserAccount(subAccountId)],
4101
+ userAccounts: [this.getUserAccountOrThrow(subAccountId)],
3931
4102
  useMarketLastSlotCache: true,
3932
4103
  });
3933
4104
  const orderParams = {
@@ -3965,7 +4136,7 @@ class VelocityClient {
3965
4136
  : marketIndexes;
3966
4137
  if (filterInvalidMarkets) {
3967
4138
  for (const marketIndex of marketIndexes) {
3968
- const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
4139
+ const perpMarketAccount = this.getPerpMarketAccountOrThrow(marketIndex);
3969
4140
  const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
3970
4141
  const stateAccountAndSlot = this.accountSubscriber.getStateAccountAndSlot();
3971
4142
  const oracleGuardRails = stateAccountAndSlot.data.oracleGuardRails;
@@ -4205,7 +4376,7 @@ class VelocityClient {
4205
4376
  }
4206
4377
  async liquidatePerp(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice, txParams, liquidatorSubAccountId) {
4207
4378
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
4208
- this.perpMarketLastSlotCache.set(marketIndex, slot);
4379
+ this.cachePerpMarketSlot(slot, marketIndex);
4209
4380
  return txSig;
4210
4381
  }
4211
4382
  async getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice, liquidatorSubAccountId) {
@@ -4213,7 +4384,10 @@ class VelocityClient {
4213
4384
  const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
4214
4385
  const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
4215
4386
  const remainingAccounts = this.getRemainingAccounts({
4216
- userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
4387
+ userAccounts: [
4388
+ this.getUserAccountOrThrow(liquidatorSubAccountId),
4389
+ userAccount,
4390
+ ],
4217
4391
  useMarketLastSlotCache: true,
4218
4392
  writablePerpMarketIndexes: [marketIndex],
4219
4393
  });
@@ -4233,7 +4407,7 @@ class VelocityClient {
4233
4407
  }
4234
4408
  async liquidatePerpWithFill(userAccountPublicKey, userAccount, marketIndex, makerInfos, txParams, liquidatorSubAccountId) {
4235
4409
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidatePerpWithFillIx(userAccountPublicKey, userAccount, marketIndex, makerInfos, liquidatorSubAccountId), txParams), [], this.opts);
4236
- this.perpMarketLastSlotCache.set(marketIndex, slot);
4410
+ this.cachePerpMarketSlot(slot, marketIndex);
4237
4411
  return txSig;
4238
4412
  }
4239
4413
  async getLiquidatePerpWithFillIx(userAccountPublicKey, userAccount, marketIndex, makerInfos, liquidatorSubAccountId) {
@@ -4273,8 +4447,7 @@ class VelocityClient {
4273
4447
  }
4274
4448
  async liquidateSpot(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, txParams, liquidatorSubAccountId) {
4275
4449
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidateSpotIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
4276
- this.spotMarketLastSlotCache.set(assetMarketIndex, slot);
4277
- this.spotMarketLastSlotCache.set(liabilityMarketIndex, slot);
4450
+ this.cacheSpotMarketSlot(slot, assetMarketIndex, liabilityMarketIndex);
4278
4451
  return txSig;
4279
4452
  }
4280
4453
  async getLiquidateSpotIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId) {
@@ -4282,7 +4455,10 @@ class VelocityClient {
4282
4455
  const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
4283
4456
  const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
4284
4457
  const remainingAccounts = this.getRemainingAccounts({
4285
- userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
4458
+ userAccounts: [
4459
+ this.getUserAccountOrThrow(liquidatorSubAccountId),
4460
+ userAccount,
4461
+ ],
4286
4462
  useMarketLastSlotCache: true,
4287
4463
  writableSpotMarketIndexes: [liabilityMarketIndex, assetMarketIndex],
4288
4464
  });
@@ -4299,8 +4475,8 @@ class VelocityClient {
4299
4475
  });
4300
4476
  }
4301
4477
  async getJupiterLiquidateSpotWithSwapIxV6({ jupiterClient, liabilityMarketIndex, assetMarketIndex, swapAmount, assetTokenAccount, liabilityTokenAccount, slippageBps, swapMode, onlyDirectRoutes, quote, userAccount, userAccountPublicKey, liquidatorSubAccountId, maxAccounts, }) {
4302
- const liabilityMarket = this.getSpotMarketAccount(liabilityMarketIndex);
4303
- const assetMarket = this.getSpotMarketAccount(assetMarketIndex);
4478
+ const liabilityMarket = this.getSpotMarketAccountOrThrow(liabilityMarketIndex);
4479
+ const assetMarket = this.getSpotMarketAccountOrThrow(assetMarketIndex);
4304
4480
  if (!quote) {
4305
4481
  const fetchedQuote = await jupiterClient.getQuote({
4306
4482
  inputMint: assetMarket.mint,
@@ -4378,8 +4554,8 @@ class VelocityClient {
4378
4554
  writableSpotMarketIndexes: [liabilityMarketIndex, assetMarketIndex],
4379
4555
  readableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
4380
4556
  });
4381
- const liabilitySpotMarket = this.getSpotMarketAccount(liabilityMarketIndex);
4382
- const assetSpotMarket = this.getSpotMarketAccount(assetMarketIndex);
4557
+ const liabilitySpotMarket = this.getSpotMarketAccountOrThrow(liabilityMarketIndex);
4558
+ const assetSpotMarket = this.getSpotMarketAccountOrThrow(assetMarketIndex);
4383
4559
  const liabilityTokenProgram = this.getTokenProgramForSpotMarket(liabilitySpotMarket);
4384
4560
  const assetTokenProgram = this.getTokenProgramForSpotMarket(assetSpotMarket);
4385
4561
  if (!liabilityTokenProgram.equals(assetTokenProgram)) {
@@ -4442,68 +4618,10 @@ class VelocityClient {
4442
4618
  });
4443
4619
  return { beginSwapIx, endSwapIx };
4444
4620
  }
4445
- async getInsuranceFundSwapIx({ inMarketIndex, outMarketIndex, amountIn, inTokenAccount, outTokenAccount, }) {
4446
- const remainingAccounts = await this.getRemainingAccounts({
4447
- userAccounts: [],
4448
- writableSpotMarketIndexes: [inMarketIndex, outMarketIndex],
4449
- });
4450
- const inSpotMarket = this.getSpotMarketAccount(inMarketIndex);
4451
- const outSpotMarket = this.getSpotMarketAccount(outMarketIndex);
4452
- if (this.isToken2022(inSpotMarket) || this.isToken2022(outSpotMarket)) {
4453
- remainingAccounts.push({
4454
- pubkey: inSpotMarket.mint,
4455
- isWritable: false,
4456
- isSigner: false,
4457
- });
4458
- remainingAccounts.push({
4459
- pubkey: outSpotMarket.mint,
4460
- isWritable: false,
4461
- isSigner: false,
4462
- });
4463
- if (this.isTransferHook(inSpotMarket)) {
4464
- this.addExtraAccountMetasToRemainingAccounts(inSpotMarket.mint, remainingAccounts);
4465
- }
4466
- if (this.isTransferHook(outSpotMarket)) {
4467
- this.addExtraAccountMetasToRemainingAccounts(outSpotMarket.mint, remainingAccounts);
4468
- }
4469
- }
4470
- const ifRebalanceConfig = (0, pda_1.getIfRebalanceConfigPublicKey)(this.program.programId, inMarketIndex, outMarketIndex);
4471
- const beginSwapIx = await this.program.instruction.beginInsuranceFundSwap(inMarketIndex, outMarketIndex, amountIn, {
4472
- accounts: {
4473
- state: await this.getStatePublicKey(),
4474
- authority: this.wallet.publicKey,
4475
- outInsuranceFundVault: outSpotMarket.insuranceFund.vault,
4476
- inInsuranceFundVault: inSpotMarket.insuranceFund.vault,
4477
- outTokenAccount,
4478
- inTokenAccount,
4479
- ifRebalanceConfig: ifRebalanceConfig,
4480
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
4481
- velocitySigner: this.getStateAccount().signer,
4482
- instructions: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
4483
- },
4484
- remainingAccounts,
4485
- });
4486
- const endSwapIx = await this.program.instruction.endInsuranceFundSwap(inMarketIndex, outMarketIndex, {
4487
- accounts: {
4488
- state: await this.getStatePublicKey(),
4489
- authority: this.wallet.publicKey,
4490
- outInsuranceFundVault: outSpotMarket.insuranceFund.vault,
4491
- inInsuranceFundVault: inSpotMarket.insuranceFund.vault,
4492
- outTokenAccount,
4493
- inTokenAccount,
4494
- ifRebalanceConfig: ifRebalanceConfig,
4495
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
4496
- velocitySigner: this.getStateAccount().signer,
4497
- instructions: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
4498
- },
4499
- remainingAccounts,
4500
- });
4501
- return { beginSwapIx, endSwapIx };
4502
- }
4503
4621
  async liquidateBorrowForPerpPnl(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, txParams, liquidatorSubAccountId) {
4504
4622
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
4505
- this.perpMarketLastSlotCache.set(perpMarketIndex, slot);
4506
- this.spotMarketLastSlotCache.set(liabilityMarketIndex, slot);
4623
+ this.cachePerpMarketSlot(slot, perpMarketIndex);
4624
+ this.cacheSpotMarketSlot(slot, liabilityMarketIndex);
4507
4625
  return txSig;
4508
4626
  }
4509
4627
  async getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId) {
@@ -4511,7 +4629,10 @@ class VelocityClient {
4511
4629
  const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
4512
4630
  const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
4513
4631
  const remainingAccounts = this.getRemainingAccounts({
4514
- userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
4632
+ userAccounts: [
4633
+ this.getUserAccountOrThrow(liquidatorSubAccountId),
4634
+ userAccount,
4635
+ ],
4515
4636
  writablePerpMarketIndexes: [perpMarketIndex],
4516
4637
  writableSpotMarketIndexes: [liabilityMarketIndex],
4517
4638
  });
@@ -4529,8 +4650,8 @@ class VelocityClient {
4529
4650
  }
4530
4651
  async liquidatePerpPnlForDeposit(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice, txParams, liquidatorSubAccountId) {
4531
4652
  const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
4532
- this.perpMarketLastSlotCache.set(perpMarketIndex, slot);
4533
- this.spotMarketLastSlotCache.set(assetMarketIndex, slot);
4653
+ this.cachePerpMarketSlot(slot, perpMarketIndex);
4654
+ this.cacheSpotMarketSlot(slot, assetMarketIndex);
4534
4655
  return txSig;
4535
4656
  }
4536
4657
  async getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice, liquidatorSubAccountId) {
@@ -4538,7 +4659,10 @@ class VelocityClient {
4538
4659
  const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
4539
4660
  const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
4540
4661
  const remainingAccounts = this.getRemainingAccounts({
4541
- userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
4662
+ userAccounts: [
4663
+ this.getUserAccountOrThrow(liquidatorSubAccountId),
4664
+ userAccount,
4665
+ ],
4542
4666
  writablePerpMarketIndexes: [perpMarketIndex],
4543
4667
  writableSpotMarketIndexes: [assetMarketIndex],
4544
4668
  });
@@ -4563,7 +4687,10 @@ class VelocityClient {
4563
4687
  const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
4564
4688
  const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
4565
4689
  const remainingAccounts = this.getRemainingAccounts({
4566
- userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
4690
+ userAccounts: [
4691
+ this.getUserAccountOrThrow(liquidatorSubAccountId),
4692
+ userAccount,
4693
+ ],
4567
4694
  writablePerpMarketIndexes: [marketIndex],
4568
4695
  writableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
4569
4696
  });
@@ -4593,10 +4720,13 @@ class VelocityClient {
4593
4720
  const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
4594
4721
  const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
4595
4722
  const remainingAccounts = this.getRemainingAccounts({
4596
- userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
4723
+ userAccounts: [
4724
+ this.getUserAccountOrThrow(liquidatorSubAccountId),
4725
+ userAccount,
4726
+ ],
4597
4727
  writableSpotMarketIndexes: [marketIndex],
4598
4728
  });
4599
- const spotMarket = this.getSpotMarketAccount(marketIndex);
4729
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
4600
4730
  const tokenProgramId = this.getTokenProgramForSpotMarket(spotMarket);
4601
4731
  this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts);
4602
4732
  if (this.isTransferHook(spotMarket)) {
@@ -4637,7 +4767,7 @@ class VelocityClient {
4637
4767
  return txSig;
4638
4768
  }
4639
4769
  async getUpdatePrelaunchOracleIx(perpMarketIndex) {
4640
- const perpMarket = this.getPerpMarketAccount(perpMarketIndex);
4770
+ const perpMarket = this.getPerpMarketAccountOrThrow(perpMarketIndex);
4641
4771
  if (!(0, types_1.isVariant)(perpMarket.oracleSource, 'prelaunch')) {
4642
4772
  throw new Error(`Wrong oracle source ${perpMarket.oracleSource}`);
4643
4773
  }
@@ -4654,7 +4784,7 @@ class VelocityClient {
4654
4784
  return txSig;
4655
4785
  }
4656
4786
  async getUpdatePerpBidAskTwapIx(perpMarketIndex, makers) {
4657
- const perpMarket = this.getPerpMarketAccount(perpMarketIndex);
4787
+ const perpMarket = this.getPerpMarketAccountOrThrow(perpMarketIndex);
4658
4788
  const remainingAccounts = [];
4659
4789
  for (const [maker, makerStats] of makers) {
4660
4790
  remainingAccounts.push({
@@ -4707,10 +4837,14 @@ class VelocityClient {
4707
4837
  this.eventEmitter.emit(eventName, data);
4708
4838
  }
4709
4839
  getOracleDataForPerpMarket(marketIndex) {
4710
- return this.accountSubscriber.getOraclePriceDataAndSlotForPerpMarket(marketIndex).data;
4840
+ const oraclePriceDataAndSlot = this.accountSubscriber.getOraclePriceDataAndSlotForPerpMarket(marketIndex);
4841
+ if (!oraclePriceDataAndSlot) {
4842
+ throw new Error(`No oracle price data for perp market ${marketIndex}`);
4843
+ }
4844
+ return oraclePriceDataAndSlot.data;
4711
4845
  }
4712
4846
  getMMOracleDataForPerpMarket(marketIndex) {
4713
- const perpMarket = this.getPerpMarketAccount(marketIndex);
4847
+ const perpMarket = this.getPerpMarketAccountOrThrow(marketIndex);
4714
4848
  const oracleData = this.getOracleDataForPerpMarket(marketIndex);
4715
4849
  const stateAccountAndSlot = this.accountSubscriber.getStateAccountAndSlot();
4716
4850
  const isMMOracleActive = !perpMarket.marketStats.mmOracleSlot.eq(numericConstants_1.ZERO);
@@ -4730,11 +4864,12 @@ class VelocityClient {
4730
4864
  .gt(oracleData.sequenceId.div(new anchor_1.BN(10000)));
4731
4865
  let isExchangeOracleMoreRecent = true;
4732
4866
  if (doSlotCheckForRecency &&
4733
- oracleData.slot <= perpMarket.marketStats.mmOracleSlot) {
4867
+ oracleData.slot.lte(perpMarket.marketStats.mmOracleSlot)) {
4734
4868
  isExchangeOracleMoreRecent = false;
4735
4869
  }
4736
4870
  else if (!doSlotCheckForRecency &&
4737
- oracleData.sequenceId < mmOracleSequenceId) {
4871
+ oracleData.sequenceId != null &&
4872
+ oracleData.sequenceId.lt(mmOracleSequenceId)) {
4738
4873
  isExchangeOracleMoreRecent = false;
4739
4874
  }
4740
4875
  const conf = (0, utils_3.getOracleConfidenceFromMMOracleData)(perpMarket.marketStats.mmOraclePrice, oracleData);
@@ -4761,7 +4896,11 @@ class VelocityClient {
4761
4896
  }
4762
4897
  }
4763
4898
  getOracleDataForSpotMarket(marketIndex) {
4764
- return this.accountSubscriber.getOraclePriceDataAndSlotForSpotMarket(marketIndex).data;
4899
+ const oraclePriceDataAndSlot = this.accountSubscriber.getOraclePriceDataAndSlotForSpotMarket(marketIndex);
4900
+ if (!oraclePriceDataAndSlot) {
4901
+ throw new Error(`No oracle price data for spot market ${marketIndex}`);
4902
+ }
4903
+ return oraclePriceDataAndSlot.data;
4765
4904
  }
4766
4905
  async initializeInsuranceFundStake(marketIndex, txParams) {
4767
4906
  const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getInitializeInsuranceFundStakeIx(marketIndex), txParams), [], this.opts);
@@ -4771,7 +4910,7 @@ class VelocityClient {
4771
4910
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
4772
4911
  const accounts = {
4773
4912
  insuranceFundStake: ifStakeAccountPublicKey,
4774
- spotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
4913
+ spotMarket: this.getSpotMarketAccountOrThrow(marketIndex).pubkey,
4775
4914
  userStats: (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.wallet.publicKey // only allow payer to initialize own insurance fund stake account
4776
4915
  ),
4777
4916
  authority: this.wallet.publicKey,
@@ -4785,7 +4924,7 @@ class VelocityClient {
4785
4924
  });
4786
4925
  }
4787
4926
  async getAddInsuranceFundStakeIx(marketIndex, amount, collateralAccountPublicKey) {
4788
- const spotMarket = this.getSpotMarketAccount(marketIndex);
4927
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
4789
4928
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
4790
4929
  const remainingAccounts = [];
4791
4930
  this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts);
@@ -4832,7 +4971,7 @@ class VelocityClient {
4832
4971
  */
4833
4972
  async getAddInsuranceFundStakeIxs({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, fromSubaccount, }) {
4834
4973
  const addIfStakeIxs = [];
4835
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
4974
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
4836
4975
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
4837
4976
  const createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
4838
4977
  const tokenProgramId = this.getTokenProgramForSpotMarket(spotMarketAccount);
@@ -4870,7 +5009,7 @@ class VelocityClient {
4870
5009
  return addIfStakeIxs;
4871
5010
  }
4872
5011
  async requestRemoveInsuranceFundStake(marketIndex, amount, txParams) {
4873
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
5012
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
4874
5013
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
4875
5014
  const ix = await this.program.instruction.requestRemoveInsuranceFundStake(marketIndex, amount, {
4876
5015
  accounts: {
@@ -4888,7 +5027,7 @@ class VelocityClient {
4888
5027
  return txSig;
4889
5028
  }
4890
5029
  async cancelRequestRemoveInsuranceFundStake(marketIndex, txParams) {
4891
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
5030
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
4892
5031
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
4893
5032
  const ix = await this.program.instruction.cancelRequestRemoveInsuranceFundStake(marketIndex, {
4894
5033
  accounts: {
@@ -4907,7 +5046,7 @@ class VelocityClient {
4907
5046
  }
4908
5047
  async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey, txParams) {
4909
5048
  const removeIfStakeIxs = [];
4910
- const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
5049
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
4911
5050
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
4912
5051
  const additionalSigners = [];
4913
5052
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
@@ -4966,7 +5105,7 @@ class VelocityClient {
4966
5105
  }
4967
5106
  async getUpdateUserQuoteAssetInsuranceStakeIx(authority) {
4968
5107
  const marketIndex = numericConstants_1.QUOTE_SPOT_MARKET_INDEX;
4969
- const spotMarket = this.getSpotMarketAccount(marketIndex);
5108
+ const spotMarket = this.getSpotMarketAccountOrThrow(marketIndex);
4970
5109
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, authority, marketIndex);
4971
5110
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, authority);
4972
5111
  const ix = this.program.instruction.updateUserQuoteAssetInsuranceStake({
@@ -4981,36 +5120,13 @@ class VelocityClient {
4981
5120
  });
4982
5121
  return ix;
4983
5122
  }
4984
- async updateUserGovTokenInsuranceStake(authority, txParams) {
4985
- const ix = await this.getUpdateUserGovTokenInsuranceStakeIx(authority);
4986
- const tx = await this.buildTransaction(ix, txParams);
4987
- const { txSig } = await this.sendTransaction(tx, [], this.opts);
4988
- return txSig;
4989
- }
4990
- async getUpdateUserGovTokenInsuranceStakeIx(authority) {
4991
- const marketIndex = numericConstants_1.GOV_SPOT_MARKET_INDEX;
4992
- const spotMarket = this.getSpotMarketAccount(marketIndex);
4993
- const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, authority, marketIndex);
4994
- const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, authority);
4995
- const ix = this.program.instruction.updateUserGovTokenInsuranceStake({
4996
- accounts: {
4997
- state: await this.getStatePublicKey(),
4998
- spotMarket: spotMarket.pubkey,
4999
- insuranceFundStake: ifStakeAccountPublicKey,
5000
- userStats: userStatsPublicKey,
5001
- signer: this.wallet.publicKey,
5002
- insuranceFundVault: spotMarket.insuranceFund.vault,
5003
- },
5004
- });
5005
- return ix;
5006
- }
5007
5123
  async settleRevenueToInsuranceFund(spotMarketIndex, txParams) {
5008
5124
  const tx = await this.buildTransaction(await this.getSettleRevenueToInsuranceFundIx(spotMarketIndex), txParams);
5009
5125
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
5010
5126
  return txSig;
5011
5127
  }
5012
5128
  async getSettleRevenueToInsuranceFundIx(spotMarketIndex) {
5013
- const spotMarketAccount = this.getSpotMarketAccount(spotMarketIndex);
5129
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(spotMarketIndex);
5014
5130
  const tokenProgramId = this.getTokenProgramForSpotMarket(spotMarketAccount);
5015
5131
  const remainingAccounts = [];
5016
5132
  this.addTokenMintToRemainingAccounts(spotMarketAccount, remainingAccounts);
@@ -5030,17 +5146,35 @@ class VelocityClient {
5030
5146
  });
5031
5147
  return ix;
5032
5148
  }
5149
+ async sweepPerpMarketFees(perpMarketIndex, txParams) {
5150
+ const tx = await this.buildTransaction(await this.getSweepPerpMarketFeesIx(perpMarketIndex), txParams);
5151
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
5152
+ return txSig;
5153
+ }
5154
+ async getSweepPerpMarketFeesIx(perpMarketIndex) {
5155
+ const perpMarketAccount = this.getPerpMarketAccountOrThrow(perpMarketIndex);
5156
+ const spotMarketAccount = this.getSpotMarketAccountOrThrow(perpMarketAccount.quoteSpotMarketIndex);
5157
+ const ix = await this.program.instruction.sweepPerpMarketFees(perpMarketIndex, {
5158
+ accounts: {
5159
+ state: await this.getStatePublicKey(),
5160
+ perpMarket: perpMarketAccount.pubkey,
5161
+ spotMarket: spotMarketAccount.pubkey,
5162
+ oracle: perpMarketAccount.oracle,
5163
+ },
5164
+ });
5165
+ return ix;
5166
+ }
5033
5167
  async resolvePerpPnlDeficit(spotMarketIndex, perpMarketIndex, txParams) {
5034
5168
  const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getResolvePerpPnlDeficitIx(spotMarketIndex, perpMarketIndex), txParams), [], this.opts);
5035
5169
  return txSig;
5036
5170
  }
5037
5171
  async getResolvePerpPnlDeficitIx(spotMarketIndex, perpMarketIndex) {
5038
5172
  const remainingAccounts = this.getRemainingAccounts({
5039
- userAccounts: [this.getUserAccount()],
5173
+ userAccounts: [],
5040
5174
  writablePerpMarketIndexes: [perpMarketIndex],
5041
5175
  writableSpotMarketIndexes: [spotMarketIndex],
5042
5176
  });
5043
- const spotMarket = this.getSpotMarketAccount(spotMarketIndex);
5177
+ const spotMarket = this.getSpotMarketAccountOrThrow(spotMarketIndex);
5044
5178
  const tokenProgramId = this.getTokenProgramForSpotMarket(spotMarket);
5045
5179
  return await this.program.instruction.resolvePerpPnlDeficit(spotMarketIndex, perpMarketIndex, {
5046
5180
  accounts: {
@@ -5055,7 +5189,7 @@ class VelocityClient {
5055
5189
  });
5056
5190
  }
5057
5191
  async getDepositIntoSpotMarketRevenuePoolIx(marketIndex, amount, userTokenAccountPublicKey) {
5058
- const spotMarket = await this.getSpotMarketAccount(marketIndex);
5192
+ const spotMarket = await this.getSpotMarketAccountOrThrow(marketIndex);
5059
5193
  const remainingAccounts = [];
5060
5194
  this.addTokenMintToRemainingAccounts(spotMarket, remainingAccounts);
5061
5195
  if (this.isTransferHook(spotMarket)) {
@@ -5089,8 +5223,8 @@ class VelocityClient {
5089
5223
  }
5090
5224
  getPerpMarketExtendedInfo(marketIndex) {
5091
5225
  var _a, _b;
5092
- const marketAccount = this.getPerpMarketAccount(marketIndex);
5093
- const quoteAccount = this.getSpotMarketAccount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
5226
+ const marketAccount = this.getPerpMarketAccountOrThrow(marketIndex);
5227
+ const quoteAccount = this.getSpotMarketAccountOrThrow(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
5094
5228
  const extendedInfo = {
5095
5229
  marketIndex,
5096
5230
  minOrderSize: (_a = marketAccount.marketStats) === null || _a === void 0 ? void 0 : _a.minOrderSize,
@@ -5121,12 +5255,12 @@ class VelocityClient {
5121
5255
  let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
5122
5256
  let makerFee = feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
5123
5257
  if (marketIndex !== undefined) {
5124
- let marketAccount = null;
5258
+ let marketAccount;
5125
5259
  if ((0, types_1.isVariant)(marketType, 'perp')) {
5126
- marketAccount = this.getPerpMarketAccount(marketIndex);
5260
+ marketAccount = this.getPerpMarketAccountOrThrow(marketIndex);
5127
5261
  }
5128
5262
  else {
5129
- marketAccount = this.getSpotMarketAccount(marketIndex);
5263
+ marketAccount = this.getSpotMarketAccountOrThrow(marketIndex);
5130
5264
  }
5131
5265
  takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
5132
5266
  makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
@@ -5189,7 +5323,7 @@ class VelocityClient {
5189
5323
  return [verifyIx, ix];
5190
5324
  }
5191
5325
  async getPauseSpotMarketDepositWithdrawIx(spotMarketIndex) {
5192
- const spotMarket = await this.getSpotMarketAccount(spotMarketIndex);
5326
+ const spotMarket = await this.getSpotMarketAccountOrThrow(spotMarketIndex);
5193
5327
  return this.program.instruction.pauseSpotMarketDepositWithdraw({
5194
5328
  accounts: {
5195
5329
  state: await this.getStatePublicKey(),
@@ -5223,7 +5357,7 @@ class VelocityClient {
5223
5357
  programId: this.program.programId,
5224
5358
  keys: [
5225
5359
  {
5226
- pubkey: this.getPerpMarketAccount(marketIndex).pubkey,
5360
+ pubkey: this.getPerpMarketAccountOrThrow(marketIndex).pubkey,
5227
5361
  isWritable: true,
5228
5362
  isSigner: false,
5229
5363
  },
@@ -5266,7 +5400,7 @@ class VelocityClient {
5266
5400
  programId: this.program.programId,
5267
5401
  keys: [
5268
5402
  {
5269
- pubkey: this.getPerpMarketAccount(marketIndex).pubkey,
5403
+ pubkey: this.getPerpMarketAccountOrThrow(marketIndex).pubkey,
5270
5404
  isWritable: true,
5271
5405
  isSigner: false,
5272
5406
  },
@@ -5360,7 +5494,7 @@ class VelocityClient {
5360
5494
  state: await this.getStatePublicKey(),
5361
5495
  keeper: this.wallet.publicKey,
5362
5496
  ammCache: (0, pda_1.getAmmCachePublicKey)(this.program.programId),
5363
- quoteMarket: this.getSpotMarketAccount(0).pubkey,
5497
+ quoteMarket: this.getSpotMarketAccountOrThrow(0).pubkey,
5364
5498
  },
5365
5499
  remainingAccounts,
5366
5500
  });
@@ -5370,7 +5504,7 @@ class VelocityClient {
5370
5504
  return txSig;
5371
5505
  }
5372
5506
  async getUpdateConstituentOracleInfoIx(constituent) {
5373
- const spotMarket = this.getSpotMarketAccount(constituent.spotMarketIndex);
5507
+ const spotMarket = this.getSpotMarketAccountOrThrow(constituent.spotMarketIndex);
5374
5508
  return this.program.instruction.updateConstituentOracleInfo({
5375
5509
  accounts: {
5376
5510
  keeper: this.wallet.publicKey,
@@ -5392,12 +5526,12 @@ class VelocityClient {
5392
5526
  });
5393
5527
  const constituentInTokenAccount = (0, pda_1.getConstituentVaultPublicKey)(this.program.programId, lpPool, inMarketIndex);
5394
5528
  const constituentOutTokenAccount = (0, pda_1.getConstituentVaultPublicKey)(this.program.programId, lpPool, outMarketIndex);
5395
- const userInTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(this.getSpotMarketAccount(inMarketIndex).mint, userAuthority);
5396
- const userOutTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(this.getSpotMarketAccount(outMarketIndex).mint, userAuthority);
5529
+ const userInTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(this.getSpotMarketAccountOrThrow(inMarketIndex).mint, userAuthority);
5530
+ const userOutTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(this.getSpotMarketAccountOrThrow(outMarketIndex).mint, userAuthority);
5397
5531
  const inConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool, inMarketIndex);
5398
5532
  const outConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool, outMarketIndex);
5399
- const inMarketMint = this.getSpotMarketAccount(inMarketIndex).mint;
5400
- const outMarketMint = this.getSpotMarketAccount(outMarketIndex).mint;
5533
+ const inMarketMint = this.getSpotMarketAccountOrThrow(inMarketIndex).mint;
5534
+ const outMarketMint = this.getSpotMarketAccountOrThrow(outMarketIndex).mint;
5401
5535
  const constituentTargetBase = (0, pda_1.getConstituentTargetBasePublicKey)(this.program.programId, lpPool);
5402
5536
  return this.program.instruction.lpPoolSwap(inMarketIndex, outMarketIndex, inAmount, minOutAmount, {
5403
5537
  remainingAccounts,
@@ -5469,7 +5603,7 @@ class VelocityClient {
5469
5603
  userAccounts: [],
5470
5604
  writableSpotMarketIndexes: [inMarketIndex],
5471
5605
  });
5472
- const spotMarket = this.getSpotMarketAccount(inMarketIndex);
5606
+ const spotMarket = this.getSpotMarketAccountOrThrow(inMarketIndex);
5473
5607
  const inMarketMint = spotMarket.mint;
5474
5608
  const isSolMarket = inMarketMint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
5475
5609
  let wSolTokenAccount;
@@ -5531,7 +5665,7 @@ class VelocityClient {
5531
5665
  userAccounts: [],
5532
5666
  readableSpotMarketIndexes: [inMarketIndex],
5533
5667
  });
5534
- const spotMarket = this.getSpotMarketAccount(inMarketIndex);
5668
+ const spotMarket = this.getSpotMarketAccountOrThrow(inMarketIndex);
5535
5669
  const inMarketMint = spotMarket.mint;
5536
5670
  const inConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool.pubkey, inMarketIndex);
5537
5671
  const lpMint = lpPool.mint;
@@ -5564,7 +5698,7 @@ class VelocityClient {
5564
5698
  userAccounts: [],
5565
5699
  writableSpotMarketIndexes: [outMarketIndex],
5566
5700
  });
5567
- const spotMarket = this.getSpotMarketAccount(outMarketIndex);
5701
+ const spotMarket = this.getSpotMarketAccountOrThrow(outMarketIndex);
5568
5702
  const outMarketMint = spotMarket.mint;
5569
5703
  const outConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool.pubkey, outMarketIndex);
5570
5704
  if (outMarketMint.equals(spotMarkets_1.WRAPPED_SOL_MINT)) {
@@ -5610,7 +5744,7 @@ class VelocityClient {
5610
5744
  userAccounts: [],
5611
5745
  writableSpotMarketIndexes: [outMarketIndex],
5612
5746
  });
5613
- const spotMarket = this.getSpotMarketAccount(outMarketIndex);
5747
+ const spotMarket = this.getSpotMarketAccountOrThrow(outMarketIndex);
5614
5748
  const outMarketMint = spotMarket.mint;
5615
5749
  const outConstituent = (0, pda_1.getConstituentPublicKey)(this.program.programId, lpPool.pubkey, outMarketIndex);
5616
5750
  const lpMint = lpPool.mint;
@@ -5710,7 +5844,7 @@ class VelocityClient {
5710
5844
  const remainingAccounts = [];
5711
5845
  remainingAccounts.push(...perpMarketIndexes.map((index) => {
5712
5846
  return {
5713
- pubkey: this.getPerpMarketAccount(index).pubkey,
5847
+ pubkey: this.getPerpMarketAccountOrThrow(index).pubkey,
5714
5848
  isSigner: false,
5715
5849
  isWritable: true,
5716
5850
  };
@@ -5824,7 +5958,7 @@ class VelocityClient {
5824
5958
  });
5825
5959
  }
5826
5960
  isOrderIncreasingPosition(orderParams, subAccountId) {
5827
- const userAccount = this.getUserAccount(subAccountId);
5961
+ const userAccount = this.getUserAccountOrThrow(subAccountId);
5828
5962
  const perpPosition = userAccount.perpPositions.find((p) => p.marketIndex === orderParams.marketIndex);
5829
5963
  if (!perpPosition)
5830
5964
  return true;