@mysten/deepbook-v3 1.1.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/client.d.mts +58 -763
  3. package/dist/client.d.mts.map +1 -1
  4. package/dist/client.mjs +253 -2127
  5. package/dist/client.mjs.map +1 -1
  6. package/dist/contracts/utils/index.mjs +1 -1
  7. package/dist/index.d.mts +4 -4
  8. package/dist/index.mjs +4 -4
  9. package/dist/pyth/pyth.mjs +1 -1
  10. package/dist/queries/accountQueries.mjs +104 -0
  11. package/dist/queries/accountQueries.mjs.map +1 -0
  12. package/dist/queries/balanceManagerQueries.mjs +113 -0
  13. package/dist/queries/balanceManagerQueries.mjs.map +1 -0
  14. package/dist/queries/context.mjs +15 -0
  15. package/dist/queries/context.mjs.map +1 -0
  16. package/dist/queries/marginManagerQueries.mjs +365 -0
  17. package/dist/queries/marginManagerQueries.mjs.map +1 -0
  18. package/dist/queries/marginPoolQueries.mjs +184 -0
  19. package/dist/queries/marginPoolQueries.mjs.map +1 -0
  20. package/dist/queries/orderQueries.mjs +165 -0
  21. package/dist/queries/orderQueries.mjs.map +1 -0
  22. package/dist/queries/poolQueries.mjs +234 -0
  23. package/dist/queries/poolQueries.mjs.map +1 -0
  24. package/dist/queries/priceFeedQueries.mjs +83 -0
  25. package/dist/queries/priceFeedQueries.mjs.map +1 -0
  26. package/dist/queries/quantityQueries.mjs +216 -0
  27. package/dist/queries/quantityQueries.mjs.map +1 -0
  28. package/dist/queries/referralQueries.mjs +96 -0
  29. package/dist/queries/referralQueries.mjs.map +1 -0
  30. package/dist/queries/registryQueries.mjs +162 -0
  31. package/dist/queries/registryQueries.mjs.map +1 -0
  32. package/dist/queries/tpslQueries.mjs +62 -0
  33. package/dist/queries/tpslQueries.mjs.map +1 -0
  34. package/dist/transactions/balanceManager.d.mts.map +1 -1
  35. package/dist/transactions/balanceManager.mjs +5 -4
  36. package/dist/transactions/balanceManager.mjs.map +1 -1
  37. package/dist/transactions/deepbook.d.mts +24 -24
  38. package/dist/transactions/deepbook.d.mts.map +1 -1
  39. package/dist/transactions/deepbook.mjs +48 -47
  40. package/dist/transactions/deepbook.mjs.map +1 -1
  41. package/dist/transactions/deepbookAdmin.d.mts +2 -2
  42. package/dist/transactions/deepbookAdmin.d.mts.map +1 -1
  43. package/dist/transactions/deepbookAdmin.mjs +10 -9
  44. package/dist/transactions/deepbookAdmin.mjs.map +1 -1
  45. package/dist/transactions/flashLoans.d.mts.map +1 -1
  46. package/dist/transactions/flashLoans.mjs +6 -4
  47. package/dist/transactions/flashLoans.mjs.map +1 -1
  48. package/dist/transactions/governance.d.mts.map +1 -1
  49. package/dist/transactions/governance.mjs +5 -4
  50. package/dist/transactions/governance.mjs.map +1 -1
  51. package/dist/transactions/marginAdmin.d.mts +7 -7
  52. package/dist/transactions/marginAdmin.d.mts.map +1 -1
  53. package/dist/transactions/marginAdmin.mjs +8 -7
  54. package/dist/transactions/marginAdmin.mjs.map +1 -1
  55. package/dist/transactions/marginLiquidations.d.mts +3 -3
  56. package/dist/transactions/marginLiquidations.d.mts.map +1 -1
  57. package/dist/transactions/marginLiquidations.mjs +5 -4
  58. package/dist/transactions/marginLiquidations.mjs.map +1 -1
  59. package/dist/transactions/marginMaintainer.d.mts +5 -5
  60. package/dist/transactions/marginMaintainer.d.mts.map +1 -1
  61. package/dist/transactions/marginMaintainer.mjs +15 -14
  62. package/dist/transactions/marginMaintainer.mjs.map +1 -1
  63. package/dist/transactions/marginManager.d.mts +23 -23
  64. package/dist/transactions/marginManager.d.mts.map +1 -1
  65. package/dist/transactions/marginManager.mjs +12 -11
  66. package/dist/transactions/marginManager.mjs.map +1 -1
  67. package/dist/transactions/marginPool.d.mts.map +1 -1
  68. package/dist/transactions/marginPool.mjs +3 -2
  69. package/dist/transactions/marginPool.mjs.map +1 -1
  70. package/dist/transactions/marginRegistry.d.mts +15 -15
  71. package/dist/transactions/marginTPSL.d.mts +1 -1
  72. package/dist/transactions/marginTPSL.d.mts.map +1 -1
  73. package/dist/transactions/marginTPSL.mjs +6 -8
  74. package/dist/transactions/marginTPSL.mjs.map +1 -1
  75. package/dist/transactions/poolProxy.d.mts.map +1 -1
  76. package/dist/transactions/poolProxy.mjs +13 -13
  77. package/dist/transactions/poolProxy.mjs.map +1 -1
  78. package/dist/types/index.d.mts +189 -52
  79. package/dist/types/index.d.mts.map +1 -1
  80. package/dist/types/index.mjs.map +1 -1
  81. package/dist/utils/constants.d.mts.map +1 -1
  82. package/dist/utils/constants.mjs +10 -0
  83. package/dist/utils/constants.mjs.map +1 -1
  84. package/dist/utils/conversion.mjs +26 -0
  85. package/dist/utils/conversion.mjs.map +1 -0
  86. package/package.json +5 -4
  87. package/src/client.ts +421 -2662
  88. package/src/index.ts +27 -0
  89. package/src/queries/accountQueries.ts +117 -0
  90. package/src/queries/balanceManagerQueries.ts +156 -0
  91. package/src/queries/context.ts +44 -0
  92. package/src/queries/index.ts +16 -0
  93. package/src/queries/marginManagerQueries.ts +575 -0
  94. package/src/queries/marginPoolQueries.ts +226 -0
  95. package/src/queries/orderQueries.ts +202 -0
  96. package/src/queries/poolQueries.ts +266 -0
  97. package/src/queries/priceFeedQueries.ts +141 -0
  98. package/src/queries/quantityQueries.ts +266 -0
  99. package/src/queries/referralQueries.ts +112 -0
  100. package/src/queries/registryQueries.ts +185 -0
  101. package/src/queries/tpslQueries.ts +88 -0
  102. package/src/transactions/balanceManager.ts +5 -4
  103. package/src/transactions/deepbook.ts +98 -84
  104. package/src/transactions/deepbookAdmin.ts +10 -9
  105. package/src/transactions/flashLoans.ts +5 -4
  106. package/src/transactions/governance.ts +5 -4
  107. package/src/transactions/marginAdmin.ts +8 -7
  108. package/src/transactions/marginLiquidations.ts +5 -4
  109. package/src/transactions/marginMaintainer.ts +15 -14
  110. package/src/transactions/marginManager.ts +12 -11
  111. package/src/transactions/marginPool.ts +3 -2
  112. package/src/transactions/marginTPSL.ts +11 -6
  113. package/src/transactions/poolProxy.ts +12 -11
  114. package/src/types/index.ts +200 -53
  115. package/src/utils/constants.ts +10 -0
  116. package/src/utils/conversion.ts +33 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marginManagerQueries.mjs","names":["#ctx"],"sources":["../../src/queries/marginManagerQueries.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/sui/bcs';\nimport { Transaction } from '@mysten/sui/transactions';\nimport { normalizeSuiAddress } from '@mysten/sui/utils';\n\nimport type {\n\tBorrowedShares,\n\tMarginManagerAssets,\n\tMarginManagerBalancesResult,\n\tMarginManagerDebts,\n\tMarginManagerState,\n} from '../types/index.js';\nimport { FLOAT_SCALAR } from '../utils/config.js';\nimport { formatTokenAmount } from './context.js';\nimport type { QueryContext } from './context.js';\n\nexport class MarginManagerQueries {\n\t#ctx: QueryContext;\n\n\tconstructor(ctx: QueryContext) {\n\t\tthis.#ctx = ctx;\n\t}\n\n\tasync getMarginManagerOwner(marginManagerKey: string): Promise<string> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.ownerByPoolKey(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn normalizeSuiAddress(bcs.Address.parse(bytes));\n\t}\n\n\tasync getMarginManagerDeepbookPool(marginManagerKey: string): Promise<string> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.deepbookPool(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn normalizeSuiAddress(bcs.Address.parse(bytes));\n\t}\n\n\tasync getMarginManagerMarginPoolId(marginManagerKey: string): Promise<string | null> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.marginPoolId(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst option = bcs.option(bcs.Address).parse(bytes);\n\t\treturn option ? normalizeSuiAddress(option) : null;\n\t}\n\n\tasync getMarginManagerBorrowedShares(marginManagerKey: string): Promise<BorrowedShares> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.borrowedShares(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst baseBytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst quoteBytes = res.commandResults![0].returnValues[1].bcs;\n\t\tconst baseShares = bcs.U64.parse(baseBytes).toString();\n\t\tconst quoteShares = bcs.U64.parse(quoteBytes).toString();\n\n\t\treturn { baseShares, quoteShares };\n\t}\n\n\tasync getMarginManagerBorrowedBaseShares(marginManagerKey: string): Promise<string> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.borrowedBaseShares(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn bcs.U64.parse(bytes).toString();\n\t}\n\n\tasync getMarginManagerBorrowedQuoteShares(marginManagerKey: string): Promise<string> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.borrowedQuoteShares(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn bcs.U64.parse(bytes).toString();\n\t}\n\n\tasync getMarginManagerHasBaseDebt(marginManagerKey: string): Promise<boolean> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.hasBaseDebt(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn bcs.bool().parse(bytes);\n\t}\n\n\tasync getMarginManagerBalanceManagerId(marginManagerAddress: string): Promise<string> {\n\t\tconst res = await this.#ctx.client.core.getObject({\n\t\t\tobjectId: marginManagerAddress,\n\t\t\tinclude: { content: true },\n\t\t});\n\n\t\tif (!res.object?.content) {\n\t\t\tthrow new Error(`Margin manager not found: ${marginManagerAddress}`);\n\t\t}\n\n\t\tconst MarginManagerBalanceManagerId = bcs.struct('MarginManagerBalanceManagerId', {\n\t\t\tid: bcs.Address,\n\t\t\towner: bcs.Address,\n\t\t\tdeepbook_pool: bcs.Address,\n\t\t\tmargin_pool_id: bcs.option(bcs.Address),\n\t\t\tbalance_manager_id: bcs.Address,\n\t\t});\n\n\t\tconst parsed = MarginManagerBalanceManagerId.parse(res.object.content);\n\t\treturn normalizeSuiAddress(parsed.balance_manager_id);\n\t}\n\n\tasync getMarginManagerAssets(\n\t\tmarginManagerKey: string,\n\t\tdecimals: number = 6,\n\t): Promise<MarginManagerAssets> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.calculateAssets(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst baseBytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst quoteBytes = res.commandResults![0].returnValues[1].bcs;\n\t\tconst pool = this.#ctx.config.getPool(manager.poolKey);\n\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\n\t\tconst baseAsset = formatTokenAmount(\n\t\t\tBigInt(bcs.U64.parse(baseBytes)),\n\t\t\tbaseCoin.scalar,\n\t\t\tdecimals,\n\t\t);\n\t\tconst quoteAsset = formatTokenAmount(\n\t\t\tBigInt(bcs.U64.parse(quoteBytes)),\n\t\t\tquoteCoin.scalar,\n\t\t\tdecimals,\n\t\t);\n\n\t\treturn { baseAsset, quoteAsset };\n\t}\n\n\tasync getMarginManagerDebts(\n\t\tmarginManagerKey: string,\n\t\tdecimals: number = 6,\n\t): Promise<MarginManagerDebts> {\n\t\tconst hasBaseDebt = await this.getMarginManagerHasBaseDebt(marginManagerKey);\n\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst pool = this.#ctx.config.getPool(manager.poolKey);\n\t\tconst debtCoinKey = hasBaseDebt ? pool.baseCoin : pool.quoteCoin;\n\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.calculateDebts(manager.poolKey, debtCoinKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {\n\t\t\tthrow new Error(`Failed to get margin manager debts: ${'Unknown error'}`);\n\t\t}\n\n\t\tconst baseBytes = res.commandResults[0].returnValues[0].bcs;\n\t\tconst quoteBytes = res.commandResults[0].returnValues[1].bcs;\n\t\tconst debtCoin = this.#ctx.config.getCoin(debtCoinKey);\n\n\t\tconst baseDebt = formatTokenAmount(BigInt(bcs.U64.parse(baseBytes)), debtCoin.scalar, decimals);\n\t\tconst quoteDebt = formatTokenAmount(\n\t\t\tBigInt(bcs.U64.parse(quoteBytes)),\n\t\t\tdebtCoin.scalar,\n\t\t\tdecimals,\n\t\t);\n\n\t\treturn { baseDebt, quoteDebt };\n\t}\n\n\tasync getMarginManagerState(\n\t\tmarginManagerKey: string,\n\t\tdecimals: number = 6,\n\t): Promise<MarginManagerState> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.managerState(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {\n\t\t\tthrow new Error(`Failed to get margin manager state: Unknown error`);\n\t\t}\n\n\t\tconst pool = this.#ctx.config.getPool(manager.poolKey);\n\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\n\t\tconst managerId = normalizeSuiAddress(\n\t\t\tbcs.Address.parse(res.commandResults[0].returnValues[0].bcs),\n\t\t);\n\t\tconst deepbookPoolId = normalizeSuiAddress(\n\t\t\tbcs.Address.parse(res.commandResults[0].returnValues[1].bcs),\n\t\t);\n\t\tconst riskRatio =\n\t\t\tNumber(bcs.U64.parse(res.commandResults[0].returnValues[2].bcs)) / FLOAT_SCALAR;\n\t\tconst baseAsset = formatTokenAmount(\n\t\t\tBigInt(bcs.U64.parse(res.commandResults[0].returnValues[3].bcs)),\n\t\t\tbaseCoin.scalar,\n\t\t\tdecimals,\n\t\t);\n\t\tconst quoteAsset = formatTokenAmount(\n\t\t\tBigInt(bcs.U64.parse(res.commandResults[0].returnValues[4].bcs)),\n\t\t\tquoteCoin.scalar,\n\t\t\tdecimals,\n\t\t);\n\t\tconst baseDebt = formatTokenAmount(\n\t\t\tBigInt(bcs.U64.parse(res.commandResults[0].returnValues[5].bcs)),\n\t\t\tbaseCoin.scalar,\n\t\t\tdecimals,\n\t\t);\n\t\tconst quoteDebt = formatTokenAmount(\n\t\t\tBigInt(bcs.U64.parse(res.commandResults[0].returnValues[6].bcs)),\n\t\t\tquoteCoin.scalar,\n\t\t\tdecimals,\n\t\t);\n\t\tconst basePythPrice = bcs.U64.parse(res.commandResults[0].returnValues[7].bcs);\n\t\tconst basePythDecimals = Number(\n\t\t\tbcs.u8().parse(new Uint8Array(res.commandResults[0].returnValues[8].bcs)),\n\t\t);\n\t\tconst quotePythPrice = bcs.U64.parse(res.commandResults[0].returnValues[9].bcs);\n\t\tconst quotePythDecimals = Number(\n\t\t\tbcs.u8().parse(new Uint8Array(res.commandResults[0].returnValues[10].bcs)),\n\t\t);\n\t\tconst currentPrice = BigInt(bcs.U64.parse(res.commandResults[0].returnValues[11].bcs));\n\t\tconst lowestTriggerAbovePrice = BigInt(\n\t\t\tbcs.U64.parse(res.commandResults[0].returnValues[12].bcs),\n\t\t);\n\t\tconst highestTriggerBelowPrice = BigInt(\n\t\t\tbcs.U64.parse(res.commandResults[0].returnValues[13].bcs),\n\t\t);\n\n\t\treturn {\n\t\t\tmanagerId,\n\t\t\tdeepbookPoolId,\n\t\t\triskRatio,\n\t\t\tbaseAsset,\n\t\t\tquoteAsset,\n\t\t\tbaseDebt,\n\t\t\tquoteDebt,\n\t\t\tbasePythPrice: basePythPrice.toString(),\n\t\t\tbasePythDecimals,\n\t\t\tquotePythPrice: quotePythPrice.toString(),\n\t\t\tquotePythDecimals,\n\t\t\tcurrentPrice,\n\t\t\tlowestTriggerAbovePrice,\n\t\t\thighestTriggerBelowPrice,\n\t\t};\n\t}\n\n\tasync getMarginManagerStates(\n\t\tmarginManagers: Record<string, string>,\n\t\tdecimals: number = 6,\n\t): Promise<Record<string, MarginManagerState>> {\n\t\tconst entries = Object.entries(marginManagers);\n\t\tif (entries.length === 0) {\n\t\t\treturn {};\n\t\t}\n\n\t\tconst tx = new Transaction();\n\n\t\tfor (const [managerId, poolKey] of entries) {\n\t\t\ttx.add(this.#ctx.marginManager.managerState(poolKey, managerId));\n\t\t}\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults) {\n\t\t\tthrow new Error(`Failed to get margin manager states: Unknown error`);\n\t\t}\n\n\t\tconst results: Record<string, MarginManagerState> = {};\n\n\t\tfor (let i = 0; i < entries.length; i++) {\n\t\t\tconst commandResult = res.commandResults[i];\n\t\t\tif (!commandResult || !commandResult.returnValues) {\n\t\t\t\tthrow new Error(`Failed to get margin manager state for index ${i}: No return values`);\n\t\t\t}\n\n\t\t\tconst [, poolKey] = entries[i];\n\t\t\tconst pool = this.#ctx.config.getPool(poolKey);\n\t\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\t\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\n\t\t\tconst managerId = normalizeSuiAddress(bcs.Address.parse(commandResult.returnValues[0].bcs));\n\t\t\tconst deepbookPoolId = normalizeSuiAddress(\n\t\t\t\tbcs.Address.parse(commandResult.returnValues[1].bcs),\n\t\t\t);\n\t\t\tconst riskRatio = Number(bcs.U64.parse(commandResult.returnValues[2].bcs)) / FLOAT_SCALAR;\n\t\t\tconst baseAsset = formatTokenAmount(\n\t\t\t\tBigInt(bcs.U64.parse(commandResult.returnValues[3].bcs)),\n\t\t\t\tbaseCoin.scalar,\n\t\t\t\tdecimals,\n\t\t\t);\n\t\t\tconst quoteAsset = formatTokenAmount(\n\t\t\t\tBigInt(bcs.U64.parse(commandResult.returnValues[4].bcs)),\n\t\t\t\tquoteCoin.scalar,\n\t\t\t\tdecimals,\n\t\t\t);\n\t\t\tconst baseDebt = formatTokenAmount(\n\t\t\t\tBigInt(bcs.U64.parse(commandResult.returnValues[5].bcs)),\n\t\t\t\tbaseCoin.scalar,\n\t\t\t\tdecimals,\n\t\t\t);\n\t\t\tconst quoteDebt = formatTokenAmount(\n\t\t\t\tBigInt(bcs.U64.parse(commandResult.returnValues[6].bcs)),\n\t\t\t\tquoteCoin.scalar,\n\t\t\t\tdecimals,\n\t\t\t);\n\t\t\tconst basePythPrice = bcs.U64.parse(commandResult.returnValues[7].bcs);\n\t\t\tconst basePythDecimals = Number(\n\t\t\t\tbcs.u8().parse(new Uint8Array(commandResult.returnValues[8].bcs)),\n\t\t\t);\n\t\t\tconst quotePythPrice = bcs.U64.parse(commandResult.returnValues[9].bcs);\n\t\t\tconst quotePythDecimals = Number(\n\t\t\t\tbcs.u8().parse(new Uint8Array(commandResult.returnValues[10].bcs)),\n\t\t\t);\n\t\t\tconst currentPrice = BigInt(bcs.U64.parse(commandResult.returnValues[11].bcs));\n\t\t\tconst lowestTriggerAbovePrice = BigInt(bcs.U64.parse(commandResult.returnValues[12].bcs));\n\t\t\tconst highestTriggerBelowPrice = BigInt(bcs.U64.parse(commandResult.returnValues[13].bcs));\n\n\t\t\tresults[managerId] = {\n\t\t\t\tmanagerId,\n\t\t\t\tdeepbookPoolId,\n\t\t\t\triskRatio,\n\t\t\t\tbaseAsset,\n\t\t\t\tquoteAsset,\n\t\t\t\tbaseDebt,\n\t\t\t\tquoteDebt,\n\t\t\t\tbasePythPrice: basePythPrice.toString(),\n\t\t\t\tbasePythDecimals,\n\t\t\t\tquotePythPrice: quotePythPrice.toString(),\n\t\t\t\tquotePythDecimals,\n\t\t\t\tcurrentPrice,\n\t\t\t\tlowestTriggerAbovePrice,\n\t\t\t\thighestTriggerBelowPrice,\n\t\t\t};\n\t\t}\n\n\t\treturn results;\n\t}\n\n\tasync getMarginManagerBaseBalance(\n\t\tmarginManagerKey: string,\n\t\tdecimals: number = 9,\n\t): Promise<string> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.baseBalance(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {\n\t\t\tthrow new Error(`Failed to get margin manager base balance: Unknown error`);\n\t\t}\n\n\t\tconst bytes = res.commandResults[0].returnValues[0].bcs;\n\t\tconst pool = this.#ctx.config.getPool(manager.poolKey);\n\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\n\t\treturn formatTokenAmount(BigInt(bcs.U64.parse(bytes)), baseCoin.scalar, decimals);\n\t}\n\n\tasync getMarginManagerQuoteBalance(\n\t\tmarginManagerKey: string,\n\t\tdecimals: number = 9,\n\t): Promise<string> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.quoteBalance(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {\n\t\t\tthrow new Error(`Failed to get margin manager quote balance: Unknown error`);\n\t\t}\n\n\t\tconst bytes = res.commandResults[0].returnValues[0].bcs;\n\t\tconst pool = this.#ctx.config.getPool(manager.poolKey);\n\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\n\t\treturn formatTokenAmount(BigInt(bcs.U64.parse(bytes)), quoteCoin.scalar, decimals);\n\t}\n\n\tasync getMarginManagerDeepBalance(\n\t\tmarginManagerKey: string,\n\t\tdecimals: number = 6,\n\t): Promise<string> {\n\t\tconst manager = this.#ctx.config.getMarginManager(marginManagerKey);\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginManager.deepBalance(manager.poolKey, manager.address));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {\n\t\t\tthrow new Error(`Failed to get margin manager DEEP balance: Unknown error`);\n\t\t}\n\n\t\tconst bytes = res.commandResults[0].returnValues[0].bcs;\n\t\tconst deepCoin = this.#ctx.config.getCoin('DEEP');\n\n\t\treturn formatTokenAmount(BigInt(bcs.U64.parse(bytes)), deepCoin.scalar, decimals);\n\t}\n\n\tasync getMarginManagerBalances(\n\t\tmarginManagers: Record<string, string>,\n\t\tdecimals: number = 9,\n\t): Promise<Record<string, MarginManagerBalancesResult>> {\n\t\tconst entries = Object.entries(marginManagers);\n\t\tif (entries.length === 0) {\n\t\t\treturn {};\n\t\t}\n\n\t\tconst tx = new Transaction();\n\n\t\tfor (const [managerId, poolKey] of entries) {\n\t\t\ttx.add(this.#ctx.marginManager.baseBalance(poolKey, managerId));\n\t\t\ttx.add(this.#ctx.marginManager.quoteBalance(poolKey, managerId));\n\t\t\ttx.add(this.#ctx.marginManager.deepBalance(poolKey, managerId));\n\t\t}\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults) {\n\t\t\tthrow new Error('Failed to get margin manager balances: No command results');\n\t\t}\n\n\t\tconst results: Record<string, MarginManagerBalancesResult> = {};\n\t\tconst deepCoin = this.#ctx.config.getCoin('DEEP');\n\n\t\tfor (let i = 0; i < entries.length; i++) {\n\t\t\tconst [managerId, poolKey] = entries[i];\n\t\t\tconst pool = this.#ctx.config.getPool(poolKey);\n\t\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\t\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\n\t\t\tconst baseResult = res.commandResults[i * 3];\n\t\t\tconst quoteResult = res.commandResults[i * 3 + 1];\n\t\t\tconst deepResult = res.commandResults[i * 3 + 2];\n\n\t\t\tif (!baseResult?.returnValues || !quoteResult?.returnValues || !deepResult?.returnValues) {\n\t\t\t\tthrow new Error(`Failed to get balances for margin manager ${managerId}: No return values`);\n\t\t\t}\n\n\t\t\tresults[managerId] = {\n\t\t\t\tbase: formatTokenAmount(\n\t\t\t\t\tBigInt(bcs.U64.parse(baseResult.returnValues[0].bcs)),\n\t\t\t\t\tbaseCoin.scalar,\n\t\t\t\t\tdecimals,\n\t\t\t\t),\n\t\t\t\tquote: formatTokenAmount(\n\t\t\t\t\tBigInt(bcs.U64.parse(quoteResult.returnValues[0].bcs)),\n\t\t\t\t\tquoteCoin.scalar,\n\t\t\t\t\tdecimals,\n\t\t\t\t),\n\t\t\t\tdeep: formatTokenAmount(\n\t\t\t\t\tBigInt(bcs.U64.parse(deepResult.returnValues[0].bcs)),\n\t\t\t\t\tdeepCoin.scalar,\n\t\t\t\t\tdecimals,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\treturn results;\n\t}\n}\n"],"mappings":";;;;;;;AAkBA,IAAa,uBAAb,MAAkC;CACjC;CAEA,YAAY,KAAmB;AAC9B,QAAKA,MAAO;;CAGb,MAAM,sBAAsB,kBAA2C;EACtE,MAAM,UAAU,MAAKA,IAAK,OAAO,iBAAiB,iBAAiB;EACnE,MAAM,KAAK,IAAI,aAAa;AAC5B,KAAG,IAAI,MAAKA,IAAK,cAAc,eAAe,QAAQ,SAAS,QAAQ,QAAQ,CAAC;EAOhF,MAAM,SALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEgB,eAAgB,GAAG,aAAa,GAAG;AACrD,SAAO,oBAAoB,IAAI,QAAQ,MAAM,MAAM,CAAC;;CAGrD,MAAM,6BAA6B,kBAA2C;EAC7E,MAAM,UAAU,MAAKA,IAAK,OAAO,iBAAiB,iBAAiB;EACnE,MAAM,KAAK,IAAI,aAAa;AAC5B,KAAG,IAAI,MAAKA,IAAK,cAAc,aAAa,QAAQ,SAAS,QAAQ,QAAQ,CA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E1D,MAAM,aAAa,IAAI,eAAe,IAAI;GAC1C,MAAM,cAAc,IAAI,eAAe,IAAI,IAAI;GAC/C,MAAM,aAAa,IAAI,eAAe,IAAI,IAAI;AAE9C,OAAI,CAAC,YAAY,gBAAgB,CAAC,aAAa,gBAAgB,CAAC,YAAY,aAC3E,OAAM,IAAI,MAAM,6CAA6C,UAAU,oBAAoB;AAG5F,WAAQ,aAAa;IACpB,MAAM,kBACL,OAAO,IAAI,IAAI,MAAM,WAAW,aAAa,GAAG,IAAI,CAAC,EACrD,SAAS,QACT,SACA;IACD,OAAO,kBACN,OAAO,IAAI,IAAI,MAAM,YAAY,aAAa,GAAG,IAAI,CAAC,EACtD,UAAU,QACV,SACA;IACD,MAAM,kBACL,OAAO,IAAI,IAAI,MAAM,WAAW,aAAa,GAAG,IAAI,CAAC,EACrD,SAAS,QACT,SACA;IACD;;AAGF,SAAO"}
