@mysten/deepbook-v3 0.22.2 → 0.23.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 (65) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/cjs/client.d.ts +167 -1
  3. package/dist/cjs/client.js +354 -3
  4. package/dist/cjs/client.js.map +2 -2
  5. package/dist/cjs/contracts/utils/index.d.ts +13 -0
  6. package/dist/cjs/contracts/utils/index.js +6 -9
  7. package/dist/cjs/contracts/utils/index.js.map +2 -2
  8. package/dist/cjs/index.d.ts +3 -1
  9. package/dist/cjs/index.js +16 -3
  10. package/dist/cjs/index.js.map +2 -2
  11. package/dist/cjs/transactions/deepbook.d.ts +189 -1
  12. package/dist/cjs/transactions/deepbook.js +548 -1
  13. package/dist/cjs/transactions/deepbook.js.map +2 -2
  14. package/dist/cjs/types/index.d.ts +41 -0
  15. package/dist/cjs/types/index.js.map +1 -1
  16. package/dist/cjs/utils/config.d.ts +8 -8
  17. package/dist/cjs/utils/config.js +12 -11
  18. package/dist/cjs/utils/config.js.map +2 -2
  19. package/dist/cjs/utils/constants.d.ts +8 -0
  20. package/dist/cjs/utils/constants.js +18 -7
  21. package/dist/cjs/utils/constants.js.map +2 -2
  22. package/dist/cjs/utils/errors.d.ts +42 -0
  23. package/dist/cjs/utils/errors.js +70 -0
  24. package/dist/cjs/utils/errors.js.map +7 -0
  25. package/dist/cjs/utils/validation.d.ts +50 -0
  26. package/dist/cjs/utils/validation.js +67 -0
  27. package/dist/cjs/utils/validation.js.map +7 -0
  28. package/dist/esm/client.d.ts +167 -1
  29. package/dist/esm/client.js +355 -4
  30. package/dist/esm/client.js.map +2 -2
  31. package/dist/esm/contracts/utils/index.d.ts +13 -0
  32. package/dist/esm/contracts/utils/index.js +6 -9
  33. package/dist/esm/contracts/utils/index.js.map +2 -2
  34. package/dist/esm/index.d.ts +3 -1
  35. package/dist/esm/index.js +31 -5
  36. package/dist/esm/index.js.map +2 -2
  37. package/dist/esm/transactions/deepbook.d.ts +189 -1
  38. package/dist/esm/transactions/deepbook.js +549 -2
  39. package/dist/esm/transactions/deepbook.js.map +2 -2
  40. package/dist/esm/types/index.d.ts +41 -0
  41. package/dist/esm/types/index.js.map +1 -1
  42. package/dist/esm/utils/config.d.ts +8 -8
  43. package/dist/esm/utils/config.js +12 -11
  44. package/dist/esm/utils/config.js.map +2 -2
  45. package/dist/esm/utils/constants.d.ts +8 -0
  46. package/dist/esm/utils/constants.js +18 -7
  47. package/dist/esm/utils/constants.js.map +2 -2
  48. package/dist/esm/utils/errors.d.ts +42 -0
  49. package/dist/esm/utils/errors.js +50 -0
  50. package/dist/esm/utils/errors.js.map +7 -0
  51. package/dist/esm/utils/validation.d.ts +50 -0
  52. package/dist/esm/utils/validation.js +47 -0
  53. package/dist/esm/utils/validation.js.map +7 -0
  54. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  55. package/dist/tsconfig.tsbuildinfo +1 -1
  56. package/package.json +15 -8
  57. package/src/client.ts +427 -4
  58. package/src/contracts/utils/index.ts +27 -11
  59. package/src/index.ts +21 -2
  60. package/src/transactions/deepbook.ts +647 -2
  61. package/src/types/index.ts +47 -0
  62. package/src/utils/config.ts +28 -15
  63. package/src/utils/constants.ts +17 -6
  64. package/src/utils/errors.ts +67 -0
  65. package/src/utils/validation.ts +91 -0
