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

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,7 +21,6 @@ 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,
25
24
  SDK_MODES: () => SDK_MODES
26
25
  });
27
26
  module.exports = __toCommonJS(index_exports);
@@ -39,33 +38,7 @@ var ChainRegistry = class _ChainRegistry {
39
38
  }
40
39
  return _ChainRegistry.instance;
41
40
  }
42
- // DEV: if adding new default chains, also update README.md
43
41
  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
- });
69
42
  this.registerChain({
70
43
  chainId: 11155111,
71
44
  networkId: 0,
@@ -80,6 +53,20 @@ var ChainRegistry = class _ChainRegistry {
80
53
  proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/testnet/",
81
54
  isTestnet: true
82
55
  });
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
+ });
83
70
  }
84
71
  /**
85
72
  * Register a new chain
@@ -1423,16 +1410,11 @@ var ERC20 = class extends BaseContract {
1423
1410
  var import_viem6 = require("viem");
1424
1411
 
1425
1412
  // src/constants.ts
1426
- var ARC_API_BASE_URL = "https://arc-api.polygon.technology";
1427
- var ARC_API_DEFAULT_TIMEOUT = 3e4;
1428
- var DEFAULT_CHAINS_PER_PAGE = 100;
1429
- var DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE = 1;
1430
- var MAX_TRANSACTIONS_PER_PAGE = 100;
1431
1413
  var NETWORKS = {
1432
- ETHEREUM: 1,
1433
- KATANA: 747474
1414
+ SEPOLIA: 11155111,
1415
+ CARDONA: 2442
1434
1416
  };
1435
- var DEFAULT_NETWORK = NETWORKS.ETHEREUM;
1417
+ var DEFAULT_NETWORK = NETWORKS.SEPOLIA;
1436
1418
 
1437
1419
  // src/native/index.ts
1438
1420
  var NativeClient = class {
@@ -1622,6 +1604,9 @@ var HttpClient = class {
1622
1604
  signal: controller.signal
1623
1605
  });
1624
1606
  clearTimeout(timeoutId);
1607
+ if (!response.ok) {
1608
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1609
+ }
1625
1610
  let data;
1626
1611
  const contentType = response.headers.get("content-type");
1627
1612
  if (contentType?.includes("application/json")) {
@@ -1629,12 +1614,6 @@ var HttpClient = class {
1629
1614
  } else {
1630
1615
  data = await response.text();
1631
1616
  }
1632
- if (!response.ok) {
1633
- if (contentType?.includes("application/json")) {
1634
- } else {
1635
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1636
- }
1637
- }
1638
1617
  return {
1639
1618
  data,
1640
1619
  status: response.status,
@@ -1664,12 +1643,11 @@ var HttpClient = class {
1664
1643
  continue;
1665
1644
  }
1666
1645
  if (Array.isArray(value)) {
1667
- const filteredValues = value.filter(
1668
- (item) => item !== void 0 && item !== null
1669
- );
1670
- if (filteredValues.length > 0) {
1671
- result[fullKey] = filteredValues.join(",");
1672
- }
1646
+ value.forEach((item, index) => {
1647
+ if (item !== void 0 && item !== null) {
1648
+ result[`${fullKey}[${index}]`] = String(item);
1649
+ }
1650
+ });
1673
1651
  } else if (typeof value === "object") {
1674
1652
  Object.assign(
1675
1653
  result,
@@ -1699,17 +1677,16 @@ var ArcApiService = class {
1699
1677
  this.httpClient = new HttpClient({ baseUrl, timeout });
1700
1678
  }
1701
1679
  // responsible for both chains metadata and tokens
1702
- // supports limit/offset based pagination
1703
1680
  async chains({
1704
1681
  withSupportedTokens = false,
1705
- limit = 10,
1706
- offset,
1682
+ limit = 20,
1683
+ startAfter = 0,
1707
1684
  chainIds
1708
1685
  } = {}) {
1709
1686
  return this.httpClient.get("/metadata/chains", {
1710
1687
  withSupportedTokens,
1711
1688
  limit,
1712
- offset,
1689
+ startAfter,
1713
1690
  chainIds
1714
1691
  });
1715
1692
  }
@@ -1718,17 +1695,10 @@ var ArcApiService = class {
1718
1695
  }
1719
1696
  async buildTransaction(builtTransactionRequestBody) {
1720
1697
  return this.httpClient.post(
1721
- "/routes/build-transaction",
1698
+ "/build-transaction",
1722
1699
  builtTransactionRequestBody
1723
1700
  );
1724
1701
  }
1725
- async buildClaimTransaction(sourceNetworkId, depositCount) {
1726
- return this.httpClient.post("/routes/build-transaction-for-claim", {
1727
- sourceNetworkId,
1728
- depositCount
1729
- });
1730
- }
1731
- // supports cursor based pagination only
1732
1702
  async transactions(transactionsRequestQueryParams) {
1733
1703
  return this.httpClient.get("/transactions", {
1734
1704
  transactionsRequestQueryParams
@@ -1736,291 +1706,124 @@ var ArcApiService = class {
1736
1706
  }
1737
1707
  };
1738
1708
 
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
-
1784
1709
  // src/core/index.ts
1785
1710
  var CoreClient = class {
1786
1711
  constructor(config) {
1787
- this.config = {
1788
- ...config,
1789
- apiBaseUrl: config?.apiBaseUrl || ARC_API_BASE_URL,
1790
- apiTimeout: config?.apiTimeout || ARC_API_DEFAULT_TIMEOUT
1791
- };
1712
+ this.config = config;
1713
+ if (!this.config) {
1714
+ throw new Error("Config is required");
1715
+ }
1716
+ if (!this.config.apiBaseUrl) {
1717
+ throw new Error("API base URL is required");
1718
+ }
1792
1719
  const { apiBaseUrl, apiTimeout } = this.config;
1793
1720
  this.arcApiService = new ArcApiService({
1794
- baseUrl: apiBaseUrl || ARC_API_BASE_URL,
1795
- timeout: apiTimeout || ARC_API_DEFAULT_TIMEOUT
1721
+ baseUrl: apiBaseUrl,
1722
+ timeout: apiTimeout ?? 3e4
1796
1723
  });
1797
1724
  }
1798
- /**
1799
- * Generic pagination helper for chains API calls (limit and offset based pagination)
1800
- * Handles automatic pagination to fetch all available data
1801
- * @param params - Parameters for the chains API call
1802
- * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1803
- */
1804
- async getAllChainsPaginated(params, pageSize = DEFAULT_CHAINS_PER_PAGE) {
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
- ];
1854
- return {
1855
- chains: allChains
1856
- };
1857
- } catch (error) {
1858
- if (error instanceof ApiError) {
1859
- throw error;
1860
- }
1861
- throw ApiError.createFallbackError(error, "Get chains metadata");
1862
- }
1863
- }
1864
1725
  /**
1865
1726
  * Get all chains metadata from AggLayer API
1866
- * Handles pagination automatically to fetch all available chains
1867
1727
  */
