@mysten/deepbook-v3 1.1.5 → 1.2.1

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 +23 -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 +12 -12
  35. package/dist/transactions/balanceManager.d.mts.map +1 -1
  36. package/dist/transactions/balanceManager.mjs +5 -4
  37. package/dist/transactions/balanceManager.mjs.map +1 -1
  38. package/dist/transactions/deepbook.d.mts +24 -24
  39. package/dist/transactions/deepbook.d.mts.map +1 -1
  40. package/dist/transactions/deepbook.mjs +48 -47
  41. package/dist/transactions/deepbook.mjs.map +1 -1
  42. package/dist/transactions/deepbookAdmin.d.mts +2 -2
  43. package/dist/transactions/deepbookAdmin.d.mts.map +1 -1
  44. package/dist/transactions/deepbookAdmin.mjs +10 -9
  45. package/dist/transactions/deepbookAdmin.mjs.map +1 -1
  46. package/dist/transactions/flashLoans.d.mts.map +1 -1
  47. package/dist/transactions/flashLoans.mjs +6 -4
  48. package/dist/transactions/flashLoans.mjs.map +1 -1
  49. package/dist/transactions/governance.d.mts.map +1 -1
  50. package/dist/transactions/governance.mjs +5 -4
  51. package/dist/transactions/governance.mjs.map +1 -1
  52. package/dist/transactions/marginAdmin.d.mts +7 -7
  53. package/dist/transactions/marginAdmin.d.mts.map +1 -1
  54. package/dist/transactions/marginAdmin.mjs +8 -7
  55. package/dist/transactions/marginAdmin.mjs.map +1 -1
  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 +17 -12
  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/marginTPSL.d.mts +1 -1
  71. package/dist/transactions/marginTPSL.d.mts.map +1 -1
  72. package/dist/transactions/marginTPSL.mjs +6 -8
  73. package/dist/transactions/marginTPSL.mjs.map +1 -1
  74. package/dist/transactions/poolProxy.d.mts.map +1 -1
  75. package/dist/transactions/poolProxy.mjs +13 -13
  76. package/dist/transactions/poolProxy.mjs.map +1 -1
  77. package/dist/types/index.d.mts +189 -52
  78. package/dist/types/index.d.mts.map +1 -1
  79. package/dist/types/index.mjs.map +1 -1
  80. package/dist/utils/constants.d.mts +4 -0
  81. package/dist/utils/constants.d.mts.map +1 -1
  82. package/dist/utils/constants.mjs +12 -5
  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 +6 -5
  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 +13 -12
  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 +12 -5
  116. package/src/utils/conversion.ts +33 -0
