@continuumdao/ctm-mpc-defi 0.2.2 → 0.2.3

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
@@ -1476,44 +1476,64 @@ function swappableCurveGraphNodeKeys(adj) {
1476
1476
  }
1477
1477
 
1478
1478
  // src/protocols/evm/curve-dao/apiSession.ts
1479
+ var CURVE_SESSION_CACHE_TTL_MS = 5 * 60 * 1e3;
1480
+ var curveSessionCache = /* @__PURE__ */ new Map();
1479
1481
  async function fetchAllCurvePools(curve) {
1480
1482
  const run = (p) => p.catch(() => void 0);
1481
- await Promise.all([
1482
- run(curve.factory.fetchPools()),
1483
- run(curve.crvUSDFactory.fetchPools()),
1484
- run(curve.EYWAFactory.fetchPools()),
1485
- run(curve.cryptoFactory.fetchPools()),
1486
- run(curve.twocryptoFactory.fetchPools()),
1487
- run(curve.tricryptoFactory.fetchPools()),
1488
- run(curve.stableNgFactory.fetchPools())
1489
- ]);
1483
+ const tasks = [];
1484
+ for (const key of [
1485
+ "factory",
1486
+ "crvUSDFactory",
1487
+ "EYWAFactory",
1488
+ "cryptoFactory",
1489
+ "twocryptoFactory",
1490
+ "tricryptoFactory",
1491
+ "stableNgFactory"
1492
+ ]) {
1493
+ const f = curve[key];
1494
+ if (f?.fetchPools) {
1495
+ tasks.push(run(f.fetchPools()));
1496
+ }
1497
+ }
1498
+ await Promise.all(tasks);
1490
1499
  }
1491
1500
  async function loadFullCurveSessionForRpc(rpcUrl) {
1492
1501
  const url = (rpcUrl ?? "").trim();
1493
- if (!url) return null;
1502
+ if (!url) {
1503
+ throw new Error("rpcUrl is required.");
1504
+ }
1505
+ const cached = curveSessionCache.get(url);
1506
+ if (cached && cached.expiresAt > Date.now()) {
1507
+ return cached.session;
1508
+ }
1494
1509
  try {
1495
1510
  const { default: curve } = await import('@curvefi/api');
1496
1511
  await curve.init("JsonRpc", { url }, {});
1497
1512
  const wrapped = curve.getNetworkConstants().NATIVE_COIN?.wrappedAddress;
1498
1513
  if (!curve.hasRouter || !curve.hasRouter()) {
1499
- return {
1514
+ const session2 = {
1500
1515
  curve,
1501
1516
  adj: /* @__PURE__ */ new Map(),
1502
1517
  swappableNodeKeys: /* @__PURE__ */ new Set(),
1503
1518
  wrappedNative: wrapped
1504
1519
  };
1520
+ curveSessionCache.set(url, { session: session2, expiresAt: Date.now() + CURVE_SESSION_CACHE_TTL_MS });
1521
+ return session2;
1505
1522
  }
1506
1523
  await fetchAllCurvePools(curve);
1507
1524
  const adj = buildCurveLiquidityGraphFromApi(curve);
1508
1525
  addNativeWethBridge(adj, wrapped);
1509
- return {
1526
+ const session = {
1510
1527
  curve,
1511
1528
  adj,
1512
1529
  swappableNodeKeys: swappableCurveGraphNodeKeys(adj),
1513
1530
  wrappedNative: wrapped
1514
1531
  };
1515
- } catch {
1516
- return null;
1532
+ curveSessionCache.set(url, { session, expiresAt: Date.now() + CURVE_SESSION_CACHE_TTL_MS });
1533
+ return session;
1534
+ } catch (e) {
1535
+ const msg = e instanceof Error ? e.message : String(e);
1536
+ throw new Error(`Curve session init failed for RPC: ${msg}`);
1517
1537
  }
1518
1538
  }
1519
1539