@agglayer/sdk 1.0.0-beta.17 → 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,
@@ -1685,7 +1702,7 @@ var ArcApiService = class {
1685
1702
  );
1686
1703
  }
1687
1704
  async buildClaimTransaction(sourceNetworkId, depositCount) {
1688
- return this.httpClient.get("/routes/build-transaction-for-claim", {
1705
+ return this.httpClient.post("/routes/build-transaction-for-claim", {
1689
1706
  sourceNetworkId,
1690
1707
  depositCount
1691
1708
  });
@@ -1698,20 +1715,63 @@ var ArcApiService = class {
1698
1715
  }
1699
1716
  };
1700
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
+
1701
1763
  // src/core/index.ts
1702
1764
  var CoreClient = class {
1703
1765
  constructor(config) {
1704
- this.config = config;
1705
- if (!this.config) {
1706
- throw new Error("Config is required");
1707
- }
1708
- if (!this.config.apiBaseUrl) {
1709
- throw new Error("API base URL is required");
1710
- }
1766
+ this.config = {
1767
+ ...config,
1768
+ apiBaseUrl: config?.apiBaseUrl || ARC_API_BASE_URL,
1769
+ apiTimeout: config?.apiTimeout || ARC_API_DEFAULT_TIMEOUT
1770
+ };
1711
1771
  const { apiBaseUrl, apiTimeout } = this.config;
1712
1772
  this.arcApiService = new ArcApiService({
1713
- baseUrl: apiBaseUrl,
1714
- timeout: apiTimeout ?? 3e4
1773
+ baseUrl: apiBaseUrl || ARC_API_BASE_URL,
1774
+ timeout: apiTimeout || ARC_API_DEFAULT_TIMEOUT
1715
1775
  });
1716
1776
  }
1717
1777
  /**
@@ -1721,57 +1781,64 @@ var CoreClient = class {
1721
1781
  * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1722
1782
  */
1723
1783
  async getAllChainsPaginated(params, pageSize = DEFAULT_CHAINS_PER_PAGE) {
1724
- const firstResponse = await this.arcApiService.chains({
1725
- ...params,
1726
- limit: pageSize
1727
- });
1728
- if (firstResponse.data.status !== "success") {
1729
- throw new Error(firstResponse.data.message);
1730
- }
1731
- const firstPageData = firstResponse.data.data;
1732
- const pagination = firstResponse.data.pagination;
1733
- const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1734
- if (!pagination?.total || pagination.total <= pageSize) {
1735
- return {
1736
- chains: firstPageChains
1737
- };
1738
- }
1739
- const totalPages = Math.ceil(pagination.total / pageSize);
1740
- const remainingPages = totalPages - 1;
1741
- 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
+ ];
1742
1833
  return {
1743
- chains: firstPageChains
1834
+ chains: allChains
1744
1835
  };
1745
- }
1746
- const remainingPagePromises = Array.from(
1747
- { length: remainingPages },
1748
- (_, index) => {
1749
- const offset = (index + 1) * pageSize;
1750
- return this.arcApiService.chains({
1751
- ...params,
1752
- limit: pageSize,
1753
- offset
1754
- });
1755
- }
1756
- );
1757
- const remainingResponses = await Promise.all(remainingPagePromises);
1758
- for (const response of remainingResponses) {
1759
- if (response.data.status !== "success") {
1760
- throw new Error(response.data.message);
1836
+ } catch (error) {
1837
+ if (error instanceof ApiError) {
1838
+ throw error;
1761
1839
  }
1840
+ throw ApiError.createFallbackError(error, "Get chains metadata");
1762
1841
  }
1763
- const allChains = [
1764
- ...firstPageChains,
1765
- ...remainingResponses.flatMap((response) => {
1766
- if (response.data.status === "success") {
1767
- return Array.isArray(response.data.data) ? response.data.data : [];
1768
- }
1769
- return [];
1770
- })
1771
- ];
1772
- return {
1773
- chains: allChains
1774
- };
1775
1842
  }
1776
1843
  /**
1777
1844
  * Get all chains metadata from AggLayer API
@@ -1815,11 +1882,18 @@ var CoreClient = class {
1815
1882
  * Get all routes from AggLayer API
1816
1883
  */
1817
1884
  async getRoutes(routesRequestParams) {
1818
- const response = await this.arcApiService.routes(routesRequestParams);
1819
- if (response.data.status === "success") {
1820
- 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");
1821
1896
  }
1822
- throw new Error(response.data.message);
1823
1897
  }
1824
1898
  /**
1825
1899
  * Get calldata from a route
@@ -1831,13 +1905,28 @@ var CoreClient = class {
1831
1905
  return route.transactionRequest;
1832
1906
  }
1833
1907
  if (route.steps.length === 0 || !route.steps[0]) {
1834
- 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
+ );
1835
1912
  }
1836
- const response = await this.arcApiService.buildTransaction(route.steps[0]);
1837
- if (response.data.status === "success") {
1838
- 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
+ );
1839
1929
  }
1840
- throw new Error(response.data.message);
1841
1930
  }
1842
1931
  /**
1843
1932
  * Get calldata for claim step
@@ -1849,53 +1938,68 @@ var CoreClient = class {
1849
1938
  * @param depositCount - The deposit count associated with the transfer.
1850
1939
  */
1851
1940
  async getClaimUnsignedTransaction(buildClaimTxParams) {
1852
- const response = await this.arcApiService.buildClaimTransaction(
1853
- buildClaimTxParams.sourceNetworkId,
1854
- buildClaimTxParams.depositCount
1855
- );
1856
- if (response.data.status === "success") {
1857
- return response.data.data;
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
+ );
1858
1958
  }
1859
- throw new Error(response.data.message);
1860
1959
  }
1861
1960
  /**
1862
1961
  * Get all transactions via web sockets
1863
1962
  */
1864
1963
  async getTransactions(transactionsRequestQueryParams) {
1865
1964
  if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1866
- throw new Error(
1867
- `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"
1868
1968
  );
1869
1969
  }
1870
- const response = await this.arcApiService.transactions(
1871
- transactionsRequestQueryParams
1872
- );
1873
- if (response.data.status === "success") {
1874
- 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");
1875
1983
  }
1876
- throw new Error(response.data.message);
1877
1984
  }
1878
1985
  };
1879
1986
 
1880
1987
  // src/index.ts
1988
+ var defaultConfig = {
1989
+ mode: [SDK_MODES.CORE]
1990
+ };
1881
1991
  var AggLayerSDK = class {
1882
- constructor(config) {
1992
+ constructor(config = defaultConfig) {
1883
1993
  this.config = config;
1884
1994
  if (!config.mode || config.mode && config.mode.length === 0) {
1885
1995
  this.config.mode = ["CORE"];
1886
1996
  }
1887
- if (config.mode.includes(SDK_MODES.CORE)) {
1888
- if (!this.config.core) {
1889
- throw new Error("Core config is required");
1890
- }
1891
- this.core = new CoreClient(this.config.core);
1997
+ if (this.config.mode?.includes(SDK_MODES.CORE)) {
1998
+ this.core = new CoreClient(this.config?.core);
1892
1999
  }
1893
2000
  if (this.config.mode?.includes(SDK_MODES.NATIVE)) {
1894
- if (!this.config.native) {
1895
- throw new Error("NATIVE config is required");
1896
- }
1897
2001
  const nativeConfig = {
1898
- defaultNetwork: this.config.native?.defaultNetwork || DEFAULT_NETWORK,
2002
+ defaultNetwork: this.config.native?.defaultNetwork ?? DEFAULT_NETWORK,
1899
2003
  ...this.config.native?.chains && {
1900
2004
  chains: this.config.native.chains
1901
2005
  },
@@ -1929,6 +2033,7 @@ var AggLayerSDK = class {
1929
2033
  };
1930
2034
  export {
1931
2035
  AggLayerSDK,
2036
+ ApiError,
1932
2037
  SDK_MODES
1933
2038
  };
1934
2039
  //# sourceMappingURL=index.mjs.map