@kasufinance/kasu-sdk 2.13.1 → 2.14.0

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.
@@ -15500,6 +15500,185 @@ KasuPoolExternalTVLAbi__factory.abi = _abi;
15500
15500
  function poolTvl(onChainBalance, offChainTvl) {
15501
15501
  return parseFloat(onChainBalance) + parseFloat(offChainTvl);
15502
15502
  }
15503
+ /**
15504
+ * What `combineTvlHistories` throws instead of returning a number it cannot
15505
+ * stand behind. `leg` and `entry` are the zero-based position of the
15506
+ * offending input, when there is one.
15507
+ */
15508
+ class TvlHistoryInputError extends Error {
15509
+ constructor(code, leg, entry) {
15510
+ const at = [
15511
+ leg === undefined ? '' : ` at leg ${String(leg)}`,
15512
+ entry === undefined ? '' : `, entry ${String(entry)}`,
15513
+ ].join('');
15514
+ super(`combineTvlHistories: input refused (${code})${at}`);
15515
+ this.name = 'TvlHistoryInputError';
15516
+ this.code = code;
15517
+ this.leg = leg;
15518
+ this.entry = entry;
15519
+ Object.setPrototypeOf(this, TvlHistoryInputError.prototype);
15520
+ }
15521
+ }
15522
+ function isNonNegativeFinite(value) {
15523
+ return typeof value === 'number' && Number.isFinite(value) && value >= 0;
15524
+ }
15525
+ /**
15526
+ * Throws unless the leg is well formed. Index loops, not `forEach`: a hole in
15527
+ * a sparse array is skipped by the array methods and would surface later as
15528
+ * a raw `TypeError` instead of a refusal.
15529
+ */
15530
+ function validateLeg(leg, legIndex) {
15531
+ if (typeof leg !== 'object' || leg === null) {
15532
+ throw new TvlHistoryInputError('leg', legIndex);
15533
+ }
15534
+ const { entries, usdRate, opening } = leg;
15535
+ if (typeof usdRate !== 'number' ||
15536
+ !Number.isFinite(usdRate) ||
15537
+ usdRate <= 0) {
15538
+ throw new TvlHistoryInputError('usd-rate', legIndex);
15539
+ }
15540
+ if (!Array.isArray(entries)) {
15541
+ throw new TvlHistoryInputError('entries', legIndex);
15542
+ }
15543
+ const list = entries;
15544
+ if (list.length === 0) {
15545
+ throw new TvlHistoryInputError('empty-leg', legIndex);
15546
+ }
15547
+ let previous = -Infinity;
15548
+ for (let index = 0; index < list.length; index += 1) {
15549
+ const entry = list[index];
15550
+ if (typeof entry !== 'object' || entry === null) {
15551
+ throw new TvlHistoryInputError('entry', legIndex, index);
15552
+ }
15553
+ const { timestamp, tvl, kind } = entry;
15554
+ if (!isNonNegativeFinite(timestamp)) {
15555
+ throw new TvlHistoryInputError('timestamp', legIndex, index);
15556
+ }
15557
+ if (!isNonNegativeFinite(tvl)) {
15558
+ throw new TvlHistoryInputError('tvl', legIndex, index);
15559
+ }
15560
+ if (kind !== 'clearing' && kind !== 'current') {
15561
+ throw new TvlHistoryInputError('kind', legIndex, index);
15562
+ }
15563
+ if (kind === 'current' && index !== list.length - 1) {
15564
+ throw new TvlHistoryInputError('current', legIndex, index);
15565
+ }
15566
+ if (timestamp < previous) {
15567
+ throw new TvlHistoryInputError('order', legIndex, index);
15568
+ }
15569
+ previous = timestamp;
15570
+ }
15571
+ if (opening !== undefined && opening !== null) {
15572
+ const first = list[0];
15573
+ const { timestamp, tvl, kind } = (typeof opening === 'object' ? opening : {});
15574
+ if (!isNonNegativeFinite(timestamp) ||
15575
+ !isNonNegativeFinite(tvl) ||
15576
+ kind !== 'clearing' ||
15577
+ timestamp > first.timestamp) {
15578
+ throw new TvlHistoryInputError('opening', legIndex);
15579
+ }
15580
+ }
15581
+ return leg;
15582
+ }
15583
+ /**
15584
+ * A strategy's TVL in USD over time, from its deployments' histories — Tax
15585
+ * Pay runs on Base (USDC) and on XDC (AUDD), and its total lent is both.
15586
+ * Pure: the caller fetches each leg (`strategyDeployments` names them) and
15587
+ * passes the exchange rates.
15588
+ *
15589
+ * A step merge by time. At every distinct timestamp where any leg has a
15590
+ * clearing, the value is the sum over legs of that leg's latest point at or
15591
+ * before then, times its `usdRate`. A leg's `opening` — what it held just
15592
+ * before the window — is one more step: it counts from its own timestamp
15593
+ * until the leg's first entry, and the leg is 0 before it (or throughout,
15594
+ * before its first entry, without one).
15595
+ * Points that share a timestamp — clearings run in one block, or two chains
15596
+ * clearing in the same second — are one output point. The series ends with
15597
+ * one `current` point at the latest of the legs' `current` timestamps, equal
15598
+ * to the sum of their `current` values. Every leg must end in a `current`
15599
+ * point, or none may.
15600
+ *
15601
+ * The window is the union of the legs' windows: the series starts at the
15602
+ * earliest first entry of any leg. An `opening` is a value, never a point.
15603
+ * Without it, two windows cut at offset clearing times would count the
15604
+ * later-starting leg 0 at the left edge and draw a false step up; with it,
15605
+ * only a genuine late start (`opening: null`) counts 0.
15606
+ *
15607
+ * Every leg and entry is validated first: a bad input — an empty leg
15608
+ * included — throws `TvlHistoryInputError` (its `code` says why), so the
15609
+ * result never holds `NaN` or `Infinity`. The inputs are not modified.
15610
+ *
15611
+ * ```ts
15612
+ * const window = { count: 52 };
15613
+ * const points = combineTvlHistories([
15614
+ * { ...(await base.strategies.getPoolTvlHistoryWindow(basePool, window)), usdRate: 1 },
15615
+ * { ...(await xdc.strategies.getPoolTvlHistoryWindow(xdcPool, window)), usdRate: audToUsd },
15616
+ * ]);
15617
+ * ```
15618
+ */
15619
+ function combineTvlHistories(legs) {
15620
+ if (!Array.isArray(legs) || legs.length === 0) {
15621
+ throw new TvlHistoryInputError('legs');
15622
+ }
15623
+ const valid = [];
15624
+ for (let index = 0; index < legs.length; index += 1) {
15625
+ valid.push(validateLeg(legs[index], index));
15626
+ }
15627
+ const endsInCurrent = valid.map((leg) => leg.entries[leg.entries.length - 1].kind === 'current');
15628
+ const odd = endsInCurrent.findIndex((ends) => ends !== endsInCurrent[0]);
15629
+ if (odd !== -1)
15630
+ throw new TvlHistoryInputError('current', odd);
15631
+ const finalTimestamp = endsInCurrent[0]
15632
+ ? Math.max(...valid.map((leg) => leg.entries[leg.entries.length - 1].timestamp))
15633
+ : undefined;
15634
+ const clearingTimes = [
15635
+ ...new Set(valid.flatMap((leg) => leg.entries
15636
+ .filter((entry) => entry.kind === 'clearing')
15637
+ .map((entry) => entry.timestamp))),
15638
+ ]
15639
+ .filter((t) => finalTimestamp === undefined || t < finalTimestamp)
15640
+ .sort((a, b) => a - b);
15641
+ // One cursor per leg: the times ascend, so each leg is walked once. A
15642
+ // leg's `current` point is a step like any other, which keeps a chain
15643
+ // whose subgraph lags behind the other's last clearing at its own latest.
15644
+ const cursors = valid.map(() => -1);
15645
+ const sumAt = (t) => {
15646
+ let sum = 0;
15647
+ valid.forEach((leg, index) => {
15648
+ let cursor = cursors[index];
15649
+ while (cursor + 1 < leg.entries.length &&
15650
+ leg.entries[cursor + 1].timestamp <= t) {
15651
+ cursor += 1;
15652
+ }
15653
+ cursors[index] = cursor;
15654
+ // Before the first entry, the opening is the step — from its
15655
+ // own time on, never earlier.
15656
+ const opening = leg.opening;
15657
+ const value = cursor >= 0
15658
+ ? leg.entries[cursor].tvl
15659
+ : opening && opening.timestamp <= t
15660
+ ? opening.tvl
15661
+ : 0;
15662
+ sum += value * leg.usdRate;
15663
+ });
15664
+ if (!Number.isFinite(sum))
15665
+ throw new TvlHistoryInputError('overflow');
15666
+ return sum;
15667
+ };
15668
+ const combined = clearingTimes.map((t) => ({
15669
+ timestamp: t,
15670
+ tvlUsd: sumAt(t),
15671
+ kind: 'clearing',
15672
+ }));
15673
+ if (finalTimestamp !== undefined) {
15674
+ combined.push({
15675
+ timestamp: finalTimestamp,
15676
+ tvlUsd: sumAt(finalTimestamp),
15677
+ kind: 'current',
15678
+ });
15679
+ }
15680
+ return combined;
15681
+ }
15503
15682
 
