@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.
@@ -215,44 +215,64 @@ function swappableCurveGraphNodeKeys(adj) {
215
215
  }
216
216
 
217
217
  // src/protocols/evm/curve-dao/apiSession.ts
218
+ var CURVE_SESSION_CACHE_TTL_MS = 5 * 60 * 1e3;
219
+ var curveSessionCache = /* @__PURE__ */ new Map();
218
220
  async function fetchAllCurvePools(curve) {
219
221
  const run = (p) => p.catch(() => void 0);
220
- await Promise.all([
221
- run(curve.factory.fetchPools()),
222
- run(curve.crvUSDFactory.fetchPools()),
223
- run(curve.EYWAFactory.fetchPools()),
224
- run(curve.cryptoFactory.fetchPools()),
225
- run(curve.twocryptoFactory.fetchPools()),
226
- run(curve.tricryptoFactory.fetchPools()),
227
- run(curve.stableNgFactory.fetchPools())
228
- ]);
222
+ const tasks = [];
223
+ for (const key of [
224
+ "factory",
225
+ "crvUSDFactory",
226
+ "EYWAFactory",
227
+ "cryptoFactory",
228
+ "twocryptoFactory",
229
+ "tricryptoFactory",
230
+ "stableNgFactory"
231
+ ]) {
232
+ const f = curve[key];
233
+ if (f?.fetchPools) {
234
+ tasks.push(run(f.fetchPools()));
235
+ }
236
+ }
237
+ await Promise.all(tasks);
229
238
  }
230
239
  async function loadFullCurveSessionForRpc(rpcUrl) {
231
240
  const url = (rpcUrl ?? "").trim();
232
- if (!url) return null;
241
+ if (!url) {
242
+ throw new Error("rpcUrl is required.");
243
+ }
244
+ const cached = curveSessionCache.get(url);
245
+ if (cached && cached.expiresAt > Date.now()) {
246
+ return cached.session;
247
+ }
233
248
  try {
234
249
  const { default: curve } = await import('@curvefi/api');
235
250
  await curve.init("JsonRpc", { url }, {});
236
251
  const wrapped = curve.getNetworkConstants().NATIVE_COIN?.wrappedAddress;
237
252
  if (!curve.hasRouter || !curve.hasRouter()) {
238
- return {
253
+ const session2 = {
239
254
  curve,
240
255
  adj: /* @__PURE__ */ new Map(),
241
256
  swappableNodeKeys: /* @__PURE__ */ new Set(),
242
257
  wrappedNative: wrapped
243
258
  };
259
+ curveSessionCache.set(url, { session: session2, expiresAt: Date.now() + CURVE_SESSION_CACHE_TTL_MS });
260
+ return session2;
244
261
  }
245
262
  await fetchAllCurvePools(curve);
246
263
  const adj = buildCurveLiquidityGraphFromApi(curve);
247
264
  addNativeWethBridge(adj, wrapped);
248
- return {
265
+ const session = {
249
266
  curve,
250
267
  adj,
251
268
  swappableNodeKeys: swappableCurveGraphNodeKeys(adj),
252
269
  wrappedNative: wrapped
253
270
  };
254
- } catch {
255
- return null;
271
+ curveSessionCache.set(url, { session, expiresAt: Date.now() + CURVE_SESSION_CACHE_TTL_MS });
272
+ return session;
273
+ } catch (e) {
274
+ const msg = e instanceof Error ? e.message : String(e);
275
+ throw new Error(`Curve session init failed for RPC: ${msg}`);
256
276
  }
257
277
  }
258
278