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