@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.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  AggLayerSDK: () => AggLayerSDK,
24
+ ApiError: () => ApiError,
24
25
  SDK_MODES: () => SDK_MODES
25
26
  });
26
27
  module.exports = __toCommonJS(index_exports);
@@ -38,7 +39,33 @@ var ChainRegistry = class _ChainRegistry {
38
39
  }
39
40
  return _ChainRegistry.instance;
40
41
  }
42
+ // DEV: if adding new default chains, also update README.md
41
43
  initializeDefaultChains() {
44
+ this.registerChain({
45
+ chainId: 1,
46
+ networkId: 0,
47
+ name: "Ethereum",
48
+ rpcUrl: "https://eth.llamarpc.com",
49
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
50
+ blockExplorer: { name: "Etherscan", url: "https://etherscan.io" },
51
+ bridgeAddress: "",
52
+ proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/mainnet/",
53
+ isTestnet: false
54
+ });
55
+ this.registerChain({
56
+ chainId: 747474,
57
+ networkId: 20,
58
+ name: "Katana",
59
+ rpcUrl: "https://rpc.katana.network",
60
+ nativeCurrency: { name: "Ethereum", symbol: "ETH", decimals: 18 },
61
+ blockExplorer: {
62
+ name: "Katana Explorer",
63
+ url: "https://katanascan.com"
64
+ },
65
+ bridgeAddress: "",
66
+ proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/mainnet/",
67
+ isTestnet: false
68
+ });
42
69
  this.registerChain({
43
70
  chainId: 11155111,
44
71
  networkId: 0,
@@ -53,20 +80,6 @@ var ChainRegistry = class _ChainRegistry {
53
80
  proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/testnet/",
54
81
  isTestnet: true
55
82
  });
56
- this.registerChain({
57
- chainId: 2442,
58
- networkId: 1,
59
- name: "Polygon Cardona",
60
- rpcUrl: "https://rpc.cardona.zkevm-rpc.com",
61
- nativeCurrency: { name: "Ethereum", symbol: "ETH", decimals: 18 },
62
- blockExplorer: {
63
- name: "Cardona PolygonScan",
64
- url: "https://cardona-zkevm.polygonscan.com"
65
- },
66
- bridgeAddress: "0x528e26b25a34a4A5d0dbDa1d57D318153d2ED582",
67
- proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/testnet/",
68
- isTestnet: true
69
- });
70
83
  }
71
84
  /**
72
85
  * Register a new chain
@@ -1410,14 +1423,16 @@ var ERC20 = class extends BaseContract {
1410
1423
  var import_viem6 = require("viem");
1411
1424
 
1412
1425
  // src/constants.ts
1413
- var NETWORKS = {
1414
- SEPOLIA: 11155111,
1415
- CARDONA: 2442
1416
- };
1417
- var DEFAULT_NETWORK = NETWORKS.SEPOLIA;
1426
+ var ARC_API_BASE_URL = "https://arc-api.polygon.technology";
1427
+ var ARC_API_DEFAULT_TIMEOUT = 3e4;
1418
1428
  var DEFAULT_CHAINS_PER_PAGE = 100;
1419
1429
  var DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE = 1;
1420
1430
  var MAX_TRANSACTIONS_PER_PAGE = 100;
1431
+ var NETWORKS = {
1432
+ ETHEREUM: 1,
1433
+ KATANA: 747474
1434
+ };
1435
+ var DEFAULT_NETWORK = NETWORKS.ETHEREUM;
1421
1436
 
1422
1437
  // src/native/index.ts
1423
1438
  var NativeClient = class {
@@ -1607,9 +1622,6 @@ var HttpClient = class {
1607
1622
  signal: controller.signal
1608
1623
  });
1609
1624
  clearTimeout(timeoutId);
1610
- if (!response.ok) {
1611
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1612
- }
1613
1625
  let data;
1614
1626
  const contentType = response.headers.get("content-type");
1615
1627
  if (contentType?.includes("application/json")) {
@@ -1617,6 +1629,12 @@ var HttpClient = class {
1617
1629
  } else {
1618
1630
  data = await response.text();
1619
1631
  }
1632
+ if (!response.ok) {
1633
+ if (contentType?.includes("application/json")) {
1634
+ } else {
1635
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1636
+ }
1637
+ }
1620
1638
  return {
1621
1639
  data,
1622
1640
  status: response.status,
@@ -1705,7 +1723,7 @@ var ArcApiService = class {
1705
1723
  );
1706
1724
  }
1707
1725
  async buildClaimTransaction(sourceNetworkId, depositCount) {
1708
- return this.httpClient.get("/routes/build-transaction-for-claim", {
1726
+ return this.httpClient.post("/routes/build-transaction-for-claim", {
1709
1727
  sourceNetworkId,
1710
1728
  depositCount
1711
1729
  });
@@ -1716,22 +1734,69 @@ var ArcApiService = class {
1716
1734
  transactionsRequestQueryParams
1717
1735
  });
1718
1736
  }
1737
+ async tokenMappings(tokenMappingQueryParams) {
1738
+ const { tokenAddress } = tokenMappingQueryParams;
1739
+ return this.httpClient.get(`/token-mappings/${tokenAddress}`);
1740
+ }
1741
+ };
1742
+
1743
+ // src/core/utils/apiError.ts
1744
+ var ApiError = class _ApiError extends Error {
1745
+ constructor(errorResponse) {
1746
+ super(errorResponse.message);
1747
+ this.name = errorResponse.name;
1748
+ this.code = errorResponse.code;
1749
+ this.details = errorResponse.details;
1750
+ if (Error.captureStackTrace) {
1751
+ Error.captureStackTrace(this, _ApiError);
1752
+ }
1753
+ }
1754
+ /**
1755
+ * Create API error from error response
1756
+ */
1757
+ static fromErrorResponse(errorResponse) {
1758
+ return new _ApiError(errorResponse);
1759
+ }
1760
+ /**
1761
+ * Create fallback error when API completely fails
1762
+ */
1763
+ static createFallbackError(originalError, operation) {
1764
+ return new _ApiError({
1765
+ status: "error",
1766
+ message: `${operation} failed: ${originalError.message}`,
1767
+ name: "ApiConnectionError",
1768
+ code: 500,
1769
+ details: {
1770
+ originalError: originalError.message,
1771
+ operation
1772
+ }
1773
+ });
1774
+ }
1775
+ /**
1776
+ * Convert to plain object for serialization
1777
+ */
1778
+ toJSON() {
1779
+ return {
1780
+ name: this.name,
1781
+ message: this.message,
1782
+ code: this.code,
1783
+ details: this.details
1784
+ };
1785
+ }
1719
1786
  };
