@agglayer/sdk 1.0.0-beta.17 → 1.0.0-beta.19

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
  });
@@ -1696,22 +1713,69 @@ var ArcApiService = class {
1696
1713
  transactionsRequestQueryParams
1697
1714
  });
1698
1715
  }
1716
+ async tokenMappings(tokenMappingQueryParams) {
1717
+ const { tokenAddress } = tokenMappingQueryParams;
1718
+ return this.httpClient.get(`/token-mappings/${tokenAddress}`);
1719
+ }
1720
+ };
1721
+
1722
+ // src/core/utils/apiError.ts
1723
+ var ApiError = class _ApiError extends Error {
1724
+ constructor(errorResponse) {
1725
+ super(errorResponse.message);
1726
+ this.name = errorResponse.name;
1727
+ this.code = errorResponse.code;
1728
+ this.details = errorResponse.details;
1729
+ if (Error.captureStackTrace) {
1730
+ Error.captureStackTrace(this, _ApiError);
1731
+ }
1732
+ }
1733
+ /**
1734
+ * Create API error from error response
1735
+ */
1736
+ static fromErrorResponse(errorResponse) {
1737
+ return new _ApiError(errorResponse);
1738
+ }
1739
+ /**
1740
+ * Create fallback error when API completely fails
1741
+ */
1742
+ static createFallbackError(originalError, operation) {
1743
+ return new _ApiError({
1744
+ status: "error",
1745
+ message: `${operation} failed: ${originalError.message}`,
1746
+ name: "ApiConnectionError",
1747
+ code: 500,
1748
+ details: {
1749
+ originalError: originalError.message,
1750
+ operation
1751
+ }
1752
+ });
1753
+ }
1754
+ /**
1755
+ * Convert to plain object for serialization
1756
+ */
1757
+ toJSON() {
1758
+ return {
1759
+ name: this.name,
1760
+ message: this.message,
1761
+ code: this.code,
1762
+ details: this.details
1763
+ };
1764
+ }
1699
1765
  };
1700
1766
 
1701
1767
  // src/core/index.ts
1702
1768
  var CoreClient = class {
1703
1769
  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
- }
1770
+ this.config = {
1771
+ ...config,
1772
+ apiBaseUrl: config?.apiBaseUrl || ARC_API_BASE_URL,
1773
+ apiTimeout: config?.apiTimeout || ARC_API_DEFAULT_TIMEOUT
1774
+ };
1711
1775
  const { apiBaseUrl, apiTimeout } = this.config;
1712
1776
  this.arcApiService = new ArcApiService({
1713
- baseUrl: apiBaseUrl,
1714
- timeout: apiTimeout ?? 3e4
1777
+ baseUrl: apiBaseUrl || ARC_API_BASE_URL,
1778
+ timeout: apiTimeout || ARC_API_DEFAULT_TIMEOUT
1715
1779
  });
1716
1780
  }
1717
1781
  /**
@@ -1721,57 +1785,64 @@ var CoreClient = class {
1721
1785
  * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1722
1786
  */
