@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.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,17 +1675,10 @@ 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
@@ -1715,291 +1686,124 @@ var ArcApiService = class {
1715
1686
  }
1716
1687
  };
1717
1688
 
1718
- // src/core/utils/apiError.ts
1719
- var ApiError = class _ApiError extends Error {
1720
- constructor(errorResponse) {
1721
- super(errorResponse.message);
1722
- this.name = errorResponse.name;
1723
- this.code = errorResponse.code;
1724
- this.details = errorResponse.details;
1725
- if (Error.captureStackTrace) {
1726
- Error.captureStackTrace(this, _ApiError);
1727
- }
1728
- }
1729
- /**
1730
- * Create API error from error response
1731
- */
1732
- static fromErrorResponse(errorResponse) {
1733
- return new _ApiError(errorResponse);
1734
- }
1735
- /**
1736
- * Create fallback error when API completely fails
1737
- */
1738
- static createFallbackError(originalError, operation) {
1739
- return new _ApiError({
1740
- status: "error",
1741
- message: `${operation} failed: ${originalError.message}`,
1742
- name: "ApiConnectionError",
1743
- code: 500,
1744
- details: {
1745
- originalError: originalError.message,
1746
- operation
1747
- }
1748
- });
1749
- }
1750
- /**
1751
- * Convert to plain object for serialization
1752
- */
1753
- toJSON() {
1754
- return {
1755
- name: this.name,
1756
- message: this.message,
1757
- code: this.code,
1758
- details: this.details
1759
- };
1760
- }
1761
- };
1762
-
1763
1689
  // src/core/index.ts
1764
1690
  var CoreClient = class {
1765
1691
  constructor(config) {
1766
- this.config = {
1767
- ...config,
1768
- apiBaseUrl: config?.apiBaseUrl || ARC_API_BASE_URL,
1769
- apiTimeout: config?.apiTimeout || ARC_API_DEFAULT_TIMEOUT
1770
- };
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
+ }
1771
1699
  const { apiBaseUrl, apiTimeout } = this.config;
1772
1700
  this.arcApiService = new ArcApiService({
1773
- baseUrl: apiBaseUrl || ARC_API_BASE_URL,
1774
- timeout: apiTimeout || ARC_API_DEFAULT_TIMEOUT
1701
+ baseUrl: apiBaseUrl,
1702
+ timeout: apiTimeout ?? 3e4
1775
1703
  });
1776
1704
  }
1777
- /**
1778
- * Generic pagination helper for chains API calls (limit and offset based pagination)
1779
- * Handles automatic pagination to fetch all available data
1780
- * @param params - Parameters for the chains API call
1781
- * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1782
- */
1783
- async getAllChainsPaginated(params, pageSize = DEFAULT_CHAINS_PER_PAGE) {
1784
- try {
1785
- const firstResponse = await this.arcApiService.chains({
1786
- ...params,
1787
- limit: pageSize
1788
- });
1789
- if (firstResponse.data.status !== "success") {
1790
- throw ApiError.fromErrorResponse(firstResponse.data);
1791
- }
1792
- const firstPageData = firstResponse.data.data;
1793
- const pagination = firstResponse.data.pagination;
1794
- const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1795
- if (!pagination?.total || pagination.total <= pageSize) {
1796
- return {
1797
- chains: firstPageChains
1798
- };
1799
- }
1800
- const totalPages = Math.ceil(pagination.total / pageSize);
1801
- const remainingPages = totalPages - 1;
1802
- if (remainingPages === 0) {
1803
- return {
1804
- chains: firstPageChains
1805
- };
1806
- }
1807
- const remainingPagePromises = Array.from(
1808
- { length: remainingPages },
1809
- (_, index) => {
1810
- const offset = (index + 1) * pageSize;
1811
- return this.arcApiService.chains({
1812
- ...params,
1813
- limit: pageSize,
1814
- offset
1815
- });
1816
- }
1817
- );
1818
- const remainingResponses = await Promise.all(remainingPagePromises);
1819
- for (const response of remainingResponses) {
1820
- if (response.data.status !== "success") {
1821
- throw ApiError.fromErrorResponse(response.data);
1822
- }
1823
- }
1824
- const allChains = [
1825
- ...firstPageChains,
1826
- ...remainingResponses.flatMap((response) => {
1827
- if (response.data.status === "success") {
1828
- return Array.isArray(response.data.data) ? response.data.data : [];
1829
- }
1830
- return [];
1831
- })
1832
- ];
1833
- return {
1834
- chains: allChains
1835
- };
1836
- } catch (error) {
1837
- if (error instanceof ApiError) {
1838
- throw error;
1839
- }
1840
- throw ApiError.createFallbackError(error, "Get chains metadata");
1841
- }
1842
- }
1843
1705
  /**
1844
1706
  * Get all chains metadata from AggLayer API
1845
- * Handles pagination automatically to fetch all available chains
1846
1707
  */