package/src/client.ts CHANGED
@@ -11,12 +11,18 @@ import { DeepBookContract } from './transactions/deepbook.js';
11
11
  import { DeepBookAdminContract } from './transactions/deepbookAdmin.js';
12
12
  import { FlashLoanContract } from './transactions/flashLoans.js';
13
13
  import { GovernanceContract } from './transactions/governance.js';
14
- import type { BalanceManager, Environment, MarginManager } from './types/index.js';
14
+ import type {
15
+ BalanceManager,
16
+ Environment,
17
+ MarginManager,
18
+ CanPlaceLimitOrderParams,
19
+ CanPlaceMarketOrderParams,
20
+ } from './types/index.js';
15
21
  import {
16
22
  DEEP_SCALAR,
17
23
  DeepBookConfig,
18
24
  FLOAT_SCALAR,
19
- PRICE_INFO_OBJECT_MAX_AGE,
25
+ PRICE_INFO_OBJECT_MAX_AGE_MS,
20
26
  } from './utils/config.js';
21
27
  import type { CoinMap, PoolMap } from './utils/constants.js';
22
28
  import { MarginAdminContract } from './transactions/marginAdmin.js';
@@ -784,8 +790,8 @@ export class DeepBookClient {
784
790
 
785
791
  async getPriceInfoObject(tx: Transaction, coinKey: string): Promise<string> {
786
792
  const currentTime = Date.now();
787
- const dbusdcPriceInfoObjectAge = (await this.getPriceInfoObjectAge(coinKey)) * 1000;
788
- if (currentTime - dbusdcPriceInfoObjectAge < PRICE_INFO_OBJECT_MAX_AGE) {
793
+ const priceInfoObjectAge = (await this.getPriceInfoObjectAge(coinKey)) * 1000;
794
+ if (currentTime - priceInfoObjectAge < PRICE_INFO_OBJECT_MAX_AGE_MS) {
789
795
  return await this.#config.getCoin(coinKey).priceInfoObjectId!;
790
796
  }
791
797
 
@@ -1420,6 +1426,9 @@ export class DeepBookClient {
1420
1426
  basePythDecimals: number;
1421
1427
  quotePythPrice: string;
1422
1428
  quotePythDecimals: number;
1429
+ currentPrice: bigint;
1430
+ lowestTriggerAbovePrice: bigint;
1431
+ highestTriggerBelowPrice: bigint;
1423
1432
  }> {
1424
1433
  const manager = this.#config.getMarginManager(marginManagerKey);
1425
1434
  const tx = new Transaction();
@@ -1478,6 +1487,13 @@ export class DeepBookClient {
1478
1487
  const quotePythDecimals = Number(
1479
1488
  bcs.u8().parse(new Uint8Array(res.results[0].returnValues[10][0])),
1480
1489
  );
1490
+ const currentPrice = BigInt(bcs.U64.parse(new Uint8Array(res.results[0].returnValues[11][0])));
1491
+ const lowestTriggerAbovePrice = BigInt(
1492
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[12][0])),
1493
+ );
1494
+ const highestTriggerBelowPrice = BigInt(
1495
+ bcs.U64.parse(new Uint8Array(res.results[0].returnValues[13][0])),
1496
+ );
1481
1497
 
1482
1498
  return {
1483
1499
  managerId,
@@ -1491,6 +1507,9 @@ export class DeepBookClient {
1491
1507
  basePythDecimals,
1492
1508
  quotePythPrice: quotePythPrice.toString(),
1493
1509
  quotePythDecimals,
1510
+ currentPrice,
1511
+ lowestTriggerAbovePrice,
1512
+ highestTriggerBelowPrice,
1494
1513
  };
1495
1514
  }
1496
1515
 
@@ -1831,6 +1850,410 @@ export class DeepBookClient {
1831
1850
  return vecSet.contents.map((id) => normalizeSuiAddress(id));
1832
1851
  }
1833
1852
 
1853
+ /**
1854
+ * @description Check if a pool is a stable pool
1855
+ * @param {string} poolKey Key of the pool
1856
+ * @returns {Promise<boolean>} Whether the pool is a stable pool
1857
+ */
1858
+ async stablePool(poolKey: string): Promise<boolean> {
1859
+ const tx = new Transaction();
1860
+ tx.add(this.deepBook.stablePool(poolKey));
1861
+
1862
+ const res = await this.client.devInspectTransactionBlock({
1863
+ sender: normalizeSuiAddress(this.#address),
1864
+ transactionBlock: tx,
1865
+ });
1866
+
1867
+ const bytes = res.results![0].returnValues![0][0];
1868
+ return bcs.bool().parse(new Uint8Array(bytes));
1869
+ }
1870
+
1871
+ /**
1872
+ * @description Check if a pool is registered
1873
+ * @param {string} poolKey Key of the pool
1874
+ * @returns {Promise<boolean>} Whether the pool is registered
1875
+ */
1876
+ async registeredPool(poolKey: string): Promise<boolean> {
1877
+ const tx = new Transaction();
1878
+ tx.add(this.deepBook.registeredPool(poolKey));
1879
+
1880
+ const res = await this.client.devInspectTransactionBlock({
1881
+ sender: normalizeSuiAddress(this.#address),
1882
+ transactionBlock: tx,
1883
+ });
1884
+
1885
+ const bytes = res.results![0].returnValues![0][0];
1886
+ return bcs.bool().parse(new Uint8Array(bytes));
1887
+ }
1888
+
1889
+ /**
1890
+ * @description Get the quote quantity out using input token as fee
1891
+ * @param {string} poolKey Key of the pool
1892
+ * @param {number} baseQuantity Base quantity
1893
+ * @returns {Promise<{baseQuantity: number, baseOut: number, quoteOut: number, deepRequired: number}>}
1894
+ */
1895
+ async getQuoteQuantityOutInputFee(poolKey: string, baseQuantity: number) {
1896
+ const tx = new Transaction();
1897
+ const pool = this.#config.getPool(poolKey);
1898
+ const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
1899
+ const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
1900
+
1901
+ tx.add(this.deepBook.getQuoteQuantityOutInputFee(poolKey, baseQuantity));
1902
+ const res = await this.client.devInspectTransactionBlock({
1903
+ sender: normalizeSuiAddress(this.#address),
1904
+ transactionBlock: tx,
1905
+ });
1906
+
1907
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
1908
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
1909
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
1910
+
1911
+ return {
1912
+ baseQuantity,
1913
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
1914
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
1915
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
1916
+ };
1917
+ }
1918
+
1919
+ /**
1920
+ * @description Get the base quantity out using input token as fee
1921
+ * @param {string} poolKey Key of the pool
1922
+ * @param {number} quoteQuantity Quote quantity
1923
+ * @returns {Promise<{quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number}>}
1924
+ */
1925
+ async getBaseQuantityOutInputFee(poolKey: string, quoteQuantity: number) {
1926
+ const tx = new Transaction();
1927
+ const pool = this.#config.getPool(poolKey);
1928
+ const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
1929
+ const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
1930
+
1931
+ tx.add(this.deepBook.getBaseQuantityOutInputFee(poolKey, quoteQuantity));
1932
+ const res = await this.client.devInspectTransactionBlock({
1933
+ sender: normalizeSuiAddress(this.#address),
1934
+ transactionBlock: tx,
1935
+ });
1936
+
1937
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
1938
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
1939
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
1940
+
1941
+ return {
1942
+ quoteQuantity,
1943
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
1944
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
1945
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
1946
+ };
1947
+ }
1948
+
1949
+ /**
1950
+ * @description Get the quantity out using input token as fee
1951
+ * @param {string} poolKey Key of the pool
1952
+ * @param {number} baseQuantity Base quantity
1953
+ * @param {number} quoteQuantity Quote quantity
1954
+ * @returns {Promise<{baseQuantity: number, quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number}>}
1955
+ */
1956
+ async getQuantityOutInputFee(poolKey: string, baseQuantity: number, quoteQuantity: number) {
1957
+ const tx = new Transaction();
1958
+ const pool = this.#config.getPool(poolKey);
1959
+ const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
1960
+ const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
1961
+
1962
+ tx.add(this.deepBook.getQuantityOutInputFee(poolKey, baseQuantity, quoteQuantity));
1963
+ const res = await this.client.devInspectTransactionBlock({
1964
+ sender: normalizeSuiAddress(this.#address),
1965
+ transactionBlock: tx,
1966
+ });
1967
+
1968
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
1969
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
1970
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
1971
+
1972
+ return {
1973
+ baseQuantity,
1974
+ quoteQuantity,
1975
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
1976
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
1977
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
1978
+ };
1979
+ }
1980
+
1981
+ /**
1982
+ * @description Get the base quantity needed to receive target quote quantity
1983
+ * @param {string} poolKey Key of the pool
1984
+ * @param {number} targetQuoteQuantity Target quote quantity
1985
+ * @param {boolean} payWithDeep Whether to pay fees with DEEP
1986
+ * @returns {Promise<{baseIn: number, quoteOut: number, deepRequired: number}>}
1987
+ */
1988
+ async getBaseQuantityIn(poolKey: string, targetQuoteQuantity: number, payWithDeep: boolean) {
1989
+ const tx = new Transaction();
1990
+ const pool = this.#config.getPool(poolKey);
1991
+ const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
1992
+ const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
1993
+
1994
+ tx.add(this.deepBook.getBaseQuantityIn(poolKey, targetQuoteQuantity, payWithDeep));
1995
+ const res = await this.client.devInspectTransactionBlock({
1996
+ sender: normalizeSuiAddress(this.#address),
1997
+ transactionBlock: tx,
1998
+ });
1999
+
2000
+ const baseIn = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
2001
+ const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
2002
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
2003
+
2004
+ return {
2005
+ baseIn: Number((baseIn / baseScalar).toFixed(9)),
2006
+ quoteOut: Number((quoteOut / quoteScalar).toFixed(9)),
2007
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
2008
+ };
2009
+ }
2010
+
2011
+ /**
2012
+ * @description Get the quote quantity needed to receive target base quantity
2013
+ * @param {string} poolKey Key of the pool
2014
+ * @param {number} targetBaseQuantity Target base quantity
2015
+ * @param {boolean} payWithDeep Whether to pay fees with DEEP
2016
+ * @returns {Promise<{baseOut: number, quoteIn: number, deepRequired: number}>}
2017
+ */
2018
+ async getQuoteQuantityIn(poolKey: string, targetBaseQuantity: number, payWithDeep: boolean) {
2019
+ const tx = new Transaction();
2020
+ const pool = this.#config.getPool(poolKey);
2021
+ const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
2022
+ const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
2023
+
2024
+ tx.add(this.deepBook.getQuoteQuantityIn(poolKey, targetBaseQuantity, payWithDeep));
2025
+ const res = await this.client.devInspectTransactionBlock({
2026
+ sender: normalizeSuiAddress(this.#address),
2027
+ transactionBlock: tx,
2028
+ });
2029
+
2030
+ const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
2031
+ const quoteIn = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
2032
+ const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
2033
+
2034
+ return {
2035
+ baseOut: Number((baseOut / baseScalar).toFixed(9)),
2036
+ quoteIn: Number((quoteIn / quoteScalar).toFixed(9)),
2037
+ deepRequired: Number((deepRequired / DEEP_SCALAR).toFixed(9)),
2038
+ };
2039
+ }
2040
+
2041
+ /**
2042
+ * @description Get account order details for a balance manager
2043
+ * @param {string} poolKey Key of the pool
2044
+ * @param {string} managerKey Key of the balance manager
2045
+ * @returns {Promise<Array>} Array of order details
2046
+ */
2047
+ async getAccountOrderDetails(poolKey: string, managerKey: string) {
2048
+ const tx = new Transaction();
2049
+ tx.add(this.deepBook.getAccountOrderDetails(poolKey, managerKey));
2050
+
2051
+ const res = await this.client.devInspectTransactionBlock({
2052
+ sender: normalizeSuiAddress(this.#address),
2053
+ transactionBlock: tx,
2054
+ });
2055
+
2056
+ try {
2057
+ const orderInformation = res.results![0].returnValues![0][0];
2058
+ return bcs.vector(Order).parse(new Uint8Array(orderInformation));
2059
+ } catch {
2060
+ return [];
2061
+ }
2062
+ }
2063
+
2064
+ /**
2065
+ * @description Get the DEEP required for an order
2066
+ * @param {string} poolKey Key of the pool
2067
+ * @param {number} baseQuantity Base quantity
2068
+ * @param {number} price Price
2069
+ * @returns {Promise<{deepRequiredTaker: number, deepRequiredMaker: number}>}
2070
+ */
2071
+ async getOrderDeepRequired(poolKey: string, baseQuantity: number, price: number) {
2072
+ const tx = new Transaction();
2073
+ tx.add(this.deepBook.getOrderDeepRequired(poolKey, baseQuantity, price));
2074
+
2075
+ const res = await this.client.devInspectTransactionBlock({
2076
+ sender: normalizeSuiAddress(this.#address),
2077
+ transactionBlock: tx,
2078
+ });
2079
+
2080
+ const deepRequiredTaker = Number(
2081
+ bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])),
2082
+ );
2083
+ const deepRequiredMaker = Number(
2084
+ bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])),
2085
+ );
2086
+
2087
+ return {
2088
+ deepRequiredTaker: Number((deepRequiredTaker / DEEP_SCALAR).toFixed(9)),
2089
+ deepRequiredMaker: Number((deepRequiredMaker / DEEP_SCALAR).toFixed(9)),
2090
+ };
2091
+ }
2092
+
2093
+ /**
2094
+ * @description Check if account exists for a balance manager
2095
+ * @param {string} poolKey Key of the pool
2096
+ * @param {string} managerKey Key of the balance manager
2097
+ * @returns {Promise<boolean>} Whether account exists
2098
+ */
2099
+ async accountExists(poolKey: string, managerKey: string): Promise<boolean> {
2100
+ const tx = new Transaction();
2101
+ tx.add(this.deepBook.accountExists(poolKey, managerKey));
2102
+
2103
+ const res = await this.client.devInspectTransactionBlock({
2104
+ sender: normalizeSuiAddress(this.#address),
2105
+ transactionBlock: tx,
2106
+ });
2107
+
2108
+ const bytes = res.results![0].returnValues![0][0];
2109
+ return bcs.bool().parse(new Uint8Array(bytes));
2110
+ }
2111
+
2112
+ /**
2113
+ * @description Get the next epoch trade parameters
2114
+ * @param {string} poolKey Key of the pool
2115
+ * @returns {Promise<{takerFee: number, makerFee: number, stakeRequired: number}>}
2116
+ */
2117
+ async poolTradeParamsNext(poolKey: string) {
2118
+ const tx = new Transaction();
2119
+ tx.add(this.deepBook.poolTradeParamsNext(poolKey));
2120
+
2121
+ const res = await this.client.devInspectTransactionBlock({
2122
+ sender: normalizeSuiAddress(this.#address),
2123
+ transactionBlock: tx,
2124
+ });
2125
+
2126
+ const takerFee = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
2127
+ const makerFee = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
2128
+ const stakeRequired = Number(
2129
+ bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])),
2130
+ );
2131
+
2132
+ return {
2133
+ takerFee: takerFee / FLOAT_SCALAR,
2134
+ makerFee: makerFee / FLOAT_SCALAR,
2135
+ stakeRequired: stakeRequired / DEEP_SCALAR,
2136
+ };
2137
+ }
2138
+
2139
+ /**
2140
+ * @description Get the quorum for a pool
2141
+ * @param {string} poolKey Key of the pool
2142
+ * @returns {Promise<number>} The quorum amount in DEEP
2143
+ */
2144
+ async quorum(poolKey: string): Promise<number> {
2145
+ const tx = new Transaction();
2146
+ tx.add(this.deepBook.quorum(poolKey));
2147
+
2148
+ const res = await this.client.devInspectTransactionBlock({
2149
+ sender: normalizeSuiAddress(this.#address),
2150
+ transactionBlock: tx,
2151
+ });
2152
+
2153
+ const bytes = res.results![0].returnValues![0][0];
2154
+ const quorum = Number(bcs.U64.parse(new Uint8Array(bytes)));
2155
+ return quorum / DEEP_SCALAR;
2156
+ }
2157
+
2158
+ /**
2159
+ * @description Get the pool ID
2160
+ * @param {string} poolKey Key of the pool
2161
+ * @returns {Promise<string>} The pool ID
2162
+ */
2163
+ async poolId(poolKey: string): Promise<string> {
2164
+ const tx = new Transaction();
2165
+ tx.add(this.deepBook.poolId(poolKey));
2166
+
2167
+ const res = await this.client.devInspectTransactionBlock({
2168
+ sender: normalizeSuiAddress(this.#address),
2169
+ transactionBlock: tx,
2170
+ });
2171
+
2172
+ const bytes = res.results![0].returnValues![0][0];
2173
+ return normalizeSuiAddress(bcs.Address.parse(new Uint8Array(bytes)));
2174
+ }
2175
+
2176
+ /**
2177
+ * @description Check if a limit order can be placed
2178
+ * @param {CanPlaceLimitOrderParams} params Parameters for checking limit order placement
2179
+ * @returns {Promise<boolean>} Whether order can be placed
2180
+ */
2181
+ async canPlaceLimitOrder(params: CanPlaceLimitOrderParams): Promise<boolean> {
2182
+ const tx = new Transaction();
2183
+ tx.add(this.deepBook.canPlaceLimitOrder(params));
2184
+
2185
+ const res = await this.client.devInspectTransactionBlock({
2186
+ sender: normalizeSuiAddress(this.#address),
2187
+ transactionBlock: tx,
2188
+ });
2189
+
2190
+ const bytes = res.results![0].returnValues![0][0];
2191
+ return bcs.bool().parse(new Uint8Array(bytes));
2192
+ }
2193
+
2194
+ /**
2195
+ * @description Check if a market order can be placed
2196
+ * @param {CanPlaceMarketOrderParams} params Parameters for checking market order placement
2197
+ * @returns {Promise<boolean>} Whether order can be placed
2198
+ */
2199
+ async canPlaceMarketOrder(params: CanPlaceMarketOrderParams): Promise<boolean> {
2200
+ const tx = new Transaction();
2201
+ tx.add(this.deepBook.canPlaceMarketOrder(params));
2202
+
2203
+ const res = await this.client.devInspectTransactionBlock({
2204
+ sender: normalizeSuiAddress(this.#address),
2205
+ transactionBlock: tx,
2206
+ });
2207
+
2208
+ const bytes = res.results![0].returnValues![0][0];
2209
+ return bcs.bool().parse(new Uint8Array(bytes));
2210
+ }
2211
+
2212
+ /**
2213
+ * @description Check if market order params are valid
2214
+ * @param {string} poolKey Key of the pool
2215
+ * @param {number} quantity Quantity
2216
+ * @returns {Promise<boolean>} Whether params are valid
2217
+ */
2218
+ async checkMarketOrderParams(poolKey: string, quantity: number): Promise<boolean> {
2219
+ const tx = new Transaction();
2220
+ tx.add(this.deepBook.checkMarketOrderParams(poolKey, quantity));
2221
+
2222
+ const res = await this.client.devInspectTransactionBlock({
2223
+ sender: normalizeSuiAddress(this.#address),
2224
+ transactionBlock: tx,
2225
+ });
2226
+
2227
+ const bytes = res.results![0].returnValues![0][0];
2228
+ return bcs.bool().parse(new Uint8Array(bytes));
2229
+ }
2230
+
2231
+ /**
2232
+ * @description Check if limit order params are valid
2233
+ * @param {string} poolKey Key of the pool
2234
+ * @param {number} price Price
2235
+ * @param {number} quantity Quantity
2236
+ * @param {number} expireTimestamp Expiration timestamp
2237
+ * @returns {Promise<boolean>} Whether params are valid
2238
+ */
2239
+ async checkLimitOrderParams(
2240
+ poolKey: string,
2241
+ price: number,
2242
+ quantity: number,
2243
+ expireTimestamp: number,
2244
+ ): Promise<boolean> {
2245
+ const tx = new Transaction();
2246
+ tx.add(this.deepBook.checkLimitOrderParams(poolKey, price, quantity, expireTimestamp));
2247
+
2248
+ const res = await this.client.devInspectTransactionBlock({
2249
+ sender: normalizeSuiAddress(this.#address),
2250
+ transactionBlock: tx,
2251
+ });
2252
+
2253
+ const bytes = res.results![0].returnValues![0][0];
2254
+ return bcs.bool().parse(new Uint8Array(bytes));
2255
+ }
2256
+
1834
2257
  /**
1835
2258
  * @description Helper function to format token amounts without floating point errors
1836
2259
  * @param {bigint} rawAmount The raw amount as bigint
@@ -5,6 +5,7 @@ import { bcs, TypeTagSerializer, BcsStruct, BcsEnum, BcsTuple } from '@mysten/su
5
5
  import { normalizeSuiAddress } from '@mysten/sui/utils';
6
6
  import type { TransactionArgument } from '@mysten/sui/transactions';
7
7
  import { isArgument } from '@mysten/sui/transactions';
8
+ import { ValidationError, ErrorMessages } from '../../utils/errors.js';
8
9
 
9
10
  const MOVE_STDLIB_ADDRESS = normalizeSuiAddress('0x1');
10
11
  const SUI_FRAMEWORK_ADDRESS = normalizeSuiAddress('0x2');
@@ -12,6 +13,11 @@ const SUI_SYSTEM_ADDRESS = normalizeSuiAddress('0x3');
12
13
 
13
14
  export type RawTransactionArgument<T> = T | TransactionArgument;
14
15
 
16
+ /**
17
+ * @description Get the BCS schema for a given type tag
18
+ * @param {string | TypeTag} typeTag - The Move type tag to get the schema for
19
+ * @returns {BcsType<any> | null} The BCS schema if found, null otherwise
20
+ */
15
21
  export function getPureBcsSchema(typeTag: string | TypeTag): BcsType<any> | null {
16
22
  const parsedTag = typeof typeTag === 'string' ? TypeTagSerializer.parseFromStr(typeTag) : typeTag;
17
23
 
@@ -60,16 +66,22 @@ export function getPureBcsSchema(typeTag: string | TypeTag): BcsType<any> | null
60
66
  return null;
61
67
  }
62
68
 
69
+ /**
70
+ * @description Normalize Move function arguments to TransactionArguments
71
+ * @param {unknown[] | object} args - The arguments to normalize
72
+ * @param {string[]} argTypes - The expected Move type strings for each argument
73
+ * @param {string[]} [parameterNames] - Optional parameter names when args is an object
74
+ * @returns {TransactionArgument[]} Array of normalized transaction arguments
75
+ * @throws {Error} If arguments are invalid or don't match expected types
76
+ */
63
77
  export function normalizeMoveArguments(
64
78
  args: unknown[] | object,
65
79
  argTypes: string[],
66
80
  parameterNames?: string[],
67
- ) {
81
+ ): TransactionArgument[] {
68
82
  const argLen = Array.isArray(args) ? args.length : Object.keys(args).length;
69
83
  if (parameterNames && argLen !== parameterNames.length) {
70
- throw new Error(
71
- `Invalid number of arguments, expected ${parameterNames.length}, got ${argLen}`,
72
- );
84
+ throw new ValidationError(ErrorMessages.INVALID_ARGUMENT_COUNT(parameterNames.length, argLen));
73
85
  }
74
86
 
75
87
  const normalizedArgs: TransactionArgument[] = [];
@@ -99,20 +111,18 @@ export function normalizeMoveArguments(
99
111
  let arg;
100
112
  if (Array.isArray(args)) {
101
113
  if (index >= args.length) {
102
- throw new Error(
103
- `Invalid number of arguments, expected at least ${index + 1}, got ${args.length}`,
104
- );
114
+ throw new ValidationError(ErrorMessages.INVALID_ARGUMENT_COUNT(index + 1, args.length));
105
115
  }
106
116
  arg = args[index];
107
117
  } else {
108
118
  if (!parameterNames) {
109
- throw new Error(`Expected arguments to be passed as an array`);
119
+ throw new ValidationError(`Expected arguments to be passed as an array`);
110
120
  }
111
121
  const name = parameterNames[index];
112
122
  arg = args[name as keyof typeof args];
113
123
 
114
124
  if (arg == null) {
115
- throw new Error(`Parameter ${name} is required`);
125
+ throw new ValidationError(ErrorMessages.PARAMETER_REQUIRED(name));
116
126
  }
117
127
  }
118
128
 
@@ -135,7 +145,7 @@ export function normalizeMoveArguments(
135
145
  continue;
136
146
  }
137
147
 
138
- throw new Error(`Invalid argument ${stringify(arg)} for type ${type}`);
148
+ throw new ValidationError(ErrorMessages.INVALID_ARGUMENT(stringify(arg) as string, type));
139
149
  }
140
150
 
141
151
  return normalizedArgs;
@@ -156,7 +166,13 @@ export class MoveTuple<
156
166
  const Name extends string,
157
167
  > extends BcsTuple<T, Name> {}
158
168
 
159
- function stringify(val: unknown) {
169
+ /**
170
+ * @description Convert a value to string representation for error messages
171
+ * @param {unknown} val - The value to stringify
172
+ * @returns {unknown} String representation of the value
173
+ * @private
174
+ */
175
+ function stringify(val: unknown): unknown {
160
176
  if (typeof val === 'object') {
161
177
  return JSON.stringify(val, (val: unknown) => val);
162
178
  }
package/src/index.ts CHANGED
@@ -63,6 +63,25 @@ export {
63
63
  FLOAT_SCALAR,
64
64
  GAS_BUDGET,
65
65
  MAX_TIMESTAMP,
66
- POOL_CREATION_FEE,
67
- PRICE_INFO_OBJECT_MAX_AGE,
66
+ POOL_CREATION_FEE_DEEP,
67
+ PRICE_INFO_OBJECT_MAX_AGE_MS,
68
68
  } from './utils/config.js';
69
+
70
+ // Error handling utilities
71
+ export {
72
+ DeepBookError,
73
+ ResourceNotFoundError,
74
+ ConfigurationError,
75
+ ValidationError,
76
+ ErrorMessages,
77
+ } from './utils/errors.js';
78
+
79
+ // Validation utilities
80
+ export {
81
+ validateRequired,
82
+ validateAddress,
83
+ validatePositiveNumber,
84
+ validateNonNegativeNumber,
85
+ validateRange,
86
+ validateNonEmptyArray,
87
+ } from './utils/validation.js';