1868
1728
  async getAllChains() {
1869
- return this.getAllChainsPaginated({});
1729
+ const response = await this.arcApiService.chains();
1730
+ if (response.data.status === "success") {
1731
+ return response.data.data;
1732
+ }
1733
+ throw new Error(response.data.message);
1870
1734
  }
1871
1735
  /**
1872
1736
  * Get chain metadata by id from AggLayer API
1873
- * Handles pagination automatically to fetch all available chain metadata
1874
1737
  * @param ids - the ids of the chains to get metadata for
1875
1738
  */
1876
1739
  async getChainMetadataByChainIds(ids) {
1877
- return this.getAllChainsPaginated({ chainIds: ids });
1740
+ const response = await this.arcApiService.chains({ chainIds: ids });
1741
+ if (response.data.status === "success") {
1742
+ return response.data.data;
1743
+ }
1744
+ throw new Error(response.data.message);
1878
1745
  }
1879
1746
  /**
1880
1747
  * Get all tokens from AggLayer API
1881
- *
1882
- * Developer Note: This method is not recommended to use frequently or from frontend.
1883
- * As it can be very slow and resource intensive.
1884
- * It is recommended to use getChainDataAndTokensByChainIds instead.
1885
- */
1886
- async getAllTokens() {
1887
- return this.getAllChainsPaginated(
1888
- { withSupportedTokens: true },
1889
- DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE
1890
- );
1748
+ */
1749
+ async getTokens() {
1750
+ const response = await this.arcApiService.chains({
1751
+ withSupportedTokens: true
1752
+ });
1753
+ if (response.data.status === "success") {
1754
+ return response.data.data;
1755
+ }
1756
+ throw new Error(response.data.message);
1891
1757
  }
1892
1758
  /**
1893
1759
  * Get chain data and tokens by AggLayer API
1894
1760
  * @param ids - the ids of the chains to get data and tokens for
1895
1761
  */
1896
1762
  async getChainDataAndTokensByChainIds(ids) {
1897
- return this.getAllChainsPaginated({
1763
+ const response = await this.arcApiService.chains({
1898
1764
  chainIds: ids,
1899
1765
  withSupportedTokens: true
1900
1766
  });
1767
+ if (response.data.status === "success") {
1768
+ return response.data.data;
1769
+ }
1770
+ throw new Error(response.data.message);
1901
1771
  }
1902
1772
  /**
1903
1773
  * Get all routes from AggLayer API
1904
1774
  */
1905
1775
  async getRoutes(routesRequestParams) {
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");
1917
- }
1918
- }
1919
- /**
1920
- * Get calldata from a route
1921
- * If route has transactionRequest field, return it directly as calldata
1922
- * Otherwise, call buildTransaction on route.steps[0] and return that as calldata
1923
- */
1924
- async getUnsignedTransaction(route) {
1925
- if (route.transactionRequest) {
1926
- return route.transactionRequest;
1927
- }
1928
- if (route.steps.length === 0 || !route.steps[0]) {
1929
- throw ApiError.createFallbackError(
1930
- new Error("Route has no steps to build transaction from"),
1931
- "Get unsigned transaction"
1932
- );
1933
- }
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
- );
1776
+ const response = await this.arcApiService.routes(routesRequestParams);
1777
+ if (response.data.status === "success") {
1778
+ return response.data.data;
1950
1779
  }
1780
+ throw new Error(response.data.message);
1951
1781
  }
1952
1782
  /**
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.
1783
+ * Build transaction from a step object
1960
1784
  */
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
- );
1785
+ async buildTransaction(builtTransactionRequestBody) {
1786
+ const response = await this.arcApiService.buildTransaction(
1787
+ builtTransactionRequestBody
1788
+ );
1789
+ if (response.data.status === "success") {
1790
+ return response.data.data;
1979
1791
  }
1792
+ throw new Error(response.data.message);
1980
1793
  }