@@ -0,0 +1,88 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { bcs } from '@mysten/sui/bcs';
5
+ import { Transaction } from '@mysten/sui/transactions';
6
+
7
+ import type { QueryContext } from './context.js';
8
+
9
+ export class TPSLQueries {
10
+ #ctx: QueryContext;
11
+
12
+ constructor(ctx: QueryContext) {
13
+ this.#ctx = ctx;
14
+ }
15
+
16
+ async getConditionalOrderIds(marginManagerKey: string): Promise<string[]> {
17
+ const manager = this.#ctx.config.getMarginManager(marginManagerKey);
18
+ const tx = new Transaction();
19
+ tx.add(this.#ctx.marginTPSL.conditionalOrderIds(manager.poolKey, manager.address));
20
+
21
+ const res = await this.#ctx.client.core.simulateTransaction({
22
+ transaction: tx,
23
+ include: { commandResults: true, effects: true },
24
+ });
25
+
26
+ if (res.FailedTransaction) {
27
+ throw new Error(
28
+ `Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
29
+ );
30
+ }
31
+
32
+ if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
33
+ throw new Error(`Failed to get conditional order IDs: Unknown error`);
34
+ }
35
+
36
+ const bytes = res.commandResults[0].returnValues[0].bcs;
37
+ const orderIds = bcs.vector(bcs.u64()).parse(bytes);
38
+ return orderIds.map((id) => id.toString());
39
+ }
40
+
41
+ async getLowestTriggerAbovePrice(marginManagerKey: string): Promise<bigint> {
42
+ const manager = this.#ctx.config.getMarginManager(marginManagerKey);
43
+ const tx = new Transaction();
44
+ tx.add(this.#ctx.marginTPSL.lowestTriggerAbovePrice(manager.poolKey, manager.address));
45
+
46
+ const res = await this.#ctx.client.core.simulateTransaction({
47
+ transaction: tx,
48
+ include: { commandResults: true, effects: true },
49
+ });
50
+
51
+ if (res.FailedTransaction) {
52
+ throw new Error(
53
+ `Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
54
+ );
55
+ }
56
+
57
+ if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
58
+ throw new Error(`Failed to get lowest trigger above price: Unknown error`);
59
+ }
60
+
61
+ const bytes = res.commandResults[0].returnValues[0].bcs;
62
+ return BigInt(bcs.U64.parse(bytes));
63
+ }
64
+
65
+ async getHighestTriggerBelowPrice(marginManagerKey: string): Promise<bigint> {
66
+ const manager = this.#ctx.config.getMarginManager(marginManagerKey);
67
+ const tx = new Transaction();
68
+ tx.add(this.#ctx.marginTPSL.highestTriggerBelowPrice(manager.poolKey, manager.address));
69
+
70
+ const res = await this.#ctx.client.core.simulateTransaction({
71
+ transaction: tx,
72
+ include: { commandResults: true, effects: true },
73
+ });
74
+
75
+ if (res.FailedTransaction) {
76
+ throw new Error(
77
+ `Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
78
+ );
79
+ }
80
+
81
+ if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
82
+ throw new Error(`Failed to get highest trigger below price: Unknown error`);
83
+ }
84
+
85
+ const bytes = res.commandResults[0].returnValues[0].bcs;
86
+ return BigInt(bcs.U64.parse(bytes));
87
+ }
88
+ }
@@ -4,6 +4,7 @@ import { coinWithBalance } from '@mysten/sui/transactions';
4
4
  import type { Transaction, TransactionArgument } from '@mysten/sui/transactions';
5
5
 
6
6
  import type { DeepBookConfig } from '../utils/config.js';
7
+ import { convertQuantity } from '../utils/conversion.js';
7
8
 
8
9
  /**
9
10
  * BalanceManagerContract class for managing BalanceManager operations.
@@ -70,7 +71,7 @@ export class BalanceManagerContract {
70
71
  tx.setSenderIfNotSet(this.#config.address);
71
72
  const managerId = this.#config.getBalanceManager(managerKey).address;
72
73
  const coin = this.#config.getCoin(coinKey);
73
- const depositInput = Math.round(amountToDeposit * coin.scalar);
74
+ const depositInput = convertQuantity(amountToDeposit, coin.scalar);
74
75
  const deposit = coinWithBalance({
75
76
  type: coin.type,
76
77
  balance: depositInput,
@@ -96,7 +97,7 @@ export class BalanceManagerContract {
96
97
  (tx: Transaction) => {
97
98
  const managerId = this.#config.getBalanceManager(managerKey).address;
98
99
  const coin = this.#config.getCoin(coinKey);
99
- const withdrawInput = Math.round(amountToWithdraw * coin.scalar);
100
+ const withdrawInput = convertQuantity(amountToWithdraw, coin.scalar);
100
101
  const coinObject = tx.moveCall({
101
102
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::withdraw`,
102
103
  arguments: [tx.object(managerId), tx.pure.u64(withdrawInput)],
@@ -240,7 +241,7 @@ export class BalanceManagerContract {
240
241
  }
241
242
  const depositCapId = manager.depositCap;
242
243
  const coin = this.#config.getCoin(coinKey);
243
- const depositInput = Math.round(amountToDeposit * coin.scalar);
244
+ const depositInput = convertQuantity(amountToDeposit, coin.scalar);
244
245
  const deposit = coinWithBalance({
245
246
  type: coin.type,
246
247
  balance: depositInput,
@@ -269,7 +270,7 @@ export class BalanceManagerContract {
269
270
  }
270
271
  const withdrawCapId = manager.withdrawCap;
271
272
  const coin = this.#config.getCoin(coinKey);
272
- const withdrawAmount = Math.round(amountToWithdraw * coin.scalar);
273
+ const withdrawAmount = convertQuantity(amountToWithdraw, coin.scalar);
273
274
  return tx.moveCall({
274
275
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::withdraw_with_cap`,
275
276
  arguments: [tx.object(managerId), tx.object(withdrawCapId), tx.pure.u64(withdrawAmount)],
@@ -21,6 +21,7 @@ import {
21
21
  MAX_TIMESTAMP,
22
22
  POOL_CREATION_FEE_DEEP,
23
23
  } from '../utils/config.js';
24
+ import { convertQuantity, convertPrice, convertRate } from '../utils/conversion.js';
24
25
 
25
26
  /**
26
27
  * DeepBookContract class for managing DeepBook operations.
@@ -59,8 +60,8 @@ export class DeepBookContract {
59
60
  const balanceManager = this.#config.getBalanceManager(balanceManagerKey);
60
61
  const baseCoin = this.#config.getCoin(pool.baseCoin);
61
62
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
62
- const inputPrice = Math.round((price * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar);
63
- const inputQuantity = Math.round(quantity * baseCoin.scalar);
63
+ const inputPrice = convertPrice(price, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar);
64
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
64
65
 
65
66
  const tradeProof = tx.add(this.#config.balanceManager.generateProof(balanceManagerKey));
66
67
 
@@ -106,7 +107,7 @@ export class DeepBookContract {
106
107
  const baseCoin = this.#config.getCoin(pool.baseCoin);
107
108
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
108
109
  const tradeProof = tx.add(this.#config.balanceManager.generateProof(balanceManagerKey));
109
- const inputQuantity = Math.round(quantity * baseCoin.scalar);
110
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
110
111
 
111
112
  tx.moveCall({
112
113
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::place_market_order`,
@@ -141,7 +142,7 @@ export class DeepBookContract {
141
142
  const baseCoin = this.#config.getCoin(pool.baseCoin);
142
143
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
143
144
  const tradeProof = tx.add(this.#config.balanceManager.generateProof(balanceManagerKey));
144
- const inputQuantity = Math.round(newQuantity * baseCoin.scalar);
145
+ const inputQuantity = convertQuantity(newQuantity, baseCoin.scalar);
145
146
 
146
147
  tx.moveCall({
147
148
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::modify_order`,
@@ -359,7 +360,7 @@ export class DeepBookContract {
359
360
  const pool = this.#config.getPool(poolKey);
360
361
  const baseCoin = this.#config.getCoin(pool.baseCoin);
361
362
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
362
- const adjustedNumber = Math.round(multiplier * FLOAT_SCALAR);
363
+ const adjustedNumber = convertRate(multiplier, FLOAT_SCALAR);
363
364
 
364
365
  tx.moveCall({
365
366
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::mint_referral`,
@@ -380,7 +381,7 @@ export class DeepBookContract {
380
381
  const pool = this.#config.getPool(poolKey);
381
382
  const baseCoin = this.#config.getCoin(pool.baseCoin);
382
383
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
383
- const adjustedNumber = Math.round(multiplier * FLOAT_SCALAR);
384
+ const adjustedNumber = convertRate(multiplier, FLOAT_SCALAR);
384
385
 
385
386
  tx.moveCall({
386
387
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::update_pool_referral_multiplier`,
@@ -516,7 +517,7 @@ export class DeepBookContract {
516
517
  * @param {number} baseQuantity Base quantity to convert
517
518
  * @returns A function that takes a Transaction object
518
519
  */
519
- getQuoteQuantityOut = (poolKey: string, baseQuantity: number) => (tx: Transaction) => {
520
+ getQuoteQuantityOut = (poolKey: string, baseQuantity: number | bigint) => (tx: Transaction) => {
520
521
  const pool = this.#config.getPool(poolKey);
521
522
  const baseCoin = this.#config.getCoin(pool.baseCoin);
522
523
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
@@ -525,7 +526,7 @@ export class DeepBookContract {
525
526
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_quote_quantity_out`,
526
527
  arguments: [
527
528
  tx.object(pool.address),
528
- tx.pure.u64(Math.round(baseQuantity * baseCoin.scalar)),
529
+ tx.pure.u64(convertQuantity(baseQuantity, baseCoin.scalar)),
529
530
  tx.object.clock(),
530
531
  ],
531
532
  typeArguments: [baseCoin.type, quoteCoin.type],
@@ -538,7 +539,7 @@ export class DeepBookContract {
538
539
  * @param {number} quoteQuantity Quote quantity to convert
539
540
  * @returns A function that takes a Transaction object
540
541
  */
541
- getBaseQuantityOut = (poolKey: string, quoteQuantity: number) => (tx: Transaction) => {
542
+ getBaseQuantityOut = (poolKey: string, quoteQuantity: number | bigint) => (tx: Transaction) => {
542
543
  const pool = this.#config.getPool(poolKey);
543
544
  const baseCoin = this.#config.getCoin(pool.baseCoin);
544
545
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
@@ -548,7 +549,7 @@ export class DeepBookContract {
548
549
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_base_quantity_out`,
549
550
  arguments: [
550
551
  tx.object(pool.address),
551
- tx.pure.u64(Math.round(quoteQuantity * quoteScalar)),
552
+ tx.pure.u64(convertQuantity(quoteQuantity, quoteScalar)),
552
553
  tx.object.clock(),
553
554
  ],
554
555
  typeArguments: [baseCoin.type, quoteCoin.type],
@@ -563,7 +564,8 @@ export class DeepBookContract {
563
564
  * @returns A function that takes a Transaction object
564
565
  */
565
566
  getQuantityOut =
566
- (poolKey: string, baseQuantity: number, quoteQuantity: number) => (tx: Transaction) => {
567
+ (poolKey: string, baseQuantity: number | bigint, quoteQuantity: number | bigint) =>
568
+ (tx: Transaction) => {
567
569
  const pool = this.#config.getPool(poolKey);
568
570
  const baseCoin = this.#config.getCoin(pool.baseCoin);
569
571
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
@@ -573,8 +575,8 @@ export class DeepBookContract {
573
575
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_quantity_out`,
574
576
  arguments: [
575
577
  tx.object(pool.address),
576
- tx.pure.u64(Math.round(baseQuantity * baseCoin.scalar)),
577
- tx.pure.u64(Math.round(quoteQuantity * quoteScalar)),
578
+ tx.pure.u64(convertQuantity(baseQuantity, baseCoin.scalar)),
579
+ tx.pure.u64(convertQuantity(quoteQuantity, quoteScalar)),
578
580
  tx.object.clock(),
579
581
  ],
580
582
  typeArguments: [baseCoin.type, quoteCoin.type],
@@ -609,7 +611,8 @@ export class DeepBookContract {
609
611
  * @returns A function that takes a Transaction object
610
612
  */
611
613
  getLevel2Range =
612
- (poolKey: string, priceLow: number, priceHigh: number, isBid: boolean) => (tx: Transaction) => {
614
+ (poolKey: string, priceLow: number | bigint, priceHigh: number | bigint, isBid: boolean) =>
615
+ (tx: Transaction) => {
613
616
  const pool = this.#config.getPool(poolKey);
614
617
  const baseCoin = this.#config.getCoin(pool.baseCoin);
615
618
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
@@ -618,8 +621,8 @@ export class DeepBookContract {
618
621
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_level2_range`,
619
622
  arguments: [
620
623
  tx.object(pool.address),
621
- tx.pure.u64(Math.round((priceLow * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar)),
622
- tx.pure.u64(Math.round((priceHigh * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar)),
624
+ tx.pure.u64(convertPrice(priceLow, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar)),
625
+ tx.pure.u64(convertPrice(priceHigh, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar)),
623
626
  tx.pure.bool(isBid),
624
627
  tx.object.clock(),
625
628
  ],
@@ -697,13 +700,16 @@ export class DeepBookContract {
697
700
 
698
701
  const baseCoinInput =
699
702
  params.baseCoin ??
700
- coinWithBalance({ type: baseCoin.type, balance: Math.round(baseAmount * baseCoin.scalar) });
703
+ coinWithBalance({
704
+ type: baseCoin.type,
705
+ balance: convertQuantity(baseAmount, baseCoin.scalar),
706
+ });
701
707
 
702
708
  const deepCoin =
703
709
  params.deepCoin ??
704
- coinWithBalance({ type: deepCoinType, balance: Math.round(deepAmount * DEEP_SCALAR) });
710
+ coinWithBalance({ type: deepCoinType, balance: convertQuantity(deepAmount, DEEP_SCALAR) });
705
711
 
706
- const minQuoteInput = Math.round(minQuote * quoteCoin.scalar);
712
+ const minQuoteInput = convertQuantity(minQuote, quoteCoin.scalar);
707
713
 
708
714
  const [baseCoinResult, quoteCoinResult, deepCoinResult] = tx.moveCall({
709
715
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::swap_exact_base_for_quote`,
@@ -743,14 +749,14 @@ export class DeepBookContract {
743
749
  params.quoteCoin ??
744
750
  coinWithBalance({
745
751
  type: quoteCoin.type,
746
- balance: Math.round(quoteAmount * quoteCoin.scalar),
752
+ balance: convertQuantity(quoteAmount, quoteCoin.scalar),
747
753
  });
748
754
 
749
755
  const deepCoin =
750
756
  params.deepCoin ??
751
- coinWithBalance({ type: deepCoinType, balance: Math.round(deepAmount * DEEP_SCALAR) });
757
+ coinWithBalance({ type: deepCoinType, balance: convertQuantity(deepAmount, DEEP_SCALAR) });
752
758
 
753
- const minBaseInput = Math.round(minBase * baseCoin.scalar);
759
+ const minBaseInput = convertQuantity(minBase, baseCoin.scalar);
754
760
 
755
761
  const [baseCoinResult, quoteCoinResult, deepCoinResult] = tx.moveCall({
756
762
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::swap_exact_quote_for_base`,
@@ -788,7 +794,7 @@ export class DeepBookContract {
788
794
  ? (baseCoin ??
789
795
  coinWithBalance({
790
796
  type: baseCoinType.type,
791
- balance: Math.round(amount * baseCoinType.scalar),
797
+ balance: convertQuantity(amount, baseCoinType.scalar),
792
798
  }))
793
799
  : coinWithBalance({ type: baseCoinType.type, balance: 0 });
794
800
 
@@ -797,15 +803,16 @@ export class DeepBookContract {
797
803
  : (quoteCoin ??
798
804
  coinWithBalance({
799
805
  type: quoteCoinType.type,
800
- balance: Math.round(amount * quoteCoinType.scalar),
806
+ balance: convertQuantity(amount, quoteCoinType.scalar),
801
807
  }));
802
808
 
803
809
  const deepCoinInput =
804
810
  deepCoin ??
805
- coinWithBalance({ type: deepCoinType, balance: Math.round(deepAmount * DEEP_SCALAR) });
811
+ coinWithBalance({ type: deepCoinType, balance: convertQuantity(deepAmount, DEEP_SCALAR) });
806
812
 
807
- const minOutInput = Math.round(
808
- minOut * (isBaseToCoin ? quoteCoinType.scalar : baseCoinType.scalar),
813
+ const minOutInput = convertQuantity(
814
+ minOut,
815
+ isBaseToCoin ? quoteCoinType.scalar : baseCoinType.scalar,
809
816
  );
810
817
 
811
818
  const [baseCoinResult, quoteCoinResult, deepCoinResult] = tx.moveCall({
@@ -851,9 +858,9 @@ export class DeepBookContract {
851
858
  baseCoin ??
852
859
  coinWithBalance({
853
860
  type: baseCoinType.type,
854
- balance: Math.round(baseAmount * baseCoinType.scalar),
861
+ balance: convertQuantity(baseAmount, baseCoinType.scalar),
855
862
  });
856
- const minQuoteInput = Math.round(minQuote * quoteCoinType.scalar);
863
+ const minQuoteInput = convertQuantity(minQuote, quoteCoinType.scalar);
857
864
 
858
865
  const [baseCoinResult, quoteCoinResult] = tx.moveCall({
859
866
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::swap_exact_base_for_quote_with_manager`,
@@ -900,9 +907,9 @@ export class DeepBookContract {
900
907
  quoteCoin ??
901
908
  coinWithBalance({
902
909
  type: quoteCoinType.type,
903
- balance: Math.round(quoteAmount * quoteCoinType.scalar),
910
+ balance: convertQuantity(quoteAmount, quoteCoinType.scalar),
904
911
  });
905
- const minBaseInput = Math.round(minBase * baseCoinType.scalar);
912
+ const minBaseInput = convertQuantity(minBase, baseCoinType.scalar);
906
913
 
907
914
  const [baseCoinResult, quoteCoinResult] = tx.moveCall({
908
915
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::swap_exact_quote_for_base_with_manager`,
@@ -952,7 +959,7 @@ export class DeepBookContract {
952
959
  ? (baseCoin ??
953
960
  coinWithBalance({
954
961
  type: baseCoinType.type,
955
- balance: Math.round(amount * baseCoinType.scalar),
962
+ balance: convertQuantity(amount, baseCoinType.scalar),
956
963
  }))
957
964
  : coinWithBalance({ type: baseCoinType.type, balance: 0 });
958
965
 
@@ -961,11 +968,12 @@ export class DeepBookContract {
961
968
  : (quoteCoin ??
962
969
  coinWithBalance({
963
970
  type: quoteCoinType.type,
964
- balance: Math.round(amount * quoteCoinType.scalar),
971
+ balance: convertQuantity(amount, quoteCoinType.scalar),
965
972
  }));
966
973
 
967
- const minOutInput = Math.round(
968
- minOut * (isBaseToCoin ? quoteCoinType.scalar : baseCoinType.scalar),
974
+ const minOutInput = convertQuantity(
975
+ minOut,
976
+ isBaseToCoin ? quoteCoinType.scalar : baseCoinType.scalar,
969
977
  );
970
978
 
971
979
  const [baseCoinResult, quoteCoinResult] = tx.moveCall({
@@ -1002,9 +1010,9 @@ export class DeepBookContract {
1002
1010
  const baseScalar = baseCoin.scalar;
1003
1011
  const quoteScalar = quoteCoin.scalar;
1004
1012
 
1005
- const adjustedTickSize = Math.round((tickSize * FLOAT_SCALAR * quoteScalar) / baseScalar);
1006
- const adjustedLotSize = Math.round(lotSize * baseScalar);
1007
- const adjustedMinSize = Math.round(minSize * baseScalar);
1013
+ const adjustedTickSize = convertPrice(tickSize, FLOAT_SCALAR, quoteScalar, baseScalar);
1014
+ const adjustedLotSize = convertQuantity(lotSize, baseScalar);
1015
+ const adjustedMinSize = convertQuantity(minSize, baseScalar);
1008
1016
 
1009
1017
  const deepCoinInput =
1010
1018
  deepCoin ??
@@ -1203,21 +1211,22 @@ export class DeepBookContract {
1203
1211
  * @param {number} baseQuantity Base quantity to convert
1204
1212
  * @returns A function that takes a Transaction object
1205
1213
  */
1206
- getQuoteQuantityOutInputFee = (poolKey: string, baseQuantity: number) => (tx: Transaction) => {
1207
- const pool = this.#config.getPool(poolKey);
1208
- const baseCoin = this.#config.getCoin(pool.baseCoin);
1209
- const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1214
+ getQuoteQuantityOutInputFee =
1215
+ (poolKey: string, baseQuantity: number | bigint) => (tx: Transaction) => {
1216
+ const pool = this.#config.getPool(poolKey);
1217
+ const baseCoin = this.#config.getCoin(pool.baseCoin);
1218
+ const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1210
1219
 
1211
- return tx.moveCall({
1212
- target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_quote_quantity_out_input_fee`,
1213
- arguments: [
1214
- tx.object(pool.address),
1215
- tx.pure.u64(Math.round(baseQuantity * baseCoin.scalar)),
1216
- tx.object.clock(),
1217
- ],
1218
- typeArguments: [baseCoin.type, quoteCoin.type],
1219
- });
1220
- };
1220
+ return tx.moveCall({
1221
+ target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_quote_quantity_out_input_fee`,
1222
+ arguments: [
1223
+ tx.object(pool.address),
1224
+ tx.pure.u64(convertQuantity(baseQuantity, baseCoin.scalar)),
1225
+ tx.object.clock(),
1226
+ ],
1227
+ typeArguments: [baseCoin.type, quoteCoin.type],
1228
+ });
1229
+ };
1221
1230
 
1222
1231
  /**
1223
1232
  * @description Get the base quantity out for a given quote quantity using input token as fee
@@ -1225,21 +1234,22 @@ export class DeepBookContract {
1225
1234
  * @param {number} quoteQuantity Quote quantity to convert
1226
1235
  * @returns A function that takes a Transaction object
1227
1236
  */
1228
- getBaseQuantityOutInputFee = (poolKey: string, quoteQuantity: number) => (tx: Transaction) => {
1229
- const pool = this.#config.getPool(poolKey);
1230
- const baseCoin = this.#config.getCoin(pool.baseCoin);
1231
- const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1237
+ getBaseQuantityOutInputFee =
1238
+ (poolKey: string, quoteQuantity: number | bigint) => (tx: Transaction) => {
1239
+ const pool = this.#config.getPool(poolKey);
1240
+ const baseCoin = this.#config.getCoin(pool.baseCoin);
1241
+ const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1232
1242
 
1233
- return tx.moveCall({
1234
- target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_base_quantity_out_input_fee`,
1235
- arguments: [
1236
- tx.object(pool.address),
1237
- tx.pure.u64(Math.round(quoteQuantity * quoteCoin.scalar)),
1238
- tx.object.clock(),
1239
- ],
1240
- typeArguments: [baseCoin.type, quoteCoin.type],
1241
- });
1242
- };
1243
+ return tx.moveCall({
1244
+ target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_base_quantity_out_input_fee`,
1245
+ arguments: [
1246
+ tx.object(pool.address),
1247
+ tx.pure.u64(convertQuantity(quoteQuantity, quoteCoin.scalar)),
1248
+ tx.object.clock(),
1249
+ ],
1250
+ typeArguments: [baseCoin.type, quoteCoin.type],
1251
+ });
1252
+ };
1243
1253
 
1244
1254
  /**
1245
1255
  * @description Get the quantity out for a given base or quote quantity using input token as fee
@@ -1249,7 +1259,8 @@ export class DeepBookContract {
1249
1259
  * @returns A function that takes a Transaction object
1250
1260
  */
1251
1261
  getQuantityOutInputFee =
1252
- (poolKey: string, baseQuantity: number, quoteQuantity: number) => (tx: Transaction) => {
1262
+ (poolKey: string, baseQuantity: number | bigint, quoteQuantity: number | bigint) =>
1263
+ (tx: Transaction) => {
1253
1264
  const pool = this.#config.getPool(poolKey);
1254
1265
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1255
1266
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
@@ -1258,8 +1269,8 @@ export class DeepBookContract {
1258
1269
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_quantity_out_input_fee`,
1259
1270
  arguments: [
1260
1271
  tx.object(pool.address),
1261
- tx.pure.u64(Math.round(baseQuantity * baseCoin.scalar)),
1262
- tx.pure.u64(Math.round(quoteQuantity * quoteCoin.scalar)),
1272
+ tx.pure.u64(convertQuantity(baseQuantity, baseCoin.scalar)),
1273
+ tx.pure.u64(convertQuantity(quoteQuantity, quoteCoin.scalar)),
1263
1274
  tx.object.clock(),
1264
1275
  ],
1265
1276
  typeArguments: [baseCoin.type, quoteCoin.type],
@@ -1274,7 +1285,8 @@ export class DeepBookContract {
1274
1285
  * @returns A function that takes a Transaction object
1275
1286
  */
1276
1287
  getBaseQuantityIn =
1277
- (poolKey: string, targetQuoteQuantity: number, payWithDeep: boolean) => (tx: Transaction) => {
1288
+ (poolKey: string, targetQuoteQuantity: number | bigint, payWithDeep: boolean) =>
1289
+ (tx: Transaction) => {
1278
1290
  const pool = this.#config.getPool(poolKey);
1279
1291
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1280
1292
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
@@ -1283,7 +1295,7 @@ export class DeepBookContract {
1283
1295
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_base_quantity_in`,
1284
1296
  arguments: [
1285
1297
  tx.object(pool.address),
1286
- tx.pure.u64(Math.round(targetQuoteQuantity * quoteCoin.scalar)),
1298
+ tx.pure.u64(convertQuantity(targetQuoteQuantity, quoteCoin.scalar)),
1287
1299
  tx.pure.bool(payWithDeep),
1288
1300
  tx.object.clock(),
1289
1301
  ],
@@ -1299,7 +1311,8 @@ export class DeepBookContract {
1299
1311
  * @returns A function that takes a Transaction object
1300
1312
  */
1301
1313
  getQuoteQuantityIn =
1302
- (poolKey: string, targetBaseQuantity: number, payWithDeep: boolean) => (tx: Transaction) => {
1314
+ (poolKey: string, targetBaseQuantity: number | bigint, payWithDeep: boolean) =>
1315
+ (tx: Transaction) => {
1303
1316
  const pool = this.#config.getPool(poolKey);
1304
1317
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1305
1318
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
@@ -1308,7 +1321,7 @@ export class DeepBookContract {
1308
1321
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_quote_quantity_in`,
1309
1322
  arguments: [
1310
1323
  tx.object(pool.address),
1311
- tx.pure.u64(Math.round(targetBaseQuantity * baseCoin.scalar)),
1324
+ tx.pure.u64(convertQuantity(targetBaseQuantity, baseCoin.scalar)),
1312
1325
  tx.pure.bool(payWithDeep),
1313
1326
  tx.object.clock(),
1314
1327
  ],
@@ -1343,12 +1356,13 @@ export class DeepBookContract {
1343
1356
  * @returns A function that takes a Transaction object
1344
1357
  */
1345
1358
  getOrderDeepRequired =
1346
- (poolKey: string, baseQuantity: number, price: number) => (tx: Transaction) => {
1359
+ (poolKey: string, baseQuantity: number | bigint, price: number | bigint) =>
1360
+ (tx: Transaction) => {
1347
1361
  const pool = this.#config.getPool(poolKey);
1348
1362
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1349
1363
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1350
- const inputPrice = Math.round((price * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar);
1351
- const inputQuantity = Math.round(baseQuantity * baseCoin.scalar);
1364
+ const inputPrice = convertPrice(price, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar);
1365
+ const inputQuantity = convertQuantity(baseQuantity, baseCoin.scalar);
1352
1366
 
1353
1367
  return tx.moveCall({
1354
1368
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::get_order_deep_required`,
@@ -1440,8 +1454,8 @@ export class DeepBookContract {
1440
1454
  const manager = this.#config.getBalanceManager(balanceManagerKey);
1441
1455
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1442
1456
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1443
- const inputPrice = Math.round((price * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar);
1444
- const inputQuantity = Math.round(quantity * baseCoin.scalar);
1457
+ const inputPrice = convertPrice(price, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar);
1458
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
1445
1459
 
1446
1460
  return tx.moveCall({
1447
1461
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::can_place_limit_order`,
@@ -1471,7 +1485,7 @@ export class DeepBookContract {
1471
1485
  const manager = this.#config.getBalanceManager(balanceManagerKey);
1472
1486
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1473
1487
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1474
- const inputQuantity = Math.round(quantity * baseCoin.scalar);
1488
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
1475
1489
 
1476
1490
  return tx.moveCall({
1477
1491
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::can_place_market_order`,
@@ -1493,11 +1507,11 @@ export class DeepBookContract {
1493
1507
  * @param {number} quantity Quantity
1494
1508
  * @returns A function that takes a Transaction object
1495
1509
  */
1496
- checkMarketOrderParams = (poolKey: string, quantity: number) => (tx: Transaction) => {
1510
+ checkMarketOrderParams = (poolKey: string, quantity: number | bigint) => (tx: Transaction) => {
1497
1511
  const pool = this.#config.getPool(poolKey);
1498
1512
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1499
1513
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1500
- const inputQuantity = Math.round(quantity * baseCoin.scalar);
1514
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
1501
1515
 
1502
1516
  return tx.moveCall({
1503
1517
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::check_market_order_params`,
@@ -1515,13 +1529,13 @@ export class DeepBookContract {
1515
1529
  * @returns A function that takes a Transaction object
1516
1530
  */
1517
1531
  checkLimitOrderParams =
1518
- (poolKey: string, price: number, quantity: number, expireTimestamp: number) =>
1532
+ (poolKey: string, price: number | bigint, quantity: number | bigint, expireTimestamp: number) =>
1519
1533
  (tx: Transaction) => {
1520
1534
  const pool = this.#config.getPool(poolKey);
1521
1535
  const baseCoin = this.#config.getCoin(pool.baseCoin);
1522
1536
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
1523
- const inputPrice = Math.round((price * FLOAT_SCALAR * quoteCoin.scalar) / baseCoin.scalar);
1524
- const inputQuantity = Math.round(quantity * baseCoin.scalar);
1537
+ const inputPrice = convertPrice(price, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar);
1538
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
1525
1539
 
1526
1540
  return tx.moveCall({
1527
1541
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::check_limit_order_params`,
@@ -6,6 +6,7 @@ import type { Transaction } from '@mysten/sui/transactions';
6
6
  import type { CreatePoolAdminParams, SetEwmaParams } from '../types/index.js';
7
7
  import type { DeepBookConfig } from '../utils/config.js';
8
8
  import { FLOAT_SCALAR } from '../utils/config.js';
9
+ import { convertQuantity, convertPrice, convertRate } from '../utils/conversion.js';
9
10
 
10
11
  /**
11
12
  * DeepBookAdminContract class for managing admin actions.
@@ -47,9 +48,9 @@ export class DeepBookAdminContract {
47
48
  const baseScalar = baseCoin.scalar;
48
49
  const quoteScalar = quoteCoin.scalar;
49
50
 
50
- const adjustedTickSize = Math.round((tickSize * FLOAT_SCALAR * quoteScalar) / baseScalar);
51
- const adjustedLotSize = Math.round(lotSize * baseScalar);
52
- const adjustedMinSize = Math.round(minSize * baseScalar);
51
+ const adjustedTickSize = convertPrice(tickSize, FLOAT_SCALAR, quoteScalar, baseScalar);
52
+ const adjustedLotSize = convertQuantity(lotSize, baseScalar);
53
+ const adjustedMinSize = convertQuantity(minSize, baseScalar);
53
54
 
54
55
  tx.moveCall({
55
56
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::create_pool_admin`,
@@ -197,7 +198,7 @@ export class DeepBookAdminContract {
197
198
  const baseScalar = baseCoin.scalar;
198
199
  const quoteScalar = quoteCoin.scalar;
199
200
 
200
- const adjustedTickSize = Math.round((newTickSize * FLOAT_SCALAR * quoteScalar) / baseScalar);
201
+ const adjustedTickSize = convertPrice(newTickSize, FLOAT_SCALAR, quoteScalar, baseScalar);
201
202
 
202
203
  tx.moveCall({
203
204
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::adjust_tick_size_admin`,
@@ -227,8 +228,8 @@ export class DeepBookAdminContract {
227
228
 
228
229
  const baseScalar = baseCoin.scalar;
229
230
 
230
- const adjustedLotSize = Math.round(newLotSize * baseScalar);
231
- const adjustedMinSize = Math.round(newMinSize * baseScalar);
231
+ const adjustedLotSize = convertQuantity(newLotSize, baseScalar);
232
+ const adjustedMinSize = convertQuantity(newMinSize, baseScalar);
232
233
 
233
234
  tx.moveCall({
234
235
  target: `${this.#config.DEEPBOOK_PACKAGE_ID}::pool::adjust_min_lot_size_admin`,
@@ -262,9 +263,9 @@ export class DeepBookAdminContract {
262
263
  */
263
264
  setEwmaParams = (poolKey: string, params: SetEwmaParams) => (tx: Transaction) => {
264
265
  const { alpha, zScoreThreshold, additionalTakerFee } = params;
265
- const adjustedAlpha = Math.round(alpha * FLOAT_SCALAR);
266
- const adjustedZScoreThreshold = Math.round(zScoreThreshold * FLOAT_SCALAR);
267
- const adjustedAdditionalTakerFee = Math.round(additionalTakerFee * FLOAT_SCALAR);
266
+ const adjustedAlpha = convertRate(alpha, FLOAT_SCALAR);
267
+ const adjustedZScoreThreshold = convertRate(zScoreThreshold, FLOAT_SCALAR);
268
+ const adjustedAdditionalTakerFee = convertRate(additionalTakerFee, FLOAT_SCALAR);
268
269
  const pool = this.#config.getPool(poolKey);
269
270
  const baseCoin = this.#config.getCoin(pool.baseCoin);
270
271
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);