@owney/sdk 0.7.29-beta.0 → 0.7.29

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.cjs CHANGED
@@ -509,9 +509,18 @@ function netApy(net, gross, field) {
509
509
  debugLog("zyfai:apy", `gross fallback for ${field}`, { gross });
510
510
  return gross;
511
511
  }
512
- function rawPoolApyForChain(entry, chainId, tokenSymbol) {
512
+ function usdPerUnit(tokenSymbol, prices) {
513
+ const symbol = tokenSymbol?.toUpperCase();
514
+ if (symbol === "USDC") return 1;
515
+ const price = symbol ? prices?.[symbol] : void 0;
516
+ return typeof price === "number" && Number.isFinite(price) && price > 0 ? price : void 0;
517
+ }
518
+ function rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices) {
513
519
  let weightedSum = 0;
514
520
  let totalBalance = 0;
521
+ let usdWeightedSum = 0;
522
+ let usdTotal = 0;
523
+ let unpriced = false;
515
524
  for (const p of entry.positions ?? []) {
516
525
  if (chainId !== void 0 && p.chainId !== chainId) continue;
517
526
  if (tokenSymbol && p.tokenSymbol?.toUpperCase() !== tokenSymbol.toUpperCase()) continue;
@@ -520,7 +529,16 @@ function rawPoolApyForChain(entry, chainId, tokenSymbol) {
520
529
  if (balance <= 0 || typeof apy !== "number") continue;
521
530
  weightedSum += apy * balance;
522
531
  totalBalance += balance;
532
+ if (tokenSymbol) continue;
533
+ const price = usdPerUnit(p.tokenSymbol, usdPrices);
534
+ if (price === void 0) {
535
+ unpriced = true;
536
+ continue;
537
+ }
538
+ usdWeightedSum += apy * balance * price;
539
+ usdTotal += balance * price;
523
540
  }
541
+ if (!tokenSymbol && !unpriced && usdTotal > 0) return usdWeightedSum / usdTotal;
524
542
  return totalBalance > 0 ? weightedSum / totalBalance : null;
525
543
  }
526
544
  function historyCoverage(raw) {
@@ -555,10 +573,10 @@ function historyCoverage(raw) {
555
573
  pairs: [...pairs.values()]
556
574
  };
557
575
  }
558
- function mapApyHistory(raw, chainId, tokenSymbol) {
576
+ function mapApyHistory(raw, chainId, tokenSymbol, usdPrices) {
559
577
  const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({
560
578
  date,
561
- apy: rawPoolApyForChain(entry, chainId, tokenSymbol),
579
+ apy: rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices),
562
580
  // Provider position balances are treated as decimal amounts of the
563
581
  // selected token. Carry its unit dynamically for every asset; never
564
582
  // attach a token unit to an unscoped mix of assets.
@@ -1187,6 +1205,15 @@ function assemblePoolCatalog(protocols, poolsByProtocol, asset, chains) {
1187
1205
  var ERC7579_IS_MODULE_INSTALLED_ABI = (0, import_viem.parseAbi)([
1188
1206
  "function isModuleInstalled(uint256 moduleTypeId, address module, bytes additionalContext) view returns (bool)"
1189
1207
  ]);
1208
+ var CHAINLINK_ETH_USD_FEED = {
1209
+ 8453: "0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70",
1210
+ 42161: "0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612",
1211
+ 1: "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"
1212
+ };
1213
+ var CHAINLINK_ABI = (0, import_viem.parseAbi)([
1214
+ "function latestRoundData() view returns (uint80, int256, uint256, uint256, uint80)"
1215
+ ]);
1216
+ var ETH_USD_TTL_MS = 6e4;
1190
1217
  var DEFAULT_ZYFAI_RPC_URLS = {
1191
1218
  8453: "https://base-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
1192
1219
  42161: "https://arb-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
@@ -1252,6 +1279,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
1252
1279
  // successful decode is cached forever; failures are NOT cached so a transient
1253
1280
  // RPC error retries on the next fetch.
1254
1281
  withdrawAmountCache = /* @__PURE__ */ new Map();
1282
+ ethUsd = null;
1255
1283
  earningsReads = /* @__PURE__ */ new Map();
1256
1284
  earningsRefreshes = /* @__PURE__ */ new Map();
1257
1285
  earningsSnapshot = null;
@@ -2299,10 +2327,41 @@ var ZyfaiAgent = class _ZyfaiAgent {
2299
2327
  return pending;
2300
2328
  }
2301
2329
  async getAccountApy(state, chainId, days, tokenSymbol, scopeChainId) {
2302
- const { smartWallet } = await this.resolveSmartWallet(state, chainId);
2303
- const raw = await this.sdk.getDailyApyHistory(smartWallet, days);
2330
+ const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
2331
+ const [raw, ethUsd] = await Promise.all([
2332
+ this.sdk.getDailyApyHistory(smartWallet, days),
2333
+ tokenSymbol ? void 0 : this.readEthUsd(validChainId)
2334
+ ]);
2304
2335
  const scope = scopeChainId === null ? void 0 : scopeChainId ?? chainId;
2305
- return mapApyHistory(raw, scope, tokenSymbol);
2336
+ const usdPrices = ethUsd ? { WETH: ethUsd, ETH: ethUsd } : void 0;
2337
+ return mapApyHistory(raw, scope, tokenSymbol, usdPrices);
2338
+ }
2339
+ /**
2340
+ * Today's ETH/USD from Chainlink, cached for a minute. Every day of the
2341
+ * series is weighted at this one price: each day's mix is right, but ETH's
2342
+ * move over the window is not reflected (ROUT-682). Undefined on failure,
2343
+ * which leaves the series on token-amount weighting rather than failing it.
2344
+ */
2345
+ async readEthUsd(chainId) {
2346
+ if (this.ethUsd && Date.now() - this.ethUsd.at < ETH_USD_TTL_MS) {
2347
+ return this.ethUsd.price;
2348
+ }
2349
+ try {
2350
+ const [, answer] = await this.getPublicClient(chainId).readContract({
2351
+ address: CHAINLINK_ETH_USD_FEED[chainId],
2352
+ abi: CHAINLINK_ABI,
2353
+ functionName: "latestRoundData"
2354
+ });
2355
+ const price = Number(answer) / 1e8;
2356
+ if (!Number.isFinite(price) || price <= 0) return void 0;
2357
+ this.ethUsd = { price, at: Date.now() };
2358
+ return price;
2359
+ } catch (err) {
2360
+ debugLog("zyfai:apy", "ETH/USD read failed; token-amount weighting", {
2361
+ err
2362
+ });
2363
+ return void 0;
2364
+ }
2306
2365
  }
2307
2366
  async getDailyEarnings(state, chainId, days, tokenSymbol) {
2308
2367
  const { smartWallet } = await this.resolveSmartWallet(state, chainId);
package/dist/index.js CHANGED
@@ -469,9 +469,18 @@ function netApy(net, gross, field) {
469
469
  debugLog("zyfai:apy", `gross fallback for ${field}`, { gross });
470
470
  return gross;
471
471
  }
472
- function rawPoolApyForChain(entry, chainId, tokenSymbol) {
472
+ function usdPerUnit(tokenSymbol, prices) {
473
+ const symbol = tokenSymbol?.toUpperCase();
474
+ if (symbol === "USDC") return 1;
475
+ const price = symbol ? prices?.[symbol] : void 0;
476
+ return typeof price === "number" && Number.isFinite(price) && price > 0 ? price : void 0;
477
+ }
478
+ function rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices) {
473
479
  let weightedSum = 0;
474
480
  let totalBalance = 0;
481
+ let usdWeightedSum = 0;
482
+ let usdTotal = 0;
483
+ let unpriced = false;
475
484
  for (const p of entry.positions ?? []) {
476
485
  if (chainId !== void 0 && p.chainId !== chainId) continue;
477
486
  if (tokenSymbol && p.tokenSymbol?.toUpperCase() !== tokenSymbol.toUpperCase()) continue;
@@ -480,7 +489,16 @@ function rawPoolApyForChain(entry, chainId, tokenSymbol) {
480
489
  if (balance <= 0 || typeof apy !== "number") continue;
481
490
  weightedSum += apy * balance;
482
491
  totalBalance += balance;
492
+ if (tokenSymbol) continue;
493
+ const price = usdPerUnit(p.tokenSymbol, usdPrices);
494
+ if (price === void 0) {
495
+ unpriced = true;
496
+ continue;
497
+ }
498
+ usdWeightedSum += apy * balance * price;
499
+ usdTotal += balance * price;
483
500
  }
501
+ if (!tokenSymbol && !unpriced && usdTotal > 0) return usdWeightedSum / usdTotal;
484
502
  return totalBalance > 0 ? weightedSum / totalBalance : null;
485
503
  }
486
504
  function historyCoverage(raw) {
@@ -515,10 +533,10 @@ function historyCoverage(raw) {
515
533
  pairs: [...pairs.values()]
516
534
  };
517
535
  }
518
- function mapApyHistory(raw, chainId, tokenSymbol) {
536
+ function mapApyHistory(raw, chainId, tokenSymbol, usdPrices) {
519
537
  const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({
520
538
  date,
521
- apy: rawPoolApyForChain(entry, chainId, tokenSymbol),
539
+ apy: rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices),
522
540
  // Provider position balances are treated as decimal amounts of the
523
541
  // selected token. Carry its unit dynamically for every asset; never
524
542
  // attach a token unit to an unscoped mix of assets.
@@ -1147,6 +1165,15 @@ function assemblePoolCatalog(protocols, poolsByProtocol, asset, chains) {
1147
1165
  var ERC7579_IS_MODULE_INSTALLED_ABI = parseAbi([
1148
1166
  "function isModuleInstalled(uint256 moduleTypeId, address module, bytes additionalContext) view returns (bool)"
1149
1167
  ]);
1168
+ var CHAINLINK_ETH_USD_FEED = {
1169
+ 8453: "0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70",
1170
+ 42161: "0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612",
1171
+ 1: "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"
1172
+ };
1173
+ var CHAINLINK_ABI = parseAbi([
1174
+ "function latestRoundData() view returns (uint80, int256, uint256, uint256, uint80)"
1175
+ ]);
1176
+ var ETH_USD_TTL_MS = 6e4;
1150
1177
  var DEFAULT_ZYFAI_RPC_URLS = {
1151
1178
  8453: "https://base-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
1152
1179
  42161: "https://arb-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
@@ -1212,6 +1239,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
1212
1239
  // successful decode is cached forever; failures are NOT cached so a transient
1213
1240
  // RPC error retries on the next fetch.
1214
1241
  withdrawAmountCache = /* @__PURE__ */ new Map();
1242
+ ethUsd = null;
1215
1243
  earningsReads = /* @__PURE__ */ new Map();
1216
1244
  earningsRefreshes = /* @__PURE__ */ new Map();
1217
1245
  earningsSnapshot = null;
@@ -2259,10 +2287,41 @@ var ZyfaiAgent = class _ZyfaiAgent {
2259
2287
  return pending;
2260
2288
  }
2261
2289
  async getAccountApy(state, chainId, days, tokenSymbol, scopeChainId) {
2262
- const { smartWallet } = await this.resolveSmartWallet(state, chainId);
2263
- const raw = await this.sdk.getDailyApyHistory(smartWallet, days);
2290
+ const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
2291
+ const [raw, ethUsd] = await Promise.all([
2292
+ this.sdk.getDailyApyHistory(smartWallet, days),
2293
+ tokenSymbol ? void 0 : this.readEthUsd(validChainId)
2294
+ ]);
2264
2295
  const scope = scopeChainId === null ? void 0 : scopeChainId ?? chainId;
2265
- return mapApyHistory(raw, scope, tokenSymbol);
2296
+ const usdPrices = ethUsd ? { WETH: ethUsd, ETH: ethUsd } : void 0;
2297
+ return mapApyHistory(raw, scope, tokenSymbol, usdPrices);
2298
+ }
2299
+ /**
2300
+ * Today's ETH/USD from Chainlink, cached for a minute. Every day of the
2301
+ * series is weighted at this one price: each day's mix is right, but ETH's
2302
+ * move over the window is not reflected (ROUT-682). Undefined on failure,
2303
+ * which leaves the series on token-amount weighting rather than failing it.
2304
+ */
2305
+ async readEthUsd(chainId) {
2306
+ if (this.ethUsd && Date.now() - this.ethUsd.at < ETH_USD_TTL_MS) {
2307
+ return this.ethUsd.price;
2308
+ }
2309
+ try {
2310
+ const [, answer] = await this.getPublicClient(chainId).readContract({
2311
+ address: CHAINLINK_ETH_USD_FEED[chainId],
2312
+ abi: CHAINLINK_ABI,
2313
+ functionName: "latestRoundData"
2314
+ });
2315
+ const price = Number(answer) / 1e8;
2316
+ if (!Number.isFinite(price) || price <= 0) return void 0;
2317
+ this.ethUsd = { price, at: Date.now() };
2318
+ return price;
2319
+ } catch (err) {
2320
+ debugLog("zyfai:apy", "ETH/USD read failed; token-amount weighting", {
2321
+ err
2322
+ });
2323
+ return void 0;
2324
+ }
2266
2325
  }
2267
2326
  async getDailyEarnings(state, chainId, days, tokenSymbol) {
2268
2327
  const { smartWallet } = await this.resolveSmartWallet(state, chainId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.7.29-beta.0",
3
+ "version": "0.7.29",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",