1847
1708
  async getAllChains() {
1848
- 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);
1849
1714
  }
1850
1715
  /**
1851
1716
  * Get chain metadata by id from AggLayer API
1852
- * Handles pagination automatically to fetch all available chain metadata
1853
1717
  * @param ids - the ids of the chains to get metadata for
1854
1718
  */
1855
1719
  async getChainMetadataByChainIds(ids) {
1856
- 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);
1857
1725
  }
1858
1726
  /**
1859
1727
  * Get all tokens from AggLayer API
1860
- *
1861
- * Developer Note: This method is not recommended to use frequently or from frontend.
1862
- * As it can be very slow and resource intensive.
1863
- * It is recommended to use getChainDataAndTokensByChainIds instead.
1864
- */
1865
- async getAllTokens() {
1866
- return this.getAllChainsPaginated(
1867
- { withSupportedTokens: true },
1868
- DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE
1869
- );
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);
1870
1737
  }
1871
1738
  /**
1872
1739
  * Get chain data and tokens by AggLayer API
1873
1740
  * @param ids - the ids of the chains to get data and tokens for
1874
1741
  */
1875
1742
  async getChainDataAndTokensByChainIds(ids) {
1876
- return this.getAllChainsPaginated({
1743
+ const response = await this.arcApiService.chains({
1877
1744
  chainIds: ids,
1878
1745
  withSupportedTokens: true
1879
1746
  });
1747
+ if (response.data.status === "success") {
1748
+ return response.data.data;
1749
+ }
1750
+ throw new Error(response.data.message);
1880
1751
  }
1881
1752
  /**
1882
1753
  * Get all routes from AggLayer API
1883
1754
  */
1884
1755
  async getRoutes(routesRequestParams) {
1885
- try {
1886
- const response = await this.arcApiService.routes(routesRequestParams);
1887
- if (response.data.status === "success") {
1888
- return response.data.data;
1889
- }
1890
- throw ApiError.fromErrorResponse(response.data);
1891
- } catch (error) {
1892
- if (error instanceof ApiError) {
1893
- throw error;
1894
- }
1895
- throw ApiError.createFallbackError(error, "Get routes");
1896
- }
1897
- }
1898
- /**
1899
- * Get calldata from a route
1900
- * If route has transactionRequest field, return it directly as calldata
1901
- * Otherwise, call buildTransaction on route.steps[0] and return that as calldata
1902
- */
1903
- async getUnsignedTransaction(route) {
1904
- if (route.transactionRequest) {
1905
- return route.transactionRequest;
1906
- }
1907
- if (route.steps.length === 0 || !route.steps[0]) {
1908
- throw ApiError.createFallbackError(
1909
- new Error("Route has no steps to build transaction from"),
1910
- "Get unsigned transaction"
1911
- );
1912
- }
1913
- try {
1914
- const response = await this.arcApiService.buildTransaction(
1915
- route.steps[0]
1916
- );
1917
- if (response.data.status === "success") {
1918
- return response.data.data;
1919
- }
1920
- throw ApiError.fromErrorResponse(response.data);
1921
- } catch (error) {
1922
- if (error instanceof ApiError) {
1923
- throw error;
1924
- }
1925
- throw ApiError.createFallbackError(
1926
- error,
1927
- "Get unsigned transaction"
1928
- );
1756
+ const response = await this.arcApiService.routes(routesRequestParams);
1757
+ if (response.data.status === "success") {
1758
+ return response.data.data;
1929
1759
  }
1760
+ throw new Error(response.data.message);
1930
1761
  }
1931
1762
  /**
1932
- * Get calldata for claim step
1933
- * Needs to be called separately as claim step is not part of route.
1934
- *
1935
- * @developer Note: Do not misinterpret network ID as chain ID.
1936
- *
1937
- * @param sourceNetworkId - The source network ID where the transfer was initiated.
1938
- * @param depositCount - The deposit count associated with the transfer.
1763
+ * Build transaction from a step object
1939
1764
  */
1940
- async getClaimUnsignedTransaction(buildClaimTxParams) {
1941
- try {
1942
- const response = await this.arcApiService.buildClaimTransaction(
1943
- buildClaimTxParams.sourceNetworkId,
1944
- buildClaimTxParams.depositCount
1945
- );
1946
- if (response.data.status === "success") {
1947
- return response.data.data;
1948
- }
1949
- throw ApiError.fromErrorResponse(response.data);
1950
- } catch (error) {
1951
- if (error instanceof ApiError) {
1952
- throw error;
1953
- }
1954
- throw ApiError.createFallbackError(
1955
- error,
1956
- "Get claim unsigned transaction"
1957
- );
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;
1958
1771
  }
1772
+ throw new Error(response.data.message);
1959
1773
  }
1960
1774
  /**
1961
1775
  * Get all transactions via web sockets
1962
1776
  */
1963
1777
  async getTransactions(transactionsRequestQueryParams) {
1964
- if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1965
- throw ApiError.createFallbackError(
1966
- new Error(`Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`),
1967
- "Get transactions"
1968
- );
1969
- }
1970
- try {
1971
- const response = await this.arcApiService.transactions(
1972
- transactionsRequestQueryParams
1973
- );
1974
- if (response.data.status === "success") {
1975
- return response.data.data;
1976
- }
1977
- throw ApiError.fromErrorResponse(response.data);
1978
- } catch (error) {
1979
- if (error instanceof ApiError) {
1980
- throw error;
1981
- }
1982
- throw ApiError.createFallbackError(error, "Get transactions");
1778
+ const response = await this.arcApiService.transactions(
1779
+ transactionsRequestQueryParams
1780
+ );
1781
+ if (response.data.status === "success") {
1782
+ return response.data.data;
1983
1783
  }
1784
+ throw new Error(response.data.message);
1984
1785
  }
1985
1786
  };
1986
1787
 
1987
1788
  // src/index.ts
1988
- var defaultConfig = {
1989
- mode: [SDK_MODES.CORE]
1990
- };
1991
1789
  var AggLayerSDK = class {
1992
- constructor(config = defaultConfig) {
1790
+ constructor(config) {
1993
1791
  this.config = config;
1994
1792
  if (!config.mode || config.mode && config.mode.length === 0) {
1995
1793
  this.config.mode = ["CORE"];
1996
1794
  }
1997
- if (this.config.mode?.includes(SDK_MODES.CORE)) {
1998
- 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);
1999
1800
  }
2000
1801
  if (this.config.mode?.includes(SDK_MODES.NATIVE)) {
1802
+ if (!this.config.native) {
1803
+ throw new Error("NATIVE config is required");
1804
+ }
2001
1805
  const nativeConfig = {
2002
- defaultNetwork: this.config.native?.defaultNetwork ?? DEFAULT_NETWORK,
1806
+ defaultNetwork: this.config.native?.defaultNetwork || DEFAULT_NETWORK,
2003
1807
  ...this.config.native?.chains && {
2004
1808
  chains: this.config.native.chains
2005
1809
  },
@@ -2033,7 +1837,6 @@ var AggLayerSDK = class {
2033
1837
  };
2034
1838
  export {
2035
1839
  AggLayerSDK,
2036
- ApiError,
2037
1840
  SDK_MODES
2038
1841
  };
2039
1842
  //# sourceMappingURL=index.mjs.map