@agglayer/sdk 1.0.0-beta.16 → 1.0.0-beta.18

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.
package/dist/index.mjs CHANGED
@@ -11,7 +11,33 @@ var ChainRegistry = class _ChainRegistry {
11
11
  }
12
12
  return _ChainRegistry.instance;
13
13
  }
14
+ // DEV: if adding new default chains, also update README.md
14
15
  initializeDefaultChains() {
16
+ this.registerChain({
17
+ chainId: 1,
18
+ networkId: 0,
19
+ name: "Ethereum",
20
+ rpcUrl: "https://eth.llamarpc.com",
21
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
22
+ blockExplorer: { name: "Etherscan", url: "https://etherscan.io" },
23
+ bridgeAddress: "",
24
+ proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/mainnet/",
25
+ isTestnet: false
26
+ });
27
+ this.registerChain({
28
+ chainId: 747474,
29
+ networkId: 20,
30
+ name: "Katana",
31
+ rpcUrl: "https://rpc.katana.network",
32
+ nativeCurrency: { name: "Ethereum", symbol: "ETH", decimals: 18 },
33
+ blockExplorer: {
34
+ name: "Katana Explorer",
35
+ url: "https://katanascan.com"
36
+ },
37
+ bridgeAddress: "",
38
+ proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/mainnet/",
39
+ isTestnet: false
40
+ });
15
41
  this.registerChain({
16
42
  chainId: 11155111,
17
43
  networkId: 0,
@@ -26,20 +52,6 @@ var ChainRegistry = class _ChainRegistry {
26
52
  proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/testnet/",
27
53
  isTestnet: true
28
54
  });
29
- this.registerChain({
30
- chainId: 2442,
31
- networkId: 1,
32
- name: "Polygon Cardona",
33
- rpcUrl: "https://rpc.cardona.zkevm-rpc.com",
34
- nativeCurrency: { name: "Ethereum", symbol: "ETH", decimals: 18 },
35
- blockExplorer: {
36
- name: "Cardona PolygonScan",
37
- url: "https://cardona-zkevm.polygonscan.com"
38
- },
39
- bridgeAddress: "0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582",
40
- proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/testnet/",
41
- isTestnet: true
42
- });
43
55
  }
44
56
  /**
45
57
  * Register a new chain
@@ -1390,14 +1402,16 @@ var ERC20 = class extends BaseContract {
1390
1402
  import { createPublicClient as createPublicClient3, http as http3 } from "viem";
1391
1403
 
1392
1404
  // src/constants.ts
1393
- var NETWORKS = {
1394
- SEPOLIA: 11155111,
1395
- CARDONA: 2442
1396
- };
1397
- var DEFAULT_NETWORK = NETWORKS.SEPOLIA;
1405
+ var ARC_API_BASE_URL = "https://arc-api.polygon.technology";
1406
+ var ARC_API_DEFAULT_TIMEOUT = 3e4;
1398
1407
  var DEFAULT_CHAINS_PER_PAGE = 100;
1399
1408
  var DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE = 1;
1400
1409
  var MAX_TRANSACTIONS_PER_PAGE = 100;
1410
+ var NETWORKS = {
1411
+ ETHEREUM: 1,
1412
+ KATANA: 747474
1413
+ };
1414
+ var DEFAULT_NETWORK = NETWORKS.ETHEREUM;
1401
1415
 
1402
1416
  // src/native/index.ts
1403
1417
  var NativeClient = class {
@@ -1587,9 +1601,6 @@ var HttpClient = class {
1587
1601
  signal: controller.signal
1588
1602
  });
1589
1603
  clearTimeout(timeoutId);
1590
- if (!response.ok) {
1591
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1592
- }
1593
1604
  let data;
1594
1605
  const contentType = response.headers.get("content-type");
1595
1606
  if (contentType?.includes("application/json")) {
@@ -1597,6 +1608,12 @@ var HttpClient = class {
1597
1608
  } else {
1598
1609
  data = await response.text();
1599
1610
  }
1611
+ if (!response.ok) {
1612
+ if (contentType?.includes("application/json")) {
1613
+ } else {
1614
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1615
+ }
1616
+ }
1600
1617
  return {
1601
1618
  data,
1602
1619
  status: response.status,
@@ -1684,6 +1701,12 @@ var ArcApiService = class {
1684
1701
  builtTransactionRequestBody
1685
1702
  );
1686
1703
  }
1704
+ async buildClaimTransaction(sourceNetworkId, depositCount) {
1705
+ return this.httpClient.post("/routes/build-transaction-for-claim", {
1706
+ sourceNetworkId,
1707
+ depositCount
1708
+ });
1709
+ }
1687
1710
  // supports cursor based pagination only
1688
1711
  async transactions(transactionsRequestQueryParams) {
1689
1712
  return this.httpClient.get("/transactions", {
@@ -1692,20 +1715,63 @@ var ArcApiService = class {
1692
1715
  }
1693
1716
  };
1694
1717
 
1718
+ // src/core/utils/apiError.ts
1719
+ var ApiError = class _ApiError extends Error {
1720
+ constructor(errorResponse) {
1721
+ super(errorResponse.message);
1722
+ this.name = errorResponse.name;
1723
+ this.code = errorResponse.code;
1724
+ this.details = errorResponse.details;
1725
+ if (Error.captureStackTrace) {
1726
+ Error.captureStackTrace(this, _ApiError);
1727
+ }
1728
+ }
1729
+ /**
1730
+ * Create API error from error response
1731
+ */
1732
+ static fromErrorResponse(errorResponse) {
1733
+ return new _ApiError(errorResponse);
1734
+ }
1735
+ /**
1736
+ * Create fallback error when API completely fails
1737
+ */
1738
+ static createFallbackError(originalError, operation) {
1739
+ return new _ApiError({
1740
+ status: "error",
1741
+ message: `${operation} failed: ${originalError.message}`,
1742
+ name: "ApiConnectionError",
1743
+ code: 500,
1744
+ details: {
1745
+ originalError: originalError.message,
1746
+ operation
1747
+ }
1748
+ });
1749
+ }
1750
+ /**
1751
+ * Convert to plain object for serialization
1752
+ */
1753
+ toJSON() {
1754
+ return {
1755
+ name: this.name,
1756
+ message: this.message,
1757
+ code: this.code,
1758
+ details: this.details
1759
+ };
1760
+ }
1761
+ };
1762
+
1695
1763
  // src/core/index.ts
