@agglayer/sdk 1.0.0-beta.14 → 1.0.0-beta.16

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
@@ -1395,6 +1395,9 @@ var NETWORKS = {
1395
1395
  CARDONA: 2442
1396
1396
  };
1397
1397
  var DEFAULT_NETWORK = NETWORKS.SEPOLIA;
1398
+ var DEFAULT_CHAINS_PER_PAGE = 100;
1399
+ var DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE = 1;
1400
+ var MAX_TRANSACTIONS_PER_PAGE = 100;
1398
1401
 
1399
1402
  // src/native/index.ts
1400
1403
  var NativeClient = class {
@@ -1623,11 +1626,12 @@ var HttpClient = class {
1623
1626
  continue;
1624
1627
  }
1625
1628
  if (Array.isArray(value)) {
1626
- value.forEach((item, index) => {
1627
- if (item !== void 0 && item !== null) {
1628
- result[`${fullKey}[${index}]`] = String(item);
1629
- }
1630
- });
1629
+ const filteredValues = value.filter(
1630
+ (item) => item !== void 0 && item !== null
1631
+ );
1632
+ if (filteredValues.length > 0) {
1633
+ result[fullKey] = filteredValues.join(",");
1634
+ }
1631
1635
  } else if (typeof value === "object") {
1632
1636
  Object.assign(
1633
1637
  result,
@@ -1657,16 +1661,17 @@ var ArcApiService = class {
1657
1661
  this.httpClient = new HttpClient({ baseUrl, timeout });
1658
1662
  }
1659
1663
  // responsible for both chains metadata and tokens
1664
+ // supports limit/offset based pagination
1660
1665
  async chains({
1661
1666
  withSupportedTokens = false,
1662
- limit = 20,
1663
- startAfter,
1667
+ limit = 10,
1668
+ offset,
1664
1669
  chainIds
1665
1670
  } = {}) {
1666
1671
  return this.httpClient.get("/metadata/chains", {
1667
1672
  withSupportedTokens,
1668
1673
  limit,
1669
- startAfter,
1674
+ offset,
1670
1675
  chainIds
1671
1676
  });
1672
1677
  }
@@ -1675,10 +1680,11 @@ var ArcApiService = class {
1675
1680
  }
1676
1681
  async buildTransaction(builtTransactionRequestBody) {
1677
1682
  return this.httpClient.post(
1678
- "/build-transaction",
1683
+ "/routes/build-transaction",
1679
1684
  builtTransactionRequestBody
1680
1685
  );
1681
1686
  }
1687
+ // supports cursor based pagination only
1682
1688
  async transactions(transactionsRequestQueryParams) {
1683
1689
  return this.httpClient.get("/transactions", {
1684
1690
  transactionsRequestQueryParams
@@ -1702,52 +1708,102 @@ var CoreClient = class {
1702
1708
  timeout: apiTimeout ?? 3e4
1703
1709
  });
1704
1710
  }
1711
+ /**
1712
+ * Generic pagination helper for chains API calls (limit and offset based pagination)
1713
+ * Handles automatic pagination to fetch all available data
1714
+ * @param params - Parameters for the chains API call
1715
+ * @param pageSize - Number of items per page (defaults to DEFAULT_CHAINS_PER_PAGE)
1716
+ */
1717
+ async getAllChainsPaginated(params, pageSize = DEFAULT_CHAINS_PER_PAGE) {
1718
+ const firstResponse = await this.arcApiService.chains({
1719
+ ...params,
1720
+ limit: pageSize
1721
+ });
1722
+ if (firstResponse.data.status !== "success") {
1723
+ throw new Error(firstResponse.data.message);
1724
+ }
1725
+ const firstPageData = firstResponse.data.data;
1726
+ const pagination = firstResponse.data.pagination;
1727
+ const firstPageChains = Array.isArray(firstPageData) ? firstPageData : [];
1728
+ if (!pagination?.total || pagination.total <= pageSize) {
1729
+ return {
1730
+ chains: firstPageChains
1731
+ };
1732
+ }
1733
+ const totalPages = Math.ceil(pagination.total / pageSize);
1734
+ const remainingPages = totalPages - 1;
1735
+ if (remainingPages === 0) {
1736
+ return {
1737
+ chains: firstPageChains
1738
+ };
1739
+ }
1740
+ const remainingPagePromises = Array.from(
1741
+ { length: remainingPages },
1742
+ (_, index) => {
1743
+ const offset = (index + 1) * pageSize;
1744
+ return this.arcApiService.chains({
1745
+ ...params,
1746
+ limit: pageSize,
1747
+ offset
1748
+ });
1749
+ }
1750
+ );
1751
+ const remainingResponses = await Promise.all(remainingPagePromises);
1752
+ for (const response of remainingResponses) {
1753
+ if (response.data.status !== "success") {
1754
+ throw new Error(response.data.message);
1755
+ }
1756
+ }
1757
+ const allChains = [
1758
+ ...firstPageChains,
1759
+ ...remainingResponses.flatMap((response) => {
1760
+ if (response.data.status === "success") {
1761
+ return Array.isArray(response.data.data) ? response.data.data : [];
1762
+ }
1763
+ return [];
1764
+ })
1765
+ ];
1766
+ return {
1767
+ chains: allChains
1768
+ };
1769
+ }
1705
1770
  /**
1706
1771
  * Get all chains metadata from AggLayer API
1772
+ * Handles pagination automatically to fetch all available chains
1707
1773
  */
1708
1774
  async getAllChains() {
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);
1775
+ return this.getAllChainsPaginated({});
1714
1776
  }
1715
1777
  /**
1716
1778
  * Get chain metadata by id from AggLayer API
1779
+ * Handles pagination automatically to fetch all available chain metadata
1717
1780
  * @param ids - the ids of the chains to get metadata for
1718
1781
  */
1719
1782
  async getChainMetadataByChainIds(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);
1783
+ return this.getAllChainsPaginated({ chainIds: ids });
1725
1784
  }
1726
1785
  /**
1727
1786
  * Get all tokens from AggLayer API
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);
1787
+ *
1788
+ * Developer Note: This method is not recommended to use frequently or from frontend.
1789
+ * As it can be very slow and resource intensive.
1790
+ * It is recommended to use getChainDataAndTokensByChainIds instead.
1791
+ */
1792
+ async getAllTokens() {
1793
+ return this.getAllChainsPaginated(
1794
+ { withSupportedTokens: true },
1795
+ DEFAULT_CHAINS_WITH_TOKENS_PER_PAGE
1796
+ );
1737
1797
  }
1738
1798
  /**
1739
1799
  * Get chain data and tokens by AggLayer API
1740
1800
  * @param ids - the ids of the chains to get data and tokens for
1741
1801
  */
1742
1802
  async getChainDataAndTokensByChainIds(ids) {
1743
- const response = await this.arcApiService.chains({
1803
+ return this.getAllChainsPaginated({
1744
1804
  chainIds: ids,
1745
1805
  withSupportedTokens: true
1746
1806
  });
1747
- if (response.data.status === "success") {
1748
- return response.data.data;
1749
- }
1750
- throw new Error(response.data.message);
1751
1807
  }
1752
1808
  /**
1753
1809
  * Get all routes from AggLayer API
@@ -1760,12 +1816,18 @@ var CoreClient = class {
1760
1816
  throw new Error(response.data.message);
1761
1817
  }
1762
1818
  /**
1763
- * Build transaction from a step object
1819
+ * Get calldata from a route
1820
+ * If route has transactionRequest field, return it directly as calldata
1821
+ * Otherwise, call buildTransaction on route.steps[0] and return that as calldata
1764
1822
  */
1765
- async buildTransaction(builtTransactionRequestBody) {
1766
- const response = await this.arcApiService.buildTransaction(
1767
- builtTransactionRequestBody
1768
- );
1823
+ async getUnsignedTransaction(route) {
1824
+ if (route.transactionRequest) {
1825
+ return route.transactionRequest;
1826
+ }
1827
+ if (route.steps.length === 0 || !route.steps[0]) {
1828
+ throw new Error("Route has no steps to build transaction from");
1829
+ }
1830
+ const response = await this.arcApiService.buildTransaction(route.steps[0]);
1769
1831
  if (response.data.status === "success") {
1770
1832
  return response.data.data;
1771
1833
  }
@@ -1775,6 +1837,11 @@ var CoreClient = class {
1775
1837
  * Get all transactions via web sockets
1776
1838
  */
1777
1839
  async getTransactions(transactionsRequestQueryParams) {
1840
+ if (transactionsRequestQueryParams.limit && transactionsRequestQueryParams.limit > MAX_TRANSACTIONS_PER_PAGE) {
1841
+ throw new Error(
1842
+ `Limit cannot be greater than ${MAX_TRANSACTIONS_PER_PAGE}`
1843
+ );
1844
+ }
1778
1845
  const response = await this.arcApiService.transactions(
1779
1846
  transactionsRequestQueryParams
1780
1847
  );