1981
1794
  /**
1982
1795
  * Get all transactions via web sockets
1983
1796
  */
1984
1797
  async getTransactions(transactionsRequestQueryParams) {
1985
- if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1986
- throw ApiError.createFallbackError(
1987
- new Error(`Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`),
1988
- "Get transactions"
1989
- );
1990
- }
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");
1798
+ const response = await this.arcApiService.transactions(
1799
+ transactionsRequestQueryParams
1800
+ );
1801
+ if (response.data.status === "success") {
1802
+ return response.data.data;
2004
1803
  }
1804
+ throw new Error(response.data.message);
2005
1805
  }
2006
1806
  };
2007
1807
 
2008
1808
  // src/index.ts
2009
- var defaultConfig = {
2010
- mode: [SDK_MODES.CORE]
2011
- };
2012
1809
  var AggLayerSDK = class {
2013
- constructor(config = defaultConfig) {
1810
+ constructor(config) {
2014
1811
  this.config = config;
2015
1812
  if (!config.mode || config.mode && config.mode.length === 0) {
2016
1813
  this.config.mode = ["CORE"];
2017
1814
  }
2018
- if (this.config.mode?.includes(SDK_MODES.CORE)) {
2019
- this.core = new CoreClient(this.config?.core);
1815
+ if (config.mode.includes(SDK_MODES.CORE)) {
1816
+ if (!this.config.core) {
1817
+ throw new Error("Core config is required");
1818
+ }
1819
+ this.core = new CoreClient(this.config.core);
2020
1820
  }
2021
1821
  if (this.config.mode?.includes(SDK_MODES.NATIVE)) {
1822
+ if (!this.config.native) {
1823
+ throw new Error("NATIVE config is required");
1824
+ }
2022
1825
  const nativeConfig = {
2023
- defaultNetwork: this.config.native?.defaultNetwork ?? DEFAULT_NETWORK,
1826
+ defaultNetwork: this.config.native?.defaultNetwork || DEFAULT_NETWORK,
2024
1827
  ...this.config.native?.chains && {
2025
1828
  chains: this.config.native.chains
2026
1829
  },
@@ -2055,7 +1858,6 @@ var AggLayerSDK = class {
2055
1858
  // Annotate the CommonJS export names for ESM import in node:
2056
1859
  0 && (module.exports = {
2057
1860
  AggLayerSDK,
2058
- ApiError,
2059
1861
  SDK_MODES
2060
1862
  });
2061
1863
  //# sourceMappingURL=index.js.map