15504
15683
  /**
15505
15684
  * Epochs in a year — the single source of truth for every rate conversion in
@@ -16248,6 +16427,95 @@ function fetchPoolClearings(request, poolId, scope, cancellation) {
16248
16427
  return { clearings, snapshot, requests };
16249
16428
  });
16250
16429
  }
16430
+ /**
16431
+ * Pure: the opening clearing — the newest of `before` (finished clearings the
16432
+ * window left out, older by epoch) that finished STRICTLY before the first
16433
+ * in-window clearing's block. Newest by block, then epoch, so a clearing
16434
+ * numbered out of order (XDC AUDD's epoch 1, a week before epoch 88) is
16435
+ * placed by when it happened. A clearing in the first in-window block itself
16436
+ * (a catch-up block that finished several epochs at once) is not an opening:
16437
+ * it holds the first entry's own value, not the one before it. `null` when
16438
+ * there is none — see `needsClearingBeforeBlock`.
16439
+ */
16440
+ function pickOpeningClearing(before, inWindow) {
16441
+ const firstBlock = Math.min(...inWindow.map((c) => c.block));
16442
+ let opening = null;
16443
+ for (const clearing of before) {
16444
+ if (clearing.block >= firstBlock)
16445
+ continue;
16446
+ if (opening === null ||
16447
+ clearing.block > opening.block ||
16448
+ (clearing.block === opening.block &&
16449
+ clearing.epochId > opening.epochId)) {
16450
+ opening = clearing;
16451
+ }
16452
+ }
16453
+ return opening;
16454
+ }
16455
+ /**
16456
+ * Pure: whether the opening needs `fetchClearingBefore`'s by-block read.
16457
+ * Only when the window left clearings out but every one of them finished in
16458
+ * or after the first in-window block — the extra rows sat in a catch-up
16459
+ * block — so an older one, if any, was not fetched. Otherwise the opening
16460
+ * (or its absence) is already known and nothing more is read.
16461
+ */
16462
+ function needsClearingBeforeBlock(before, opening) {
16463
+ return opening === null && before.length > 0;
16464
+ }
16465
+ /**
16466
+ * The newest finished clearing in a block below `blockLt`, by block: the
16467
+ * opening when every clearing the window left out shares its first block.
16468
+ * ONE extra request for one row, no snapshot. Clearings that share that
16469
+ * older block share its time and balance, so which one comes back does not
16470
+ * change the value.
16471
+ */
16472
+ const poolTvlClearingBeforeBlockQuery = gql `
16473
+ query PoolTvlClearingBeforeBlock($pool: String!, $blockLt: BigInt!) {
16474
+ clearings: lendingPoolClearings(
16475
+ first: 1
16476
+ orderBy: endBlockNumber
16477
+ orderDirection: desc
16478
+ where: {
16479
+ lendingPool: $pool
16480
+ isFinished: true
16481
+ endBlockNumber_lt: $blockLt
16482
+ }
16483
+ ) {
16484
+ epochId
16485
+ endBlockNumber
16486
+ endTimestamp
16487
+ }
16488
+ }
16489
+ `;
16490
+ function fetchClearingBeforeBlock(request, poolId, blockLt, cancellation) {
16491
+ return __awaiter(this, void 0, void 0, function* () {
16492
+ cancellation.throwIfCancelled();
16493
+ const page = yield request(poolTvlClearingBeforeBlockQuery, { pool: poolId, blockLt: String(blockLt) }, cancellation.signal);
16494
+ const row = page.clearings.at(0);
16495
+ return row === undefined ? null : parseClearing(row);
16496
+ });
16497
+ }
16498
+ /**
16499
+ * The newest finished clearing numbered below `beforeEpoch`: the opening
16500
+ * candidate of a `fromEpoch` window, whose own fetch never reads that far
16501
+ * back. ONE extra request for one row, no snapshot.
16502
+ */
16503
+ function fetchClearingBefore(request, poolId, beforeEpoch, cancellation) {
16504
+ return __awaiter(this, void 0, void 0, function* () {
16505
+ cancellation.throwIfCancelled();
16506
+ const page = yield request(poolTvlClearingsQuery, {
16507
+ poolId,
16508
+ pool: poolId,
16509
+ epochGte: '0',
16510
+ epochLt: String(beforeEpoch),
16511
+ direction: 'desc',
16512
+ first: 1,
16513
+ withSnapshot: false,
16514
+ }, cancellation.signal);
16515
+ const row = page.clearings.at(0);
16516
+ return row === undefined ? null : parseClearing(row);
16517
+ });
16518
+ }
16251
16519
  /**
16252
16520
  * The pool's balance (display units, the subgraph's decimal string) at the end
16253
16521
  * of each block, `TVL_HISTORY_BLOCKS_PER_REQUEST` blocks per request. A block
@@ -16651,6 +16919,42 @@ class DataService {
16651
16919
  */
