@haneullabs/deepbook-v3 0.1.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 (378) hide show
  1. package/CHANGELOG.md +984 -0
  2. package/README.md +1 -0
  3. package/dist/cjs/client.d.ts +861 -0
  4. package/dist/cjs/client.js +1982 -0
  5. package/dist/cjs/client.js.map +7 -0
  6. package/dist/cjs/contracts/deepbook/account.d.ts +105 -0
  7. package/dist/cjs/contracts/deepbook/account.js +163 -0
  8. package/dist/cjs/contracts/deepbook/account.js.map +7 -0
  9. package/dist/cjs/contracts/deepbook/balance_manager.d.ts +268 -0
  10. package/dist/cjs/contracts/deepbook/balance_manager.js +361 -0
  11. package/dist/cjs/contracts/deepbook/balance_manager.js.map +7 -0
  12. package/dist/cjs/contracts/deepbook/balances.d.ts +10 -0
  13. package/dist/cjs/contracts/deepbook/balances.js +35 -0
  14. package/dist/cjs/contracts/deepbook/balances.js.map +7 -0
  15. package/dist/cjs/contracts/deepbook/big_vector.d.ts +62 -0
  16. package/dist/cjs/contracts/deepbook/big_vector.js +80 -0
  17. package/dist/cjs/contracts/deepbook/big_vector.js.map +7 -0
  18. package/dist/cjs/contracts/deepbook/book.d.ts +34 -0
  19. package/dist/cjs/contracts/deepbook/book.js +50 -0
  20. package/dist/cjs/contracts/deepbook/book.js.map +7 -0
  21. package/dist/cjs/contracts/deepbook/constants.d.ts +176 -0
  22. package/dist/cjs/contracts/deepbook/constants.js +338 -0
  23. package/dist/cjs/contracts/deepbook/constants.js.map +7 -0
  24. package/dist/cjs/contracts/deepbook/deep_price.d.ts +60 -0
  25. package/dist/cjs/contracts/deepbook/deep_price.js +87 -0
  26. package/dist/cjs/contracts/deepbook/deep_price.js.map +7 -0
  27. package/dist/cjs/contracts/deepbook/deps/std/type_name.d.ts +14 -0
  28. package/dist/cjs/contracts/deepbook/deps/std/type_name.js +42 -0
  29. package/dist/cjs/contracts/deepbook/deps/std/type_name.js.map +7 -0
  30. package/dist/cjs/contracts/deepbook/deps/sui/bag.d.ts +33 -0
  31. package/dist/cjs/contracts/deepbook/deps/sui/bag.js +47 -0
  32. package/dist/cjs/contracts/deepbook/deps/sui/bag.js.map +7 -0
  33. package/dist/cjs/contracts/deepbook/deps/sui/balance.d.ts +9 -0
  34. package/dist/cjs/contracts/deepbook/deps/sui/balance.js +33 -0
  35. package/dist/cjs/contracts/deepbook/deps/sui/balance.js.map +7 -0
  36. package/dist/cjs/contracts/deepbook/deps/sui/object.d.ts +5 -0
  37. package/dist/cjs/contracts/deepbook/deps/sui/object.js +33 -0
  38. package/dist/cjs/contracts/deepbook/deps/sui/object.js.map +7 -0
  39. package/dist/cjs/contracts/deepbook/deps/sui/table.d.ts +28 -0
  40. package/dist/cjs/contracts/deepbook/deps/sui/table.js +47 -0
  41. package/dist/cjs/contracts/deepbook/deps/sui/table.js.map +7 -0
  42. package/dist/cjs/contracts/deepbook/deps/sui/vec_map.d.ts +27 -0
  43. package/dist/cjs/contracts/deepbook/deps/sui/vec_map.js +45 -0
  44. package/dist/cjs/contracts/deepbook/deps/sui/vec_map.js.map +7 -0
  45. package/dist/cjs/contracts/deepbook/deps/sui/vec_set.d.ts +15 -0
  46. package/dist/cjs/contracts/deepbook/deps/sui/vec_set.js +35 -0
  47. package/dist/cjs/contracts/deepbook/deps/sui/vec_set.js.map +7 -0
  48. package/dist/cjs/contracts/deepbook/deps/sui/versioned.d.ts +7 -0
  49. package/dist/cjs/contracts/deepbook/deps/sui/versioned.js +45 -0
  50. package/dist/cjs/contracts/deepbook/deps/sui/versioned.js.map +7 -0
  51. package/dist/cjs/contracts/deepbook/ewma.d.ts +17 -0
  52. package/dist/cjs/contracts/deepbook/ewma.js +39 -0
  53. package/dist/cjs/contracts/deepbook/ewma.js.map +7 -0
  54. package/dist/cjs/contracts/deepbook/fill.d.ts +153 -0
  55. package/dist/cjs/contracts/deepbook/fill.js +251 -0
  56. package/dist/cjs/contracts/deepbook/fill.js.map +7 -0
  57. package/dist/cjs/contracts/deepbook/governance.d.ts +63 -0
  58. package/dist/cjs/contracts/deepbook/governance.js +79 -0
  59. package/dist/cjs/contracts/deepbook/governance.js.map +7 -0
  60. package/dist/cjs/contracts/deepbook/history.d.ts +60 -0
  61. package/dist/cjs/contracts/deepbook/history.js +78 -0
  62. package/dist/cjs/contracts/deepbook/history.js.map +7 -0
  63. package/dist/cjs/contracts/deepbook/math.d.ts +94 -0
  64. package/dist/cjs/contracts/deepbook/math.js +132 -0
  65. package/dist/cjs/contracts/deepbook/math.js.map +7 -0
  66. package/dist/cjs/contracts/deepbook/order.d.ts +135 -0
  67. package/dist/cjs/contracts/deepbook/order.js +218 -0
  68. package/dist/cjs/contracts/deepbook/order.js.map +7 -0
  69. package/dist/cjs/contracts/deepbook/order_info.d.ts +296 -0
  70. package/dist/cjs/contracts/deepbook/order_info.js +386 -0
  71. package/dist/cjs/contracts/deepbook/order_info.js.map +7 -0
  72. package/dist/cjs/contracts/deepbook/order_query.d.ts +85 -0
  73. package/dist/cjs/contracts/deepbook/order_query.js +96 -0
  74. package/dist/cjs/contracts/deepbook/order_query.js.map +7 -0
  75. package/dist/cjs/contracts/deepbook/pool.d.ts +1218 -0
  76. package/dist/cjs/contracts/deepbook/pool.js +1236 -0
  77. package/dist/cjs/contracts/deepbook/pool.js.map +7 -0
  78. package/dist/cjs/contracts/deepbook/registry.d.ts +134 -0
  79. package/dist/cjs/contracts/deepbook/registry.js +182 -0
  80. package/dist/cjs/contracts/deepbook/registry.js.map +7 -0
  81. package/dist/cjs/contracts/deepbook/state.d.ts +117 -0
  82. package/dist/cjs/contracts/deepbook/state.js +104 -0
  83. package/dist/cjs/contracts/deepbook/state.js.map +7 -0
  84. package/dist/cjs/contracts/deepbook/trade_params.d.ts +7 -0
  85. package/dist/cjs/contracts/deepbook/trade_params.js +35 -0
  86. package/dist/cjs/contracts/deepbook/trade_params.js.map +7 -0
  87. package/dist/cjs/contracts/deepbook/vault.d.ts +30 -0
  88. package/dist/cjs/contracts/deepbook/vault.js +65 -0
  89. package/dist/cjs/contracts/deepbook/vault.js.map +7 -0
  90. package/dist/cjs/contracts/utils/index.d.ts +25 -0
  91. package/dist/cjs/contracts/utils/index.js +148 -0
  92. package/dist/cjs/contracts/utils/index.js.map +7 -0
  93. package/dist/cjs/index.d.ts +23 -0
  94. package/dist/cjs/index.js +81 -0
  95. package/dist/cjs/index.js.map +7 -0
  96. package/dist/cjs/package.json +4 -0
  97. package/dist/cjs/pyth/PriceServiceConnection.d.ts +26 -0
  98. package/dist/cjs/pyth/PriceServiceConnection.js +68 -0
  99. package/dist/cjs/pyth/PriceServiceConnection.js.map +7 -0
  100. package/dist/cjs/pyth/pyth-helpers.d.ts +7 -0
  101. package/dist/cjs/pyth/pyth-helpers.js +36 -0
  102. package/dist/cjs/pyth/pyth-helpers.js.map +7 -0
  103. package/dist/cjs/pyth/pyth.d.ts +65 -0
  104. package/dist/cjs/pyth/pyth.js +269 -0
  105. package/dist/cjs/pyth/pyth.js.map +7 -0
  106. package/dist/cjs/transactions/balanceManager.d.ts +168 -0
  107. package/dist/cjs/transactions/balanceManager.js +384 -0
  108. package/dist/cjs/transactions/balanceManager.js.map +7 -0
  109. package/dist/cjs/transactions/deepbook.d.ts +477 -0
  110. package/dist/cjs/transactions/deepbook.js +1335 -0
  111. package/dist/cjs/transactions/deepbook.js.map +7 -0
  112. package/dist/cjs/transactions/deepbookAdmin.d.ts +105 -0
  113. package/dist/cjs/transactions/deepbookAdmin.js +334 -0
  114. package/dist/cjs/transactions/deepbookAdmin.js.map +7 -0
  115. package/dist/cjs/transactions/flashLoans.d.ts +56 -0
  116. package/dist/cjs/transactions/flashLoans.js +124 -0
  117. package/dist/cjs/transactions/flashLoans.js.map +7 -0
  118. package/dist/cjs/transactions/governance.d.ts +42 -0
  119. package/dist/cjs/transactions/governance.js +135 -0
  120. package/dist/cjs/transactions/governance.js.map +7 -0
  121. package/dist/cjs/transactions/marginAdmin.d.ts +133 -0
  122. package/dist/cjs/transactions/marginAdmin.js +393 -0
  123. package/dist/cjs/transactions/marginAdmin.js.map +7 -0
  124. package/dist/cjs/transactions/marginLiquidations.d.ts +61 -0
  125. package/dist/cjs/transactions/marginLiquidations.js +173 -0
  126. package/dist/cjs/transactions/marginLiquidations.js.map +7 -0
  127. package/dist/cjs/transactions/marginMaintainer.d.ts +80 -0
  128. package/dist/cjs/transactions/marginMaintainer.js +251 -0
  129. package/dist/cjs/transactions/marginMaintainer.js.map +7 -0
  130. package/dist/cjs/transactions/marginManager.d.ts +236 -0
  131. package/dist/cjs/transactions/marginManager.js +688 -0
  132. package/dist/cjs/transactions/marginManager.js.map +7 -0
  133. package/dist/cjs/transactions/marginPool.d.ts +134 -0
  134. package/dist/cjs/transactions/marginPool.js +330 -0
  135. package/dist/cjs/transactions/marginPool.js.map +7 -0
  136. package/dist/cjs/transactions/marginRegistry.d.ts +94 -0
  137. package/dist/cjs/transactions/marginRegistry.js +221 -0
  138. package/dist/cjs/transactions/marginRegistry.js.map +7 -0
  139. package/dist/cjs/transactions/marginTPSL.d.ts +93 -0
  140. package/dist/cjs/transactions/marginTPSL.js +286 -0
  141. package/dist/cjs/transactions/marginTPSL.js.map +7 -0
  142. package/dist/cjs/transactions/poolProxy.d.ts +104 -0
  143. package/dist/cjs/transactions/poolProxy.js +435 -0
  144. package/dist/cjs/transactions/poolProxy.js.map +7 -0
  145. package/dist/cjs/types/bcs.d.ts +5 -0
  146. package/dist/cjs/types/bcs.js +33 -0
  147. package/dist/cjs/types/bcs.js.map +7 -0
  148. package/dist/cjs/types/index.d.ts +218 -0
  149. package/dist/cjs/types/index.js +38 -0
  150. package/dist/cjs/types/index.js.map +7 -0
  151. package/dist/cjs/utils/config.d.ts +65 -0
  152. package/dist/cjs/utils/config.js +144 -0
  153. package/dist/cjs/utils/config.js.map +7 -0
  154. package/dist/cjs/utils/constants.d.ts +76 -0
  155. package/dist/cjs/utils/constants.js +382 -0
  156. package/dist/cjs/utils/constants.js.map +7 -0
  157. package/dist/cjs/utils/errors.d.ts +42 -0
  158. package/dist/cjs/utils/errors.js +70 -0
  159. package/dist/cjs/utils/errors.js.map +7 -0
  160. package/dist/cjs/utils/validation.d.ts +50 -0
  161. package/dist/cjs/utils/validation.js +67 -0
  162. package/dist/cjs/utils/validation.js.map +7 -0
  163. package/dist/esm/client.d.ts +861 -0
  164. package/dist/esm/client.js +1967 -0
  165. package/dist/esm/client.js.map +7 -0
  166. package/dist/esm/contracts/deepbook/account.d.ts +105 -0
  167. package/dist/esm/contracts/deepbook/account.js +133 -0
  168. package/dist/esm/contracts/deepbook/account.js.map +7 -0
  169. package/dist/esm/contracts/deepbook/balance_manager.d.ts +268 -0
  170. package/dist/esm/contracts/deepbook/balance_manager.js +331 -0
  171. package/dist/esm/contracts/deepbook/balance_manager.js.map +7 -0
  172. package/dist/esm/contracts/deepbook/balances.d.ts +10 -0
  173. package/dist/esm/contracts/deepbook/balances.js +15 -0
  174. package/dist/esm/contracts/deepbook/balances.js.map +7 -0
  175. package/dist/esm/contracts/deepbook/big_vector.d.ts +62 -0
  176. package/dist/esm/contracts/deepbook/big_vector.js +50 -0
  177. package/dist/esm/contracts/deepbook/big_vector.js.map +7 -0
  178. package/dist/esm/contracts/deepbook/book.d.ts +34 -0
  179. package/dist/esm/contracts/deepbook/book.js +20 -0
  180. package/dist/esm/contracts/deepbook/book.js.map +7 -0
  181. package/dist/esm/contracts/deepbook/constants.d.ts +176 -0
  182. package/dist/esm/contracts/deepbook/constants.js +318 -0
  183. package/dist/esm/contracts/deepbook/constants.js.map +7 -0
  184. package/dist/esm/contracts/deepbook/deep_price.d.ts +60 -0
  185. package/dist/esm/contracts/deepbook/deep_price.js +67 -0
  186. package/dist/esm/contracts/deepbook/deep_price.js.map +7 -0
  187. package/dist/esm/contracts/deepbook/deps/std/type_name.d.ts +14 -0
  188. package/dist/esm/contracts/deepbook/deps/std/type_name.js +22 -0
  189. package/dist/esm/contracts/deepbook/deps/std/type_name.js.map +7 -0
  190. package/dist/esm/contracts/deepbook/deps/sui/bag.d.ts +33 -0
  191. package/dist/esm/contracts/deepbook/deps/sui/bag.js +17 -0
  192. package/dist/esm/contracts/deepbook/deps/sui/bag.js.map +7 -0
  193. package/dist/esm/contracts/deepbook/deps/sui/balance.d.ts +9 -0
  194. package/dist/esm/contracts/deepbook/deps/sui/balance.js +13 -0
  195. package/dist/esm/contracts/deepbook/deps/sui/balance.js.map +7 -0
  196. package/dist/esm/contracts/deepbook/deps/sui/object.d.ts +5 -0
  197. package/dist/esm/contracts/deepbook/deps/sui/object.js +13 -0
  198. package/dist/esm/contracts/deepbook/deps/sui/object.js.map +7 -0
  199. package/dist/esm/contracts/deepbook/deps/sui/table.d.ts +28 -0
  200. package/dist/esm/contracts/deepbook/deps/sui/table.js +17 -0
  201. package/dist/esm/contracts/deepbook/deps/sui/table.js.map +7 -0
  202. package/dist/esm/contracts/deepbook/deps/sui/vec_map.d.ts +27 -0
  203. package/dist/esm/contracts/deepbook/deps/sui/vec_map.js +25 -0
  204. package/dist/esm/contracts/deepbook/deps/sui/vec_map.js.map +7 -0
  205. package/dist/esm/contracts/deepbook/deps/sui/vec_set.d.ts +15 -0
  206. package/dist/esm/contracts/deepbook/deps/sui/vec_set.js +15 -0
  207. package/dist/esm/contracts/deepbook/deps/sui/vec_set.js.map +7 -0
  208. package/dist/esm/contracts/deepbook/deps/sui/versioned.d.ts +7 -0
  209. package/dist/esm/contracts/deepbook/deps/sui/versioned.js +15 -0
  210. package/dist/esm/contracts/deepbook/deps/sui/versioned.js.map +7 -0
  211. package/dist/esm/contracts/deepbook/ewma.d.ts +17 -0
  212. package/dist/esm/contracts/deepbook/ewma.js +19 -0
  213. package/dist/esm/contracts/deepbook/ewma.js.map +7 -0
  214. package/dist/esm/contracts/deepbook/fill.d.ts +153 -0
  215. package/dist/esm/contracts/deepbook/fill.js +221 -0
  216. package/dist/esm/contracts/deepbook/fill.js.map +7 -0
  217. package/dist/esm/contracts/deepbook/governance.d.ts +63 -0
  218. package/dist/esm/contracts/deepbook/governance.js +49 -0
  219. package/dist/esm/contracts/deepbook/governance.js.map +7 -0
  220. package/dist/esm/contracts/deepbook/history.d.ts +60 -0
  221. package/dist/esm/contracts/deepbook/history.js +48 -0
  222. package/dist/esm/contracts/deepbook/history.js.map +7 -0
  223. package/dist/esm/contracts/deepbook/math.d.ts +94 -0
  224. package/dist/esm/contracts/deepbook/math.js +112 -0
  225. package/dist/esm/contracts/deepbook/math.js.map +7 -0
  226. package/dist/esm/contracts/deepbook/order.d.ts +135 -0
  227. package/dist/esm/contracts/deepbook/order.js +188 -0
  228. package/dist/esm/contracts/deepbook/order.js.map +7 -0
  229. package/dist/esm/contracts/deepbook/order_info.d.ts +296 -0
  230. package/dist/esm/contracts/deepbook/order_info.js +356 -0
  231. package/dist/esm/contracts/deepbook/order_info.js.map +7 -0
  232. package/dist/esm/contracts/deepbook/order_query.d.ts +85 -0
  233. package/dist/esm/contracts/deepbook/order_query.js +66 -0
  234. package/dist/esm/contracts/deepbook/order_query.js.map +7 -0
  235. package/dist/esm/contracts/deepbook/pool.d.ts +1218 -0
  236. package/dist/esm/contracts/deepbook/pool.js +1206 -0
  237. package/dist/esm/contracts/deepbook/pool.js.map +7 -0
  238. package/dist/esm/contracts/deepbook/registry.d.ts +134 -0
  239. package/dist/esm/contracts/deepbook/registry.js +152 -0
  240. package/dist/esm/contracts/deepbook/registry.js.map +7 -0
  241. package/dist/esm/contracts/deepbook/state.d.ts +117 -0
  242. package/dist/esm/contracts/deepbook/state.js +74 -0
  243. package/dist/esm/contracts/deepbook/state.js.map +7 -0
  244. package/dist/esm/contracts/deepbook/trade_params.d.ts +7 -0
  245. package/dist/esm/contracts/deepbook/trade_params.js +15 -0
  246. package/dist/esm/contracts/deepbook/trade_params.js.map +7 -0
  247. package/dist/esm/contracts/deepbook/vault.d.ts +30 -0
  248. package/dist/esm/contracts/deepbook/vault.js +35 -0
  249. package/dist/esm/contracts/deepbook/vault.js.map +7 -0
  250. package/dist/esm/contracts/utils/index.d.ts +25 -0
  251. package/dist/esm/contracts/utils/index.js +128 -0
  252. package/dist/esm/contracts/utils/index.js.map +7 -0
  253. package/dist/esm/index.d.ts +23 -0
  254. package/dist/esm/index.js +81 -0
  255. package/dist/esm/index.js.map +7 -0
  256. package/dist/esm/package.json +4 -0
  257. package/dist/esm/pyth/PriceServiceConnection.d.ts +26 -0
  258. package/dist/esm/pyth/PriceServiceConnection.js +38 -0
  259. package/dist/esm/pyth/PriceServiceConnection.js.map +7 -0
  260. package/dist/esm/pyth/pyth-helpers.d.ts +7 -0
  261. package/dist/esm/pyth/pyth-helpers.js +16 -0
  262. package/dist/esm/pyth/pyth-helpers.js.map +7 -0
  263. package/dist/esm/pyth/pyth.d.ts +65 -0
  264. package/dist/esm/pyth/pyth.js +249 -0
  265. package/dist/esm/pyth/pyth.js.map +7 -0
  266. package/dist/esm/transactions/balanceManager.d.ts +168 -0
  267. package/dist/esm/transactions/balanceManager.js +364 -0
  268. package/dist/esm/transactions/balanceManager.js.map +7 -0
  269. package/dist/esm/transactions/deepbook.d.ts +477 -0
  270. package/dist/esm/transactions/deepbook.js +1321 -0
  271. package/dist/esm/transactions/deepbook.js.map +7 -0
  272. package/dist/esm/transactions/deepbookAdmin.d.ts +105 -0
  273. package/dist/esm/transactions/deepbookAdmin.js +314 -0
  274. package/dist/esm/transactions/deepbookAdmin.js.map +7 -0
  275. package/dist/esm/transactions/flashLoans.d.ts +56 -0
  276. package/dist/esm/transactions/flashLoans.js +104 -0
  277. package/dist/esm/transactions/flashLoans.js.map +7 -0
  278. package/dist/esm/transactions/governance.d.ts +42 -0
  279. package/dist/esm/transactions/governance.js +115 -0
  280. package/dist/esm/transactions/governance.js.map +7 -0
  281. package/dist/esm/transactions/marginAdmin.d.ts +133 -0
  282. package/dist/esm/transactions/marginAdmin.js +373 -0
  283. package/dist/esm/transactions/marginAdmin.js.map +7 -0
  284. package/dist/esm/transactions/marginLiquidations.d.ts +61 -0
  285. package/dist/esm/transactions/marginLiquidations.js +153 -0
  286. package/dist/esm/transactions/marginLiquidations.js.map +7 -0
  287. package/dist/esm/transactions/marginMaintainer.d.ts +80 -0
  288. package/dist/esm/transactions/marginMaintainer.js +231 -0
  289. package/dist/esm/transactions/marginMaintainer.js.map +7 -0
  290. package/dist/esm/transactions/marginManager.d.ts +236 -0
  291. package/dist/esm/transactions/marginManager.js +668 -0
  292. package/dist/esm/transactions/marginManager.js.map +7 -0
  293. package/dist/esm/transactions/marginPool.d.ts +134 -0
  294. package/dist/esm/transactions/marginPool.js +310 -0
  295. package/dist/esm/transactions/marginPool.js.map +7 -0
  296. package/dist/esm/transactions/marginRegistry.d.ts +94 -0
  297. package/dist/esm/transactions/marginRegistry.js +201 -0
  298. package/dist/esm/transactions/marginRegistry.js.map +7 -0
  299. package/dist/esm/transactions/marginTPSL.d.ts +93 -0
  300. package/dist/esm/transactions/marginTPSL.js +266 -0
  301. package/dist/esm/transactions/marginTPSL.js.map +7 -0
  302. package/dist/esm/transactions/poolProxy.d.ts +104 -0
  303. package/dist/esm/transactions/poolProxy.js +415 -0
  304. package/dist/esm/transactions/poolProxy.js.map +7 -0
  305. package/dist/esm/types/bcs.d.ts +5 -0
  306. package/dist/esm/types/bcs.js +13 -0
  307. package/dist/esm/types/bcs.js.map +7 -0
  308. package/dist/esm/types/index.d.ts +218 -0
  309. package/dist/esm/types/index.js +18 -0
  310. package/dist/esm/types/index.js.map +7 -0
  311. package/dist/esm/utils/config.d.ts +65 -0
  312. package/dist/esm/utils/config.js +135 -0
  313. package/dist/esm/utils/config.js.map +7 -0
  314. package/dist/esm/utils/constants.d.ts +76 -0
  315. package/dist/esm/utils/constants.js +362 -0
  316. package/dist/esm/utils/constants.js.map +7 -0
  317. package/dist/esm/utils/errors.d.ts +42 -0
  318. package/dist/esm/utils/errors.js +50 -0
  319. package/dist/esm/utils/errors.js.map +7 -0
  320. package/dist/esm/utils/validation.d.ts +50 -0
  321. package/dist/esm/utils/validation.js +47 -0
  322. package/dist/esm/utils/validation.js.map +7 -0
  323. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  324. package/dist/tsconfig.tsbuildinfo +1 -0
  325. package/package.json +63 -0
  326. package/src/client.ts +2451 -0
  327. package/src/contracts/deepbook/account.ts +199 -0
  328. package/src/contracts/deepbook/balance_manager.ts +532 -0
  329. package/src/contracts/deepbook/balances.ts +19 -0
  330. package/src/contracts/deepbook/big_vector.ts +74 -0
  331. package/src/contracts/deepbook/book.ts +24 -0
  332. package/src/contracts/deepbook/constants.ts +458 -0
  333. package/src/contracts/deepbook/deep_price.ts +84 -0
  334. package/src/contracts/deepbook/deps/std/type_name.ts +23 -0
  335. package/src/contracts/deepbook/deps/sui/bag.ts +41 -0
  336. package/src/contracts/deepbook/deps/sui/balance.ts +18 -0
  337. package/src/contracts/deepbook/deps/sui/object.ts +14 -0
  338. package/src/contracts/deepbook/deps/sui/table.ts +36 -0
  339. package/src/contracts/deepbook/deps/sui/vec_map.ts +33 -0
  340. package/src/contracts/deepbook/deps/sui/vec_set.ts +22 -0
  341. package/src/contracts/deepbook/deps/sui/versioned.ts +13 -0
  342. package/src/contracts/deepbook/ewma.ts +26 -0
  343. package/src/contracts/deepbook/fill.ts +336 -0
  344. package/src/contracts/deepbook/governance.ts +52 -0
  345. package/src/contracts/deepbook/history.ts +51 -0
  346. package/src/contracts/deepbook/math.ts +210 -0
  347. package/src/contracts/deepbook/order.ts +269 -0
  348. package/src/contracts/deepbook/order_info.ts +513 -0
  349. package/src/contracts/deepbook/order_query.ts +115 -0
  350. package/src/contracts/deepbook/pool.ts +2269 -0
  351. package/src/contracts/deepbook/registry.ts +242 -0
  352. package/src/contracts/deepbook/state.ts +74 -0
  353. package/src/contracts/deepbook/trade_params.ts +16 -0
  354. package/src/contracts/deepbook/vault.ts +37 -0
  355. package/src/contracts/utils/index.ts +184 -0
  356. package/src/index.ts +95 -0
  357. package/src/pyth/PriceServiceConnection.ts +48 -0
  358. package/src/pyth/pyth-helpers.ts +23 -0
  359. package/src/pyth/pyth.ts +305 -0
  360. package/src/transactions/balanceManager.ts +399 -0
  361. package/src/transactions/deepbook.ts +1519 -0
  362. package/src/transactions/deepbookAdmin.ts +332 -0
  363. package/src/transactions/flashLoans.ts +123 -0
  364. package/src/transactions/governance.ts +123 -0
  365. package/src/transactions/marginAdmin.ts +396 -0
  366. package/src/transactions/marginLiquidations.ts +175 -0
  367. package/src/transactions/marginMaintainer.ts +280 -0
  368. package/src/transactions/marginManager.ts +708 -0
  369. package/src/transactions/marginPool.ts +339 -0
  370. package/src/transactions/marginRegistry.ts +212 -0
  371. package/src/transactions/marginTPSL.ts +296 -0
  372. package/src/transactions/poolProxy.ts +438 -0
  373. package/src/types/bcs.ts +8 -0
  374. package/src/types/index.ts +255 -0
  375. package/src/utils/config.ts +177 -0
  376. package/src/utils/constants.ts +374 -0
  377. package/src/utils/errors.ts +67 -0
  378. package/src/utils/validation.ts +91 -0
