@continuumdao/ctm-mpc-defi 0.2.31 → 0.2.32

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.
@@ -21,6 +21,9 @@ var __export = (target, all) => {
21
21
  function getAaveGraphqlProxyUrl() {
22
22
  return aaveGraphqlProxyUrl;
23
23
  }
24
+ function getEulerGraphqlProxyUrl() {
25
+ return eulerGraphqlProxyUrl;
26
+ }
24
27
  function getMorphoGraphqlProxyUrl() {
25
28
  return morphoGraphqlProxyUrl;
26
29
  }
@@ -58,7 +61,7 @@ async function getJsonViaOptionalProxy(args) {
58
61
  }
59
62
  return await r.json();
60
63
  }
61
- var aaveGraphqlProxyUrl, morphoGraphqlProxyUrl;
64
+ var aaveGraphqlProxyUrl, eulerGraphqlProxyUrl, morphoGraphqlProxyUrl;
62
65
  var init_defiProxy = __esm({
63
66
  "src/core/defiProxy.ts"() {
64
67
  }
@@ -668,9 +671,9 @@ async function loadFullCurveSessionForRpc(rpcUrl) {
668
671
  if (!url) {
669
672
  throw new Error("rpcUrl is required.");
670
673
  }
671
- const cached = curveSessionCache.get(url);
672
- if (cached && cached.expiresAt > Date.now()) {
673
- return cached.session;
674
+ const cached2 = curveSessionCache.get(url);
675
+ if (cached2 && cached2.expiresAt > Date.now()) {
676
+ return cached2.session;
674
677
  }
675
678
  try {
676
679
  const { default: curve } = await import('@curvefi/api');
@@ -7067,6 +7070,242 @@ var aaveV4ProtocolModule = {
7067
7070
  ]
7068
7071
  };
7069
7072
  registerProtocolModule(aaveV4ProtocolModule);
7073
+ init_defiProxy();
7074
+ var EULER_EVAULT_CAP_UNLIMITED_WEI = (1n << 256n) - 1n;
7075
+ var UINT256_MAX = EULER_EVAULT_CAP_UNLIMITED_WEI;
7076
+ parseAbi([
7077
+ "function caps() view returns (uint16 supplyCap, uint16 borrowCap)",
7078
+ "function cash() view returns (uint256)",
7079
+ "function totalBorrows() view returns (uint256)",
7080
+ "function totalAssets() view returns (uint256)"
7081
+ ]);
7082
+ function resolveEulerAmountCapToWei(rawCap16) {
7083
+ const amountCap = BigInt(rawCap16) & 0xffffn;
7084
+ if (amountCap === 0n) return UINT256_MAX;
7085
+ const exp = amountCap & 63n;
7086
+ const mantissa = amountCap >> 6n;
7087
+ return 10n ** exp * mantissa / 100n;
7088
+ }
7089
+ function eulerSubgraphVaultCapToUnderlyingWei(raw) {
7090
+ try {
7091
+ const v = BigInt((raw ?? "").trim() || "0");
7092
+ if (v >= UINT256_MAX - 3n) return v;
7093
+ if (v <= 0xffffn) return resolveEulerAmountCapToWei(v);
7094
+ return v;
7095
+ } catch {
7096
+ return 0n;
7097
+ }
7098
+ }
7099
+
7100
+ // src/protocols/evm/euler-v2/eulerV2Subgraph.ts
7101
+ var EULER_V2_GOLDSKY_SUBGRAPH_URL_BY_CHAIN_ID = {
7102
+ 1: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-mainnet/latest/gn",
7103
+ 8453: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-base/latest/gn",
7104
+ 1923: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-swell/latest/gn",
7105
+ 146: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-sonic/latest/gn",
7106
+ 60808: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-bob/latest/gn",
7107
+ 80094: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-berachain/latest/gn",
7108
+ 43114: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-avalanche/latest/gn",
7109
+ 42161: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-arbitrum/latest/gn",
7110
+ 130: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-unichain/latest/gn",
7111
+ 57073: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-ink/latest/gn",
7112
+ 56: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-bsc/latest/gn",
7113
+ 999: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-hyperevm/latest/gn",
7114
+ 10: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-optimism/latest/gn",
7115
+ 100: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-gnosis/latest/gn",
7116
+ 480: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-worldchain/latest/gn",
7117
+ 239: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-tac/latest/gn",
7118
+ 9745: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-plasma/latest/gn",
7119
+ 5e3: "https://api.goldsky.com/api/public/project_cm4iagnemt1wp01xn4gh1agft/subgraphs/euler-v2-mantle/latest/gn"
7120
+ };
7121
+ var WRAPPED_NATIVE_FALLBACK = {
7122
+ 1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
7123
+ 10: "0x4200000000000000000000000000000000000006",
7124
+ 56: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
7125
+ 100: "0xe91d153e0b41518a2ce8dd3d7944fa863463a97d",
7126
+ 130: "0x4200000000000000000000000000000000000006",
7127
+ 146: "0x03980325872071166574bcd94670450917897468",
7128
+ 239: "0xB63B9f0eb4A6E6f191529D71d4D88cc8900Df2C9",
7129
+ // TAC: WTAC (https://docs.tac.build/ecosystem/token-list)
7130
+ 480: "0x4200000000000000000000000000000000000006",
7131
+ 999: "0x5555555555555555555555555555555555555555",
7132
+ // HyperEVM: WHYPE (Hyperliquid docs)
7133
+ 1923: "0x4200000000000000000000000000000000000006",
7134
+ 42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
7135
+ 43114: "0xB31f66AA3C1e785363F0875A1B74D27b85FE0459",
7136
+ 5e3: "0x78c1b0C915c4FAA5FffA6CAbf0219DA63d7f4cb8",
7137
+ // Mantle: WMNT
7138
+ 57073: "0x4200000000000000000000000000000000000006",
7139
+ 60808: "0x4200000000000000000000000000000000000006",
7140
+ 80094: "0x6969696969696969696969696969696969696969",
7141
+ // Berachain: WBERA (https://docs.berachain.com)
7142
+ 8453: "0x4200000000000000000000000000000000000006",
7143
+ 9745: "0x6100E367285b01F48D07953803A2d8dCA5D19873"
7144
+ // Plasma: WXPL
7145
+ };
7146
+ for (const id of Object.keys(EULER_V2_GOLDSKY_SUBGRAPH_URL_BY_CHAIN_ID).map(Number)) {
7147
+ if (WRAPPED_NATIVE_FALLBACK[id] == null) {
7148
+ throw new Error(`eulerV2Subgraph: add WRAPPED_NATIVE_FALLBACK for Euler Goldsky chain ${id}`);
7149
+ }
7150
+ }
7151
+ function eulerV2GoldskyUrlForChain(chainId) {
7152
+ return EULER_V2_GOLDSKY_SUBGRAPH_URL_BY_CHAIN_ID[chainId];
7153
+ }
7154
+ async function resolveEulerWrappedNativeToken(chainId) {
7155
+ try {
7156
+ const fromAave = await fetchAaveV4NativeWrappedToken(chainId);
7157
+ if (fromAave) return fromAave;
7158
+ } catch {
7159
+ }
7160
+ const fb = WRAPPED_NATIVE_FALLBACK[chainId];
7161
+ if (fb && isAddress(fb)) return getAddress(fb);
7162
+ return null;
7163
+ }
7164
+ async function eulerSubgraphGql(endpoint, query, variables, chainId) {
7165
+ const body = { query, variables: variables ?? {} };
7166
+ const proxy = getEulerGraphqlProxyUrl();
7167
+ const j = await postJsonViaOptionalProxy({
7168
+ directUrl: endpoint,
7169
+ body,
7170
+ proxyUrl: proxy,
7171
+ proxyEnvelope: chainId != null ? { chainId, query, variables: variables ?? {} } : body
7172
+ });
7173
+ if (j.errors?.length) {
7174
+ throw new Error(j.errors.map((e) => e.message ?? "Unknown").join("; "));
7175
+ }
7176
+ if (j.data == null) throw new Error("Euler subgraph: empty response");
7177
+ return j.data;
7178
+ }
7179
+ async function eulerV2QueryGraphQl(chainId, query, variables) {
7180
+ const endpoint = eulerV2GoldskyUrlForChain(chainId);
7181
+ if (!endpoint) {
7182
+ throw new Error(`Euler v2 subgraph is not configured for chain ${chainId}`);
7183
+ }
7184
+ return eulerSubgraphGql(endpoint, query, variables, chainId);
7185
+ }
7186
+ function normAssetAddr(raw) {
7187
+ const s = (raw ?? "").trim();
7188
+ if (!s || !isAddress(s)) return null;
7189
+ return getAddress(s).toLowerCase();
7190
+ }
7191
+ function vaultShowsBorrowTab(borrowCap, totalBorrows) {
7192
+ let borrows = 0n;
7193
+ try {
7194
+ borrows = BigInt((totalBorrows ?? "").trim() || "0");
7195
+ } catch {
7196
+ borrows = 0n;
7197
+ }
7198
+ if (borrows > 0n) return true;
7199
+ const w = eulerSubgraphVaultCapToUnderlyingWei(borrowCap);
7200
+ if (w >= EULER_EVAULT_CAP_UNLIMITED_WEI - 3n) return true;
7201
+ return w > 0n;
7202
+ }
7203
+ var VAULT_PAGE = `
7204
+ query V($first: Int!, $skip: Int!) {
7205
+ eulerVaults(first: $first, skip: $skip) {
7206
+ asset
7207
+ borrowCap
7208
+ state { totalBorrows }
7209
+ }
7210
+ }
7211
+ `;
7212
+ var EARN_PAGE = `
7213
+ query E($first: Int!, $skip: Int!) {
7214
+ eulerEarnVaults(first: $first, skip: $skip) {
7215
+ asset
7216
+ }
7217
+ }
7218
+ `;
7219
+ async function paginateEulerVaultRows(chainId) {
7220
+ const out = [];
7221
+ let skip = 0;
7222
+ const first = 1e3;
7223
+ while (true) {
7224
+ const d = await eulerV2QueryGraphQl(chainId, VAULT_PAGE, { first, skip });
7225
+ const rows = d.eulerVaults ?? [];
7226
+ if (!rows.length) break;
7227
+ for (const r of rows) {
7228
+ const tb = (r.state?.totalBorrows ?? "0").toString();
7229
+ out.push({ asset: r.asset ?? null, borrowCap: r.borrowCap, totalBorrows: tb });
7230
+ }
7231
+ if (rows.length < first) break;
7232
+ skip += first;
7233
+ }
7234
+ return out;
7235
+ }
7236
+ async function paginateEulerEarnAssets(chainId) {
7237
+ const assets = [];
7238
+ let skip = 0;
7239
+ const first = 1e3;
7240
+ while (true) {
7241
+ const d = await eulerV2QueryGraphQl(
7242
+ chainId,
7243
+ EARN_PAGE,
7244
+ {
7245
+ first,
7246
+ skip
7247
+ }
7248
+ );
7249
+ const rows = d.eulerEarnVaults ?? [];
7250
+ if (!rows.length) break;
7251
+ for (const r of rows) {
7252
+ const a = normAssetAddr(r.asset ?? void 0);
7253
+ if (a) assets.push(a);
7254
+ }
7255
+ if (rows.length < first) break;
7256
+ skip += first;
7257
+ }
7258
+ return assets;
7259
+ }
7260
+ var eulerV2ChainAssetCache = /* @__PURE__ */ new Map();
7261
+ async function fetchEulerV2ChainAssetCache(chainId) {
7262
+ const endpoint = eulerV2GoldskyUrlForChain(chainId);
7263
+ if (!endpoint) {
7264
+ return { nativeWrapped: null, modesByUnderlying: /* @__PURE__ */ new Map() };
7265
+ }
7266
+ const [nativeWrapped, vaultRows, earnAssets] = await Promise.all([
7267
+ resolveEulerWrappedNativeToken(chainId),
7268
+ paginateEulerVaultRows(chainId),
7269
+ paginateEulerEarnAssets(chainId)
7270
+ ]);
7271
+ const modes = /* @__PURE__ */ new Map();
7272
+ const bump = (addr, patch) => {
7273
+ const cur = modes.get(addr) ?? { lend: false, borrow: false, earn: false };
7274
+ modes.set(addr, {
7275
+ lend: cur.lend || !!patch.lend,
7276
+ borrow: cur.borrow || !!patch.borrow,
7277
+ earn: cur.earn || !!patch.earn
7278
+ });
7279
+ };
7280
+ for (const e of earnAssets) bump(e, { earn: true });
7281
+ for (const row of vaultRows) {
7282
+ const a = normAssetAddr(row.asset);
7283
+ if (!a) continue;
7284
+ bump(a, {
7285
+ lend: true,
7286
+ borrow: vaultShowsBorrowTab(row.borrowCap, row.totalBorrows)
7287
+ });
7288
+ }
7289
+ return { nativeWrapped, modesByUnderlying: modes };
7290
+ }
7291
+ function ensureEulerV2ChainAssetCache(chainId) {
7292
+ const hit = eulerV2ChainAssetCache.get(chainId);
7293
+ if (hit) return hit;
7294
+ const p = fetchEulerV2ChainAssetCache(chainId).catch((e) => {
7295
+ eulerV2ChainAssetCache.delete(chainId);
7296
+ throw e;
7297
+ });
7298
+ eulerV2ChainAssetCache.set(chainId, p);
7299
+ return p;
7300
+ }
7301
+
7302
+ // src/protocols/evm/euler-v2/loadEulerV2SupportedChainIds.ts
7303
+ var cached = null;
7304
+ function loadEulerV2SupportedChainIds() {
7305
+ if (cached) return Promise.resolve(cached);
7306
+ cached = new Set(Object.keys(EULER_V2_GOLDSKY_SUBGRAPH_URL_BY_CHAIN_ID).map((k) => Number(k)));
7307
+ return Promise.resolve(cached);
7308
+ }
7070
7309
 
7071
7310
  // src/protocols/evm/euler-v2/index.ts
7072
7311
  var EULER_V2_PROTOCOL_ID = "euler-v2";
@@ -9043,20 +9282,6 @@ function getProtocolDiscoverySummary(protocolId) {
9043
9282
  };
9044
9283
  }
9045
9284
  init_support();
9046
- var EULER_V2_SUBGRAPH_CHAIN_IDS = [
9047
- 1,
9048
- 8453,
9049
- 42161,
9050
- 10,
9051
- 137,
9052
- 56,
9053
- 43114,
9054
- 100,
9055
- 59144,
9056
- 146,
9057
- 1923,
9058
- 130
9059
- ];
9060
9285
  function advisor(protocolId, tokenFilter, impl) {
9061
9286
  return { protocolId, tokenFilter, ...impl };
9062
9287
  }
@@ -9290,13 +9515,34 @@ var PROTOCOL_SUPPORT_ADVISORS = {
9290
9515
  }),
9291
9516
  "euler-v2": advisor("euler-v2", "subgraph_vaults", {
9292
9517
  async supportedChainIds() {
9293
- return [...EULER_V2_SUBGRAPH_CHAIN_IDS];
9518
+ const set = await loadEulerV2SupportedChainIds();
9519
+ return [...set].sort((a, b) => a - b);
9294
9520
  },
9295
- async supportedTokens() {
9521
+ async supportedTokens(chainId) {
9522
+ const cache = await ensureEulerV2ChainAssetCache(chainId);
9523
+ const tokens = [...cache.modesByUnderlying.entries()].map(([address, modes]) => ({
9524
+ address,
9525
+ roles: [
9526
+ ...modes.lend ? ["vault_underlying"] : [],
9527
+ ...modes.borrow ? ["loan"] : [],
9528
+ ...modes.earn ? ["earn"] : []
9529
+ ]
9530
+ }));
9296
9531
  return {
9297
- tokens: [],
9298
- notes: "Euler vault/collateral assets vary by chain. Pass vault and asset addresses from Euler app or subgraph."
9532
+ tokens,
9533
+ nativeWrapped: cache.nativeWrapped ?? void 0,
9534
+ notes: "Euler EVK vault underlyings and Earn vault assets from the Goldsky subgraph. Use address as underlyingAddress for ctm_euler_v2_fetch_lend_vaults."
9299
9535
  };
9536
+ },
9537
+ async isTokenSupported(chainId, address) {
9538
+ const cache = await ensureEulerV2ChainAssetCache(chainId);
9539
+ let normalized;
9540
+ try {
9541
+ normalized = getAddress(address).toLowerCase();
9542
+ } catch {
9543
+ return false;
9544
+ }
9545
+ return cache.modesByUnderlying.has(normalized);
9300
9546
  }
9301
9547
  }),
9302
9548
  [MAPLE_PROTOCOL_ID]: advisor(MAPLE_PROTOCOL_ID, "fixed_addresses", {