@agglayer/sdk 1.0.0-beta.19 → 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.mjs CHANGED
@@ -11,33 +11,7 @@ var ChainRegistry = class _ChainRegistry {
11
11
  }
12
12
  return _ChainRegistry.instance;
13
13
  }
14
- // DEV: if adding new default chains, also update README.md
15
14
  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
- });
41
15
  this.registerChain({
42
16
  chainId: 11155111,
43
17
  networkId: 0,
@@ -52,6 +26,20 @@ var ChainRegistry = class _ChainRegistry {
52
26
  proofApiUrl: "https://api-gateway.polygon.technology/api/v3/proof/testnet/",
53
27
  isTestnet: true
54
28
  });
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
+ });
55
43
  }
56
44
  /**
57
45
  * Register a new chain
@@ -1402,16 +1390,11 @@ var ERC20 = class extends BaseContract {
1402
1390
  import { createPublicClient as createPublicClient3, http as http3 } from "viem";
1403
1391
 
1404
1392
  // src/constants.ts
1405
- var ARC_API_BASE_URL = "https://arc-api.polygon.technology";
1406
- var ARC_API_DEFAULT_TIMEOUT = 3e4;
1407
- var DEFAULT_CHAINS_PER_PAGE = 100;
1408
- var DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE = 1;
1409
- var MAX_TRANSACTIONS_PER_PAGE = 100;
1410
1393
  var NETWORKS = {
1411
- ETHEREUM: 1,
1412
- KATANA: 747474
1394
+ SEPOLIA: 11155111,
1395
+ CARDONA: 2442
1413
1396
  };
1414
- var DEFAULT_NETWORK = NETWORKS.ETHEREUM;
1397
+ var DEFAULT_NETWORK = NETWORKS.SEPOLIA;
1415
1398
 
1416
1399
  // src/native/index.ts
1417
1400
  var NativeClient = class {
@@ -1601,6 +1584,9 @@ var HttpClient = class {
1601
1584
  signal: controller.signal
1602
1585
  });
1603
1586
  clearTimeout(timeoutId);
1587
+ if (!response.ok) {
1588
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1589
+ }
1604
1590
  let data;
1605
1591
  const contentType = response.headers.get("content-type");
1606
1592
  if (contentType?.includes("application/json")) {
@@ -1608,12 +1594,6 @@ var HttpClient = class {
1608
1594
  } else {
1609
1595
  data = await response.text();
1610
1596
  }
1611
- if (!response.ok) {
1612
- if (contentType?.includes("application/json")) {
1613
- } else {
1614
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1615
- }
1616
- }
1617
1597
  return {
1618
1598
  data,
1619
1599
  status: response.status,
@@ -1643,12 +1623,11 @@ var HttpClient = class {
1643
1623
  continue;
1644
1624
  }
1645
1625
  if (Array.isArray(value)) {
1646
- const filteredValues = value.filter(
1647
- (item) => item !== void 0 && item !== null
1648
- );
1649
- if (filteredValues.length > 0) {
1650
- result[fullKey] = filteredValues.join(",");
1651
- }
1626
+ value.forEach((item, index) => {
1627
+ if (item !== void 0 && item !== null) {
1628
+ result[`${fullKey}[${index}]`] = String(item);
1629
+ }
1630
+ });
1652
1631
  } else if (typeof value === "object") {
1653
1632
  Object.assign(
1654
1633
  result,
@@ -1678,17 +1657,16 @@ var ArcApiService = class {
1678
1657
  this.httpClient = new HttpClient({ baseUrl, timeout });
1679
1658
  }
1680
1659
  // responsible for both chains metadata and tokens
1681
- // supports limit/offset based pagination
1682
1660
  async chains({
1683
1661
  withSupportedTokens = false,
1684
- limit = 10,
1685
- offset,
1662
+ limit = 20,
1663
+ startAfter = 0,
1686
1664
  chainIds
1687
1665
  } = {}) {
1688
1666
  return this.httpClient.get("/metadata/chains", {
1689
1667
  withSupportedTokens,
1690
1668
  limit,
1691
- offset,
1669
+ startAfter,
1692
1670
  chainIds
1693
1671
  });
1694
1672
  }
@@ -1697,338 +1675,135 @@ var ArcApiService = class {
1697
1675
  }
1698
1676
  async buildTransaction(builtTransactionRequestBody) {
1699
1677
  return this.httpClient.post(
1700
- "/routes/build-transaction",
1678
+ "/build-transaction",
1701
1679
  builtTransactionRequestBody
1702
1680
  );
1703
1681
  }
1704
- async buildClaimTransaction(sourceNetworkId, depositCount) {
1705
- return this.httpClient.post("/routes/build-transaction-for-claim", {
1706
- sourceNetworkId,
1707
- depositCount
1708
- });
1709
- }
1710
- // supports cursor based pagination only
1711
1682
  async transactions(transactionsRequestQueryParams) {
1712
1683
  return this.httpClient.get("/transactions", {
1713
1684
  transactionsRequestQueryParams
1714
1685
  });
1715
1686
  }
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
- }
1765
1687
  };
1766
1688
 
1767
1689
  // src/core/index.ts
1768
1690
  var CoreClient = class {
1769
1691
  constructor(config) {
1770
- this.config = {
1771
- ...config,
1772
- apiBaseUrl: config?.apiBaseUrl || ARC_API_BASE_URL,
1773
- apiTimeout: config?.apiTimeout || ARC_API_DEFAULT_TIMEOUT
1774
- };
1692
+ this.config = config;
1693
+ if (!this.config) {
1694
+ throw new Error("Config is required");
1695
+ }
1696
+ if (!this.config.apiBaseUrl) {
1697
+ throw new Error("API base URL is required");
1698
+ }
1775
1699
  const { apiBaseUrl, apiTimeout } = this.config;
1776
1700
  this.arcApiService = new ArcApiService({
1777
- baseUrl: apiBaseUrl || ARC_API_BASE_URL,
1778
- timeout: apiTimeout || ARC_API_DEFAULT_TIMEOUT
1701
+ baseUrl: apiBaseUrl,
1702
+ timeout: apiTimeout ?? 3e4
1779
1703
  });
1780
1704
  }
1781
- /**
1782
- * Generic pagination helper for chains API calls (limit and offset based pagination)
1783
- * Handles automatic pagination to fetch all available data
1784
- * @param params - Parameters for the chains API call
1785
- * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1786
- */
1787
- async getAllChainsPaginated(params, pageSize = DEFAULT_CHAINS_PER_PAGE) {
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
- ];
1837
- return {
1838
- chains: allChains
1839
- };
1840
- } catch (error) {
1841
- if (error instanceof ApiError) {
1842
- throw error;
1843
- }
1844
- throw ApiError.createFallbackError(error, "Get chains metadata");
1845
- }
1846
- }
1847
1705
  /**
1848
1706
  * Get all chains metadata from AggLayer API
1849
- * Handles pagination automatically to fetch all available chains
1850
1707
  */