1720
1787
 
1721
1788
  // src/core/index.ts
1722
1789
  var CoreClient = class {
1723
1790
  constructor(config) {
1724
- this.config = config;
1725
- if (!this.config) {
1726
- throw new Error("Config is required");
1727
- }
1728
- if (!this.config.apiBaseUrl) {
1729
- throw new Error("API base URL is required");
1730
- }
1791
+ this.config = {
1792
+ ...config,
1793
+ apiBaseUrl: config?.apiBaseUrl || ARC_API_BASE_URL,
1794
+ apiTimeout: config?.apiTimeout || ARC_API_DEFAULT_TIMEOUT
1795
+ };
1731
1796
  const { apiBaseUrl, apiTimeout } = this.config;
1732
1797
  this.arcApiService = new ArcApiService({
1733
- baseUrl: apiBaseUrl,
1734
- timeout: apiTimeout ?? 3e4
1798
+ baseUrl: apiBaseUrl || ARC_API_BASE_URL,
1799
+ timeout: apiTimeout || ARC_API_DEFAULT_TIMEOUT
1735
1800
  });
1736
1801
  }
1737
1802
  /**
@@ -1741,57 +1806,64 @@ var CoreClient = class {
1741
1806
  * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1742
1807
  */
1743
1808
  async getAllChainsPaginated(params, pageSize = DEFAULT_CHAINS_PER_PAGE) {
1744
- const firstResponse = await this.arcApiService.chains({
1745
- ...params,
1746
- limit: pageSize
1747
- });
1748
- if (firstResponse.data.status !== "success") {
1749
- throw new Error(firstResponse.data.message);
1750
- }
1751
- const firstPageData = firstResponse.data.data;
1752
- const pagination = firstResponse.data.pagination;
1753
- const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1754
- if (!pagination?.total || pagination.total <= pageSize) {
1755
- return {
1756
- chains: firstPageChains
1757
- };
1758
- }
1759
- const totalPages = Math.ceil(pagination.total / pageSize);
1760
- const remainingPages = totalPages - 1;
1761
- if (remainingPages === 0) {
1809
+ try {
1810
+ const firstResponse = await this.arcApiService.chains({
1811
+ ...params,
1812
+ limit: pageSize
1813
+ });
1814
+ if (firstResponse.data.status !== "success") {
1815
+ throw ApiError.fromErrorResponse(firstResponse.data);
1816
+ }
1817
+ const firstPageData = firstResponse.data.data;
1818
+ const pagination = firstResponse.data.pagination;
1819
+ const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1820
+ if (!pagination?.total || pagination.total <= pageSize) {
1821
+ return {
1822
+ chains: firstPageChains
1823
+ };
1824
+ }
1825
+ const totalPages = Math.ceil(pagination.total / pageSize);
1826
+ const remainingPages = totalPages - 1;
1827
+ if (remainingPages === 0) {
1828
+ return {
1829
+ chains: firstPageChains
1830
+ };
1831
+ }
1832
+ const remainingPagePromises = Array.from(
1833
+ { length: remainingPages },
1834
+ (_, index) => {
1835
+ const offset = (index + 1) * pageSize;
1836
+ return this.arcApiService.chains({
1837
+ ...params,
1838
+ limit: pageSize,
1839
+ offset
1840
+ });
1841
+ }
1842
+ );
1843
+ const remainingResponses = await Promise.all(remainingPagePromises);
1844
+ for (const response of remainingResponses) {
1845
+ if (response.data.status !== "success") {
1846
+ throw ApiError.fromErrorResponse(response.data);
1847
+ }
1848
+ }
1849
+ const allChains = [
1850
+ ...firstPageChains,
1851
+ ...remainingResponses.flatMap((response) => {
1852
+ if (response.data.status === "success") {
1853
+ return Array.isArray(response.data.data) ? response.data.data : [];
1854
+ }
1855
+ return [];
1856
+ })
1857
+ ];
1762
1858
  return {
1763
- chains: firstPageChains
1859
+ chains: allChains
1764
1860
  };
1765
- }
1766
- const remainingPagePromises = Array.from(
1767
- { length: remainingPages },
1768
- (_, index) => {
1769
- const offset = (index + 1) * pageSize;
1770
- return this.arcApiService.chains({
1771
- ...params,
1772
- limit: pageSize,
1773
- offset
1774
- });
1775
- }
1776
- );
1777
- const remainingResponses = await Promise.all(remainingPagePromises);
1778
- for (const response of remainingResponses) {
1779
- if (response.data.status !== "success") {
1780
- throw new Error(response.data.message);
1861
+ } catch (error) {
1862
+ if (error instanceof ApiError) {
1863
+ throw error;
1781
1864
  }
1865
+ throw ApiError.createFallbackError(error, "Get chains metadata");
1782
1866
  }
1783
- const allChains = [
1784
- ...firstPageChains,
1785
- ...remainingResponses.flatMap((response) => {
1786
- if (response.data.status === "success") {
1787
- return Array.isArray(response.data.data) ? response.data.data : [];
1788
- }
1789
- return [];
1790
- })
1791
- ];
1792
- return {
1793
- chains: allChains
1794
- };
1795
1867
  }
1796
1868
  /**
1797
1869
  * Get all chains metadata from AggLayer API
@@ -1835,11 +1907,18 @@ var CoreClient = class {
1835
1907
  * Get all routes from AggLayer API
1836
1908
  */
1837
1909
  async getRoutes(routesRequestParams) {
1838
- const response = await this.arcApiService.routes(routesRequestParams);
1839
- if (response.data.status === "success") {
1840
- return response.data.data;
1910
+ try {
1911
+ const response = await this.arcApiService.routes(routesRequestParams);
1912
+ if (response.data.status === "success") {
1913
+ return response.data.data;
1914
+ }
1915
+ throw ApiError.fromErrorResponse(response.data);
1916
+ } catch (error) {
1917
+ if (error instanceof ApiError) {
1918
+ throw error;
1919
+ }
1920
+ throw ApiError.createFallbackError(error, "Get routes");
1841
1921
  }
1842
- throw new Error(response.data.message);
1843
1922
  }
1844
1923
  /**
1845
1924
  * Get calldata from a route
@@ -1851,13 +1930,28 @@ var CoreClient = class {
1851
1930
  return route.transactionRequest;
1852
1931
  }
1853
1932
  if (route.steps.length === 0 || !route.steps[0]) {
1854
- throw new Error("Route has no steps to build transaction from");
1933
+ throw ApiError.createFallbackError(
1934
+ new Error("Route has no steps to build transaction from"),
1935
+ "Get unsigned transaction"
1936
+ );
1855
1937
  }
1856
- const response = await this.arcApiService.buildTransaction(route.steps[0]);
1857
- if (response.data.status === "success") {
1858
- return response.data.data;
1938
+ try {
1939
+ const response = await this.arcApiService.buildTransaction(
1940
+ route.steps[0]
1941
+ );
1942
+ if (response.data.status === "success") {
1943
+ return response.data.data;
1944
+ }
1945
+ throw ApiError.fromErrorResponse(response.data);
1946
+ } catch (error) {
1947
+ if (error instanceof ApiError) {
1948
+ throw error;
1949
+ }
1950
+ throw ApiError.createFallbackError(
1951
+ error,
1952
+ "Get unsigned transaction"
1953
+ );
1859
1954
  }
1860
- throw new Error(response.data.message);
1861
1955
  }
1862
1956
  /**
1863
1957
  * Get calldata for claim step
@@ -1869,53 +1963,93 @@ var CoreClient = class {
1869
1963
  * @param depositCount - The deposit count associated with the transfer.
1870
1964
  */
1871
1965
  async getClaimUnsignedTransaction(buildClaimTxParams) {
1872
- const response = await this.arcApiService.buildClaimTransaction(
1873
- buildClaimTxParams.sourceNetworkId,
1874
- buildClaimTxParams.depositCount
1875
- );
1876
- if (response.data.status === "success") {
1877
- return response.data.data;
1966
+ try {
1967
+ const response = await this.arcApiService.buildClaimTransaction(
1968
+ buildClaimTxParams.sourceNetworkId,
1969
+ buildClaimTxParams.depositCount
1970
+ );
1971
+ if (response.data.status === "success") {
1972
+ return response.data.data;
1973
+ }
1974
+ throw ApiError.fromErrorResponse(response.data);
1975
+ } catch (error) {
1976
+ if (error instanceof ApiError) {
1977
+ throw error;
1978
+ }
1979
+ throw ApiError.createFallbackError(
1980
+ error,
1981
+ "Get claim unsigned transaction"
1982
+ );
1878
1983
  }
1879
- throw new Error(response.data.message);
1880
1984
  }
1881
1985
  /**
1882
1986
  * Get all transactions via web sockets
1883
1987
  */
1884
1988
  async getTransactions(transactionsRequestQueryParams) {
1885
1989
  if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1886
- throw new Error(
1887
- `Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`
1990
+ throw ApiError.createFallbackError(
1991
+ new Error(`Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`),
1992
+ "Get transactions"
1888
1993
  );
1889
1994
  }
1890
- const response = await this.arcApiService.transactions(
1891
- transactionsRequestQueryParams
1892
- );
1893
- if (response.data.status === "success") {
1894
- return response.data.data;
1995
+ try {
1996
+ const response = await this.arcApiService.transactions(
1997
+ transactionsRequestQueryParams
1998
+ );
1999
+ if (response.data.status === "success") {
2000
+ return response.data.data;
2001
+ }
2002
+ throw ApiError.fromErrorResponse(response.data);
2003
+ } catch (error) {
2004
+ if (error instanceof ApiError) {
2005
+ throw error;
2006
+ }
2007
+ throw ApiError.createFallbackError(error, "Get transactions");
2008
+ }
2009
+ }
2010
+ /**
2011
+ * Get token mappings by token address
2012
+ * @developer Note: Do not misinterpret network ID as chain ID.
2013
+ *
2014
+ * @param tokenAddress
2015
+ */
2016
+ async getTokenMappings(tokenMappingQueryParams) {
2017
+ try {
2018
+ const response = await this.arcApiService.tokenMappings(
2019
+ tokenMappingQueryParams
2020
+ );
2021
+ if (response.data.status === "success") {
2022
+ return response.data.data;
2023
+ }
2024
+ throw ApiError.fromErrorResponse(response.data);
2025
+ } catch (error) {
2026
+ if (error instanceof ApiError) {
2027
+ throw error;
2028
+ }
2029
+ throw ApiError.createFallbackError(
2030
+ error,
2031
+ "Get custom token mappings"
2032
+ );
1895
2033
  }
1896
- throw new Error(response.data.message);
1897
2034
  }
1898
2035
  };
1899
2036
 
1900
2037
  // src/index.ts
2038
+ var defaultConfig = {
2039
+ mode: [SDK_MODES.CORE]
2040
+ };
1901
2041
  var AggLayerSDK = class {
1902
- constructor(config) {
2042
+ constructor(config = defaultConfig) {
1903
2043
  this.config = config;
1904
2044
  if (!config.mode || config.mode && config.mode.length === 0) {
1905
2045
  this.config.mode = ["CORE"];
1906
2046
  }
1907
- if (config.mode.includes(SDK_MODES.CORE)) {
1908
- if (!this.config.core) {
1909
- throw new Error("Core config is required");
1910
- }
1911
- this.core = new CoreClient(this.config.core);
2047
+ if (this.config.mode?.includes(SDK_MODES.CORE)) {
2048
+ this.core = new CoreClient(this.config?.core);
1912
2049
  }
1913
2050
  if (this.config.mode?.includes(SDK_MODES.NATIVE)) {
1914
- if (!this.config.native) {
1915
- throw new Error("NATIVE config is required");
1916
- }
1917
2051
  const nativeConfig = {
1918
- defaultNetwork: this.config.native?.defaultNetwork || DEFAULT_NETWORK,
2052
+ defaultNetwork: this.config.native?.defaultNetwork ?? DEFAULT_NETWORK,
1919
2053
  ...this.config.native?.chains && {
1920
2054
  chains: this.config.native.chains
1921
2055
  },
@@ -1950,6 +2084,7 @@ var AggLayerSDK = class {
1950
2084
  // Annotate the CommonJS export names for ESM import in node:
1951
2085
  0 && (module.exports = {
1952
2086
  AggLayerSDK,
2087
+ ApiError,
1953
2088
  SDK_MODES
1954
2089
  });
1955
2090
  //# sourceMappingURL=index.js.map