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