1851
1708
  async getAllChains() {
1852
- return this.getAllChainsPaginated({});
1709
+ const response = await this.arcApiService.chains();
1710
+ if (response.data.status === "success") {
1711
+ return response.data.data;
1712
+ }
1713
+ throw new Error(response.data.message);
1853
1714
  }
1854
1715
  /**
1855
1716
  * Get chain metadata by id from AggLayer API
1856
- * Handles pagination automatically to fetch all available chain metadata
1857
1717
  * @param ids - the ids of the chains to get metadata for
1858
1718
  */
1859
1719
  async getChainMetadataByChainIds(ids) {
1860
- return this.getAllChainsPaginated({ chainIds: ids });
1720
+ const response = await this.arcApiService.chains({ chainIds: ids });
1721
+ if (response.data.status === "success") {
1722
+ return response.data.data;
1723
+ }
1724
+ throw new Error(response.data.message);
1861
1725
  }
1862
1726
  /**
1863
1727
  * Get all tokens from AggLayer API
1864
- *
1865
- * Developer Note: This method is not recommended to use frequently or from frontend.
1866
- * As it can be very slow and resource intensive.
1867
- * It is recommended to use getChainDataAndTokensByChainIds instead.
1868
- */
1869
- async getAllTokens() {
1870
- return this.getAllChainsPaginated(
1871
- { withSupportedTokens: true },
1872
- DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE
1873
- );
1728
+ */
1729
+ async getTokens() {
1730
+ const response = await this.arcApiService.chains({
1731
+ withSupportedTokens: true
1732
+ });
1733
+ if (response.data.status === "success") {
1734
+ return response.data.data;
1735
+ }
1736
+ throw new Error(response.data.message);
1874
1737
  }
1875
1738
  /**
1876
1739
  * Get chain data and tokens by AggLayer API
1877
1740
  * @param ids - the ids of the chains to get data and tokens for
1878
1741
  */
1879
1742
  async getChainDataAndTokensByChainIds(ids) {
1880
- return this.getAllChainsPaginated({
1743
+ const response = await this.arcApiService.chains({
1881
1744
  chainIds: ids,
1882
1745
  withSupportedTokens: true
1883
1746
  });
1747
+ if (response.data.status === "success") {
1748
+ return response.data.data;
1749
+ }
1750
+ throw new Error(response.data.message);
1884
1751
  }
1885
1752
  /**
1886
1753
  * Get all routes from AggLayer API
1887
1754
  */
1888
1755
  async getRoutes(routesRequestParams) {
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");
1756
+ const response = await this.arcApiService.routes(routesRequestParams);
1757
+ if (response.data.status === "success") {
1758
+ return response.data.data;
1900
1759
  }
1760
+ throw new Error(response.data.message);
1901
1761
  }
1902
1762
  /**
1903
- * Get calldata from a route
1904
- * If route has transactionRequest field, return it directly as calldata
1905
- * Otherwise, call buildTransaction on route.steps[0] and return that as calldata
1763
+ * Build transaction from a step object
1906
1764
  */
1907
- async getUnsignedTransaction(route) {
1908
- if (route.transactionRequest) {
1909
- return route.transactionRequest;
1910
- }
1911
- if (route.steps.length === 0 || !route.steps[0]) {
1912
- throw ApiError.createFallbackError(
1913
- new Error("Route has no steps to build transaction from"),
1914
- "Get unsigned transaction"
1915
- );
1916
- }
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
- );
1933
- }
1934
- }
1935
- /**
1936
- * Get calldata for claim step
1937
- * Needs to be called separately as claim step is not part of route.
1938
- *
1939
- * @developer Note: Do not misinterpret network ID as chain ID.
1940
- *
1941
- * @param sourceNetworkId - The source network ID where the transfer was initiated.
1942
- * @param depositCount - The deposit count associated with the transfer.
1943
- */
1944
- async getClaimUnsignedTransaction(buildClaimTxParams) {
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
- );
1765
+ async buildTransaction(builtTransactionRequestBody) {
1766
+ const response = await this.arcApiService.buildTransaction(
1767
+ builtTransactionRequestBody
1768
+ );
1769
+ if (response.data.status === "success") {
1770
+ return response.data.data;
1962
1771
  }
1772
+ throw new Error(response.data.message);
1963
1773
  }
1964
1774
  /**
1965
1775
  * Get all transactions via web sockets
1966
1776
  */
1967
1777
  async getTransactions(transactionsRequestQueryParams) {
1968
- if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1969
- throw ApiError.createFallbackError(
1970
- new Error(`Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`),
1971
- "Get transactions"
1972
- );
1973
- }
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
- );
1778
+ const response = await this.arcApiService.transactions(
1779
+ transactionsRequestQueryParams
1780
+ );
1781
+ if (response.data.status === "success") {
1782
+ return response.data.data;
2012
1783
  }