@@ -0,0 +1,1967 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
+ var _config, _address, _DeepBookClient_instances, formatTokenAmount_fn;
10
+ import { bcs } from "@haneullabs/haneul/bcs";
11
+ import { Account, Order, OrderDeepPrice, VecSet } from "./types/bcs.js";
12
+ import { Transaction } from "@haneullabs/haneul/transactions";
13
+ import { normalizeHaneulAddress } from "@haneullabs/haneul/utils";
14
+ import { BalanceManagerContract } from "./transactions/balanceManager.js";
15
+ import { DeepBookContract } from "./transactions/deepbook.js";
16
+ import { DeepBookAdminContract } from "./transactions/deepbookAdmin.js";
17
+ import { FlashLoanContract } from "./transactions/flashLoans.js";
18
+ import { GovernanceContract } from "./transactions/governance.js";
19
+ import {
20
+ DEEP_SCALAR,
21
+ DeepBookConfig,
22
+ FLOAT_SCALAR,
23
+ PRICE_INFO_OBJECT_MAX_AGE_MS
24
+ } from "./utils/config.js";
25
+ import { MarginAdminContract } from "./transactions/marginAdmin.js";
26
+ import { MarginMaintainerContract } from "./transactions/marginMaintainer.js";
27
+ import { MarginPoolContract } from "./transactions/marginPool.js";
28
+ import { MarginManagerContract } from "./transactions/marginManager.js";
29
+ import { MarginRegistryContract } from "./transactions/marginRegistry.js";
30
+ import { MarginLiquidationsContract } from "./transactions/marginLiquidations.js";
31
+ import { HaneulPriceServiceConnection } from "./pyth/pyth.js";
32
+ import { HaneulPythClient } from "./pyth/pyth.js";
33
+ import { PoolProxyContract } from "./transactions/poolProxy.js";
34
+ import { MarginTPSLContract } from "./transactions/marginTPSL.js";
35
+ class DeepBookClient {
36
+ /**
37
+ * @param {HaneulClient} client HaneulClient instance
38
+ * @param {string} address Address of the client
39
+ * @param {Environment} env Environment configuration
40
+ * @param {Object.<string, BalanceManager>} [balanceManagers] Optional initial BalanceManager map
41
+ * @param {Object.<string, MarginManager>} [marginManagers] Optional initial MarginManager map
42
+ * @param {CoinMap} [coins] Optional initial CoinMap
43
+ * @param {PoolMap} [pools] Optional initial PoolMap
44
+ * @param {string} [adminCap] Optional admin capability
45
+ * @param {string} [marginAdminCap] Optional margin admin capability
46
+ * @param {string} [marginMaintainerCap] Optional margin maintainer capability
47
+ */
48
+ constructor({
49
+ client,
50
+ address,
51
+ env,
52
+ balanceManagers,
53
+ marginManagers,
54
+ coins,
55
+ pools,
56
+ adminCap,
57
+ marginAdminCap,
58
+ marginMaintainerCap
59
+ }) {
60
+ __privateAdd(this, _DeepBookClient_instances);
61
+ __privateAdd(this, _config);
62
+ __privateAdd(this, _address);
63
+ this.client = client;
64
+ __privateSet(this, _address, normalizeHaneulAddress(address));
65
+ __privateSet(this, _config, new DeepBookConfig({
66
+ address: __privateGet(this, _address),
67
+ env,
68
+ balanceManagers,
69
+ marginManagers,
70
+ coins,
71
+ pools,
72
+ adminCap,
73
+ marginAdminCap,
74
+ marginMaintainerCap
75
+ }));
76
+ this.balanceManager = new BalanceManagerContract(__privateGet(this, _config));
77
+ this.deepBook = new DeepBookContract(__privateGet(this, _config));
78
+ this.deepBookAdmin = new DeepBookAdminContract(__privateGet(this, _config));
79
+ this.flashLoans = new FlashLoanContract(__privateGet(this, _config));
80
+ this.governance = new GovernanceContract(__privateGet(this, _config));
81
+ this.marginAdmin = new MarginAdminContract(__privateGet(this, _config));
82
+ this.marginMaintainer = new MarginMaintainerContract(__privateGet(this, _config));
83
+ this.marginPool = new MarginPoolContract(__privateGet(this, _config));
84
+ this.marginManager = new MarginManagerContract(__privateGet(this, _config));
85
+ this.marginRegistry = new MarginRegistryContract(__privateGet(this, _config));
86
+ this.marginLiquidations = new MarginLiquidationsContract(__privateGet(this, _config));
87
+ this.poolProxy = new PoolProxyContract(__privateGet(this, _config));
88
+ this.marginTPSL = new MarginTPSLContract(__privateGet(this, _config));
89
+ }
90
+ /**
91
+ * @description Check the balance of a balance manager for a specific coin
92
+ * @param {string} managerKey Key of the balance manager
93
+ * @param {string} coinKey Key of the coin
94
+ * @returns {Promise<{ coinType: string, balance: number }>} An object with coin type and balance
95
+ */
96
+ async checkManagerBalance(managerKey, coinKey) {
97
+ const tx = new Transaction();
98
+ const coin = __privateGet(this, _config).getCoin(coinKey);
99
+ tx.add(this.balanceManager.checkManagerBalance(managerKey, coinKey));
100
+ const res = await this.client.devInspectTransactionBlock({
101
+ sender: __privateGet(this, _address),
102
+ transactionBlock: tx
103
+ });
104
+ const bytes = res.results[0].returnValues[0][0];
105
+ const parsed_balance = bcs.U64.parse(new Uint8Array(bytes));
106
+ const balanceNumber = Number(parsed_balance);
107
+ const adjusted_balance = balanceNumber / coin.scalar;
108
+ return {
109
+ coinType: coin.type,
110
+ balance: Number(adjusted_balance.toFixed(9))
111
+ };
112
+ }
113
+ /**
114
+ * @description Check if a pool is whitelisted
115
+ * @param {string} poolKey Key of the pool
116
+ * @returns {Promise<boolean>} Boolean indicating if the pool is whitelisted
117
+ */
118
+ async whitelisted(poolKey) {
119
+ const tx = new Transaction();
120
+ tx.add(this.deepBook.whitelisted(poolKey));
121
+ const res = await this.client.devInspectTransactionBlock({
122
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
123
+ transactionBlock: tx
124
+ });
125
+ const bytes = res.results[0].returnValues[0][0];
126
+ const whitelisted = bcs.Bool.parse(new Uint8Array(bytes));
127
+ return whitelisted;
128
+ }
129
+ /**
130
+ * @description Get the quote quantity out for a given base quantity
131
+ * @param {string} poolKey Key of the pool
132
+ * @param {number} baseQuantity Base quantity to convert
133
+ * @returns {Promise<{ baseQuantity: number, baseOut: number, quoteOut: number, deepRequired: number }>}
134
+ * An object with base quantity, base out, quote out, and deep required for the dry run
135
+ */
136
+ async getQuoteQuantityOut(poolKey, baseQuantity) {
137
+ const tx = new Transaction();
138
+ const pool = __privateGet(this, _config).getPool(poolKey);
139
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
140
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
141
+ tx.add(this.deepBook.getQuoteQuantityOut(poolKey, baseQuantity));
142
+ const res = await this.client.devInspectTransactionBlock({
143
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
144
+ transactionBlock: tx
145
+ });
146
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
147
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
148
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
149
+ return {
150
+ baseQuantity,
151
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
152
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
153
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
154
+ };
155
+ }
156
+ /**
157
+ * @description Get the base quantity out for a given quote quantity
158
+ * @param {string} poolKey Key of the pool
159
+ * @param {number} quoteQuantity Quote quantity to convert
160
+ * @returns {Promise<{ quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number }>}
161
+ * An object with quote quantity, base out, quote out, and deep required for the dry run
162
+ */
163
+ async getBaseQuantityOut(poolKey, quoteQuantity) {
164
+ const tx = new Transaction();
165
+ const pool = __privateGet(this, _config).getPool(poolKey);
166
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
167
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
168
+ tx.add(this.deepBook.getBaseQuantityOut(poolKey, quoteQuantity));
169
+ const res = await this.client.devInspectTransactionBlock({
170
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
171
+ transactionBlock: tx
172
+ });
173
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
174
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
175
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
176
+ return {
177
+ quoteQuantity,
178
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
179
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
180
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
181
+ };
182
+ }
183
+ /**
184
+ * @description Get the output quantities for given base and quote quantities. Only one quantity can be non-zero
185
+ * @param {string} poolKey Key of the pool
186
+ * @param {number} baseQuantity Base quantity to convert
187
+ * @param {number} quoteQuantity Quote quantity to convert
188
+ * @returns {Promise<{ baseQuantity: number, quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number }>}
189
+ * An object with base quantity, quote quantity, base out, quote out, and deep required for the dry run
190
+ */
191
+ async getQuantityOut(poolKey, baseQuantity, quoteQuantity) {
192
+ const tx = new Transaction();
193
+ const pool = __privateGet(this, _config).getPool(poolKey);
194
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
195
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
196
+ tx.add(this.deepBook.getQuantityOut(poolKey, baseQuantity, quoteQuantity));
197
+ const res = await this.client.devInspectTransactionBlock({
198
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
199
+ transactionBlock: tx
200
+ });
201
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
202
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
203
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
204
+ return {
205
+ baseQuantity,
206
+ quoteQuantity,
207
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
208
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
209
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
210
+ };
211
+ }
212
+ /**
213
+ * @description Get open orders for a balance manager in a pool
214
+ * @param {string} poolKey Key of the pool
215
+ * @param {string} managerKey Key of the balance manager
216
+ * @returns {Promise<Array>} An array of open order IDs
217
+ */
218
+ async accountOpenOrders(poolKey, managerKey) {
219
+ const tx = new Transaction();
220
+ tx.add(this.deepBook.accountOpenOrders(poolKey, managerKey));
221
+ const res = await this.client.devInspectTransactionBlock({
222
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
223
+ transactionBlock: tx
224
+ });
225
+ const order_ids = res.results[0].returnValues[0][0];
226
+ return VecSet(bcs.u128()).parse(new Uint8Array(order_ids)).contents;
227
+ }
228
+ /**
229
+ * @description Get the order information for a specific order in a pool
230
+ * @param {string} poolKey Key of the pool
231
+ * @param {string} orderId Order ID
232
+ * @returns {Promise<Object>} A promise that resolves to an object containing the order information
233
+ */
234
+ async getOrder(poolKey, orderId) {
235
+ const tx = new Transaction();
236
+ tx.add(this.deepBook.getOrder(poolKey, orderId));
237
+ const res = await this.client.devInspectTransactionBlock({
238
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
239
+ transactionBlock: tx
240
+ });
241
+ try {
242
+ const orderInformation = res.results[0].returnValues[0][0];
243
+ return Order.parse(new Uint8Array(orderInformation));
244
+ } catch {
245
+ return null;
246
+ }
247
+ }
248
+ /**
249
+ * @description Get the order information for a specific order in a pool, with normalized price
250
+ * @param {string} poolKey Key of the pool
251
+ * @param {string} orderId Order ID
252
+ * @returns {Promise<Object>} A promise that resolves to an object containing the order information with normalized price
253
+ */
254
+ async getOrderNormalized(poolKey, orderId) {
255
+ const tx = new Transaction();
256
+ tx.add(this.deepBook.getOrder(poolKey, orderId));
257
+ const res = await this.client.devInspectTransactionBlock({
258
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
259
+ transactionBlock: tx
260
+ });
261
+ try {
262
+ const orderInformation = res.results[0].returnValues[0][0];
263
+ const orderInfo = Order.parse(new Uint8Array(orderInformation));
264
+ if (!orderInfo) {
265
+ return null;
266
+ }
267
+ const baseCoin = __privateGet(this, _config).getCoin(__privateGet(this, _config).getPool(poolKey).baseCoin);
268
+ const quoteCoin = __privateGet(this, _config).getCoin(__privateGet(this, _config).getPool(poolKey).quoteCoin);
269
+ const { isBid, price: rawPrice } = this.decodeOrderId(BigInt(orderInfo.order_id));
270
+ const normalizedPrice = rawPrice * baseCoin.scalar / quoteCoin.scalar / FLOAT_SCALAR;
271
+ const normalizedOrderInfo = {
272
+ ...orderInfo,
273
+ quantity: String((Number(orderInfo.quantity) / baseCoin.scalar).toFixed(9)),
274
+ filled_quantity: String((Number(orderInfo.filled_quantity) / baseCoin.scalar).toFixed(9)),
275
+ order_deep_price: {
276
+ ...orderInfo.order_deep_price,
277
+ deep_per_asset: String(
278
+ (Number(orderInfo.order_deep_price.deep_per_asset) / DEEP_SCALAR).toFixed(9)
279
+ )
280
+ },
281
+ isBid,
282
+ normalized_price: normalizedPrice.toFixed(9)
283
+ };
284
+ return normalizedOrderInfo;
285
+ } catch {
286
+ return null;
287
+ }
288
+ }
289
+ /**
290
+ * @description Retrieves information for multiple specific orders in a pool.
291
+ * @param {string} poolKey - The key identifying the pool from which to retrieve order information.
292
+ * @param {string[]} orderIds - List of order IDs to retrieve information for.
293
+ * @returns {Promise<Object[] | null>} A promise that resolves to an array of order objects, each containing details such as
294
+ * balance manager ID, order ID, client order ID, quantity, filled quantity, fee information, order price, epoch, status,
295
+ * and expiration timestamp. Returns `null` if the retrieval fails.
296
+ */
297
+ async getOrders(poolKey, orderIds) {
298
+ const tx = new Transaction();
299
+ tx.add(this.deepBook.getOrders(poolKey, orderIds));
300
+ const res = await this.client.devInspectTransactionBlock({
301
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
302
+ transactionBlock: tx
303
+ });
304
+ try {
305
+ const orderInformation = res.results[0].returnValues[0][0];
306
+ return bcs.vector(Order).parse(new Uint8Array(orderInformation));
307
+ } catch {
308
+ return null;
309
+ }
310
+ }
311
+ /**
312
+ * @description Get level 2 order book specifying range of price
313
+ * @param {string} poolKey Key of the pool
314
+ * @param {number} priceLow Lower bound of the price range
315
+ * @param {number} priceHigh Upper bound of the price range
316
+ * @param {boolean} isBid Whether to get bid or ask orders
317
+ * @returns {Promise<{ prices: Array<number>, quantities: Array<number> }>}
318
+ * An object with arrays of prices and quantities
319
+ */
320
+ async getLevel2Range(poolKey, priceLow, priceHigh, isBid) {
321
+ const tx = new Transaction();
322
+ const pool = __privateGet(this, _config).getPool(poolKey);
323
+ const baseCoin = __privateGet(this, _config).getCoin(pool.baseCoin);
324
+ const quoteCoin = __privateGet(this, _config).getCoin(pool.quoteCoin);
325
+ tx.add(this.deepBook.getLevel2Range(poolKey, priceLow, priceHigh, isBid));
326
+ const res = await this.client.devInspectTransactionBlock({
327
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
328
+ transactionBlock: tx
329
+ });
330
+ const prices = res.results[0].returnValues[0][0];
331
+ const parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(prices));
332
+ const quantities = res.results[0].returnValues[1][0];
333
+ const parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(quantities));
334
+ return {
335
+ prices: parsed_prices.map(
336
+ (price) => Number((Number(price) / FLOAT_SCALAR / quoteCoin.scalar * baseCoin.scalar).toFixed(9))
337
+ ),
338
+ quantities: parsed_quantities.map(
339
+ (price) => Number((Number(price) / baseCoin.scalar).toFixed(9))
340
+ )
341
+ };
342
+ }
343
+ /**
344
+ * @description Get level 2 order book ticks from mid-price for a pool
345
+ * @param {string} poolKey Key of the pool
346
+ * @param {number} ticks Number of ticks from mid-price
347
+ * @returns {Promise<{ bid_prices: Array<number>, bid_quantities: Array<number>, ask_prices: Array<number>, ask_quantities: Array<number> }>}
348
+ * An object with arrays of prices and quantities
349
+ */
350
+ async getLevel2TicksFromMid(poolKey, ticks) {
351
+ const tx = new Transaction();
352
+ const pool = __privateGet(this, _config).getPool(poolKey);
353
+ const baseCoin = __privateGet(this, _config).getCoin(pool.baseCoin);
354
+ const quoteCoin = __privateGet(this, _config).getCoin(pool.quoteCoin);
355
+ tx.add(this.deepBook.getLevel2TicksFromMid(poolKey, ticks));
356
+ const res = await this.client.devInspectTransactionBlock({
357
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
358
+ transactionBlock: tx
359
+ });
360
+ const bid_prices = res.results[0].returnValues[0][0];
361
+ const bid_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_prices));
362
+ const bid_quantities = res.results[0].returnValues[1][0];
363
+ const bid_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_quantities));
364
+ const ask_prices = res.results[0].returnValues[2][0];
365
+ const ask_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_prices));
366
+ const ask_quantities = res.results[0].returnValues[3][0];
367
+ const ask_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_quantities));
368
+ return {
369
+ bid_prices: bid_parsed_prices.map(
370
+ (price) => Number((Number(price) / FLOAT_SCALAR / quoteCoin.scalar * baseCoin.scalar).toFixed(9))
371
+ ),
372
+ bid_quantities: bid_parsed_quantities.map(
373
+ (quantity) => Number((Number(quantity) / baseCoin.scalar).toFixed(9))
374
+ ),
375
+ ask_prices: ask_parsed_prices.map(
376
+ (price) => Number((Number(price) / FLOAT_SCALAR / quoteCoin.scalar * baseCoin.scalar).toFixed(9))
377
+ ),
378
+ ask_quantities: ask_parsed_quantities.map(
379
+ (quantity) => Number((Number(quantity) / baseCoin.scalar).toFixed(9))
380
+ )
381
+ };
382
+ }
383
+ /**
384
+ * @description Get the vault balances for a pool
385
+ * @param {string} poolKey Key of the pool
386
+ * @returns {Promise<{ base: number, quote: number, deep: number }>}
387
+ * An object with base, quote, and deep balances in the vault
388
+ */
389
+ async vaultBalances(poolKey) {
390
+ const tx = new Transaction();
391
+ const pool = __privateGet(this, _config).getPool(poolKey);
392
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
393
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
394
+ tx.add(this.deepBook.vaultBalances(poolKey));
395
+ const res = await this.client.devInspectTransactionBlock({
396
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
397
+ transactionBlock: tx
398
+ });
399
+ const baseInVault = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
400
+ const quoteInVault = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
401
+ const deepInVault = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
402
+ return {
403
+ base: Number((baseInVault / baseScalar).toFixed(9)),
404
+ quote: Number((quoteInVault / quoteScalar).toFixed(9)),
405
+ deep: Number((deepInVault / DEEP_SCALAR).toFixed(9))
406
+ };
407
+ }
408
+ /**
409
+ * @description Get the pool ID by asset types
410
+ * @param {string} baseType Type of the base asset
411
+ * @param {string} quoteType Type of the quote asset
412
+ * @returns {Promise<string>} The address of the pool
413
+ */
414
+ async getPoolIdByAssets(baseType, quoteType) {
415
+ const tx = new Transaction();
416
+ tx.add(this.deepBook.getPoolIdByAssets(baseType, quoteType));
417
+ const res = await this.client.devInspectTransactionBlock({
418
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
419
+ transactionBlock: tx
420
+ });
421
+ const address = bcs.Address.parse(new Uint8Array(res.results[0].returnValues[0][0]));
422
+ return address;
423
+ }
424
+ /**
425
+ * @description Get the mid price for a pool
426
+ * @param {string} poolKey Key of the pool
427
+ * @returns {Promise<number>} The mid price
428
+ */
429
+ async midPrice(poolKey) {
430
+ const tx = new Transaction();
431
+ const pool = __privateGet(this, _config).getPool(poolKey);
432
+ tx.add(this.deepBook.midPrice(poolKey));
433
+ const baseCoin = __privateGet(this, _config).getCoin(pool.baseCoin);
434
+ const quoteCoin = __privateGet(this, _config).getCoin(pool.quoteCoin);
435
+ const res = await this.client.devInspectTransactionBlock({
436
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
437
+ transactionBlock: tx
438
+ });
439
+ const bytes = res.results[0].returnValues[0][0];
440
+ const parsed_mid_price = Number(bcs.U64.parse(new Uint8Array(bytes)));
441
+ const adjusted_mid_price = parsed_mid_price * baseCoin.scalar / quoteCoin.scalar / FLOAT_SCALAR;
442
+ return Number(adjusted_mid_price.toFixed(9));
443
+ }
444
+ /**
445
+ * @description Get the trade parameters for a given pool, including taker fee, maker fee, and stake required.
446
+ * @param {string} poolKey Key of the pool
447
+ * @returns {Promise<{ takerFee: number, makerFee: number, stakeRequired: number }>}
448
+ */
449
+ async poolTradeParams(poolKey) {
450
+ const tx = new Transaction();
451
+ tx.add(this.deepBook.poolTradeParams(poolKey));
452
+ const res = await this.client.devInspectTransactionBlock({
453
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
454
+ transactionBlock: tx
455
+ });
456
+ const takerFee = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
457
+ const makerFee = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
458
+ const stakeRequired = Number(
459
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0]))
460
+ );
461
+ return {
462
+ takerFee: Number(takerFee / FLOAT_SCALAR),
463
+ makerFee: Number(makerFee / FLOAT_SCALAR),
464
+ stakeRequired: Number(stakeRequired / DEEP_SCALAR)
465
+ };
466
+ }
467
+ /**
468
+ * @description Get the trade parameters for a given pool, including tick size, lot size, and min size.
469
+ * @param {string} poolKey Key of the pool
470
+ * @returns {Promise<{ tickSize: number, lotSize: number, minSize: number }>}
471
+ */
472
+ async poolBookParams(poolKey) {
473
+ const tx = new Transaction();
474
+ const pool = __privateGet(this, _config).getPool(poolKey);
475
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
476
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
477
+ tx.add(this.deepBook.poolBookParams(poolKey));
478
+ const res = await this.client.devInspectTransactionBlock({
479
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
480
+ transactionBlock: tx
481
+ });
482
+ const tickSize = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
483
+ const lotSize = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
484
+ const minSize = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
485
+ return {
486
+ tickSize: Number(tickSize * baseScalar / quoteScalar / FLOAT_SCALAR),
487
+ lotSize: Number(lotSize / baseScalar),
488
+ minSize: Number(minSize / baseScalar)
489
+ };
490
+ }
491
+ /**
492
+ * @description Get the account information for a given pool and balance manager
493
+ * @param {string} poolKey Key of the pool
494
+ * @param {string} managerKey The key of the BalanceManager
495
+ * @returns {Promise<Object>} A promise that resolves to an object containing the account information
496
+ */
497
+ async account(poolKey, managerKey) {
498
+ const tx = new Transaction();
499
+ const pool = __privateGet(this, _config).getPool(poolKey);
500
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
501
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
502
+ tx.add(this.deepBook.account(poolKey, managerKey));
503
+ const res = await this.client.devInspectTransactionBlock({
504
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
505
+ transactionBlock: tx
506
+ });
507
+ const accountInformation = res.results[0].returnValues[0][0];
508
+ const accountInfo = Account.parse(new Uint8Array(accountInformation));
509
+ return {
510
+ epoch: accountInfo.epoch,
511
+ open_orders: accountInfo.open_orders,
512
+ taker_volume: Number(accountInfo.taker_volume) / baseScalar,
513
+ maker_volume: Number(accountInfo.maker_volume) / baseScalar,
514
+ active_stake: Number(accountInfo.active_stake) / DEEP_SCALAR,
515
+ inactive_stake: Number(accountInfo.inactive_stake) / DEEP_SCALAR,
516
+ created_proposal: accountInfo.created_proposal,
517
+ voted_proposal: accountInfo.voted_proposal,
518
+ unclaimed_rebates: {
519
+ base: Number(accountInfo.unclaimed_rebates.base) / baseScalar,
520
+ quote: Number(accountInfo.unclaimed_rebates.quote) / quoteScalar,
521
+ deep: Number(accountInfo.unclaimed_rebates.deep) / DEEP_SCALAR
522
+ },
523
+ settled_balances: {
524
+ base: Number(accountInfo.settled_balances.base) / baseScalar,
525
+ quote: Number(accountInfo.settled_balances.quote) / quoteScalar,
526
+ deep: Number(accountInfo.settled_balances.deep) / DEEP_SCALAR
527
+ },
528
+ owed_balances: {
529
+ base: Number(accountInfo.owed_balances.base) / baseScalar,
530
+ quote: Number(accountInfo.owed_balances.quote) / quoteScalar,
531
+ deep: Number(accountInfo.owed_balances.deep) / DEEP_SCALAR
532
+ }
533
+ };
534
+ }
535
+ /**
536
+ * @description Get the locked balances for a pool and balance manager
537
+ * @param {string} poolKey Key of the pool
538
+ * @param {string} managerKey The key of the BalanceManager
539
+ * @returns {Promise<{ base: number, quote: number, deep: number }>}
540
+ * An object with base, quote, and deep locked for the balance manager in the pool
541
+ */
542
+ async lockedBalance(poolKey, balanceManagerKey) {
543
+ const tx = new Transaction();
544
+ const pool = __privateGet(this, _config).getPool(poolKey);
545
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
546
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
547
+ tx.add(this.deepBook.lockedBalance(poolKey, balanceManagerKey));
548
+ const res = await this.client.devInspectTransactionBlock({
549
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
550
+ transactionBlock: tx
551
+ });
552
+ const baseLocked = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
553
+ const quoteLocked = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
554
+ const deepLocked = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
555
+ return {
556
+ base: Number((baseLocked / baseScalar).toFixed(9)),
557
+ quote: Number((quoteLocked / quoteScalar).toFixed(9)),
558
+ deep: Number((deepLocked / DEEP_SCALAR).toFixed(9))
559
+ };
560
+ }
561
+ /**
562
+ * @description Get the DEEP price conversion for a pool
563
+ * @param {string} poolKey Key of the pool
564
+ * @returns {Promise<{ asset_is_base: bool, deep_per_quote: number }>} Deep price conversion
565
+ */
566
+ async getPoolDeepPrice(poolKey) {
567
+ const tx = new Transaction();
568
+ const pool = __privateGet(this, _config).getPool(poolKey);
569
+ tx.add(this.deepBook.getPoolDeepPrice(poolKey));
570
+ const baseCoin = __privateGet(this, _config).getCoin(pool.baseCoin);
571
+ const quoteCoin = __privateGet(this, _config).getCoin(pool.quoteCoin);
572
+ const deepCoin = __privateGet(this, _config).getCoin("DEEP");
573
+ const res = await this.client.devInspectTransactionBlock({
574
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
575
+ transactionBlock: tx
576
+ });
577
+ const poolDeepPriceBytes = res.results[0].returnValues[0][0];
578
+ const poolDeepPrice = OrderDeepPrice.parse(new Uint8Array(poolDeepPriceBytes));
579
+ if (poolDeepPrice.asset_is_base) {
580
+ return {
581
+ asset_is_base: poolDeepPrice.asset_is_base,
582
+ deep_per_base: Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR * baseCoin.scalar / deepCoin.scalar
583
+ };
584
+ } else {
585
+ return {
586
+ asset_is_base: poolDeepPrice.asset_is_base,
587
+ deep_per_quote: Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR * quoteCoin.scalar / deepCoin.scalar
588
+ };
589
+ }
590
+ }
591
+ /**
592
+ * @description Decode the order ID to get bid/ask status, price, and orderId
593
+ * @param {bigint} encodedOrderId Encoded order ID
594
+ * @returns {Object} Object containing isBid, price, and orderId
595
+ */
596
+ decodeOrderId(encodedOrderId) {
597
+ const isBid = encodedOrderId >> 127n === 0n;
598
+ const price = Number(encodedOrderId >> 64n & (1n << 63n) - 1n);
599
+ const orderId = Number(encodedOrderId & (1n << 64n) - 1n);
600
+ return { isBid, price, orderId };
601
+ }
602
+ /**
603
+ * @description Get all balance manager IDs for a given owner
604
+ * @param {string} owner The owner address to get balance manager IDs for
605
+ * @returns {Promise<string[]>} Array of balance manager ID strings
606
+ */
607
+ async getBalanceManagerIds(owner) {
608
+ const tx = new Transaction();
609
+ tx.add(this.deepBook.getBalanceManagerIds(owner));
610
+ const res = await this.client.devInspectTransactionBlock({
611
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
612
+ transactionBlock: tx
613
+ });
614
+ const bytes = res.results[0].returnValues[0][0];
615
+ const vecOfAddresses = bcs.vector(bcs.Address).parse(new Uint8Array(bytes));
616
+ return vecOfAddresses.map((id) => normalizeHaneulAddress(id));
617
+ }
618
+ /**
619
+ * @description Get the owner of the referral (DeepBookPoolReferral)
620
+ * @param {string} referral The ID of the referral to get the owner of
621
+ * @returns {Promise<string>} The owner of the referral
622
+ */
623
+ async balanceManagerReferralOwner(referral) {
624
+ const tx = new Transaction();
625
+ tx.add(this.balanceManager.balanceManagerReferralOwner(referral));
626
+ const res = await this.client.devInspectTransactionBlock({
627
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
628
+ transactionBlock: tx
629
+ });
630
+ if (res.error || !res.results || res.results.length === 0) {
631
+ throw new Error(`Failed to get referral owner: ${res.error || "No results returned"}`);
632
+ }
633
+ const bytes = res.results[0].returnValues[0][0];
634
+ const owner = bcs.Address.parse(new Uint8Array(bytes));
635
+ return owner;
636
+ }
637
+ /**
638
+ * @description Get the referral balances for a pool and referral (DeepBookPoolReferral)
639
+ * @param {string} poolKey Key of the pool
640
+ * @param {string} referral The referral ID to get balances for
641
+ * @returns {Promise<{ base: number, quote: number, deep: number }>} Object with base, quote, and deep balances
642
+ */
643
+ async getPoolReferralBalances(poolKey, referral) {
644
+ const tx = new Transaction();
645
+ const pool = __privateGet(this, _config).getPool(poolKey);
646
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
647
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
648
+ tx.add(this.deepBook.getPoolReferralBalances(poolKey, referral));
649
+ const res = await this.client.devInspectTransactionBlock({
650
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
651
+ transactionBlock: tx
652
+ });
653
+ const baseBytes = res.results[0].returnValues[0][0];
654
+ const quoteBytes = res.results[0].returnValues[1][0];
655
+ const deepBytes = res.results[0].returnValues[2][0];
656
+ const baseBalance = Number(bcs.U64.parse(new Uint8Array(baseBytes)));
657
+ const quoteBalance = Number(bcs.U64.parse(new Uint8Array(quoteBytes)));
658
+ const deepBalance = Number(bcs.U64.parse(new Uint8Array(deepBytes)));
659
+ return {
660
+ base: baseBalance / baseScalar,
661
+ quote: quoteBalance / quoteScalar,
662
+ deep: deepBalance / DEEP_SCALAR
663
+ };
664
+ }
665
+ /**
666
+ * @description Get the pool ID associated with a referral (DeepBookPoolReferral)
667
+ * @param {string} referral The referral (DeepBookPoolReferral) to get the pool ID for
668
+ * @returns {Promise<string>} The pool ID
669
+ */
670
+ async balanceManagerReferralPoolId(referral) {
671
+ const tx = new Transaction();
672
+ tx.add(this.balanceManager.balanceManagerReferralPoolId(referral));
673
+ const res = await this.client.devInspectTransactionBlock({
674
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
675
+ transactionBlock: tx
676
+ });
677
+ const bytes = res.results[0].returnValues[0][0];
678
+ const poolId = bcs.Address.parse(new Uint8Array(bytes));
679
+ return normalizeHaneulAddress(poolId);
680
+ }
681
+ /**
682
+ * @description Get the multiplier for a referral (DeepBookPoolReferral)
683
+ * @param {string} poolKey Key of the pool
684
+ * @param {string} referral The referral (DeepBookPoolReferral) to get the multiplier for
685
+ * @returns {Promise<number>} The multiplier value
686
+ */
687
+ async poolReferralMultiplier(poolKey, referral) {
688
+ const tx = new Transaction();
689
+ tx.add(this.deepBook.poolReferralMultiplier(poolKey, referral));
690
+ const res = await this.client.devInspectTransactionBlock({
691
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
692
+ transactionBlock: tx
693
+ });
694
+ const bytes = res.results[0].returnValues[0][0];
695
+ const multiplier = Number(bcs.U64.parse(new Uint8Array(bytes)));
696
+ return multiplier / FLOAT_SCALAR;
697
+ }
698
+ /**
699
+ * @description Get the referral ID from a balance manager for a specific pool
700
+ * @param {string} managerKey Key of the balance manager
701
+ * @param {string} poolKey Key of the pool to get the referral for
702
+ * @returns {Promise<string | null>} The referral ID or null if not set
703
+ */
704
+ async getBalanceManagerReferralId(managerKey, poolKey) {
705
+ const tx = new Transaction();
706
+ tx.add(this.balanceManager.getBalanceManagerReferralId(managerKey, poolKey));
707
+ const res = await this.client.devInspectTransactionBlock({
708
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
709
+ transactionBlock: tx
710
+ });
711
+ try {
712
+ const bytes = res.results[0].returnValues[0][0];
713
+ const optionId = bcs.option(bcs.Address).parse(new Uint8Array(bytes));
714
+ if (optionId === null) {
715
+ return null;
716
+ }
717
+ return normalizeHaneulAddress(optionId);
718
+ } catch {
719
+ return null;
720
+ }
721
+ }
722
+ async getPriceInfoObject(tx, coinKey) {
723
+ const currentTime = Date.now();
724
+ const priceInfoObjectAge = await this.getPriceInfoObjectAge(coinKey) * 1e3;
725
+ if (currentTime - priceInfoObjectAge < PRICE_INFO_OBJECT_MAX_AGE_MS) {
726
+ return await __privateGet(this, _config).getCoin(coinKey).priceInfoObjectId;
727
+ }
728
+ const endpoint = __privateGet(this, _config).env === "testnet" ? "https://hermes-beta.pyth.network" : "https://hermes.pyth.network";
729
+ const connection = new HaneulPriceServiceConnection(endpoint);
730
+ const priceIDs = [
731
+ __privateGet(this, _config).getCoin(coinKey).feed
732
+ // ASSET/USD price ID
733
+ ];
734
+ const priceUpdateData = await connection.getPriceFeedsUpdateData(priceIDs);
735
+ const wormholeStateId = __privateGet(this, _config).pyth.wormholeStateId;
736
+ const pythStateId = __privateGet(this, _config).pyth.pythStateId;
737
+ const client = new HaneulPythClient(this.client, pythStateId, wormholeStateId);
738
+ return (await client.updatePriceFeeds(tx, priceUpdateData, priceIDs))[0];
739
+ }
740
+ /**
741
+ * @description Get the age of the price info object for a specific coin
742
+ * @param {string} coinKey Key of the coin
743
+ * @returns {Promise<string>} The arrival time of the price info object
744
+ */
745
+ async getPriceInfoObjectAge(coinKey) {
746
+ const priceInfoObjectId = __privateGet(this, _config).getCoin(coinKey).priceInfoObjectId;
747
+ const res = await this.client.getObject({
748
+ id: priceInfoObjectId,
749
+ options: {
750
+ showContent: true
751
+ }
752
+ });
753
+ if (!res.data?.content) {
754
+ throw new Error(`Price info object not found for ${coinKey}`);
755
+ }
756
+ if ("fields" in res.data.content) {
757
+ const fields = res.data.content.fields;
758
+ return fields.price_info?.fields?.arrival_time;
759
+ } else {
760
+ throw new Error(`Invalid price info object structure for ${coinKey}`);
761
+ }
762
+ }
763
+ // === Margin Pool View Methods ===
764
+ /**
765
+ * @description Get the margin pool ID
766
+ * @param {string} coinKey The key to identify the margin pool
767
+ * @returns {Promise<string>} The margin pool ID
768
+ */
769
+ async getMarginPoolId(coinKey) {
770
+ const tx = new Transaction();
771
+ tx.add(this.marginPool.getId(coinKey));
772
+ const res = await this.client.devInspectTransactionBlock({
773
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
774
+ transactionBlock: tx
775
+ });
776
+ const bytes = res.results[0].returnValues[0][0];
777
+ return bcs.Address.parse(new Uint8Array(bytes));
778
+ }
779
+ /**
780
+ * @description Check if a deepbook pool is allowed for borrowing from margin pool
781
+ * @param {string} coinKey The key to identify the margin pool
782
+ * @param {string} deepbookPoolId The ID of the deepbook pool
783
+ * @returns {Promise<boolean>} Whether the deepbook pool is allowed
784
+ */
785
+ async isDeepbookPoolAllowed(coinKey, deepbookPoolId) {
786
+ const tx = new Transaction();
787
+ tx.add(this.marginPool.deepbookPoolAllowed(coinKey, deepbookPoolId));
788
+ const res = await this.client.devInspectTransactionBlock({
789
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
790
+ transactionBlock: tx
791
+ });
792
+ const bytes = res.results[0].returnValues[0][0];
793
+ return bcs.bool().parse(new Uint8Array(bytes));
794
+ }
795
+ /**
796
+ * @description Get the total supply amount in the margin pool
797
+ * @param {string} coinKey The key to identify the margin pool
798
+ * @param {number} decimals Number of decimal places to show (default: 6)
799
+ * @returns {Promise<string>} The total supply amount as a string
800
+ */
801
+ async getMarginPoolTotalSupply(coinKey, decimals = 6) {
802
+ const tx = new Transaction();
803
+ tx.add(this.marginPool.totalSupply(coinKey));
804
+ const res = await this.client.devInspectTransactionBlock({
805
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
806
+ transactionBlock: tx
807
+ });
808
+ const bytes = res.results[0].returnValues[0][0];
809
+ const rawAmount = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
810
+ const coin = __privateGet(this, _config).getCoin(coinKey);
811
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawAmount, coin.scalar, decimals);
812
+ }
813
+ /**
814
+ * @description Get the total supply shares in the margin pool
815
+ * @param {string} coinKey The key to identify the margin pool
816
+ * @param {number} decimals Number of decimal places to show (default: 6)
817
+ * @returns {Promise<string>} The total supply shares as a string
818
+ */
819
+ async getMarginPoolSupplyShares(coinKey, decimals = 6) {
820
+ const tx = new Transaction();
821
+ tx.add(this.marginPool.supplyShares(coinKey));
822
+ const res = await this.client.devInspectTransactionBlock({
823
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
824
+ transactionBlock: tx
825
+ });
826
+ const bytes = res.results[0].returnValues[0][0];
827
+ const rawShares = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
828
+ const coin = __privateGet(this, _config).getCoin(coinKey);
829
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawShares, coin.scalar, decimals);
830
+ }
831
+ /**
832
+ * @description Get the total borrow amount in the margin pool
833
+ * @param {string} coinKey The key to identify the margin pool
834
+ * @param {number} decimals Number of decimal places to show (default: 6)
835
+ * @returns {Promise<string>} The total borrow amount as a string
836
+ */
837
+ async getMarginPoolTotalBorrow(coinKey, decimals = 6) {
838
+ const tx = new Transaction();
839
+ tx.add(this.marginPool.totalBorrow(coinKey));
840
+ const res = await this.client.devInspectTransactionBlock({
841
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
842
+ transactionBlock: tx
843
+ });
844
+ const bytes = res.results[0].returnValues[0][0];
845
+ const rawAmount = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
846
+ const coin = __privateGet(this, _config).getCoin(coinKey);
847
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawAmount, coin.scalar, decimals);
848
+ }
849
+ /**
850
+ * @description Get the total borrow shares in the margin pool
851
+ * @param {string} coinKey The key to identify the margin pool
852
+ * @param {number} decimals Number of decimal places to show (default: 6)
853
+ * @returns {Promise<string>} The total borrow shares as a string
854
+ */
855
+ async getMarginPoolBorrowShares(coinKey, decimals = 6) {
856
+ const tx = new Transaction();
857
+ tx.add(this.marginPool.borrowShares(coinKey));
858
+ const res = await this.client.devInspectTransactionBlock({
859
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
860
+ transactionBlock: tx
861
+ });
862
+ const bytes = res.results[0].returnValues[0][0];
863
+ const rawShares = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
864
+ const coin = __privateGet(this, _config).getCoin(coinKey);
865
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawShares, coin.scalar, decimals);
866
+ }
867
+ /**
868
+ * @description Get the last update timestamp of the margin pool
869
+ * @param {string} coinKey The key to identify the margin pool
870
+ * @returns {Promise<number>} The last update timestamp in milliseconds
871
+ */
872
+ async getMarginPoolLastUpdateTimestamp(coinKey) {
873
+ const tx = new Transaction();
874
+ tx.add(this.marginPool.lastUpdateTimestamp(coinKey));
875
+ const res = await this.client.devInspectTransactionBlock({
876
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
877
+ transactionBlock: tx
878
+ });
879
+ const bytes = res.results[0].returnValues[0][0];
880
+ return Number(bcs.U64.parse(new Uint8Array(bytes)));
881
+ }
882
+ /**
883
+ * @description Get the supply cap of the margin pool
884
+ * @param {string} coinKey The key to identify the margin pool
885
+ * @param {number} decimals Number of decimal places to show (default: 6)
886
+ * @returns {Promise<string>} The supply cap as a string
887
+ */
888
+ async getMarginPoolSupplyCap(coinKey, decimals = 6) {
889
+ const tx = new Transaction();
890
+ tx.add(this.marginPool.supplyCap(coinKey));
891
+ const res = await this.client.devInspectTransactionBlock({
892
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
893
+ transactionBlock: tx
894
+ });
895
+ const bytes = res.results[0].returnValues[0][0];
896
+ const rawAmount = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
897
+ const coin = __privateGet(this, _config).getCoin(coinKey);
898
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawAmount, coin.scalar, decimals);
899
+ }
900
+ /**
901
+ * @description Get the max utilization rate of the margin pool
902
+ * @param {string} coinKey The key to identify the margin pool
903
+ * @returns {Promise<number>} The max utilization rate (as a decimal, e.g., 0.95 for 95%)
904
+ */
905
+ async getMarginPoolMaxUtilizationRate(coinKey) {
906
+ const tx = new Transaction();
907
+ tx.add(this.marginPool.maxUtilizationRate(coinKey));
908
+ const res = await this.client.devInspectTransactionBlock({
909
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
910
+ transactionBlock: tx
911
+ });
912
+ const bytes = res.results[0].returnValues[0][0];
913
+ const rawRate = Number(bcs.U64.parse(new Uint8Array(bytes)));
914
+ return rawRate / FLOAT_SCALAR;
915
+ }
916
+ /**
917
+ * @description Get the protocol spread of the margin pool
918
+ * @param {string} coinKey The key to identify the margin pool
919
+ * @returns {Promise<number>} The protocol spread (as a decimal)
920
+ */
921
+ async getMarginPoolProtocolSpread(coinKey) {
922
+ const tx = new Transaction();
923
+ tx.add(this.marginPool.protocolSpread(coinKey));
924
+ const res = await this.client.devInspectTransactionBlock({
925
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
926
+ transactionBlock: tx
927
+ });
928
+ const bytes = res.results[0].returnValues[0][0];
929
+ const rawSpread = Number(bcs.U64.parse(new Uint8Array(bytes)));
930
+ return rawSpread / FLOAT_SCALAR;
931
+ }
932
+ /**
933
+ * @description Get the minimum borrow amount for the margin pool
934
+ * @param {string} coinKey The key to identify the margin pool
935
+ * @param {number} decimals Number of decimal places to show (default: 6)
936
+ * @returns {Promise<string>} The minimum borrow amount as a string
937
+ */
938
+ async getMarginPoolMinBorrow(coinKey, decimals = 6) {
939
+ const tx = new Transaction();
940
+ tx.add(this.marginPool.minBorrow(coinKey));
941
+ const res = await this.client.devInspectTransactionBlock({
942
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
943
+ transactionBlock: tx
944
+ });
945
+ const bytes = res.results[0].returnValues[0][0];
946
+ const rawAmount = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
947
+ const coin = __privateGet(this, _config).getCoin(coinKey);
948
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawAmount, coin.scalar, decimals);
949
+ }
950
+ /**
951
+ * @description Get the current interest rate of the margin pool
952
+ * @param {string} coinKey The key to identify the margin pool
953
+ * @returns {Promise<number>} The current interest rate (as a decimal)
954
+ */
955
+ async getMarginPoolInterestRate(coinKey) {
956
+ const tx = new Transaction();
957
+ tx.add(this.marginPool.interestRate(coinKey));
958
+ const res = await this.client.devInspectTransactionBlock({
959
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
960
+ transactionBlock: tx
961
+ });
962
+ const bytes = res.results[0].returnValues[0][0];
963
+ const rawRate = Number(bcs.U64.parse(new Uint8Array(bytes)));
964
+ return rawRate / FLOAT_SCALAR;
965
+ }
966
+ /**
967
+ * @description Get user supply shares for a supplier cap
968
+ * @param {string} coinKey The key to identify the margin pool
969
+ * @param {string} supplierCapId The ID of the supplier cap
970
+ * @param {number} decimals Number of decimal places to show (default: 6)
971
+ * @returns {Promise<string>} The user's supply shares as a string
972
+ */
973
+ async getUserSupplyShares(coinKey, supplierCapId, decimals = 6) {
974
+ const tx = new Transaction();
975
+ tx.add(this.marginPool.userSupplyShares(coinKey, supplierCapId));
976
+ const res = await this.client.devInspectTransactionBlock({
977
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
978
+ transactionBlock: tx
979
+ });
980
+ const bytes = res.results[0].returnValues[0][0];
981
+ const rawShares = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
982
+ const coin = __privateGet(this, _config).getCoin(coinKey);
983
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawShares, coin.scalar, decimals);
984
+ }
985
+ /**
986
+ * @description Get user supply amount for a supplier cap
987
+ * @param {string} coinKey The key to identify the margin pool
988
+ * @param {string} supplierCapId The ID of the supplier cap
989
+ * @param {number} decimals Number of decimal places to show (default: 6)
990
+ * @returns {Promise<string>} The user's supply amount as a string
991
+ */
992
+ async getUserSupplyAmount(coinKey, supplierCapId, decimals = 6) {
993
+ const tx = new Transaction();
994
+ tx.add(this.marginPool.userSupplyAmount(coinKey, supplierCapId));
995
+ const res = await this.client.devInspectTransactionBlock({
996
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
997
+ transactionBlock: tx
998
+ });
999
+ const bytes = res.results[0].returnValues[0][0];
1000
+ const rawAmount = BigInt(bcs.U64.parse(new Uint8Array(bytes)));
1001
+ const coin = __privateGet(this, _config).getCoin(coinKey);
1002
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, rawAmount, coin.scalar, decimals);
1003
+ }
1004
+ // === Margin Manager Read-Only Functions ===
1005
+ /**
1006
+ * @description Get the owner address of a margin manager
1007
+ * @param {string} marginManagerKey The key to identify the margin manager
1008
+ * @returns {Promise<string>} The owner address
1009
+ */
1010
+ async getMarginManagerOwner(marginManagerKey) {
1011
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1012
+ const tx = new Transaction();
1013
+ tx.add(this.marginManager.ownerByPoolKey(manager.poolKey, manager.address));
1014
+ const res = await this.client.devInspectTransactionBlock({
1015
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1016
+ transactionBlock: tx
1017
+ });
1018
+ const bytes = res.results[0].returnValues[0][0];
1019
+ return normalizeHaneulAddress(bcs.Address.parse(new Uint8Array(bytes)));
1020
+ }
1021
+ /**
1022
+ * @description Get the DeepBook pool ID associated with a margin manager
1023
+ * @param {string} marginManagerKey The key to identify the margin manager
1024
+ * @returns {Promise<string>} The DeepBook pool ID
1025
+ */
1026
+ async getMarginManagerDeepbookPool(marginManagerKey) {
1027
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1028
+ const tx = new Transaction();
1029
+ tx.add(this.marginManager.deepbookPool(manager.poolKey, manager.address));
1030
+ const res = await this.client.devInspectTransactionBlock({
1031
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1032
+ transactionBlock: tx
1033
+ });
1034
+ const bytes = res.results[0].returnValues[0][0];
1035
+ return normalizeHaneulAddress(bcs.Address.parse(new Uint8Array(bytes)));
1036
+ }
1037
+ /**
1038
+ * @description Get the margin pool ID (if any) associated with a margin manager
1039
+ * @param {string} marginManagerKey The key to identify the margin manager
1040
+ * @returns {Promise<string | null>} The margin pool ID or null if no active loan
1041
+ */
1042
+ async getMarginManagerMarginPoolId(marginManagerKey) {
1043
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1044
+ const tx = new Transaction();
1045
+ tx.add(this.marginManager.marginPoolId(manager.poolKey, manager.address));
1046
+ const res = await this.client.devInspectTransactionBlock({
1047
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1048
+ transactionBlock: tx
1049
+ });
1050
+ const bytes = res.results[0].returnValues[0][0];
1051
+ const option = bcs.option(bcs.Address).parse(new Uint8Array(bytes));
1052
+ return option ? normalizeHaneulAddress(option) : null;
1053
+ }
1054
+ /**
1055
+ * @description Get borrowed shares for both base and quote assets
1056
+ * @param {string} marginManagerKey The key to identify the margin manager
1057
+ * @returns {Promise<{baseShares: string, quoteShares: string}>} The borrowed shares
1058
+ */
1059
+ async getMarginManagerBorrowedShares(marginManagerKey) {
1060
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1061
+ const tx = new Transaction();
1062
+ tx.add(this.marginManager.borrowedShares(manager.poolKey, manager.address));
1063
+ const res = await this.client.devInspectTransactionBlock({
1064
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1065
+ transactionBlock: tx
1066
+ });
1067
+ const baseBytes = res.results[0].returnValues[0][0];
1068
+ const quoteBytes = res.results[0].returnValues[1][0];
1069
+ const baseShares = bcs.U64.parse(new Uint8Array(baseBytes)).toString();
1070
+ const quoteShares = bcs.U64.parse(new Uint8Array(quoteBytes)).toString();
1071
+ return { baseShares, quoteShares };
1072
+ }
1073
+ /**
1074
+ * @description Get borrowed base shares
1075
+ * @param {string} marginManagerKey The key to identify the margin manager
1076
+ * @returns {Promise<string>} The borrowed base shares
1077
+ */
1078
+ async getMarginManagerBorrowedBaseShares(marginManagerKey) {
1079
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1080
+ const tx = new Transaction();
1081
+ tx.add(this.marginManager.borrowedBaseShares(manager.poolKey, manager.address));
1082
+ const res = await this.client.devInspectTransactionBlock({
1083
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1084
+ transactionBlock: tx
1085
+ });
1086
+ const bytes = res.results[0].returnValues[0][0];
1087
+ return bcs.U64.parse(new Uint8Array(bytes)).toString();
1088
+ }
1089
+ /**
1090
+ * @description Get borrowed quote shares
1091
+ * @param {string} marginManagerKey The key to identify the margin manager
1092
+ * @returns {Promise<string>} The borrowed quote shares
1093
+ */
1094
+ async getMarginManagerBorrowedQuoteShares(marginManagerKey) {
1095
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1096
+ const tx = new Transaction();
1097
+ tx.add(this.marginManager.borrowedQuoteShares(manager.poolKey, manager.address));
1098
+ const res = await this.client.devInspectTransactionBlock({
1099
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1100
+ transactionBlock: tx
1101
+ });
1102
+ const bytes = res.results[0].returnValues[0][0];
1103
+ return bcs.U64.parse(new Uint8Array(bytes)).toString();
1104
+ }
1105
+ /**
1106
+ * @description Check if margin manager has base asset debt
1107
+ * @param {string} marginManagerKey The key to identify the margin manager
1108
+ * @returns {Promise<boolean>} True if has base debt, false otherwise
1109
+ */
1110
+ async getMarginManagerHasBaseDebt(marginManagerKey) {
1111
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1112
+ const tx = new Transaction();
1113
+ tx.add(this.marginManager.hasBaseDebt(manager.poolKey, manager.address));
1114
+ const res = await this.client.devInspectTransactionBlock({
1115
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1116
+ transactionBlock: tx
1117
+ });
1118
+ const bytes = res.results[0].returnValues[0][0];
1119
+ return bcs.bool().parse(new Uint8Array(bytes));
1120
+ }
1121
+ /**
1122
+ * @description Get the balance manager ID for a margin manager
1123
+ * @param {string} marginManagerKey The key to identify the margin manager
1124
+ * @returns {Promise<string>} The balance manager ID
1125
+ */
1126
+ async getMarginManagerBalanceManagerId(marginManagerKey) {
1127
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1128
+ const tx = new Transaction();
1129
+ tx.add(this.marginManager.balanceManager(manager.poolKey, manager.address));
1130
+ const res = await this.client.devInspectTransactionBlock({
1131
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1132
+ transactionBlock: tx
1133
+ });
1134
+ const bytes = res.results[0].returnValues[0][0];
1135
+ return normalizeHaneulAddress(bcs.Address.parse(new Uint8Array(bytes)));
1136
+ }
1137
+ /**
1138
+ * @description Calculate assets (base and quote) for a margin manager
1139
+ * @param {string} marginManagerKey The key to identify the margin manager
1140
+ * @param {number} decimals Number of decimal places to show (default: 6)
1141
+ * @returns {Promise<{baseAsset: string, quoteAsset: string}>} The base and quote assets
1142
+ */
1143
+ async getMarginManagerAssets(marginManagerKey, decimals = 6) {
1144
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1145
+ const tx = new Transaction();
1146
+ tx.add(this.marginManager.calculateAssets(manager.poolKey, manager.address));
1147
+ const res = await this.client.devInspectTransactionBlock({
1148
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1149
+ transactionBlock: tx
1150
+ });
1151
+ const baseBytes = res.results[0].returnValues[0][0];
1152
+ const quoteBytes = res.results[0].returnValues[1][0];
1153
+ const pool = __privateGet(this, _config).getPool(manager.poolKey);
1154
+ const baseCoin = __privateGet(this, _config).getCoin(pool.baseCoin);
1155
+ const quoteCoin = __privateGet(this, _config).getCoin(pool.quoteCoin);
1156
+ const baseAsset = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(baseBytes))), baseCoin.scalar, decimals);
1157
+ const quoteAsset = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(quoteBytes))), quoteCoin.scalar, decimals);
1158
+ return { baseAsset, quoteAsset };
1159
+ }
1160
+ /**
1161
+ * @description Calculate debts (base and quote) for a margin manager
1162
+ * NOTE: This function automatically determines whether to use base or quote margin pool
1163
+ * based on hasBaseDebt. You don't need to specify the debt coin type.
1164
+ * @param {string} marginManagerKey The key to identify the margin manager
1165
+ * @param {number} decimals Number of decimal places to show (default: 6)
1166
+ * @returns {Promise<{baseDebt: string, quoteDebt: string}>} The base and quote debts
1167
+ */
1168
+ async getMarginManagerDebts(marginManagerKey, decimals = 6) {
1169
+ const hasBaseDebt = await this.getMarginManagerHasBaseDebt(marginManagerKey);
1170
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1171
+ const pool = __privateGet(this, _config).getPool(manager.poolKey);
1172
+ const debtCoinKey = hasBaseDebt ? pool.baseCoin : pool.quoteCoin;
1173
+ const tx = new Transaction();
1174
+ tx.add(this.marginManager.calculateDebts(manager.poolKey, debtCoinKey, manager.address));
1175
+ const res = await this.client.devInspectTransactionBlock({
1176
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1177
+ transactionBlock: tx
1178
+ });
1179
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1180
+ throw new Error(
1181
+ `Failed to get margin manager debts: ${res.effects?.status?.error || "Unknown error"}`
1182
+ );
1183
+ }
1184
+ const baseBytes = res.results[0].returnValues[0][0];
1185
+ const quoteBytes = res.results[0].returnValues[1][0];
1186
+ const debtCoin = __privateGet(this, _config).getCoin(debtCoinKey);
1187
+ const baseDebt = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(baseBytes))), debtCoin.scalar, decimals);
1188
+ const quoteDebt = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(quoteBytes))), debtCoin.scalar, decimals);
1189
+ return { baseDebt, quoteDebt };
1190
+ }
1191
+ /**
1192
+ * @description Get comprehensive state information for a margin manager
1193
+ * @param {string} marginManagerKey The key to identify the margin manager
1194
+ * @param {number} decimals Number of decimal places to show (default: 6)
1195
+ * @returns {Promise<{
1196
+ * managerId: string,
1197
+ * deepbookPoolId: string,
1198
+ * riskRatio: number,
1199
+ * baseAsset: string,
1200
+ * quoteAsset: string,
1201
+ * baseDebt: string,
1202
+ * quoteDebt: string,
1203
+ * basePythPrice: string,
1204
+ * basePythDecimals: number,
1205
+ * quotePythPrice: string,
1206
+ * quotePythDecimals: number
1207
+ * }>} Comprehensive margin manager state
1208
+ */
1209
+ async getMarginManagerState(marginManagerKey, decimals = 6) {
1210
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1211
+ const tx = new Transaction();
1212
+ tx.add(this.marginManager.managerState(manager.poolKey, manager.address));
1213
+ const res = await this.client.devInspectTransactionBlock({
1214
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1215
+ transactionBlock: tx
1216
+ });
1217
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1218
+ throw new Error(
1219
+ `Failed to get margin manager state: ${res.effects?.status?.error || "Unknown error"}`
1220
+ );
1221
+ }
1222
+ const pool = __privateGet(this, _config).getPool(manager.poolKey);
1223
+ const baseCoin = __privateGet(this, _config).getCoin(pool.baseCoin);
1224
+ const quoteCoin = __privateGet(this, _config).getCoin(pool.quoteCoin);
1225
+ const managerId = normalizeHaneulAddress(
1226
+ bcs.Address.parse(new Uint8Array(res.results[0].returnValues[0][0]))
1227
+ );
1228
+ const deepbookPoolId = normalizeHaneulAddress(
1229
+ bcs.Address.parse(new Uint8Array(res.results[0].returnValues[1][0]))
1230
+ );
1231
+ const riskRatio = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0]))) / FLOAT_SCALAR;
1232
+ const baseAsset = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[3][0]))), baseCoin.scalar, decimals);
1233
+ const quoteAsset = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[4][0]))), quoteCoin.scalar, decimals);
1234
+ const baseDebt = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[5][0]))), baseCoin.scalar, decimals);
1235
+ const quoteDebt = __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[6][0]))), quoteCoin.scalar, decimals);
1236
+ const basePythPrice = bcs.U64.parse(new Uint8Array(res.results[0].returnValues[7][0]));
1237
+ const basePythDecimals = Number(
1238
+ bcs.u8().parse(new Uint8Array(res.results[0].returnValues[8][0]))
1239
+ );
1240
+ const quotePythPrice = bcs.U64.parse(new Uint8Array(res.results[0].returnValues[9][0]));
1241
+ const quotePythDecimals = Number(
1242
+ bcs.u8().parse(new Uint8Array(res.results[0].returnValues[10][0]))
1243
+ );
1244
+ const currentPrice = BigInt(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[11][0])));
1245
+ const lowestTriggerAbovePrice = BigInt(
1246
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[12][0]))
1247
+ );
1248
+ const highestTriggerBelowPrice = BigInt(
1249
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[13][0]))
1250
+ );
1251
+ return {
1252
+ managerId,
1253
+ deepbookPoolId,
1254
+ riskRatio,
1255
+ baseAsset,
1256
+ quoteAsset,
1257
+ baseDebt,
1258
+ quoteDebt,
1259
+ basePythPrice: basePythPrice.toString(),
1260
+ basePythDecimals,
1261
+ quotePythPrice: quotePythPrice.toString(),
1262
+ quotePythDecimals,
1263
+ currentPrice,
1264
+ lowestTriggerAbovePrice,
1265
+ highestTriggerBelowPrice
1266
+ };
1267
+ }
1268
+ /**
1269
+ * @description Get the base asset balance of a margin manager
1270
+ * @param {string} marginManagerKey The key to identify the margin manager
1271
+ * @param {number} decimals Number of decimal places to show (default: 6)
1272
+ * @returns {Promise<string>} The base asset balance
1273
+ */
1274
+ async getMarginManagerBaseBalance(marginManagerKey, decimals = 9) {
1275
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1276
+ const tx = new Transaction();
1277
+ tx.add(this.marginManager.baseBalance(manager.poolKey, manager.address));
1278
+ const res = await this.client.devInspectTransactionBlock({
1279
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1280
+ transactionBlock: tx
1281
+ });
1282
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1283
+ throw new Error(
1284
+ `Failed to get margin manager base balance: ${res.effects?.status?.error || "Unknown error"}`
1285
+ );
1286
+ }
1287
+ const bytes = res.results[0].returnValues[0][0];
1288
+ const pool = __privateGet(this, _config).getPool(manager.poolKey);
1289
+ const baseCoin = __privateGet(this, _config).getCoin(pool.baseCoin);
1290
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(bytes))), baseCoin.scalar, decimals);
1291
+ }
1292
+ /**
1293
+ * @description Get the quote asset balance of a margin manager
1294
+ * @param {string} marginManagerKey The key to identify the margin manager
1295
+ * @param {number} decimals Number of decimal places to show (default: 6)
1296
+ * @returns {Promise<string>} The quote asset balance
1297
+ */
1298
+ async getMarginManagerQuoteBalance(marginManagerKey, decimals = 9) {
1299
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1300
+ const tx = new Transaction();
1301
+ tx.add(this.marginManager.quoteBalance(manager.poolKey, manager.address));
1302
+ const res = await this.client.devInspectTransactionBlock({
1303
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1304
+ transactionBlock: tx
1305
+ });
1306
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1307
+ throw new Error(
1308
+ `Failed to get margin manager quote balance: ${res.effects?.status?.error || "Unknown error"}`
1309
+ );
1310
+ }
1311
+ const bytes = res.results[0].returnValues[0][0];
1312
+ const pool = __privateGet(this, _config).getPool(manager.poolKey);
1313
+ const quoteCoin = __privateGet(this, _config).getCoin(pool.quoteCoin);
1314
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(bytes))), quoteCoin.scalar, decimals);
1315
+ }
1316
+ /**
1317
+ * @description Get the DEEP token balance of a margin manager
1318
+ * @param {string} marginManagerKey The key to identify the margin manager
1319
+ * @param {number} decimals Number of decimal places to show (default: 6)
1320
+ * @returns {Promise<string>} The DEEP token balance
1321
+ */
1322
+ async getMarginManagerDeepBalance(marginManagerKey, decimals = 6) {
1323
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1324
+ const tx = new Transaction();
1325
+ tx.add(this.marginManager.deepBalance(manager.poolKey, manager.address));
1326
+ const res = await this.client.devInspectTransactionBlock({
1327
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1328
+ transactionBlock: tx
1329
+ });
1330
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1331
+ throw new Error(
1332
+ `Failed to get margin manager DEEP balance: ${res.effects?.status?.error || "Unknown error"}`
1333
+ );
1334
+ }
1335
+ const bytes = res.results[0].returnValues[0][0];
1336
+ const deepCoin = __privateGet(this, _config).getCoin("DEEP");
1337
+ return __privateMethod(this, _DeepBookClient_instances, formatTokenAmount_fn).call(this, BigInt(bcs.U64.parse(new Uint8Array(bytes))), deepCoin.scalar, decimals);
1338
+ }
1339
+ // === Margin TPSL (Take Profit / Stop Loss) Read-Only Functions ===
1340
+ /**
1341
+ * @description Get all conditional order IDs for a margin manager
1342
+ * @param {string} marginManagerKey The key to identify the margin manager
1343
+ * @returns {Promise<string[]>} Array of conditional order IDs
1344
+ */
1345
+ async getConditionalOrderIds(marginManagerKey) {
1346
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1347
+ const tx = new Transaction();
1348
+ tx.add(this.marginTPSL.conditionalOrderIds(manager.poolKey, manager.address));
1349
+ const res = await this.client.devInspectTransactionBlock({
1350
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1351
+ transactionBlock: tx
1352
+ });
1353
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1354
+ throw new Error(
1355
+ `Failed to get conditional order IDs: ${res.effects?.status?.error || "Unknown error"}`
1356
+ );
1357
+ }
1358
+ const bytes = res.results[0].returnValues[0][0];
1359
+ const orderIds = bcs.vector(bcs.u64()).parse(new Uint8Array(bytes));
1360
+ return orderIds.map((id) => id.toString());
1361
+ }
1362
+ /**
1363
+ * @description Get the lowest trigger price for trigger_above orders
1364
+ * Returns MAX_U64 if there are no trigger_above orders
1365
+ * @param {string} marginManagerKey The key to identify the margin manager
1366
+ * @returns {Promise<bigint>} The lowest trigger above price
1367
+ */
1368
+ async getLowestTriggerAbovePrice(marginManagerKey) {
1369
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1370
+ const tx = new Transaction();
1371
+ tx.add(this.marginTPSL.lowestTriggerAbovePrice(manager.poolKey, manager.address));
1372
+ const res = await this.client.devInspectTransactionBlock({
1373
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1374
+ transactionBlock: tx
1375
+ });
1376
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1377
+ throw new Error(
1378
+ `Failed to get lowest trigger above price: ${res.effects?.status?.error || "Unknown error"}`
1379
+ );
1380
+ }
1381
+ const bytes = res.results[0].returnValues[0][0];
1382
+ return BigInt(bcs.U64.parse(new Uint8Array(bytes)));
1383
+ }
1384
+ /**
1385
+ * @description Get the highest trigger price for trigger_below orders
1386
+ * Returns 0 if there are no trigger_below orders
1387
+ * @param {string} marginManagerKey The key to identify the margin manager
1388
+ * @returns {Promise<bigint>} The highest trigger below price
1389
+ */
1390
+ async getHighestTriggerBelowPrice(marginManagerKey) {
1391
+ const manager = __privateGet(this, _config).getMarginManager(marginManagerKey);
1392
+ const tx = new Transaction();
1393
+ tx.add(this.marginTPSL.highestTriggerBelowPrice(manager.poolKey, manager.address));
1394
+ const res = await this.client.devInspectTransactionBlock({
1395
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1396
+ transactionBlock: tx
1397
+ });
1398
+ if (!res.results || !res.results[0] || !res.results[0].returnValues) {
1399
+ throw new Error(
1400
+ `Failed to get highest trigger below price: ${res.effects?.status?.error || "Unknown error"}`
1401
+ );
1402
+ }
1403
+ const bytes = res.results[0].returnValues[0][0];
1404
+ return BigInt(bcs.U64.parse(new Uint8Array(bytes)));
1405
+ }
1406
+ // === Margin Registry Functions ===
1407
+ /**
1408
+ * @description Check if a deepbook pool is enabled for margin trading
1409
+ * @param {string} poolKey The key to identify the pool
1410
+ * @returns {Promise<boolean>} True if the pool is enabled for margin trading
1411
+ */
1412
+ async isPoolEnabledForMargin(poolKey) {
1413
+ const tx = new Transaction();
1414
+ tx.add(this.marginRegistry.poolEnabled(poolKey));
1415
+ const res = await this.client.devInspectTransactionBlock({
1416
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1417
+ transactionBlock: tx
1418
+ });
1419
+ const bytes = res.results[0].returnValues[0][0];
1420
+ return bcs.Bool.parse(new Uint8Array(bytes));
1421
+ }
1422
+ /**
1423
+ * @description Get the margin manager IDs for a given owner address
1424
+ * @param {string} owner The owner address
1425
+ * @returns {Promise<string[]>} Array of margin manager IDs
1426
+ */
1427
+ async getMarginManagerIdsForOwner(owner) {
1428
+ const tx = new Transaction();
1429
+ tx.add(this.marginRegistry.getMarginManagerIds(owner));
1430
+ const res = await this.client.devInspectTransactionBlock({
1431
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1432
+ transactionBlock: tx
1433
+ });
1434
+ const bytes = res.results[0].returnValues[0][0];
1435
+ const vecSet = VecSet(bcs.Address).parse(new Uint8Array(bytes));
1436
+ return vecSet.contents.map((id) => normalizeHaneulAddress(id));
1437
+ }
1438
+ /**
1439
+ * @description Get the base margin pool ID for a deepbook pool
1440
+ * @param {string} poolKey The key to identify the pool
1441
+ * @returns {Promise<string>} The base margin pool ID
1442
+ */
1443
+ async getBaseMarginPoolId(poolKey) {
1444
+ const tx = new Transaction();
1445
+ tx.add(this.marginRegistry.baseMarginPoolId(poolKey));
1446
+ const res = await this.client.devInspectTransactionBlock({
1447
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1448
+ transactionBlock: tx
1449
+ });
1450
+ const bytes = res.results[0].returnValues[0][0];
1451
+ const id = bcs.Address.parse(new Uint8Array(bytes));
1452
+ return "0x" + id;
1453
+ }
1454
+ /**
1455
+ * @description Get the quote margin pool ID for a deepbook pool
1456
+ * @param {string} poolKey The key to identify the pool
1457
+ * @returns {Promise<string>} The quote margin pool ID
1458
+ */
1459
+ async getQuoteMarginPoolId(poolKey) {
1460
+ const tx = new Transaction();
1461
+ tx.add(this.marginRegistry.quoteMarginPoolId(poolKey));
1462
+ const res = await this.client.devInspectTransactionBlock({
1463
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1464
+ transactionBlock: tx
1465
+ });
1466
+ const bytes = res.results[0].returnValues[0][0];
1467
+ const id = bcs.Address.parse(new Uint8Array(bytes));
1468
+ return "0x" + id;
1469
+ }
1470
+ /**
1471
+ * @description Get the minimum withdraw risk ratio for a deepbook pool
1472
+ * @param {string} poolKey The key to identify the pool
1473
+ * @returns {Promise<number>} The minimum withdraw risk ratio as a decimal (e.g., 1.5 for 150%)
1474
+ */
1475
+ async getMinWithdrawRiskRatio(poolKey) {
1476
+ const tx = new Transaction();
1477
+ tx.add(this.marginRegistry.minWithdrawRiskRatio(poolKey));
1478
+ const res = await this.client.devInspectTransactionBlock({
1479
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1480
+ transactionBlock: tx
1481
+ });
1482
+ const bytes = res.results[0].returnValues[0][0];
1483
+ const ratio = Number(bcs.U64.parse(new Uint8Array(bytes)));
1484
+ return ratio / FLOAT_SCALAR;
1485
+ }
1486
+ /**
1487
+ * @description Get the minimum borrow risk ratio for a deepbook pool
1488
+ * @param {string} poolKey The key to identify the pool
1489
+ * @returns {Promise<number>} The minimum borrow risk ratio as a decimal (e.g., 1.25 for 125%)
1490
+ */
1491
+ async getMinBorrowRiskRatio(poolKey) {
1492
+ const tx = new Transaction();
1493
+ tx.add(this.marginRegistry.minBorrowRiskRatio(poolKey));
1494
+ const res = await this.client.devInspectTransactionBlock({
1495
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1496
+ transactionBlock: tx
1497
+ });
1498
+ const bytes = res.results[0].returnValues[0][0];
1499
+ const ratio = Number(bcs.U64.parse(new Uint8Array(bytes)));
1500
+ return ratio / FLOAT_SCALAR;
1501
+ }
1502
+ /**
1503
+ * @description Get the liquidation risk ratio for a deepbook pool
1504
+ * @param {string} poolKey The key to identify the pool
1505
+ * @returns {Promise<number>} The liquidation risk ratio as a decimal (e.g., 1.125 for 112.5%)
1506
+ */
1507
+ async getLiquidationRiskRatio(poolKey) {
1508
+ const tx = new Transaction();
1509
+ tx.add(this.marginRegistry.liquidationRiskRatio(poolKey));
1510
+ const res = await this.client.devInspectTransactionBlock({
1511
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1512
+ transactionBlock: tx
1513
+ });
1514
+ const bytes = res.results[0].returnValues[0][0];
1515
+ const ratio = Number(bcs.U64.parse(new Uint8Array(bytes)));
1516
+ return ratio / FLOAT_SCALAR;
1517
+ }
1518
+ /**
1519
+ * @description Get the target liquidation risk ratio for a deepbook pool
1520
+ * @param {string} poolKey The key to identify the pool
1521
+ * @returns {Promise<number>} The target liquidation risk ratio as a decimal (e.g., 1.25 for 125%)
1522
+ */
1523
+ async getTargetLiquidationRiskRatio(poolKey) {
1524
+ const tx = new Transaction();
1525
+ tx.add(this.marginRegistry.targetLiquidationRiskRatio(poolKey));
1526
+ const res = await this.client.devInspectTransactionBlock({
1527
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1528
+ transactionBlock: tx
1529
+ });
1530
+ const bytes = res.results[0].returnValues[0][0];
1531
+ const ratio = Number(bcs.U64.parse(new Uint8Array(bytes)));
1532
+ return ratio / FLOAT_SCALAR;
1533
+ }
1534
+ /**
1535
+ * @description Get the user liquidation reward for a deepbook pool
1536
+ * @param {string} poolKey The key to identify the pool
1537
+ * @returns {Promise<number>} The user liquidation reward as a decimal (e.g., 0.05 for 5%)
1538
+ */
1539
+ async getUserLiquidationReward(poolKey) {
1540
+ const tx = new Transaction();
1541
+ tx.add(this.marginRegistry.userLiquidationReward(poolKey));
1542
+ const res = await this.client.devInspectTransactionBlock({
1543
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1544
+ transactionBlock: tx
1545
+ });
1546
+ const bytes = res.results[0].returnValues[0][0];
1547
+ const reward = Number(bcs.U64.parse(new Uint8Array(bytes)));
1548
+ return reward / FLOAT_SCALAR;
1549
+ }
1550
+ /**
1551
+ * @description Get the pool liquidation reward for a deepbook pool
1552
+ * @param {string} poolKey The key to identify the pool
1553
+ * @returns {Promise<number>} The pool liquidation reward as a decimal (e.g., 0.05 for 5%)
1554
+ */
1555
+ async getPoolLiquidationReward(poolKey) {
1556
+ const tx = new Transaction();
1557
+ tx.add(this.marginRegistry.poolLiquidationReward(poolKey));
1558
+ const res = await this.client.devInspectTransactionBlock({
1559
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1560
+ transactionBlock: tx
1561
+ });
1562
+ const bytes = res.results[0].returnValues[0][0];
1563
+ const reward = Number(bcs.U64.parse(new Uint8Array(bytes)));
1564
+ return reward / FLOAT_SCALAR;
1565
+ }
1566
+ /**
1567
+ * @description Get all allowed maintainer cap IDs
1568
+ * @returns {Promise<string[]>} Array of allowed maintainer cap IDs
1569
+ */
1570
+ async getAllowedMaintainers() {
1571
+ const tx = new Transaction();
1572
+ tx.add(this.marginRegistry.allowedMaintainers());
1573
+ const res = await this.client.devInspectTransactionBlock({
1574
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1575
+ transactionBlock: tx
1576
+ });
1577
+ const bytes = res.results[0].returnValues[0][0];
1578
+ const vecSet = VecSet(bcs.Address).parse(new Uint8Array(bytes));
1579
+ return vecSet.contents.map((id) => normalizeHaneulAddress(id));
1580
+ }
1581
+ /**
1582
+ * @description Get all allowed pause cap IDs
1583
+ * @returns {Promise<string[]>} Array of allowed pause cap IDs
1584
+ */
1585
+ async getAllowedPauseCaps() {
1586
+ const tx = new Transaction();
1587
+ tx.add(this.marginRegistry.allowedPauseCaps());
1588
+ const res = await this.client.devInspectTransactionBlock({
1589
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1590
+ transactionBlock: tx
1591
+ });
1592
+ const bytes = res.results[0].returnValues[0][0];
1593
+ const vecSet = VecSet(bcs.Address).parse(new Uint8Array(bytes));
1594
+ return vecSet.contents.map((id) => normalizeHaneulAddress(id));
1595
+ }
1596
+ /**
1597
+ * @description Check if a pool is a stable pool
1598
+ * @param {string} poolKey Key of the pool
1599
+ * @returns {Promise<boolean>} Whether the pool is a stable pool
1600
+ */
1601
+ async stablePool(poolKey) {
1602
+ const tx = new Transaction();
1603
+ tx.add(this.deepBook.stablePool(poolKey));
1604
+ const res = await this.client.devInspectTransactionBlock({
1605
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1606
+ transactionBlock: tx
1607
+ });
1608
+ const bytes = res.results[0].returnValues[0][0];
1609
+ return bcs.bool().parse(new Uint8Array(bytes));
1610
+ }
1611
+ /**
1612
+ * @description Check if a pool is registered
1613
+ * @param {string} poolKey Key of the pool
1614
+ * @returns {Promise<boolean>} Whether the pool is registered
1615
+ */
1616
+ async registeredPool(poolKey) {
1617
+ const tx = new Transaction();
1618
+ tx.add(this.deepBook.registeredPool(poolKey));
1619
+ const res = await this.client.devInspectTransactionBlock({
1620
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1621
+ transactionBlock: tx
1622
+ });
1623
+ const bytes = res.results[0].returnValues[0][0];
1624
+ return bcs.bool().parse(new Uint8Array(bytes));
1625
+ }
1626
+ /**
1627
+ * @description Get the quote quantity out using input token as fee
1628
+ * @param {string} poolKey Key of the pool
1629
+ * @param {number} baseQuantity Base quantity
1630
+ * @returns {Promise<{baseQuantity: number, baseOut: number, quoteOut: number, deepRequired: number}>}
1631
+ */
1632
+ async getQuoteQuantityOutInputFee(poolKey, baseQuantity) {
1633
+ const tx = new Transaction();
1634
+ const pool = __privateGet(this, _config).getPool(poolKey);
1635
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
1636
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
1637
+ tx.add(this.deepBook.getQuoteQuantityOutInputFee(poolKey, baseQuantity));
1638
+ const res = await this.client.devInspectTransactionBlock({
1639
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1640
+ transactionBlock: tx
1641
+ });
1642
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
1643
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
1644
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
1645
+ return {
1646
+ baseQuantity,
1647
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
1648
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
1649
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
1650
+ };
1651
+ }
1652
+ /**
1653
+ * @description Get the base quantity out using input token as fee
1654
+ * @param {string} poolKey Key of the pool
1655
+ * @param {number} quoteQuantity Quote quantity
1656
+ * @returns {Promise<{quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number}>}
1657
+ */
1658
+ async getBaseQuantityOutInputFee(poolKey, quoteQuantity) {
1659
+ const tx = new Transaction();
1660
+ const pool = __privateGet(this, _config).getPool(poolKey);
1661
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
1662
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
1663
+ tx.add(this.deepBook.getBaseQuantityOutInputFee(poolKey, quoteQuantity));
1664
+ const res = await this.client.devInspectTransactionBlock({
1665
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1666
+ transactionBlock: tx
1667
+ });
1668
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
1669
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
1670
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
1671
+ return {
1672
+ quoteQuantity,
1673
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
1674
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
1675
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
1676
+ };
1677
+ }
1678
+ /**
1679
+ * @description Get the quantity out using input token as fee
1680
+ * @param {string} poolKey Key of the pool
1681
+ * @param {number} baseQuantity Base quantity
1682
+ * @param {number} quoteQuantity Quote quantity
1683
+ * @returns {Promise<{baseQuantity: number, quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number}>}
1684
+ */
1685
+ async getQuantityOutInputFee(poolKey, baseQuantity, quoteQuantity) {
1686
+ const tx = new Transaction();
1687
+ const pool = __privateGet(this, _config).getPool(poolKey);
1688
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
1689
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
1690
+ tx.add(this.deepBook.getQuantityOutInputFee(poolKey, baseQuantity, quoteQuantity));
1691
+ const res = await this.client.devInspectTransactionBlock({
1692
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1693
+ transactionBlock: tx
1694
+ });
1695
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
1696
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
1697
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
1698
+ return {
1699
+ baseQuantity,
1700
+ quoteQuantity,
1701
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
1702
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
1703
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
1704
+ };
1705
+ }
1706
+ /**
1707
+ * @description Get the base quantity needed to receive target quote quantity
1708
+ * @param {string} poolKey Key of the pool
1709
+ * @param {number} targetQuoteQuantity Target quote quantity
1710
+ * @param {boolean} payWithDeep Whether to pay fees with DEEP
1711
+ * @returns {Promise<{baseIn: number, quoteOut: number, deepRequired: number}>}
1712
+ */
1713
+ async getBaseQuantityIn(poolKey, targetQuoteQuantity, payWithDeep) {
1714
+ const tx = new Transaction();
1715
+ const pool = __privateGet(this, _config).getPool(poolKey);
1716
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
1717
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
1718
+ tx.add(this.deepBook.getBaseQuantityIn(poolKey, targetQuoteQuantity, payWithDeep));
1719
+ const res = await this.client.devInspectTransactionBlock({
1720
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1721
+ transactionBlock: tx
1722
+ });
1723
+ const baseIn = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
1724
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
1725
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
1726
+ return {
1727
+ baseIn: Number((baseIn / baseScalar).toFixed(9)),
1728
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
1729
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
1730
+ };
1731
+ }
1732
+ /**
1733
+ * @description Get the quote quantity needed to receive target base quantity
1734
+ * @param {string} poolKey Key of the pool
1735
+ * @param {number} targetBaseQuantity Target base quantity
1736
+ * @param {boolean} payWithDeep Whether to pay fees with DEEP
1737
+ * @returns {Promise<{baseOut: number, quoteIn: number, deepRequired: number}>}
1738
+ */
1739
+ async getQuoteQuantityIn(poolKey, targetBaseQuantity, payWithDeep) {
1740
+ const tx = new Transaction();
1741
+ const pool = __privateGet(this, _config).getPool(poolKey);
1742
+ const baseScalar = __privateGet(this, _config).getCoin(pool.baseCoin).scalar;
1743
+ const quoteScalar = __privateGet(this, _config).getCoin(pool.quoteCoin).scalar;
1744
+ tx.add(this.deepBook.getQuoteQuantityIn(poolKey, targetBaseQuantity, payWithDeep));
1745
+ const res = await this.client.devInspectTransactionBlock({
1746
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1747
+ transactionBlock: tx
1748
+ });
1749
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
1750
+ const quoteIn = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
1751
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0])));
1752
+ return {
1753
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
1754
+ quoteIn: Number((quoteIn / quoteScalar).toFixed(9)),
1755
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9))
1756
+ };
1757
+ }
1758
+ /**
1759
+ * @description Get account order details for a balance manager
1760
+ * @param {string} poolKey Key of the pool
1761
+ * @param {string} managerKey Key of the balance manager
1762
+ * @returns {Promise<Array>} Array of order details
1763
+ */
1764
+ async getAccountOrderDetails(poolKey, managerKey) {
1765
+ const tx = new Transaction();
1766
+ tx.add(this.deepBook.getAccountOrderDetails(poolKey, managerKey));
1767
+ const res = await this.client.devInspectTransactionBlock({
1768
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1769
+ transactionBlock: tx
1770
+ });
1771
+ try {
1772
+ const orderInformation = res.results[0].returnValues[0][0];
1773
+ return bcs.vector(Order).parse(new Uint8Array(orderInformation));
1774
+ } catch {
1775
+ return [];
1776
+ }
1777
+ }
1778
+ /**
1779
+ * @description Get the DEEP required for an order
1780
+ * @param {string} poolKey Key of the pool
1781
+ * @param {number} baseQuantity Base quantity
1782
+ * @param {number} price Price
1783
+ * @returns {Promise<{deepRequiredTaker: number, deepRequiredMaker: number}>}
1784
+ */
1785
+ async getOrderDeepRequired(poolKey, baseQuantity, price) {
1786
+ const tx = new Transaction();
1787
+ tx.add(this.deepBook.getOrderDeepRequired(poolKey, baseQuantity, price));
1788
+ const res = await this.client.devInspectTransactionBlock({
1789
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1790
+ transactionBlock: tx
1791
+ });
1792
+ const deepRequiredTaker = Number(
1793
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0]))
1794
+ );
1795
+ const deepRequiredMaker = Number(
1796
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0]))
1797
+ );
1798
+ return {
1799
+ deepRequiredTaker: Number((deepRequiredTaker / DEEP_SCALAR).toFixed(9)),
1800
+ deepRequiredMaker: Number((deepRequiredMaker / DEEP_SCALAR).toFixed(9))
1801
+ };
1802
+ }
1803
+ /**
1804
+ * @description Check if account exists for a balance manager
1805
+ * @param {string} poolKey Key of the pool
1806
+ * @param {string} managerKey Key of the balance manager
1807
+ * @returns {Promise<boolean>} Whether account exists
1808
+ */
1809
+ async accountExists(poolKey, managerKey) {
1810
+ const tx = new Transaction();
1811
+ tx.add(this.deepBook.accountExists(poolKey, managerKey));
1812
+ const res = await this.client.devInspectTransactionBlock({
1813
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1814
+ transactionBlock: tx
1815
+ });
1816
+ const bytes = res.results[0].returnValues[0][0];
1817
+ return bcs.bool().parse(new Uint8Array(bytes));
1818
+ }
1819
+ /**
1820
+ * @description Get the next epoch trade parameters
1821
+ * @param {string} poolKey Key of the pool
1822
+ * @returns {Promise<{takerFee: number, makerFee: number, stakeRequired: number}>}
1823
+ */
1824
+ async poolTradeParamsNext(poolKey) {
1825
+ const tx = new Transaction();
1826
+ tx.add(this.deepBook.poolTradeParamsNext(poolKey));
1827
+ const res = await this.client.devInspectTransactionBlock({
1828
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1829
+ transactionBlock: tx
1830
+ });
1831
+ const takerFee = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[0][0])));
1832
+ const makerFee = Number(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[1][0])));
1833
+ const stakeRequired = Number(
1834
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[2][0]))
1835
+ );
1836
+ return {
1837
+ takerFee: takerFee / FLOAT_SCALAR,
1838
+ makerFee: makerFee / FLOAT_SCALAR,
1839
+ stakeRequired: stakeRequired / DEEP_SCALAR
1840
+ };
1841
+ }
1842
+ /**
1843
+ * @description Get the quorum for a pool
1844
+ * @param {string} poolKey Key of the pool
1845
+ * @returns {Promise<number>} The quorum amount in DEEP
1846
+ */
1847
+ async quorum(poolKey) {
1848
+ const tx = new Transaction();
1849
+ tx.add(this.deepBook.quorum(poolKey));
1850
+ const res = await this.client.devInspectTransactionBlock({
1851
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1852
+ transactionBlock: tx
1853
+ });
1854
+ const bytes = res.results[0].returnValues[0][0];
1855
+ const quorum = Number(bcs.U64.parse(new Uint8Array(bytes)));
1856
+ return quorum / DEEP_SCALAR;
1857
+ }
1858
+ /**
1859
+ * @description Get the pool ID
1860
+ * @param {string} poolKey Key of the pool
1861
+ * @returns {Promise<string>} The pool ID
1862
+ */
1863
+ async poolId(poolKey) {
1864
+ const tx = new Transaction();
1865
+ tx.add(this.deepBook.poolId(poolKey));
1866
+ const res = await this.client.devInspectTransactionBlock({
1867
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1868
+ transactionBlock: tx
1869
+ });
1870
+ const bytes = res.results[0].returnValues[0][0];
1871
+ return normalizeHaneulAddress(bcs.Address.parse(new Uint8Array(bytes)));
1872
+ }
1873
+ /**
1874
+ * @description Check if a limit order can be placed
1875
+ * @param {CanPlaceLimitOrderParams} params Parameters for checking limit order placement
1876
+ * @returns {Promise<boolean>} Whether order can be placed
1877
+ */
1878
+ async canPlaceLimitOrder(params) {
1879
+ const tx = new Transaction();
1880
+ tx.add(this.deepBook.canPlaceLimitOrder(params));
1881
+ const res = await this.client.devInspectTransactionBlock({
1882
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1883
+ transactionBlock: tx
1884
+ });
1885
+ const bytes = res.results[0].returnValues[0][0];
1886
+ return bcs.bool().parse(new Uint8Array(bytes));
1887
+ }
1888
+ /**
1889
+ * @description Check if a market order can be placed
1890
+ * @param {CanPlaceMarketOrderParams} params Parameters for checking market order placement
1891
+ * @returns {Promise<boolean>} Whether order can be placed
1892
+ */
1893
+ async canPlaceMarketOrder(params) {
1894
+ const tx = new Transaction();
1895
+ tx.add(this.deepBook.canPlaceMarketOrder(params));
1896
+ const res = await this.client.devInspectTransactionBlock({
1897
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1898
+ transactionBlock: tx
1899
+ });
1900
+ const bytes = res.results[0].returnValues[0][0];
1901
+ return bcs.bool().parse(new Uint8Array(bytes));
1902
+ }
1903
+ /**
1904
+ * @description Check if market order params are valid
1905
+ * @param {string} poolKey Key of the pool
1906
+ * @param {number} quantity Quantity
1907
+ * @returns {Promise<boolean>} Whether params are valid
1908
+ */
1909
+ async checkMarketOrderParams(poolKey, quantity) {
1910
+ const tx = new Transaction();
1911
+ tx.add(this.deepBook.checkMarketOrderParams(poolKey, quantity));
1912
+ const res = await this.client.devInspectTransactionBlock({
1913
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1914
+ transactionBlock: tx
1915
+ });
1916
+ const bytes = res.results[0].returnValues[0][0];
1917
+ return bcs.bool().parse(new Uint8Array(bytes));
1918
+ }
1919
+ /**
1920
+ * @description Check if limit order params are valid
1921
+ * @param {string} poolKey Key of the pool
1922
+ * @param {number} price Price
1923
+ * @param {number} quantity Quantity
1924
+ * @param {number} expireTimestamp Expiration timestamp
1925
+ * @returns {Promise<boolean>} Whether params are valid
1926
+ */
1927
+ async checkLimitOrderParams(poolKey, price, quantity, expireTimestamp) {
1928
+ const tx = new Transaction();
1929
+ tx.add(this.deepBook.checkLimitOrderParams(poolKey, price, quantity, expireTimestamp));
1930
+ const res = await this.client.devInspectTransactionBlock({
1931
+ sender: normalizeHaneulAddress(__privateGet(this, _address)),
1932
+ transactionBlock: tx
1933
+ });
1934
+ const bytes = res.results[0].returnValues[0][0];
1935
+ return bcs.bool().parse(new Uint8Array(bytes));
1936
+ }
1937
+ }
1938
+ _config = new WeakMap();
1939
+ _address = new WeakMap();
1940
+ _DeepBookClient_instances = new WeakSet();
1941
+ /**
1942
+ * @description Helper function to format token amounts without floating point errors
1943
+ * @param {bigint} rawAmount The raw amount as bigint
1944
+ * @param {number} scalar The token scalar (e.g., 1000000000 for 9 decimals)
1945
+ * @param {number} decimals Number of decimal places to show
1946
+ * @returns {string} Formatted amount as string
1947
+ */
1948
+ formatTokenAmount_fn = function(rawAmount, scalar, decimals) {
1949
+ const scalarBigInt = BigInt(scalar);
1950
+ const integerPart = rawAmount / scalarBigInt;
1951
+ const fractionalPart = rawAmount % scalarBigInt;
1952
+ if (fractionalPart === 0n) {
1953
+ return integerPart.toString();
1954
+ }
1955
+ const scalarDigits = scalar.toString().length - 1;
1956
+ const fractionalStr = fractionalPart.toString().padStart(scalarDigits, "0");
1957
+ const truncated = fractionalStr.slice(0, decimals);
1958
+ const trimmed = truncated.replace(/0+$/, "");
1959
+ if (!trimmed) {
1960
+ return integerPart.toString();
1961
+ }
1962
+ return `${integerPart}.${trimmed}`;
1963
+ };
1964
+ export {
1965
+ DeepBookClient
1966
+ };
1967
+ //# sourceMappingURL=client.js.map