1696
1764
  var CoreClient = class {
1697
1765
  constructor(config) {
1698
- this.config = config;
1699
- if (!this.config) {
1700
- throw new Error("Config is required");
1701
- }
1702
- if (!this.config.apiBaseUrl) {
1703
- throw new Error("API base URL is required");
1704
- }
1766
+ this.config = {
1767
+ ...config,
1768
+ apiBaseUrl: config?.apiBaseUrl || ARC_API_BASE_URL,
1769
+ apiTimeout: config?.apiTimeout || ARC_API_DEFAULT_TIMEOUT
1770
+ };
1705
1771
  const { apiBaseUrl, apiTimeout } = this.config;
1706
1772
  this.arcApiService = new ArcApiService({
1707
- baseUrl: apiBaseUrl,
1708
- timeout: apiTimeout ?? 3e4
1773
+ baseUrl: apiBaseUrl || ARC_API_BASE_URL,
1774
+ timeout: apiTimeout || ARC_API_DEFAULT_TIMEOUT
1709
1775
  });
1710
1776
  }
1711
1777
  /**
@@ -1715,57 +1781,64 @@ var CoreClient = class {
1715
1781
  * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1716
1782
  */
1717
1783
  async getAllChainsPaginated(params, pageSize = DEFAULT_CHAINS_PER_PAGE) {
1718
- const firstResponse = await this.arcApiService.chains({
1719
- ...params,
1720
- limit: pageSize
1721
- });
1722
- if (firstResponse.data.status !== "success") {
1723
- throw new Error(firstResponse.data.message);
1724
- }
1725
- const firstPageData = firstResponse.data.data;
1726
- const pagination = firstResponse.data.pagination;
1727
- const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1728
- if (!pagination?.total || pagination.total <= pageSize) {
1729
- return {
1730
- chains: firstPageChains
1731
- };
1732
- }
1733
- const totalPages = Math.ceil(pagination.total / pageSize);
1734
- const remainingPages = totalPages - 1;
1735
- if (remainingPages === 0) {
1784
+ try {
1785
+ const firstResponse = await this.arcApiService.chains({
1786
+ ...params,
1787
+ limit: pageSize
1788
+ });
1789
+ if (firstResponse.data.status !== "success") {
1790
+ throw ApiError.fromErrorResponse(firstResponse.data);
1791
+ }
1792
+ const firstPageData = firstResponse.data.data;
1793
+ const pagination = firstResponse.data.pagination;
1794
+ const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1795
+ if (!pagination?.total || pagination.total <= pageSize) {
1796
+ return {
1797
+ chains: firstPageChains
1798
+ };
1799
+ }
1800
+ const totalPages = Math.ceil(pagination.total / pageSize);
1801
+ const remainingPages = totalPages - 1;
1802
+ if (remainingPages === 0) {
1803
+ return {
1804
+ chains: firstPageChains
1805
+ };
1806
+ }
1807
+ const remainingPagePromises = Array.from(
1808
+ { length: remainingPages },
1809
+ (_, index) => {
1810
+ const offset = (index + 1) * pageSize;
1811
+ return this.arcApiService.chains({
1812
+ ...params,
1813
+ limit: pageSize,
1814
+ offset
1815
+ });
1816
+ }
1817
+ );
1818
+ const remainingResponses = await Promise.all(remainingPagePromises);
1819
+ for (const response of remainingResponses) {
1820
+ if (response.data.status !== "success") {
1821
+ throw ApiError.fromErrorResponse(response.data);
1822
+ }
1823
+ }
1824
+ const allChains = [
1825
+ ...firstPageChains,
1826
+ ...remainingResponses.flatMap((response) => {
1827
+ if (response.data.status === "success") {
1828
+ return Array.isArray(response.data.data) ? response.data.data : [];
1829
+ }
1830
+ return [];
1831
+ })
1832
+ ];
1736
1833
  return {
1737
- chains: firstPageChains
1834
+ chains: allChains
1738
1835
  };
1739
- }
1740
- const remainingPagePromises = Array.from(
1741
- { length: remainingPages },
1742
- (_, index) => {
1743
- const offset = (index + 1) * pageSize;
1744
- return this.arcApiService.chains({
1745
- ...params,
1746
- limit: pageSize,
1747
- offset
1748
- });
1749
- }
1750
- );
1751
- const remainingResponses = await Promise.all(remainingPagePromises);
1752
- for (const response of remainingResponses) {
1753
- if (response.data.status !== "success") {
1754
- throw new Error(response.data.message);
1836
+ } catch (error) {
1837
+ if (error instanceof ApiError) {
1838
+ throw error;
1755
1839
  }
1840
+ throw ApiError.createFallbackError(error, "Get chains metadata");
1756
1841
  }
1757
- const allChains = [
1758
- ...firstPageChains,
1759
- ...remainingResponses.flatMap((response) => {
1760
- if (response.data.status === "success") {
1761
- return Array.isArray(response.data.data) ? response.data.data : [];
1762
- }
1763
- return [];
1764
- })
1765
- ];
1766
- return {
1767
- chains: allChains
1768
- };
1769
1842
  }
1770
1843
  /**
1771
1844
  * Get all chains metadata from AggLayer API
@@ -1809,11 +1882,18 @@ var CoreClient = class {
1809
1882
  * Get all routes from AggLayer API
1810
1883
  */
1811
1884
  async getRoutes(routesRequestParams) {
1812
- const response = await this.arcApiService.routes(routesRequestParams);
1813
- if (response.data.status === "success") {
1814
- return response.data.data;
1885
+ try {
1886
+ const response = await this.arcApiService.routes(routesRequestParams);
1887
+ if (response.data.status === "success") {
1888
+ return response.data.data;
1889
+ }
1890
+ throw ApiError.fromErrorResponse(response.data);
1891
+ } catch (error) {
1892
+ if (error instanceof ApiError) {
1893
+ throw error;
1894
+ }
1895
+ throw ApiError.createFallbackError(error, "Get routes");
1815
1896
  }
1816
- throw new Error(response.data.message);
1817
1897
  }
1818
1898
  /**
1819
1899
  * Get calldata from a route
@@ -1825,52 +1905,101 @@ var CoreClient = class {
1825
1905
  return route.transactionRequest;
1826
1906
  }
1827
1907
  if (route.steps.length === 0 || !route.steps[0]) {
1828
- throw new Error("Route has no steps to build transaction from");
1908
+ throw ApiError.createFallbackError(
1909
+ new Error("Route has no steps to build transaction from"),
1910
+ "Get unsigned transaction"
1911
+ );
1829
1912
  }
1830
- const response = await this.arcApiService.buildTransaction(route.steps[0]);
1831
- if (response.data.status === "success") {
1832
- return response.data.data;
1913
+ try {
1914
+ const response = await this.arcApiService.buildTransaction(
1915
+ route.steps[0]
1916
+ );
1917
+ if (response.data.status === "success") {
1918
+ return response.data.data;
1919
+ }
1920
+ throw ApiError.fromErrorResponse(response.data);
1921
+ } catch (error) {
1922
+ if (error instanceof ApiError) {
1923
+ throw error;
1924
+ }
1925
+ throw ApiError.createFallbackError(
1926
+ error,
1927
+ "Get unsigned transaction"
1928
+ );
1929
+ }
1930
+ }
1931
+ /**
1932
+ * Get calldata for claim step
1933
+ * Needs to be called separately as claim step is not part of route.
1934
+ *
1935
+ * @developer Note: Do not misinterpret network ID as chain ID.
1936
+ *
1937
+ * @param sourceNetworkId - The source network ID where the transfer was initiated.
1938
+ * @param depositCount - The deposit count associated with the transfer.
1939
+ */
1940
+ async getClaimUnsignedTransaction(buildClaimTxParams) {
1941
+ try {
1942
+ const response = await this.arcApiService.buildClaimTransaction(
1943
+ buildClaimTxParams.sourceNetworkId,
1944
+ buildClaimTxParams.depositCount
1945
+ );
1946
+ if (response.data.status === "success") {
1947
+ return response.data.data;
1948
+ }
1949
+ throw ApiError.fromErrorResponse(response.data);
1950
+ } catch (error) {
1951
+ if (error instanceof ApiError) {
1952
+ throw error;
1953
+ }
1954
+ throw ApiError.createFallbackError(
1955
+ error,
1956
+ "Get claim unsigned transaction"
1957
+ );
1833
1958
  }
1834
- throw new Error(response.data.message);
1835
1959
  }
1836
1960
  /**
1837
1961
  * Get all transactions via web sockets
1838
1962
  */
1839
1963
  async getTransactions(transactionsRequestQueryParams) {
1840
1964
  if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1841
- throw new Error(
1842
- `Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`
1965
+ throw ApiError.createFallbackError(
1966
+ new Error(`Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`),
1967
+ "Get transactions"
1843
1968
  );
1844
1969
  }
1845
- const response = await this.arcApiService.transactions(
1846
- transactionsRequestQueryParams
1847
- );
1848
- if (response.data.status === "success") {
1849
- return response.data.data;
1970
+ try {
1971
+ const response = await this.arcApiService.transactions(
1972
+ transactionsRequestQueryParams
1973
+ );
1974
+ if (response.data.status === "success") {
1975
+ return response.data.data;
1976
+ }
1977
+ throw ApiError.fromErrorResponse(response.data);
1978
+ } catch (error) {
1979
+ if (error instanceof ApiError) {
1980
+ throw error;
1981
+ }
1982
+ throw ApiError.createFallbackError(error, "Get transactions");
1850
1983
  }
1851
- throw new Error(response.data.message);
1852
1984
  }
1853
1985
  };
1854
1986
 
1855
1987
  // src/index.ts
1988
+ var defaultConfig = {
1989
+ mode: [SDK_MODES.CORE]
1990
+ };
1856
1991
  var AggLayerSDK = class {
1857
- constructor(config) {
1992
+ constructor(config = defaultConfig) {
1858
1993
  this.config = config;
1859
1994
  if (!config.mode || config.mode && config.mode.length === 0) {
1860
1995
  this.config.mode = ["CORE"];
1861
1996
  }
1862
- if (config.mode.includes(SDK_MODES.CORE)) {
1863
- if (!this.config.core) {
1864
- throw new Error("Core config is required");
1865
- }
1866
- this.core = new CoreClient(this.config.core);
1997
+ if (this.config.mode?.includes(SDK_MODES.CORE)) {
1998
+ this.core = new CoreClient(this.config?.core);
1867
1999
  }
1868
2000
  if (this.config.mode?.includes(SDK_MODES.NATIVE)) {
1869
- if (!this.config.native) {
1870
- throw new Error("NATIVE config is required");
1871
- }
1872
2001
  const nativeConfig = {
1873
- defaultNetwork: this.config.native?.defaultNetwork || DEFAULT_NETWORK,
2002
+ defaultNetwork: this.config.native?.defaultNetwork ?? DEFAULT_NETWORK,
1874
2003
  ...this.config.native?.chains && {
1875
2004
  chains: this.config.native.chains
1876
2005
  },
@@ -1904,6 +2033,7 @@ var AggLayerSDK = class {
1904
2033
  };
1905
2034
  export {
1906
2035
  AggLayerSDK,
2036
+ ApiError,
1907
2037
  SDK_MODES
1908
2038
  };
1909
2039
  //# sourceMappingURL=index.mjs.map