1723
1787
  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) {
1788
+ try {
1789
+ const firstResponse = await this.arcApiService.chains({
1790
+ ...params,
1791
+ limit: pageSize
1792
+ });
1793
+ if (firstResponse.data.status !== "success") {
1794
+ throw ApiError.fromErrorResponse(firstResponse.data);
1795
+ }
1796
+ const firstPageData = firstResponse.data.data;
1797
+ const pagination = firstResponse.data.pagination;
1798
+ const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1799
+ if (!pagination?.total || pagination.total <= pageSize) {
1800
+ return {
1801
+ chains: firstPageChains
1802
+ };
1803
+ }
1804
+ const totalPages = Math.ceil(pagination.total / pageSize);
1805
+ const remainingPages = totalPages - 1;
1806
+ if (remainingPages === 0) {
1807
+ return {
1808
+ chains: firstPageChains
1809
+ };
1810
+ }
1811
+ const remainingPagePromises = Array.from(
1812
+ { length: remainingPages },
1813
+ (_, index) => {
1814
+ const offset = (index + 1) * pageSize;
1815
+ return this.arcApiService.chains({
1816
+ ...params,
1817
+ limit: pageSize,
1818
+ offset
1819
+ });
1820
+ }
1821
+ );
1822
+ const remainingResponses = await Promise.all(remainingPagePromises);
1823
+ for (const response of remainingResponses) {
1824
+ if (response.data.status !== "success") {
1825
+ throw ApiError.fromErrorResponse(response.data);
1826
+ }
1827
+ }
1828
+ const allChains = [
1829
+ ...firstPageChains,
1830
+ ...remainingResponses.flatMap((response) => {
1831
+ if (response.data.status === "success") {
1832
+ return Array.isArray(response.data.data) ? response.data.data : [];
1833
+ }
1834
+ return [];
1835
+ })
1836
+ ];
1742
1837
  return {
1743
- chains: firstPageChains
1838
+ chains: allChains
1744
1839
  };
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);
1840
+ } catch (error) {
1841
+ if (error instanceof ApiError) {
1842
+ throw error;
1761
1843
  }
1844
+ throw ApiError.createFallbackError(error, "Get chains metadata");
1762
1845
  }
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
1846
  }
1776
1847
  /**
1777
1848
  * Get all chains metadata from AggLayer API
@@ -1815,11 +1886,18 @@ var CoreClient = class {
1815
1886
  * Get all routes from AggLayer API
1816
1887
  */
1817
1888
  async getRoutes(routesRequestParams) {
1818
- const response = await this.arcApiService.routes(routesRequestParams);
1819
- if (response.data.status === "success") {
1820
- return response.data.data;
1889
+ try {
1890
+ const response = await this.arcApiService.routes(routesRequestParams);
1891
+ if (response.data.status === "success") {
1892
+ return response.data.data;
1893
+ }
1894
+ throw ApiError.fromErrorResponse(response.data);
1895
+ } catch (error) {
1896
+ if (error instanceof ApiError) {
1897
+ throw error;
1898
+ }
1899
+ throw ApiError.createFallbackError(error, "Get routes");
1821
1900
  }
1822
- throw new Error(response.data.message);
1823
1901
  }
1824
1902
  /**
1825
1903
  * Get calldata from a route
@@ -1831,13 +1909,28 @@ var CoreClient = class {
1831
1909
  return route.transactionRequest;
1832
1910
  }
1833
1911
  if (route.steps.length === 0 || !route.steps[0]) {
1834
- throw new Error("Route has no steps to build transaction from");
1912
+ throw ApiError.createFallbackError(
1913
+ new Error("Route has no steps to build transaction from"),
1914
+ "Get unsigned transaction"
1915
+ );
1835
1916
  }
1836
- const response = await this.arcApiService.buildTransaction(route.steps[0]);
1837
- if (response.data.status === "success") {
1838
- return response.data.data;
1917
+ try {
1918
+ const response = await this.arcApiService.buildTransaction(
1919
+ route.steps[0]
1920
+ );
1921
+ if (response.data.status === "success") {
1922
+ return response.data.data;
1923
+ }
1924
+ throw ApiError.fromErrorResponse(response.data);
1925
+ } catch (error) {
1926
+ if (error instanceof ApiError) {
1927
+ throw error;
1928
+ }
1929
+ throw ApiError.createFallbackError(
1930
+ error,
1931
+ "Get unsigned transaction"
1932
+ );
1839
1933
  }
1840
- throw new Error(response.data.message);
1841
1934
  }
1842
1935
  /**
1843
1936
  * Get calldata for claim step
@@ -1849,53 +1942,93 @@ var CoreClient = class {
1849
1942
  * @param depositCount - The deposit count associated with the transfer.
1850
1943
  */
1851
1944
  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;
1945
+ try {
1946
+ const response = await this.arcApiService.buildClaimTransaction(
1947
+ buildClaimTxParams.sourceNetworkId,
1948
+ buildClaimTxParams.depositCount
1949
+ );
1950
+ if (response.data.status === "success") {
1951
+ return response.data.data;
1952
+ }
1953
+ throw ApiError.fromErrorResponse(response.data);
1954
+ } catch (error) {
1955
+ if (error instanceof ApiError) {
1956
+ throw error;
1957
+ }
1958
+ throw ApiError.createFallbackError(
1959
+ error,
1960
+ "Get claim unsigned transaction"
1961
+ );
1858
1962
  }
1859
- throw new Error(response.data.message);
1860
1963
  }