1784
+ throw new Error(response.data.message);
2013
1785
  }
2014
1786
  };
2015
1787
 
2016
1788
  // src/index.ts
2017
- var defaultConfig = {
2018
- mode: [SDK_MODES.CORE]
2019
- };
2020
1789
  var AggLayerSDK = class {
2021
- constructor(config = defaultConfig) {
1790
+ constructor(config) {
2022
1791
  this.config = config;
2023
1792
  if (!config.mode || config.mode && config.mode.length === 0) {
2024
1793
  this.config.mode = ["CORE"];
2025
1794
  }
2026
- if (this.config.mode?.includes(SDK_MODES.CORE)) {
2027
- this.core = new CoreClient(this.config?.core);
1795
+ if (config.mode.includes(SDK_MODES.CORE)) {
1796
+ if (!this.config.core) {
1797
+ throw new Error("Core config is required");
1798
+ }
1799
+ this.core = new CoreClient(this.config.core);
2028
1800
  }
2029
1801
  if (this.config.mode?.includes(SDK_MODES.NATIVE)) {
1802
+ if (!this.config.native) {
1803
+ throw new Error("NATIVE config is required");
1804
+ }
2030
1805
  const nativeConfig = {
2031
- defaultNetwork: this.config.native?.defaultNetwork ?? DEFAULT_NETWORK,
1806
+ defaultNetwork: this.config.native?.defaultNetwork || DEFAULT_NETWORK,
2032
1807
  ...this.config.native?.chains && {
2033
1808
  chains: this.config.native.chains
2034
1809
  },
@@ -2062,7 +1837,6 @@ var AggLayerSDK = class {
2062
1837
  };
2063
1838
  export {
2064
1839
  AggLayerSDK,
2065
- ApiError,
2066
1840
  SDK_MODES
2067
1841
  };
2068
1842
  //# sourceMappingURL=index.mjs.map