@@ -0,0 +1,184 @@
1
+ import { FLOAT_SCALAR } from "../utils/config.mjs";
2
+ import { formatTokenAmount } from "./context.mjs";
3
+ import { bcs } from "@mysten/sui/bcs";
4
+ import { Transaction } from "@mysten/sui/transactions";
5
+
6
+ //#region src/queries/marginPoolQueries.ts
7
+ var MarginPoolQueries = class {
8
+ #ctx;
9
+ constructor(ctx) {
10
+ this.#ctx = ctx;
11
+ }
12
+ async getMarginPoolId(coinKey) {
13
+ const tx = new Transaction();
14
+ tx.add(this.#ctx.marginPool.getId(coinKey));
15
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
16
+ transaction: tx,
17
+ include: {
18
+ commandResults: true,
19
+ effects: true
20
+ }
21
+ })).commandResults[0].returnValues[0].bcs;
22
+ return bcs.Address.parse(bytes);
23
+ }
24
+ async isDeepbookPoolAllowed(coinKey, deepbookPoolId) {
25
+ const tx = new Transaction();
26
+ tx.add(this.#ctx.marginPool.deepbookPoolAllowed(coinKey, deepbookPoolId));
27
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
28
+ transaction: tx,
29
+ include: {
30
+ commandResults: true,
31
+ effects: true
32
+ }
33
+ })).commandResults[0].returnValues[0].bcs;
34
+ return bcs.bool().parse(bytes);
35
+ }
36
+ async getMarginPoolTotalSupply(coinKey, decimals = 6) {
37
+ const tx = new Transaction();
38
+ tx.add(this.#ctx.marginPool.totalSupply(coinKey));
39
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
40
+ transaction: tx,
41
+ include: {
42
+ commandResults: true,
43
+ effects: true
44
+ }
45
+ })).commandResults[0].returnValues[0].bcs;
46
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
47
+ }
48
+ async getMarginPoolSupplyShares(coinKey, decimals = 6) {
49
+ const tx = new Transaction();
50
+ tx.add(this.#ctx.marginPool.supplyShares(coinKey));
51
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
52
+ transaction: tx,
53
+ include: {
54
+ commandResults: true,
55
+ effects: true
56
+ }
57
+ })).commandResults[0].returnValues[0].bcs;
58
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
59
+ }
60
+ async getMarginPoolTotalBorrow(coinKey, decimals = 6) {
61
+ const tx = new Transaction();
62
+ tx.add(this.#ctx.marginPool.totalBorrow(coinKey));
63
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
64
+ transaction: tx,
65
+ include: {
66
+ commandResults: true,
67
+ effects: true
68
+ }
69
+ })).commandResults[0].returnValues[0].bcs;
70
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
71
+ }
72
+ async getMarginPoolBorrowShares(coinKey, decimals = 6) {
73
+ const tx = new Transaction();
74
+ tx.add(this.#ctx.marginPool.borrowShares(coinKey));
75
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
76
+ transaction: tx,
77
+ include: {
78
+ commandResults: true,
79
+ effects: true
80
+ }
81
+ })).commandResults[0].returnValues[0].bcs;
82
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
83
+ }
84
+ async getMarginPoolLastUpdateTimestamp(coinKey) {
85
+ const tx = new Transaction();
86
+ tx.add(this.#ctx.marginPool.lastUpdateTimestamp(coinKey));
87
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
88
+ transaction: tx,
89
+ include: {
90
+ commandResults: true,
91
+ effects: true
92
+ }
93
+ })).commandResults[0].returnValues[0].bcs;
94
+ return Number(bcs.U64.parse(bytes));
95
+ }
96
+ async getMarginPoolSupplyCap(coinKey, decimals = 6) {
97
+ const tx = new Transaction();
98
+ tx.add(this.#ctx.marginPool.supplyCap(coinKey));
99
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
100
+ transaction: tx,
101
+ include: {
102
+ commandResults: true,
103
+ effects: true
104
+ }
105
+ })).commandResults[0].returnValues[0].bcs;
106
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
107
+ }
108
+ async getMarginPoolMaxUtilizationRate(coinKey) {
109
+ const tx = new Transaction();
110
+ tx.add(this.#ctx.marginPool.maxUtilizationRate(coinKey));
111
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
112
+ transaction: tx,
113
+ include: {
114
+ commandResults: true,
115
+ effects: true
116
+ }
117
+ })).commandResults[0].returnValues[0].bcs;
118
+ return Number(bcs.U64.parse(bytes)) / FLOAT_SCALAR;
119
+ }
120
+ async getMarginPoolProtocolSpread(coinKey) {
121
+ const tx = new Transaction();
122
+ tx.add(this.#ctx.marginPool.protocolSpread(coinKey));
123
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
124
+ transaction: tx,
125
+ include: {
126
+ commandResults: true,
127
+ effects: true
128
+ }
129
+ })).commandResults[0].returnValues[0].bcs;
130
+ return Number(bcs.U64.parse(bytes)) / FLOAT_SCALAR;
131
+ }
132
+ async getMarginPoolMinBorrow(coinKey, decimals = 6) {
133
+ const tx = new Transaction();
134
+ tx.add(this.#ctx.marginPool.minBorrow(coinKey));
135
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
136
+ transaction: tx,
137
+ include: {
138
+ commandResults: true,
139
+ effects: true
140
+ }
141
+ })).commandResults[0].returnValues[0].bcs;
142
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
143
+ }
144
+ async getMarginPoolInterestRate(coinKey) {
145
+ const tx = new Transaction();
146
+ tx.add(this.#ctx.marginPool.interestRate(coinKey));
147
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
148
+ transaction: tx,
149
+ include: {
150
+ commandResults: true,
151
+ effects: true
152
+ }
153
+ })).commandResults[0].returnValues[0].bcs;
154
+ return Number(bcs.U64.parse(bytes)) / FLOAT_SCALAR;
155
+ }
156
+ async getUserSupplyShares(coinKey, supplierCapId, decimals = 6) {
157
+ const tx = new Transaction();
158
+ tx.add(this.#ctx.marginPool.userSupplyShares(coinKey, supplierCapId));
159
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
160
+ transaction: tx,
161
+ include: {
162
+ commandResults: true,
163
+ effects: true
164
+ }
165
+ })).commandResults[0].returnValues[0].bcs;
166
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
167
+ }
168
+ async getUserSupplyAmount(coinKey, supplierCapId, decimals = 6) {
169
+ const tx = new Transaction();
170
+ tx.add(this.#ctx.marginPool.userSupplyAmount(coinKey, supplierCapId));
171
+ const bytes = (await this.#ctx.client.core.simulateTransaction({
172
+ transaction: tx,
173
+ include: {
174
+ commandResults: true,
175
+ effects: true
176
+ }
177
+ })).commandResults[0].returnValues[0].bcs;
178
+ return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), this.#ctx.config.getCoin(coinKey).scalar, decimals);
179
+ }
180
+ };
181
+
182
+ //#endregion
183
+ export { MarginPoolQueries };
184
+ //# sourceMappingURL=marginPoolQueries.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marginPoolQueries.mjs","names":["#ctx"],"sources":["../../src/queries/marginPoolQueries.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/sui/bcs';\nimport { Transaction } from '@mysten/sui/transactions';\n\nimport { FLOAT_SCALAR } from '../utils/config.js';\nimport { formatTokenAmount } from './context.js';\nimport type { QueryContext } from './context.js';\n\nexport class MarginPoolQueries {\n\t#ctx: QueryContext;\n\n\tconstructor(ctx: QueryContext) {\n\t\tthis.#ctx = ctx;\n\t}\n\n\tasync getMarginPoolId(coinKey: string): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.getId(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn bcs.Address.parse(bytes);\n\t}\n\n\tasync isDeepbookPoolAllowed(coinKey: string, deepbookPoolId: string): Promise<boolean> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.deepbookPoolAllowed(coinKey, deepbookPoolId));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn bcs.bool().parse(bytes);\n\t}\n\n\tasync getMarginPoolTotalSupply(coinKey: string, decimals: number = 6): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.totalSupply(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawAmount = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawAmount, coin.scalar, decimals);\n\t}\n\n\tasync getMarginPoolSupplyShares(coinKey: string, decimals: number = 6): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.supplyShares(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawShares = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawShares, coin.scalar, decimals);\n\t}\n\n\tasync getMarginPoolTotalBorrow(coinKey: string, decimals: number = 6): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.totalBorrow(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawAmount = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawAmount, coin.scalar, decimals);\n\t}\n\n\tasync getMarginPoolBorrowShares(coinKey: string, decimals: number = 6): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.borrowShares(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawShares = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawShares, coin.scalar, decimals);\n\t}\n\n\tasync getMarginPoolLastUpdateTimestamp(coinKey: string): Promise<number> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.lastUpdateTimestamp(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn Number(bcs.U64.parse(bytes));\n\t}\n\n\tasync getMarginPoolSupplyCap(coinKey: string, decimals: number = 6): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.supplyCap(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawAmount = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawAmount, coin.scalar, decimals);\n\t}\n\n\tasync getMarginPoolMaxUtilizationRate(coinKey: string): Promise<number> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.maxUtilizationRate(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawRate = Number(bcs.U64.parse(bytes));\n\t\treturn rawRate / FLOAT_SCALAR;\n\t}\n\n\tasync getMarginPoolProtocolSpread(coinKey: string): Promise<number> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.protocolSpread(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawSpread = Number(bcs.U64.parse(bytes));\n\t\treturn rawSpread / FLOAT_SCALAR;\n\t}\n\n\tasync getMarginPoolMinBorrow(coinKey: string, decimals: number = 6): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.minBorrow(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawAmount = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawAmount, coin.scalar, decimals);\n\t}\n\n\tasync getMarginPoolInterestRate(coinKey: string): Promise<number> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.interestRate(coinKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawRate = Number(bcs.U64.parse(bytes));\n\t\treturn rawRate / FLOAT_SCALAR;\n\t}\n\n\tasync getUserSupplyShares(\n\t\tcoinKey: string,\n\t\tsupplierCapId: string,\n\t\tdecimals: number = 6,\n\t): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.userSupplyShares(coinKey, supplierCapId));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawShares = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawShares, coin.scalar, decimals);\n\t}\n\n\tasync getUserSupplyAmount(\n\t\tcoinKey: string,\n\t\tsupplierCapId: string,\n\t\tdecimals: number = 6,\n\t): Promise<string> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.marginPool.userSupplyAmount(coinKey, supplierCapId));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst rawAmount = BigInt(bcs.U64.parse(bytes));\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\t\treturn formatTokenAmount(rawAmount, coin.scalar, decimals);\n\t}\n}\n"],"mappings":";;;;;;AAUA,IAAa,oBAAb,MAA+B;CAC9B;CAEA,YAAY,KAAmB;AAC9B,QAAKA,MAAO;;CAGb,MAAM,gBAAgB,SAAkC;EACvD,MAAM,KAAK,IAAI,aAAa;AAC5B,KAAG,IAAI,MAAKA,IAAK,WAAW,MAAM,QAAQ,CAAC;EAO3C,MAAM,SALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEgB,eAAgB,GAAG,aAAa,GAAG;AACrD,SAAO,IAAI,QAAQ,MAAM,MAAM;;CAGhC,MAAM,sBAAsB,SAAiB,gBAA0C;EACtF,MAAM,KAAK,IAAI,aAAa;AAC5B,KAAG,IAAI,MAAKA,IAAK,WAAW,oBAAoB,SAAS,eAAe,CAAC;EAOzE,MAAM,SALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEgB,eAAgB,GAAG,aAAa,GAAG;AACrD,SAAO,IAAI,MAAM,CAAC,MAAM,MAAM;;CAG/B,MAAM,yBAAyB,SAAiB,WAAmB,GAAoB;EACtF,MAAM,KAAK,IAAI,aAAa;AAC5B,KAAG,IAAI,MAAKA,IAAK,WAAW,YAAY,Q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K,IAAI,aAAa;AAC5B,KAAG,IAAI,MAAKA,IAAK,WAAW,iBAAiB,SAAS,cAAc,CAAC;EAOrE,MAAM,SALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEgB,eAAgB,GAAG,aAAa,GAAG;AAGrD,SAAO,kBAFW,OAAO,IAAI,IAAI,MAAM,MAAM,CAAC,EACjC,MAAKA,IAAK,OAAO,QAAQ,QAAQ,CACL,QAAQ,SAAS;;CAG3D,MAAM,oBACL,SACA,eACA,WAAmB,GACD;EAClB,MAAM,KAAK,IAAI,aAAa;AAC5B,KAAG,IAAI,MAAKA,IAAK,WAAW,iBAAiB,SAAS,cAAc,CAAC;EAOrE,MAAM,SALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEgB,eAAgB,GAAG,aAAa,GAAG;AAGrD,SAAO,kBAFW,OAAO,IAAI,IAAI,MAAM,MAAM,CAAC,EACjC,MAAKA,IAAK,OAAO,QAAQ,QAAQ,CACL,QAAQ,SAAS"}
@@ -0,0 +1,165 @@
1
+ import { VecSet } from "../contracts/deepbook/deps/sui/vec_set.mjs";
2
+ import { Order } from "../contracts/deepbook/order.mjs";
3
+ import "../types/bcs.mjs";
4
+ import { DEEP_SCALAR, FLOAT_SCALAR } from "../utils/config.mjs";
5
+ import { bcs } from "@mysten/sui/bcs";
6
+ import { Transaction } from "@mysten/sui/transactions";
7
+
8
+ //#region src/queries/orderQueries.ts
9
+ var OrderQueries = class {
10
+ #ctx;
11
+ constructor(ctx) {
12
+ this.#ctx = ctx;
13
+ }
14
+ async accountOpenOrders(poolKey, managerKey) {
15
+ const tx = new Transaction();
16
+ tx.add(this.#ctx.deepBook.accountOpenOrders(poolKey, managerKey));
17
+ const order_ids = (await this.#ctx.client.core.simulateTransaction({
18
+ transaction: tx,
19
+ include: {
20
+ commandResults: true,
21
+ effects: true
22
+ }
23
+ })).commandResults[0].returnValues[0].bcs;
24
+ return VecSet(bcs.u128()).parse(new Uint8Array(order_ids)).contents;
25
+ }
26
+ async getOrder(poolKey, orderId) {
27
+ const tx = new Transaction();
28
+ tx.add(this.#ctx.deepBook.getOrder(poolKey, orderId));
29
+ const res = await this.#ctx.client.core.simulateTransaction({
30
+ transaction: tx,
31
+ include: {
32
+ commandResults: true,
33
+ effects: true
34
+ }
35
+ });
36
+ try {
37
+ const orderInformation = res.commandResults[0].returnValues[0].bcs;
38
+ return Order.parse(new Uint8Array(orderInformation));
39
+ } catch {
40
+ return null;
41
+ }
42
+ }
43
+ async getOrderNormalized(poolKey, orderId) {
44
+ const tx = new Transaction();
45
+ tx.add(this.#ctx.deepBook.getOrder(poolKey, orderId));
46
+ const res = await this.#ctx.client.core.simulateTransaction({
47
+ transaction: tx,
48
+ include: {
49
+ commandResults: true,
50
+ effects: true
51
+ }
52
+ });
53
+ try {
54
+ const orderInformation = res.commandResults[0].returnValues[0].bcs;
55
+ const orderInfo = Order.parse(new Uint8Array(orderInformation));
56
+ if (!orderInfo) return null;
57
+ const baseCoin = this.#ctx.config.getCoin(this.#ctx.config.getPool(poolKey).baseCoin);
58
+ const quoteCoin = this.#ctx.config.getCoin(this.#ctx.config.getPool(poolKey).quoteCoin);
59
+ const encodedOrderId = BigInt(orderInfo.order_id);
60
+ const isBid = encodedOrderId >> 127n === 0n;
61
+ const normalizedPrice = Number(encodedOrderId >> 64n & (1n << 63n) - 1n) * baseCoin.scalar / quoteCoin.scalar / FLOAT_SCALAR;
62
+ return {
63
+ ...orderInfo,
64
+ quantity: String((Number(orderInfo.quantity) / baseCoin.scalar).toFixed(9)),
65
+ filled_quantity: String((Number(orderInfo.filled_quantity) / baseCoin.scalar).toFixed(9)),
66
+ order_deep_price: {
67
+ ...orderInfo.order_deep_price,
68
+ deep_per_asset: String((Number(orderInfo.order_deep_price.deep_per_asset) / DEEP_SCALAR).toFixed(9))
69
+ },
70
+ isBid,
71
+ normalized_price: normalizedPrice.toFixed(9)
72
+ };
73
+ } catch {
74
+ return null;
75
+ }
76
+ }
77
+ async getOrders(poolKey, orderIds) {
78
+ const tx = new Transaction();
79
+ tx.add(this.#ctx.deepBook.getOrders(poolKey, orderIds));
80
+ const res = await this.#ctx.client.core.simulateTransaction({
81
+ transaction: tx,
82
+ include: {
83
+ commandResults: true,
84
+ effects: true
85
+ }
86
+ });
87
+ try {
88
+ const orderInformation = res.commandResults[0].returnValues[0].bcs;
89
+ return bcs.vector(Order).parse(new Uint8Array(orderInformation));
90
+ } catch {
91
+ return null;
92
+ }
93
+ }
94
+ async getLevel2Range(poolKey, priceLow, priceHigh, isBid) {
95
+ const tx = new Transaction();
96
+ const pool = this.#ctx.config.getPool(poolKey);
97
+ const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
98
+ const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
99
+ tx.add(this.#ctx.deepBook.getLevel2Range(poolKey, priceLow, priceHigh, isBid));
100
+ const res = await this.#ctx.client.core.simulateTransaction({
101
+ transaction: tx,
102
+ include: {
103
+ commandResults: true,
104
+ effects: true
105
+ }
106
+ });
107
+ const prices = res.commandResults[0].returnValues[0].bcs;
108
+ const parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(prices));
109
+ const quantities = res.commandResults[0].returnValues[1].bcs;
110
+ const parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(quantities));
111
+ return {
112
+ prices: parsed_prices.map((price) => Number((Number(price) / FLOAT_SCALAR / quoteCoin.scalar * baseCoin.scalar).toFixed(9))),
113
+ quantities: parsed_quantities.map((price) => Number((Number(price) / baseCoin.scalar).toFixed(9)))
114
+ };
115
+ }
116
+ async getLevel2TicksFromMid(poolKey, ticks) {
117
+ const tx = new Transaction();
118
+ const pool = this.#ctx.config.getPool(poolKey);
119
+ const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
120
+ const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
121
+ tx.add(this.#ctx.deepBook.getLevel2TicksFromMid(poolKey, ticks));
122
+ const res = await this.#ctx.client.core.simulateTransaction({
123
+ transaction: tx,
124
+ include: {
125
+ commandResults: true,
126
+ effects: true
127
+ }
128
+ });
129
+ const bid_prices = res.commandResults[0].returnValues[0].bcs;
130
+ const bid_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_prices));
131
+ const bid_quantities = res.commandResults[0].returnValues[1].bcs;
132
+ const bid_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_quantities));
133
+ const ask_prices = res.commandResults[0].returnValues[2].bcs;
134
+ const ask_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_prices));
135
+ const ask_quantities = res.commandResults[0].returnValues[3].bcs;
136
+ const ask_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_quantities));
137
+ return {
138
+ bid_prices: bid_parsed_prices.map((price) => Number((Number(price) / FLOAT_SCALAR / quoteCoin.scalar * baseCoin.scalar).toFixed(9))),
139
+ bid_quantities: bid_parsed_quantities.map((quantity) => Number((Number(quantity) / baseCoin.scalar).toFixed(9))),
140
+ ask_prices: ask_parsed_prices.map((price) => Number((Number(price) / FLOAT_SCALAR / quoteCoin.scalar * baseCoin.scalar).toFixed(9))),
141
+ ask_quantities: ask_parsed_quantities.map((quantity) => Number((Number(quantity) / baseCoin.scalar).toFixed(9)))
142
+ };
143
+ }
144
+ async getAccountOrderDetails(poolKey, managerKey) {
145
+ const tx = new Transaction();
146
+ tx.add(this.#ctx.deepBook.getAccountOrderDetails(poolKey, managerKey));
147
+ const res = await this.#ctx.client.core.simulateTransaction({
148
+ transaction: tx,
149
+ include: {
150
+ commandResults: true,
151
+ effects: true
152
+ }
153
+ });
154
+ try {
155
+ const orderInformation = res.commandResults[0].returnValues[0].bcs;
156
+ return bcs.vector(Order).parse(new Uint8Array(orderInformation));
157
+ } catch {
158
+ return [];
159
+ }
160
+ }
161
+ };
162
+
163
+ //#endregion
164
+ export { OrderQueries };
165
+ //# sourceMappingURL=orderQueries.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orderQueries.mjs","names":["#ctx"],"sources":["../../src/queries/orderQueries.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/sui/bcs';\nimport { Transaction } from '@mysten/sui/transactions';\n\nimport { Order } from '../types/bcs.js';\nimport { VecSet } from '../types/bcs.js';\nimport type { Level2Range, Level2TicksFromMid } from '../types/index.js';\nimport { DEEP_SCALAR, FLOAT_SCALAR } from '../utils/config.js';\nimport type { QueryContext } from './context.js';\n\nexport class OrderQueries {\n\t#ctx: QueryContext;\n\n\tconstructor(ctx: QueryContext) {\n\t\tthis.#ctx = ctx;\n\t}\n\n\tasync accountOpenOrders(poolKey: string, managerKey: string): Promise<string[]> {\n\t\tconst tx = new Transaction();\n\n\t\ttx.add(this.#ctx.deepBook.accountOpenOrders(poolKey, managerKey));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst order_ids = res.commandResults![0].returnValues[0].bcs;\n\n\t\treturn VecSet(bcs.u128()).parse(new Uint8Array(order_ids)).contents;\n\t}\n\n\tasync getOrder(poolKey: string, orderId: string): Promise<ReturnType<typeof Order.parse> | null> {\n\t\tconst tx = new Transaction();\n\n\t\ttx.add(this.#ctx.deepBook.getOrder(poolKey, orderId));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\ttry {\n\t\t\tconst orderInformation = res.commandResults![0].returnValues[0].bcs;\n\t\t\treturn Order.parse(new Uint8Array(orderInformation));\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tasync getOrderNormalized(poolKey: string, orderId: string) {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.deepBook.getOrder(poolKey, orderId));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\ttry {\n\t\t\tconst orderInformation = res.commandResults![0].returnValues[0].bcs;\n\t\t\tconst orderInfo = Order.parse(new Uint8Array(orderInformation));\n\n\t\t\tif (!orderInfo) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tconst baseCoin = this.#ctx.config.getCoin(this.#ctx.config.getPool(poolKey).baseCoin);\n\t\t\tconst quoteCoin = this.#ctx.config.getCoin(this.#ctx.config.getPool(poolKey).quoteCoin);\n\n\t\t\tconst encodedOrderId = BigInt(orderInfo.order_id);\n\t\t\tconst isBid = encodedOrderId >> 127n === 0n;\n\t\t\tconst rawPrice = Number((encodedOrderId >> 64n) & ((1n << 63n) - 1n));\n\t\t\tconst normalizedPrice = (rawPrice * baseCoin.scalar) / quoteCoin.scalar / FLOAT_SCALAR;\n\n\t\t\tconst normalizedOrderInfo = {\n\t\t\t\t...orderInfo,\n\t\t\t\tquantity: String((Number(orderInfo.quantity) / baseCoin.scalar).toFixed(9)),\n\t\t\t\tfilled_quantity: String((Number(orderInfo.filled_quantity) / baseCoin.scalar).toFixed(9)),\n\t\t\t\torder_deep_price: {\n\t\t\t\t\t...orderInfo.order_deep_price,\n\t\t\t\t\tdeep_per_asset: String(\n\t\t\t\t\t\t(Number(orderInfo.order_deep_price.deep_per_asset) / DEEP_SCALAR).toFixed(9),\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t\tisBid,\n\t\t\t\tnormalized_price: normalizedPrice.toFixed(9),\n\t\t\t};\n\t\t\treturn normalizedOrderInfo;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tasync getOrders(\n\t\tpoolKey: string,\n\t\torderIds: string[],\n\t): Promise<ReturnType<typeof Order.parse>[] | null> {\n\t\tconst tx = new Transaction();\n\n\t\ttx.add(this.#ctx.deepBook.getOrders(poolKey, orderIds));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\ttry {\n\t\t\tconst orderInformation = res.commandResults![0].returnValues[0].bcs;\n\t\t\treturn bcs.vector(Order).parse(new Uint8Array(orderInformation));\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tasync getLevel2Range(\n\t\tpoolKey: string,\n\t\tpriceLow: number | bigint,\n\t\tpriceHigh: number | bigint,\n\t\tisBid: boolean,\n\t): Promise<Level2Range> {\n\t\tconst tx = new Transaction();\n\t\tconst pool = this.#ctx.config.getPool(poolKey);\n\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\n\t\ttx.add(this.#ctx.deepBook.getLevel2Range(poolKey, priceLow, priceHigh, isBid));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst prices = res.commandResults![0].returnValues[0].bcs;\n\t\tconst parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(prices));\n\t\tconst quantities = res.commandResults![0].returnValues[1].bcs;\n\t\tconst parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(quantities));\n\n\t\treturn {\n\t\t\tprices: parsed_prices.map((price) =>\n\t\t\t\tNumber(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),\n\t\t\t),\n\t\t\tquantities: parsed_quantities.map((price) =>\n\t\t\t\tNumber((Number(price) / baseCoin.scalar).toFixed(9)),\n\t\t\t),\n\t\t};\n\t}\n\n\tasync getLevel2TicksFromMid(poolKey: string, ticks: number): Promise<Level2TicksFromMid> {\n\t\tconst tx = new Transaction();\n\t\tconst pool = this.#ctx.config.getPool(poolKey);\n\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\n\t\ttx.add(this.#ctx.deepBook.getLevel2TicksFromMid(poolKey, ticks));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bid_prices = res.commandResults![0].returnValues[0].bcs;\n\t\tconst bid_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_prices));\n\t\tconst bid_quantities = res.commandResults![0].returnValues[1].bcs;\n\t\tconst bid_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_quantities));\n\n\t\tconst ask_prices = res.commandResults![0].returnValues[2].bcs;\n\t\tconst ask_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_prices));\n\t\tconst ask_quantities = res.commandResults![0].returnValues[3].bcs;\n\t\tconst ask_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_quantities));\n\n\t\treturn {\n\t\t\tbid_prices: bid_parsed_prices.map((price) =>\n\t\t\t\tNumber(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),\n\t\t\t),\n\t\t\tbid_quantities: bid_parsed_quantities.map((quantity) =>\n\t\t\t\tNumber((Number(quantity) / baseCoin.scalar).toFixed(9)),\n\t\t\t),\n\t\t\task_prices: ask_parsed_prices.map((price) =>\n\t\t\t\tNumber(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),\n\t\t\t),\n\t\t\task_quantities: ask_parsed_quantities.map((quantity) =>\n\t\t\t\tNumber((Number(quantity) / baseCoin.scalar).toFixed(9)),\n\t\t\t),\n\t\t};\n\t}\n\n\tasync getAccountOrderDetails(\n\t\tpoolKey: string,\n\t\tmanagerKey: string,\n\t): Promise<ReturnType<typeof Order.parse>[] | []> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.deepBook.getAccountOrderDetails(poolKey, managerKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\ttry {\n\t\t\tconst orderInformation = res.commandResults![0].returnValues[0].bcs;\n\t\t\treturn bcs.vector(Order).parse(new Uint8Array(orderInformation));\n\t\t} catch {\n\t\t\treturn [];\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;AAYA,IAAa,eAAb,MAA0B;CACzB;CAEA,YAAY,KAAmB;AAC9B,QAAKA,MAAO;;CAGb,MAAM,kBAAkB,SAAiB,YAAuC;EAC/E,MAAM,KAAK,IAAI,aAAa;AAE5B,KAAG,IAAI,MAAKA,IAAK,SAAS,kBAAkB,SAAS,WAAW,CAAC;EAMjE,MAAM,aALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEoB,eAAgB,GAAG,aAAa,GAAG;AAEzD,SAAO,OAAO,IAAI,MAAM,CAAC,CAAC,MAAM,IAAI,WAAW,UAAU,CAAC,CAAC;;CAG5D,MAAM,SAAS,SAAiB,SAAiE;EAChG,MAAM,KAAK,IAAI,aAAa;AAE5B,KAAG,IAAI,MAAKA,IAAK,SAAS,SAAS,SAAS,QAAQ,CAAC;EACrD,MAAM,MAAM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC;AAEF,MAAI;GACH,MAAM,mBAAmB,IAAI,eAAgB,GAAG,aAAa,GAAG;AAChE,UAAO,MAAM,MAAM,IAAI,WAAW,iBAAiB,CAAC;UAC7C;AACP,UAAO;;;CAIT,MAAM,mBAAmB,SAAiB,SAAiB;E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