1861
1964
  /**
1862
1965
  * Get all transactions via web sockets
1863
1966
  */
1864
1967
  async getTransactions(transactionsRequestQueryParams) {
1865
1968
  if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1866
- throw new Error(
1867
- `Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`
1969
+ throw ApiError.createFallbackError(
1970
+ new Error(`Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`),
1971
+ "Get transactions"
1868
1972
  );
1869
1973
  }
1870
- const response = await this.arcApiService.transactions(
1871
- transactionsRequestQueryParams
1872
- );
1873
- if (response.data.status === "success") {
1874
- return response.data.data;
1974
+ try {
1975
+ const response = await this.arcApiService.transactions(
1976
+ transactionsRequestQueryParams
1977
+ );
1978
+ if (response.data.status === "success") {
1979
+ return response.data.data;
1980
+ }
1981
+ throw ApiError.fromErrorResponse(response.data);
1982
+ } catch (error) {
1983
+ if (error instanceof ApiError) {
1984
+ throw error;
1985
+ }
1986
+ throw ApiError.createFallbackError(error, "Get transactions");
1987
+ }
1988
+ }
1989
+ /**
1990
+ * Get token mappings by token address
1991
+ * @developer Note: Do not misinterpret network ID as chain ID.
1992
+ *
1993
+ * @param tokenAddress
1994
+ */
1995
+ async getTokenMappings(tokenMappingQueryParams) {
1996
+ try {
1997
+ const response = await this.arcApiService.tokenMappings(
1998
+ tokenMappingQueryParams
1999
+ );
2000
+ if (response.data.status === "success") {
2001
+ return response.data.data;
2002
+ }
2003
+ throw ApiError.fromErrorResponse(response.data);
2004
+ } catch (error) {
2005
+ if (error instanceof ApiError) {
2006
+ throw error;
2007
+ }
2008
+ throw ApiError.createFallbackError(
2009
+ error,
2010
+ "Get custom token mappings"
2011
+ );
1875
2012
  }
1876
- throw new Error(response.data.message);
1877
2013
  }
1878
2014
  };
1879
2015
 
1880
2016
  // src/index.ts
2017
+ var defaultConfig = {
2018
+ mode: [SDK_MODES.CORE]
2019
+ };
1881
2020
  var AggLayerSDK = class {
1882
- constructor(config) {
2021
+ constructor(config = defaultConfig) {
1883
2022
  this.config = config;
1884
2023
  if (!config.mode || config.mode && config.mode.length === 0) {
1885
2024
  this.config.mode = ["CORE"];
1886
2025
  }
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);
2026
+ if (this.config.mode?.includes(SDK_MODES.CORE)) {
2027
+ this.core = new CoreClient(this.config?.core);
1892
2028
  }
1893
2029
  if (this.config.mode?.includes(SDK_MODES.NATIVE)) {
1894
- if (!this.config.native) {
1895
- throw new Error("NATIVE config is required");
1896
- }
1897
2030
  const nativeConfig = {
1898
- defaultNetwork: this.config.native?.defaultNetwork || DEFAULT_NETWORK,
2031
+ defaultNetwork: this.config.native?.defaultNetwork ?? DEFAULT_NETWORK,
1899
2032
  ...this.config.native?.chains && {
1900
2033
  chains: this.config.native.chains
1901
2034
  },
@@ -1929,6 +2062,7 @@ var AggLayerSDK = class {
1929
2062
  };
1930
2063
  export {
1931
2064
  AggLayerSDK,
2065
+ ApiError,
1932
2066
  SDK_MODES
1933
2067
  };
1934
2068
  //# sourceMappingURL=index.mjs.map