16652
16920
  getPoolTvlHistory(poolId_1) {
16653
16921
  return __awaiter(this, arguments, void 0, function* (poolId, options = {}) {
16922
+ return (yield this._poolTvlHistory(poolId, options, false)).entries;
16923
+ });
16924
+ }
16925
+ /**
16926
+ * `getPoolTvlHistory` — the same options, errors and `entries` — plus
16927
+ * `opening`: the pool's value just before the window, so a strategy
16928
+ * summed across deployments (`combineTvlHistories`) knows what each leg
16929
+ * held before its first point instead of counting it 0.
16930
+ *
16931
+ * `opening` is the newest finished clearing STRICTLY before the window's
16932
+ * first block (by `pickOpeningClearing`), read at its own block exactly
16933
+ * like every other point and through the same past-block cache; `null`
16934
+ * when the pool has no clearing before the window. Extra cost over
16935
+ * `getPoolTvlHistory`:
16936
+ *
16937
+ * - `count` only: one more clearing row in the same request (`count + 1`,
16938
+ * which always reaches past the window when older history exists);
16939
+ * - `fromEpoch` above 0: one more one-row subgraph request, in parallel
16940
+ * with the clearings;
16941
+ * - only when every left-out clearing finished in the window's first
16942
+ * block (a catch-up block that closed several epochs at once): one
16943
+ * more one-row request by block (`fetchClearingBeforeBlock`), after
16944
+ * the clearings — the common case never makes it;
16945
+ * - either way, the opening's block in the same batched time-travel read
16946
+ * and one more `externalTVLOfPool` read (cached once final).
16947
+ *
16948
+ * `{ entries: [], opening: null }` wherever `getPoolTvlHistory` returns
16949
+ * `[]`.
16950
+ */
16951
+ getPoolTvlHistoryWindow(poolId_1) {
16952
+ return __awaiter(this, arguments, void 0, function* (poolId, options = {}) {
16953
+ return yield this._poolTvlHistory(poolId, options, true);
16954
+ });
16955
+ }
16956
+ _poolTvlHistory(poolId, options, withOpening) {
16957
+ return __awaiter(this, void 0, void 0, function* () {
16654
16958
  var _a;
16655
16959
  validatePoolTvlHistoryOptions(options);
16656
16960
  if (!isAddress(poolId)) {
@@ -16669,33 +16973,56 @@ class DataService {
16669
16973
  rpcs,
16670
16974
  cancellation,
16671
16975
  })
16672
- : null, externalTvl !== null && externalTvl !== void 0 ? externalTvl : '', cancellation));
16976
+ : null, externalTvl !== null && externalTvl !== void 0 ? externalTvl : '', cancellation, withOpening));
16673
16977
  });
16674
16978
  }
16675
- _readPoolTvlHistory(pool, options, reader, externalTvl, cancellation) {
16979
+ _readPoolTvlHistory(pool, options, reader, externalTvl, cancellation, withOpening) {
16676
16980
  return __awaiter(this, void 0, void 0, function* () {
16677
16981
  var _a, _b, _c;
16982
+ const nothing = { entries: [], opening: null };
16678
16983
  const graph = (document, variables, signal) => this._graph.request({ document, variables, signal });
16679
16984
  const scope = options.fromEpoch === undefined && options.count !== undefined
16680
- ? { kind: 'newest', count: options.count }
16985
+ ? {
16986
+ kind: 'newest',
16987
+ // One past the window: the opening, when there is one.
16988
+ count: options.count + (withOpening ? 1 : 0),
16989
+ }
16681
16990
  : { kind: 'from', fromEpoch: (_a = options.fromEpoch) !== null && _a !== void 0 ? _a : 0 };
16682
16991
  try {
16683
- const [currentEpochNumber, { clearings, snapshot }] = yield Promise.all([
16992
+ const [currentEpochNumber, { clearings, snapshot }, before] = yield Promise.all([
16684
16993
  this._systemVariablesAbi.currentEpochNumber(),
16685
16994
  fetchPoolClearings(graph, pool, scope, cancellation),
16995
+ withOpening && scope.kind === 'from' && scope.fromEpoch > 0
16996
+ ? fetchClearingBefore(graph, pool, scope.fromEpoch, cancellation)
16997
+ : Promise.resolve(null),
16686
16998
  ]);
16687
16999
  const currentEpoch = currentEpochNumber.toNumber();
16688
17000
  const window = resolvePoolTvlHistoryWindow(options, currentEpoch);
16689
17001
  const poolNow = snapshot.pool;
16690
17002
  if (!window || !poolNow)
16691
- return [];
17003
+ return nothing;
16692
17004
  const head = (_b = snapshot.meta) === null || _b === void 0 ? void 0 : _b.block;
16693
17005
  if (!head || typeof head.timestamp !== 'number') {
16694
17006
  throw new Error('getPoolTvlHistory: the subgraph returned no latest block time');
16695
17007
  }
16696
17008
  const inWindow = clearings.filter((c) => c.epochId >= window.fromEpoch &&
16697
17009
  c.epochId <= window.toEpoch);
16698
- const blocks = distinctBlocks(inWindow);
17010
+ // Clearings the window left out, older by epoch: the extra row of
17011
+ // a count fetch, or the one-row read below `fromEpoch`.
17012
+ const older = scope.kind === 'newest'
17013
+ ? clearings.filter((c) => c.epochId < window.fromEpoch)
17014
+ : before === null
17015
+ ? []
17016
+ : [before];
17017
+ let opening = withOpening
17018
+ ? pickOpeningClearing(older, inWindow)
17019
+ : null;
17020
+ // The left-out clearings all sat in the first in-window block (a
17021
+ // catch-up block): ask for the newest one before it, by block.
17022
+ if (withOpening && needsClearingBeforeBlock(older, opening)) {
17023
+ opening = yield fetchClearingBeforeBlock(graph, pool, Math.min(...inWindow.map((c) => c.block)), cancellation);
17024
+ }
17025
+ const blocks = distinctBlocks(opening ? [...inWindow, opening] : inWindow);
16699
17026
  const withCurrent = window.toEpoch >= currentEpoch;
16700
17027
  const cacheable = (block) => block <= head.number - TVL_HISTORY_CACHE_DEPTH;
16701
17028
  const [balances, offChainRead] = yield Promise.all([
@@ -16713,7 +17040,7 @@ class DataService {
16713
17040
  latest: null,
16714
17041
  },
16715
17042
  ]);
16716
- return buildPoolTvlHistory({
17043
+ const entries = buildPoolTvlHistory({
16717
17044
  clearings: inWindow,
16718
17045
  balances,
16719
17046
  offChain: offChainRead.offChain,
@@ -16727,10 +17054,22 @@ class DataService {
16727
17054
  : null,
16728
17055
  decimals: this._kasuConfig.stableAssetDecimals,
16729
17056
  });
17057
+ // The same function as every other point, so the opening cannot
17058
+ // be computed another way.
17059
+ const [openingEntry] = opening
17060
+ ? buildPoolTvlHistory({
17061
+ clearings: [opening],
17062
+ balances,
17063
+ offChain: offChainRead.offChain,
17064
+ current: null,
17065
+ decimals: this._kasuConfig.stableAssetDecimals,
17066
+ })
17067
+ : [null];
17068
+ return { entries, opening: openingEntry };
16730
17069
  }
16731
17070
  catch (error) {
16732
17071
  if (isEmptyHistoryError(error, this._kasuConfig.isLiteDeployment)) {
16733
- return [];
17072
+ return nothing;
16734
17073
  }
16735
17074
  throw error;
16736
17075
  }
@@ -25697,9 +26036,9 @@ class SponsoredCallInputError extends Error {
25697
26036
  Object.setPrototypeOf(this, SponsoredCallInputError.prototype);
25698
26037
  }
25699
26038
  }
25700
- const ADDRESS_RE$2 = /^0x[0-9a-fA-F]{40}$/;
26039
+ const ADDRESS_RE$3 = /^0x[0-9a-fA-F]{40}$/;
25701
26040
  function assertAddress(value, code) {
25702
- if (typeof value !== 'string' || !ADDRESS_RE$2.test(value)) {
26041
+ if (typeof value !== 'string' || !ADDRESS_RE$3.test(value)) {
25703
26042
  throw new SponsoredCallInputError(code);
25704
26043
  }
25705
26044
  }
@@ -25828,7 +26167,7 @@ function encodeStableTransfer(input) {
25828
26167
  };
25829
26168
  }
25830
26169
 
25831
- const ADDRESS_RE$1 = /^0x[0-9a-fA-F]{40}$/;
26170
+ const ADDRESS_RE$2 = /^0x[0-9a-fA-F]{40}$/;
25832
26171
  /**
25833
26172
  * Encoded, ready-to-sponsor `LendingPoolManager` calls — read, then encode,
25834
26173
  * NEVER send. Each returns `{ kind, call }`: post `kind` and `[call]` to a
@@ -25865,13 +26204,13 @@ class CallsFacade {
25865
26204
  requestWithdrawal(params) {
25866
26205
  return __awaiter(this, void 0, void 0, function* () {
25867
26206
  const { poolId, trancheId, amount, userAddress } = params;
25868
- if (typeof userAddress !== 'string' || !ADDRESS_RE$1.test(userAddress)) {
26207
+ if (typeof userAddress !== 'string' || !ADDRESS_RE$2.test(userAddress)) {
25869
26208
  throw new SponsoredCallInputError('user');
25870
26209
  }
25871
- if (typeof poolId !== 'string' || !ADDRESS_RE$1.test(poolId)) {
26210
+ if (typeof poolId !== 'string' || !ADDRESS_RE$2.test(poolId)) {
25872
26211
  throw new SponsoredCallInputError('pool');
25873
26212
  }
25874
- if (typeof trancheId !== 'string' || !ADDRESS_RE$1.test(trancheId)) {
26213
+ if (typeof trancheId !== 'string' || !ADDRESS_RE$2.test(trancheId)) {
25875
26214
  throw new SponsoredCallInputError('tranche');
25876
26215
  }
25877
26216
  let shares;
@@ -26523,7 +26862,7 @@ const BEFORE_EXECUTION = ENTRY_POINT_EVENTS.getEventTopic('BeforeExecution');
26523
26862
  const DEPOSIT_REQUESTED = PENDING_POOL_EVENTS.getEventTopic('DepositRequested');
26524
26863
  const ERC20$1 = IERC20MetadataAbi__factory.createInterface();
26525
26864
  const LENDING_POOL_MANAGER$1 = ILendingPoolManagerAbi__factory.createInterface();
26526
- const ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;
26865
+ const ADDRESS_RE$1 = /^0x[0-9a-fA-F]{40}$/;
26527
26866
  /**
26528
26867
  * Case-insensitive address equality; `false` for anything that is not a
26529
26868
  * 20-byte hex address. Checksums are not enforced — an all-lowercase address
@@ -26532,8 +26871,8 @@ const ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;
26532
26871
  function sameAddress(a, b) {
26533
26872
  return (typeof a === 'string' &&
26534
26873
  typeof b === 'string' &&
26535
- ADDRESS_RE.test(a) &&
26536
- ADDRESS_RE.test(b) &&
26874
+ ADDRESS_RE$1.test(a) &&
26875
+ ADDRESS_RE$1.test(b) &&
26537
26876
  a.toLowerCase() === b.toLowerCase());
26538
26877
  }
26539
26878
  function isHexData(value) {
@@ -26559,7 +26898,7 @@ function isCallShape(call) {
26559
26898
  if (call === null || typeof call !== 'object')
26560
26899
  return false;
26561
26900
  const { to, data } = call;
26562
- return typeof to === 'string' && ADDRESS_RE.test(to) && isHexData(data);
26901
+ return typeof to === 'string' && ADDRESS_RE$1.test(to) && isHexData(data);
26563
26902
  }
26564
26903
  function isZeroValue(value) {
26565
26904
  if (value === undefined)
@@ -29188,6 +29527,26 @@ class StrategiesFacade {
29188
29527
  return yield this._dataService.getPoolTvlHistory(poolId, options);
29189
29528
  });
29190
29529
  }
29530
+ /**
29531
+ * `getPoolTvlHistory` plus `opening` — the newest clearing before the
29532
+ * window, read at its own block like every other point, or `null` when
29533
+ * the pool has none — from the same read. Pass the result to
29534
+ * `combineTvlHistories` as a leg with its `usdRate`: the opening is what
29535
+ * the leg held before its first point, so a window cut at a different
29536
+ * time on another chain does not count it 0. Same options and errors as
29537
+ * `getPoolTvlHistory`; the extra cost is one clearing row (or, for
29538
+ * `fromEpoch`, one small request) and one block.
29539
+ *
29540
+ * ```ts
29541
+ * const leg = await kasu.strategies.getPoolTvlHistoryWindow(poolId, { count: 52 });
29542
+ * // { entries: PoolTvlHistoryEntry[], opening: PoolTvlHistoryEntry | null }
29543
+ * ```
29544
+ */
29545
+ getPoolTvlHistoryWindow(poolId_1) {
29546
+ return __awaiter(this, arguments, void 0, function* (poolId, options = {}) {
29547
+ return yield this._dataService.getPoolTvlHistoryWindow(poolId, options);
29548
+ });
29549
+ }
29191
29550
  /**
29192
29551
  * Fetch a single strategy by pool ID. Returns `null` if not found.
29193
29552
  */
@@ -30017,6 +30376,102 @@ function normaliseMapping(mapping) {
30017
30376
  return out;
30018
30377
  }
30019
30378
 
30379
+ /** What `strategyDeployments` throws for an id it cannot place. */
30380
+ class StrategyDeploymentInputError extends Error {
30381
+ constructor(code) {
30382
+ super(`strategyDeployments: input refused (${code})`);
30383
+ this.name = 'StrategyDeploymentInputError';
30384
+ this.code = code;
30385
+ Object.setPrototypeOf(this, StrategyDeploymentInputError.prototype);
30386
+ }
30387
+ }
30388
+ /** The canonical deployment: its pool ids ARE the strategies' ids. */
30389
+ const CANONICAL_CHAIN = 'base';
30390
+ const ADDRESS_RE = /^0x[0-9a-f]{40}$/;
30391
+ /** The deployment's mapping with both sides lowercased; Base never has one. */
30392
+ function mappingOf(configs, chain) {
30393
+ var _a;
30394
+ if (chain === CANONICAL_CHAIN)
30395
+ return [];
30396
+ return Object.entries((_a = configs[chain].poolMetadataMapping) !== null && _a !== void 0 ? _a : {}).map(([local, canonical]) => [local.toLowerCase(), canonical.toLowerCase()]);
30397
+ }
30398
+ /**
30399
+ * `strategyDeployments` over any configs — the export reads `CHAIN_CONFIGS`;
30400
+ * the spec passes its own to cover shapes the built-in mapping lacks.
30401
+ */
30402
+ function resolveStrategyDeployments(configs, poolId, chain) {
30403
+ const id = typeof poolId === 'string' ? poolId.trim().toLowerCase() : '';
30404
+ if (!ADDRESS_RE.test(id)) {
30405
+ throw new StrategyDeploymentInputError('pool-id');
30406
+ }
30407
+ if (typeof chain !== 'string' ||
30408
+ !Object.prototype.hasOwnProperty.call(configs, chain)) {
30409
+ throw new StrategyDeploymentInputError('chain');
30410
+ }
30411
+ const chains = Object.keys(configs);
30412
+ const mappings = chains.map((c) => [c, mappingOf(configs, c)]);
30413
+ const isLocalElsewhere = mappings.some(([c, mapping]) => c !== chain && mapping.some(([local]) => local === id));
30414
+ let canonical;
30415
+ if (chain === CANONICAL_CHAIN) {
30416
+ // A Base id is its own Base pool — unless the mapping says it is
30417
+ // another deployment's chain-local id, which names no Base pool.
30418
+ if (isLocalElsewhere) {
30419
+ throw new StrategyDeploymentInputError('wrong-chain');
30420
+ }
30421
+ canonical = id;
30422
+ }
30423
+ else {
30424
+ const own = mappingOf(configs, chain).find(([local]) => local === id);
30425
+ if (own) {
30426
+ canonical = own[1];
30427
+ }
30428
+ else {
30429
+ const isCanonical = mappings.some(([, mapping]) => mapping.some(([, target]) => target === id));
30430
+ if (isLocalElsewhere || isCanonical) {
30431
+ throw new StrategyDeploymentInputError('wrong-chain');
30432
+ }
30433
+ // Unmapped off Base: a strategy of one on its own chain (every
30434
+ // XDC USDC pool today). Claiming a Base counterpart would read
30435
+ // Base with an XDC address.
30436
+ return [{ chain, poolId: id }];
30437
+ }
30438
+ }
30439
+ const found = [
30440
+ { chain: CANONICAL_CHAIN, poolId: canonical },
30441
+ ];
30442
+ for (const [c, mapping] of mappings) {
30443
+ for (const [local, target] of mapping) {
30444
+ if (target === canonical)
30445
+ found.push({ chain: c, poolId: local });
30446
+ }
30447
+ }
30448
+ return found;
30449
+ }
30450
+ /**
30451
+ * Every deployment a strategy has a pool on, canonical (Base) first, then the
30452
+ * others in `CHAIN_CONFIGS` order — the legs to fetch and pass to
30453
+ * `combineTvlHistories`. One entry for a strategy that lives on one chain
30454
+ * only (PayFi, on Base).
30455
+ *
30456
+ * `chain` is the deployment `poolId` was read on — required, because an
30457
+ * address alone does not say which chain it belongs to. From either side of
30458
+ * a mapping the answer is the same set: Tax Pay's Base id on `'base'` and
30459
+ * its XDC id on `'xdc'` both return both pools. An id the mapping does not
30460
+ * know is a strategy of one on its own chain. Ids are compared
30461
+ * case-insensitively and returned lowercase.
30462
+ *
30463
+ * Throws `StrategyDeploymentInputError` for a malformed id, an unknown chain,
30464
+ * or an id the mapping places on another deployment (`'wrong-chain'`).
30465
+ *
30466
+ * ```ts
30467
+ * strategyDeployments(strategy.id, 'base');
30468
+ * // [{ chain: 'base', poolId: '0x…' }, { chain: 'xdc', poolId: '0x…' }]
30469
+ * ```
30470
+ */
30471
+ function strategyDeployments(poolId, chain) {
30472
+ return resolveStrategyDeployments(CHAIN_CONFIGS, poolId, chain);
30473
+ }
30474
+
30020
30475
  /** Directus instance holding Kasu's pool editorial content. */
30021
30476
  const DEFAULT_DIRECTUS_URL = 'https://kasu-finance.directus.app/';
30022
30477
  /**
@@ -30669,4 +31124,4 @@ function weightedAverageApy(slices) {
30669
31124
  return weighted / totalBalance;
30670
31125
  }
30671
31126
 
30672
- export { APXIUM, AU_ALPHA3, AU_MIN_CUMULATIVE_BY_STABLE, BatchPendingError, BatchRevertedError, BatchSubmissionError, CHAIN_CONFIGS, CLEARING_WINDOW_SECONDS, CONTRACT_TTL_MS, CallsFacade, DAYS_PER_EPOCH, DAYS_PER_MONTH, DEFAULT_BATCH_POLLING_POLICY, DepositBatchMismatchError, DepositFlow, DepositsFacade, ENTRY_POINT_V07, EPOCHS_IN_YEAR, Flow, FlowStore, FlowsFacade, INVALID_ACCEPTANCE_SIGNATURE_MESSAGE, INVALID_AUTH_SIGNATURE_MESSAGE, INVOICEMATE, Kasu, KasuSdk, MAX_LENDING_AMOUNT_FALLBACK, MIN_DAYS_FOR_MONTHS, MIN_LENDING_AMOUNT_FALLBACK, MIN_TRANCHE_CAPACITY, NOT_A_WITHDRAWAL_CALL_MESSAGE, NO_DIRECTUS_URL_MESSAGE, NO_HISTORY_PROVIDER_MESSAGE, NO_KYC_PARAMS_MESSAGE, NO_SPENDER_MESSAGE, NO_WITHDRAWAL_ENCODER_MESSAGE, PastStateUnavailableError, PoolTvlHistoryAbortedError, PoolTvlHistoryTimeoutError, PortfolioFacade, READ_ONLY_MESSAGE, RIXON_CAPITAL, SECONDS_PER_WEEK, SPONSORED_CALL_KINDS, SPONSORED_CALL_SELECTORS, SdkConfig, SponsoredCallInputError, StrategiesFacade, UPPER_MEZZANINE, WithdrawFlow, accruedInterest, aggregateYieldHistory, apyToEpochRate, asContractType, auMinimumRemaining, auThresholdFor, availableFixedTermConfigs, batchOutcomeOf, buildContractVersionType, buildFullNameRequestMessage, buildLegacyContractRequestMessage, buildLoanAgreementSignMessage, cancelDeadline, ceilToCents, checkBatchOutcome, checkSponsoredCallOutcome, classifyWalletFailure, compareTrancheSeniority, computeSettlementWindow, countSubmissions, dailyInterest, decodeRevert, deriveCycleDates, derivePoolStatus, deriveRequestState, encodeCancelDepositCall, encodeCancelWithdrawalCall, encodeDepositData, encodeRequestWithdrawalCall, encodeStableTransfer, encodeWithdrawAtExpiryCall, epochRateToApy, epochTrancheYield, explorerAddressUrl, explorerTxUrl, extractRevertData, fetchUnusedPoolIds, firstSubmissionTimestamp, floorToCents, formatSignTimestampUtc, getCreditOriginator, getInstitutionalLender, getStrategyRegion, getTrancheDisplayName, isAuMinimumExempt, isAustralianKyc, isBelowMinimumCapacity, isCycleClosed, isDefinitivelyNotSubmitted, isFixedTermConfigId, isUnpredictableGas, isUserRejected, lastEventTimestamp, lockDurationDays, lockDurationMonths, maxNetRateCeiling, netEffectiveApy, netTrancheApyBounds, nextCycleBoundary, parseFormattedMessage, parseMinorUnits, parseTrancheBound, pickDefaultTrancheId, pickHighestYieldTranche, pickShortDescription, poolAllTranchesFull, poolMaxApy, poolTvl, projectedAnnualEarnings, proveDepositInReceipt, proveUserOperationInReceipt, resolveBoundShortcuts, resolveDepositBounds, selectVisiblePools, submissionEvents, submitBatch, submitSponsoredCall, trancheApyBounds, trancheHasCapacity, trancheRiskRank, validateDepositBatchCalls, weeklyInterest, weightedAverageApy };
31127
+ export { APXIUM, AU_ALPHA3, AU_MIN_CUMULATIVE_BY_STABLE, BatchPendingError, BatchRevertedError, BatchSubmissionError, CHAIN_CONFIGS, CLEARING_WINDOW_SECONDS, CONTRACT_TTL_MS, CallsFacade, DAYS_PER_EPOCH, DAYS_PER_MONTH, DEFAULT_BATCH_POLLING_POLICY, DepositBatchMismatchError, DepositFlow, DepositsFacade, ENTRY_POINT_V07, EPOCHS_IN_YEAR, Flow, FlowStore, FlowsFacade, INVALID_ACCEPTANCE_SIGNATURE_MESSAGE, INVALID_AUTH_SIGNATURE_MESSAGE, INVOICEMATE, Kasu, KasuSdk, MAX_LENDING_AMOUNT_FALLBACK, MIN_DAYS_FOR_MONTHS, MIN_LENDING_AMOUNT_FALLBACK, MIN_TRANCHE_CAPACITY, NOT_A_WITHDRAWAL_CALL_MESSAGE, NO_DIRECTUS_URL_MESSAGE, NO_HISTORY_PROVIDER_MESSAGE, NO_KYC_PARAMS_MESSAGE, NO_SPENDER_MESSAGE, NO_WITHDRAWAL_ENCODER_MESSAGE, PastStateUnavailableError, PoolTvlHistoryAbortedError, PoolTvlHistoryTimeoutError, PortfolioFacade, READ_ONLY_MESSAGE, RIXON_CAPITAL, SECONDS_PER_WEEK, SPONSORED_CALL_KINDS, SPONSORED_CALL_SELECTORS, SdkConfig, SponsoredCallInputError, StrategiesFacade, StrategyDeploymentInputError, TvlHistoryInputError, UPPER_MEZZANINE, WithdrawFlow, accruedInterest, aggregateYieldHistory, apyToEpochRate, asContractType, auMinimumRemaining, auThresholdFor, availableFixedTermConfigs, batchOutcomeOf, buildContractVersionType, buildFullNameRequestMessage, buildLegacyContractRequestMessage, buildLoanAgreementSignMessage, cancelDeadline, ceilToCents, checkBatchOutcome, checkSponsoredCallOutcome, classifyWalletFailure, combineTvlHistories, compareTrancheSeniority, computeSettlementWindow, countSubmissions, dailyInterest, decodeRevert, deriveCycleDates, derivePoolStatus, deriveRequestState, encodeCancelDepositCall, encodeCancelWithdrawalCall, encodeDepositData, encodeRequestWithdrawalCall, encodeStableTransfer, encodeWithdrawAtExpiryCall, epochRateToApy, epochTrancheYield, explorerAddressUrl, explorerTxUrl, extractRevertData, fetchUnusedPoolIds, firstSubmissionTimestamp, floorToCents, formatSignTimestampUtc, getCreditOriginator, getInstitutionalLender, getStrategyRegion, getTrancheDisplayName, isAuMinimumExempt, isAustralianKyc, isBelowMinimumCapacity, isCycleClosed, isDefinitivelyNotSubmitted, isFixedTermConfigId, isUnpredictableGas, isUserRejected, lastEventTimestamp, lockDurationDays, lockDurationMonths, maxNetRateCeiling, netEffectiveApy, netTrancheApyBounds, nextCycleBoundary, parseFormattedMessage, parseMinorUnits, parseTrancheBound, pickDefaultTrancheId, pickHighestYieldTranche, pickShortDescription, poolAllTranchesFull, poolMaxApy, poolTvl, projectedAnnualEarnings, proveDepositInReceipt, proveUserOperationInReceipt, resolveBoundShortcuts, resolveDepositBounds, selectVisiblePools, strategyDeployments, submissionEvents, submitBatch, submitSponsoredCall, trancheApyBounds, trancheHasCapacity, trancheRiskRank, validateDepositBatchCalls, weeklyInterest, weightedAverageApy };
@@ -55,8 +55,8 @@ export { compareTrancheSeniority, derivePoolStatus, MIN_TRANCHE_CAPACITY, netTra
55
55
  export type { ApyBounds, PoolStatus, TrancheCapacitySignal } from './tranches';
56
56
  export { classifyWalletFailure, isUnpredictableGas, isUserRejected, } from './wallet-errors';
57
57
  export type { WalletFailure } from './wallet-errors';
58
- export { poolTvl } from './pool-tvl';
59
- export type { PoolTvlHistoryEntry } from './pool-tvl';
58
+ export { combineTvlHistories, poolTvl, TvlHistoryInputError } from './pool-tvl';
59
+ export type { PoolTvlHistoryEntry, StrategyTvlHistoryEntry, TvlHistoryInputErrorCode, TvlHistoryLeg, } from './pool-tvl';
60
60
  export { aggregateYieldHistory, epochTrancheYield } from './yield-history';
61
61
  export type { EpochLockShares, EpochTrancheYield, EpochTrancheYieldInput, YieldHistoryEntry, YieldHistoryFilter, YieldHistoryPoint, } from './yield-history';
62
62
  export { accruedInterest, dailyInterest, projectedAnnualEarnings, SECONDS_PER_WEEK, weeklyInterest, weightedAverageApy, } from './yields';