@kasufinance/kasu-sdk 2.12.0 → 2.13.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.
Files changed (53) hide show
  1. package/README.md +26 -1
  2. package/dist/bundle.cjs.js +871 -41
  3. package/dist/bundle.esm.js +868 -43
  4. package/dist/domain/index.d.ts +2 -0
  5. package/dist/domain/index.js +1 -0
  6. package/dist/domain/index.js.map +1 -1
  7. package/dist/domain/pool-tvl.d.ts +62 -0
  8. package/dist/domain/pool-tvl.js +22 -0
  9. package/dist/domain/pool-tvl.js.map +1 -0
  10. package/dist/facade/chain-configs.js +24 -0
  11. package/dist/facade/chain-configs.js.map +1 -1
  12. package/dist/facade/kasu.d.ts +2 -0
  13. package/dist/facade/kasu.js +13 -5
  14. package/dist/facade/kasu.js.map +1 -1
  15. package/dist/facade/strategies.d.ts +20 -0
  16. package/dist/facade/strategies.js +22 -0
  17. package/dist/facade/strategies.js.map +1 -1
  18. package/dist/facade/types.d.ts +20 -0
  19. package/dist/index.d.ts +4 -0
  20. package/dist/index.js +2 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/kasu-sdk.d.ts +9 -1
  23. package/dist/kasu-sdk.js +2 -2
  24. package/dist/kasu-sdk.js.map +1 -1
  25. package/dist/sdk-config.d.ts +20 -0
  26. package/dist/sdk-config.js +3 -1
  27. package/dist/sdk-config.js.map +1 -1
  28. package/dist/services/DataService/data-service.d.ts +81 -1
  29. package/dist/services/DataService/data-service.js +211 -6
  30. package/dist/services/DataService/data-service.js.map +1 -1
  31. package/dist/services/DataService/tvl-history.d.ts +296 -0
  32. package/dist/services/DataService/tvl-history.js +558 -0
  33. package/dist/services/DataService/tvl-history.js.map +1 -0
  34. package/dist/services/Portfolio/yield-history.d.ts +1 -16
  35. package/dist/services/Portfolio/yield-history.js +2 -29
  36. package/dist/services/Portfolio/yield-history.js.map +1 -1
  37. package/dist/services/shared.d.ts +16 -0
  38. package/dist/services/shared.js +30 -0
  39. package/dist/services/shared.js.map +1 -1
  40. package/package.json +1 -1
  41. package/src/domain/index.ts +3 -0
  42. package/src/domain/pool-tvl.ts +66 -0
  43. package/src/facade/chain-configs.ts +26 -0
  44. package/src/facade/kasu.ts +20 -2
  45. package/src/facade/strategies.ts +26 -0
  46. package/src/facade/types.ts +20 -0
  47. package/src/index.ts +9 -0
  48. package/src/kasu-sdk.ts +19 -2
  49. package/src/sdk-config.ts +22 -0
  50. package/src/services/DataService/data-service.ts +296 -6
  51. package/src/services/DataService/tvl-history.ts +853 -0
  52. package/src/services/Portfolio/yield-history.ts +2 -35
  53. package/src/services/shared.ts +38 -1
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var utils$2 = require('ethers/lib/utils');
4
3
  var ethers = require('ethers');
4
+ var utils$2 = require('ethers/lib/utils');
5
5
 
6
6
  const defaultJsonSerializer = JSON;
7
7
 
@@ -15481,6 +15481,28 @@ class KasuPoolExternalTVLAbi__factory {
15481
15481
  }
15482
15482
  KasuPoolExternalTVLAbi__factory.abi = _abi;
15483
15483
 
15484
+ /**
15485
+ * A pool's TVL — the total lent into it by everyone — and the shape of its
15486
+ * history. Pure: the reads live in `services/DataService/tvl-history.ts`.
15487
+ */
15488
+ /**
15489
+ * The ONE definition of a pool's TVL: what lenders have lent on chain
15490
+ * (`lendingPool.balance` in the subgraph) plus the off-chain amount the
15491
+ * pool's `ExternalTVL` contract records (`externalTVLOfPool`).
15492
+ *
15493
+ * Both inputs, and the result, are in DISPLAY units of the deployment's own
15494
+ * stable token (USDC on Base and XDC USDC, AUDD on XDC AUDD) — `'1.5'` is
15495
+ * one and a half tokens, not 1.5 minor units. Convert before formatting as
15496
+ * USD.
15497
+ *
15498
+ * `getPoolOverview` (its `totalValueLocked.total`) and `getPoolTvlHistory`
15499
+ * (every point) both call this, so the chart and the figure above it cannot
15500
+ * be computed two ways.
15501
+ */
15502
+ function poolTvl(onChainBalance, offChainTvl) {
15503
+ return parseFloat(onChainBalance) + parseFloat(offChainTvl);
15504
+ }
15505
+
15484
15506
  /**
15485
15507
  * Epochs in a year — the single source of truth for every rate conversion in
15486
15508
  * the SDK and in every consumer that undoes one.
@@ -15720,6 +15742,36 @@ function filterArray(array, id_in) {
15720
15742
  });
15721
15743
  }
15722
15744
  }
15745
+ /**
15746
+ * A GraphQL VALIDATION error naming an entity, field or argument this
15747
+ * deployment's subgraph does not have. A network error, a timeout or an
15748
+ * indexing error is not one.
15749
+ */
15750
+ function isMissingEntityError(error) {
15751
+ if (typeof error !== 'object' || error === null)
15752
+ return false;
15753
+ const response = error.response;
15754
+ const errors = response === null || response === void 0 ? void 0 : response.errors;
15755
+ if (!Array.isArray(errors) || errors.length === 0)
15756
+ return false;
15757
+ return errors.every((e) => {
15758
+ const message = e === null || e === void 0 ? void 0 : e.message;
15759
+ return (typeof message === 'string' &&
15760
+ /has no field|cannot query field|unknown (type|field|argument)|no such (type|field)/i.test(message));
15761
+ });
15762
+ }
15763
+ /**
15764
+ * The only failure a history read (`getYieldHistory`, `getPoolTvlHistory`)
15765
+ * turns into `[]`: a missing-schema error
15766
+ * on a LITE deployment, whose subgraph may predate the entities. On a full
15767
+ * deployment the entities are part of the schema, so the same error means a
15768
+ * broken query or subgraph and THROWS — an empty chart there would hide it.
15769
+ * Everything else (network, timeout, indexing) throws on every deployment, so
15770
+ * a consumer can tell "no history here" from "history unavailable".
15771
+ */
15772
+ function isEmptyHistoryError(error, liteDeployment) {
15773
+ return liteDeployment && isMissingEntityError(error);
15774
+ }
15723
15775
 
15724
15776
  /**
15725
15777
  * The error a CMS-only call raises on a deployment configured without
@@ -15909,11 +15961,569 @@ const getLendingPoolWithdrawalAndDepositsQuery = gql `
15909
15961
  }
15910
15962
  `;
15911
15963
 
15964
+ /** Clearings per subgraph page (the subgraph's own row cap). */
15965
+ const TVL_HISTORY_PAGE_SIZE = 1000;
15966
+ /**
15967
+ * A runaway guard on the clearings pagination, not a working limit: a pool
15968
+ * clears once per epoch, so 20 pages would be twenty thousand cycles.
15969
+ */
15970
+ const TVL_HISTORY_MAX_PAGES = 20;
15971
+ /**
15972
+ * How many past blocks one time-travel request reads, as GraphQL aliases. A
15973
+ * pool with ~120 clearings costs two such requests.
15974
+ */
15975
+ const TVL_HISTORY_BLOCKS_PER_REQUEST = 100;
15976
+ /** At most this many past-state RPC reads are in flight at once. */
15977
+ const TVL_HISTORY_RPC_CONCURRENCY = 8;
15978
+ /** Attempts per past-state RPC read (each over every history RPC in turn). */
15979
+ const TVL_HISTORY_RPC_ATTEMPTS = 3;
15980
+ /** Back-off before the second attempt; doubled for each one after. */
15981
+ const TVL_HISTORY_RPC_RETRY_DELAY_MS = 500;
15982
+ /** Per-call timeout of the history providers the SDK builds itself. */
15983
+ const TVL_HISTORY_RPC_TIMEOUT_MS = 10000;
15984
+ /** Default `timeoutMs` of a whole `getPoolTvlHistory` call. */
15985
+ const TVL_HISTORY_DEFAULT_TIMEOUT_MS = 30000;
15986
+ /**
15987
+ * Blocks at least this far below the subgraph's head are treated as final
15988
+ * and their reads are cached; nearer ones are read again every time.
15989
+ */
15990
+ const TVL_HISTORY_CACHE_DEPTH = 64;
15991
+ /** Entries per SDK instance before the cache starts again from empty. */
15992
+ const TVL_HISTORY_CACHE_LIMIT = 20000;
15993
+ /** `getPoolTvlHistory` took longer than its `timeoutMs`. */
15994
+ class PoolTvlHistoryTimeoutError extends Error {
15995
+ constructor(timeoutMs) {
15996
+ super(`getPoolTvlHistory: timed out after ${String(timeoutMs)} ms`);
15997
+ this.timeoutMs = timeoutMs;
15998
+ this.name = 'PoolTvlHistoryTimeoutError';
15999
+ }
16000
+ }
16001
+ /** `getPoolTvlHistory` was abandoned through its `signal`. */
16002
+ class PoolTvlHistoryAbortedError extends Error {
16003
+ constructor() {
16004
+ super('getPoolTvlHistory: aborted');
16005
+ this.name = 'PoolTvlHistoryAbortedError';
16006
+ }
16007
+ }
16008
+ /**
16009
+ * A history provider answered a past block as if nothing were deployed — no
16010
+ * code even for the pool, which cleared in that very block. A pruned node
16011
+ * does that; trusting it would draw the off-chain TVL as 0.
16012
+ */
16013
+ class PastStateUnavailableError extends Error {
16014
+ constructor(block) {
16015
+ super(`getPoolTvlHistory: history provider does not serve past state (no code for the pool at block ${String(block)}, where it cleared)`);
16016
+ this.block = block;
16017
+ this.name = 'PastStateUnavailableError';
16018
+ }
16019
+ }
16020
+ /**
16021
+ * Throws `RangeError` / `TypeError` on a malformed option.
16022
+ * `getPoolTvlHistory` calls it ONCE, BEFORE any network read;
16023
+ * `resolvePoolTvlHistoryWindow` assumes it has run.
16024
+ */
16025
+ function validatePoolTvlHistoryOptions(options) {
16026
+ const { fromEpoch, count, timeoutMs, signal } = options;
16027
+ if (fromEpoch !== undefined &&
16028
+ (!Number.isSafeInteger(fromEpoch) || fromEpoch < 0)) {
16029
+ throw new RangeError(`getPoolTvlHistory: fromEpoch must be a non-negative integer, got ${String(fromEpoch)}`);
16030
+ }
16031
+ if (count !== undefined && (!Number.isSafeInteger(count) || count < 1)) {
16032
+ throw new RangeError(`getPoolTvlHistory: count must be a positive integer, got ${String(count)}`);
16033
+ }
16034
+ if (timeoutMs !== undefined &&
16035
+ (typeof timeoutMs !== 'number' ||
16036
+ !Number.isFinite(timeoutMs) ||
16037
+ timeoutMs <= 0)) {
16038
+ throw new RangeError(`getPoolTvlHistory: timeoutMs must be a positive number, got ${String(timeoutMs)}`);
16039
+ }
16040
+ if (signal !== undefined &&
16041
+ (typeof signal !== 'object' ||
16042
+ typeof signal.aborted !== 'boolean')) {
16043
+ throw new TypeError('getPoolTvlHistory: signal must be an AbortSignal');
16044
+ }
16045
+ }
16046
+ /** The epoch window. Options must already have passed validation. */
16047
+ function resolvePoolTvlHistoryWindow(options, currentEpoch) {
16048
+ const { fromEpoch, count } = options;
16049
+ let from;
16050
+ let to = currentEpoch;
16051
+ if (fromEpoch !== undefined) {
16052
+ from = fromEpoch;
16053
+ if (count !== undefined) {
16054
+ to = Math.min(currentEpoch, fromEpoch + count - 1);
16055
+ }
16056
+ }
16057
+ else {
16058
+ from = count !== undefined ? Math.max(0, currentEpoch - count + 1) : 0;
16059
+ }
16060
+ return from > to ? null : { fromEpoch: from, toEpoch: to };
16061
+ }
16062
+ /**
16063
+ * Runs `work` under a deadline and an optional caller signal. Whichever comes
16064
+ * first rejects the returned promise with its typed error at once, aborts
16065
+ * `cancellation.signal` (cancelling in-flight subgraph requests) and makes
16066
+ * every later `throwIfCancelled()` throw, so no new read starts. An RPC call
16067
+ * already sent cannot be recalled; its answer is dropped.
16068
+ */
16069
+ function runCancellable(timeoutMs, external, work) {
16070
+ return __awaiter(this, void 0, void 0, function* () {
16071
+ if (external === null || external === void 0 ? void 0 : external.aborted)
16072
+ throw new PoolTvlHistoryAbortedError();
16073
+ const controller = new AbortController();
16074
+ let reason;
16075
+ let rejectCancelled = () => undefined;
16076
+ const cancelled = new Promise((_, reject) => {
16077
+ rejectCancelled = reject;
16078
+ });
16079
+ const cancel = (error) => {
16080
+ if (reason)
16081
+ return;
16082
+ reason = error;
16083
+ controller.abort();
16084
+ rejectCancelled(error);
16085
+ };
16086
+ const timer = setTimeout(() => {
16087
+ cancel(new PoolTvlHistoryTimeoutError(timeoutMs));
16088
+ }, timeoutMs);
16089
+ const onAbort = () => {
16090
+ cancel(new PoolTvlHistoryAbortedError());
16091
+ };
16092
+ external === null || external === void 0 ? void 0 : external.addEventListener('abort', onAbort);
16093
+ const cancellation = {
16094
+ signal: controller.signal,
16095
+ throwIfCancelled: () => {
16096
+ if (reason)
16097
+ throw reason;
16098
+ },
16099
+ };
16100
+ try {
16101
+ const running = work(cancellation);
16102
+ // The losing side must never surface as an unhandled rejection.
16103
+ running.catch(() => undefined);
16104
+ return yield Promise.race([running, cancelled]);
16105
+ }
16106
+ finally {
16107
+ clearTimeout(timer);
16108
+ external === null || external === void 0 ? void 0 : external.removeEventListener('abort', onAbort);
16109
+ }
16110
+ });
16111
+ }
16112
+ // ---------------------------------------------------------------------------
16113
+ // Subgraph reads
16114
+ // ---------------------------------------------------------------------------
16115
+ /** Above any epoch number: "no upper bound" for the clearings query. */
16116
+ const NO_EPOCH_BOUND = '1000000000000';
16117
+ /**
16118
+ * The pool's FINISHED clearings, plus — on the first page only — its balance
16119
+ * now and the subgraph's latest block.
16120
+ *
16121
+ * Two modes over one document: ascending from `$epochGte` (a `fromEpoch`
16122
+ * window, or everything), or descending below `$epochLt` (the newest N, for
16123
+ * a `count`-only window, so it never pages through the whole history).
16124
+ */
16125
+ const poolTvlClearingsQuery = gql `
16126
+ query PoolTvlClearings(
16127
+ $poolId: ID!
16128
+ $pool: String!
16129
+ $epochGte: BigInt!
16130
+ $epochLt: BigInt!
16131
+ $direction: OrderDirection!
16132
+ $first: Int!
16133
+ $withSnapshot: Boolean!
16134
+ ) {
16135
+ clearings: lendingPoolClearings(
16136
+ first: $first
16137
+ orderBy: epochId
16138
+ orderDirection: $direction
16139
+ where: {
16140
+ lendingPool: $pool
16141
+ isFinished: true
16142
+ epochId_gte: $epochGte
16143
+ epochId_lt: $epochLt
16144
+ }
16145
+ ) {
16146
+ epochId
16147
+ endBlockNumber
16148
+ endTimestamp
16149
+ }
16150
+ pool: lendingPool(id: $poolId) @include(if: $withSnapshot) {
16151
+ balance
16152
+ }
16153
+ meta: _meta @include(if: $withSnapshot) {
16154
+ block {
16155
+ number
16156
+ timestamp
16157
+ }
16158
+ }
16159
+ }
16160
+ `;
16161
+ /** The time-travel alias for one block: `b<block>`. */
16162
+ function blockAlias(block) {
16163
+ return `b${String(block)}`;
16164
+ }
16165
+ /**
16166
+ * One time-travel request: the pool's balance at the END of each block, one
16167
+ * alias per block. Block numbers are written into the document, so each must
16168
+ * be a positive safe integer — anything else throws rather than reaching the
16169
+ * query text. The pool id travels as a variable.
16170
+ */
16171
+ function poolBalancesAtBlocksQuery(blocks) {
16172
+ const fields = blocks.map((block) => {
16173
+ if (!Number.isSafeInteger(block) || block <= 0) {
16174
+ throw new Error(`getPoolTvlHistory: refusing block number ${String(block)}`);
16175
+ }
16176
+ return `${blockAlias(block)}: lendingPool(id: $poolId, block: { number: ${String(block)} }) { balance }`;
16177
+ });
16178
+ return `query PoolTvlAtBlocks($poolId: ID!) { ${fields.join(' ')} }`;
16179
+ }
16180
+ function parseClearing(row) {
16181
+ const clearing = {
16182
+ epochId: Number(row.epochId),
16183
+ block: Number(row.endBlockNumber),
16184
+ timestamp: Number(row.endTimestamp),
16185
+ };
16186
+ if (!Number.isSafeInteger(clearing.epochId) ||
16187
+ clearing.epochId < 0 ||
16188
+ !Number.isSafeInteger(clearing.block) ||
16189
+ clearing.block <= 0 ||
16190
+ !Number.isSafeInteger(clearing.timestamp) ||
16191
+ clearing.timestamp <= 0) {
16192
+ throw new Error(`getPoolTvlHistory: malformed clearing row ${JSON.stringify(row)}`);
16193
+ }
16194
+ return clearing;
16195
+ }
16196
+ /**
16197
+ * The scope's finished clearings, ascending by epoch, and the snapshot from
16198
+ * the first page. Normally ONE request.
16199
+ */
16200
+ function fetchPoolClearings(request, poolId, scope, cancellation) {
16201
+ return __awaiter(this, void 0, void 0, function* () {
16202
+ const clearings = [];
16203
+ let snapshot = {};
16204
+ let gte = scope.kind === 'from' ? scope.fromEpoch : 0;
16205
+ let lt;
16206
+ let requests = 0;
16207
+ for (;;) {
16208
+ cancellation.throwIfCancelled();
16209
+ if (requests >= TVL_HISTORY_MAX_PAGES) {
16210
+ throw new Error(`getPoolTvlHistory: more than ${String(TVL_HISTORY_MAX_PAGES)} pages of clearings for one pool`);
16211
+ }
16212
+ const first = requests === 0;
16213
+ const remaining = scope.kind === 'newest'
16214
+ ? scope.count - clearings.length
16215
+ : TVL_HISTORY_PAGE_SIZE;
16216
+ const pageSize = Math.min(TVL_HISTORY_PAGE_SIZE, remaining);
16217
+ const page = yield request(poolTvlClearingsQuery, {
16218
+ poolId,
16219
+ pool: poolId,
16220
+ epochGte: String(gte),
16221
+ epochLt: lt === undefined ? NO_EPOCH_BOUND : String(lt),
16222
+ direction: scope.kind === 'newest' ? 'desc' : 'asc',
16223
+ first: pageSize,
16224
+ withSnapshot: first,
16225
+ }, cancellation.signal);
16226
+ requests += 1;
16227
+ if (first)
16228
+ snapshot = { pool: page.pool, meta: page.meta };
16229
+ const rows = page.clearings.map(parseClearing);
16230
+ clearings.push(...rows);
16231
+ const last = rows.at(-1);
16232
+ if (rows.length < pageSize || last === undefined)
16233
+ break;
16234
+ if (scope.kind === 'newest') {
16235
+ if (clearings.length >= scope.count)
16236
+ break;
16237
+ if (lt !== undefined && last.epochId >= lt) {
16238
+ throw new Error('getPoolTvlHistory: the clearings cursor did not advance');
16239
+ }
16240
+ lt = last.epochId;
16241
+ }
16242
+ else {
16243
+ if (last.epochId < gte) {
16244
+ throw new Error('getPoolTvlHistory: the clearings cursor did not advance');
16245
+ }
16246
+ gte = last.epochId + 1;
16247
+ }
16248
+ }
16249
+ clearings.sort((a, b) => a.epochId - b.epochId);
16250
+ return { clearings, snapshot, requests };
16251
+ });
16252
+ }
16253
+ /**
16254
+ * The pool's balance (display units, the subgraph's decimal string) at the end
16255
+ * of each block, `TVL_HISTORY_BLOCKS_PER_REQUEST` blocks per request. A block
16256
+ * at which the subgraph has no such pool THROWS: a finished clearing of the
16257
+ * pool happened in it, so a missing pool means a broken read, not a zero.
16258
+ */
16259
+ function fetchPoolBalancesAtBlocks(request, poolId, blocks, cancellation) {
16260
+ return __awaiter(this, void 0, void 0, function* () {
16261
+ const chunks = [];
16262
+ for (let i = 0; i < blocks.length; i += TVL_HISTORY_BLOCKS_PER_REQUEST) {
16263
+ chunks.push(blocks.slice(i, i + TVL_HISTORY_BLOCKS_PER_REQUEST));
16264
+ }
16265
+ cancellation.throwIfCancelled();
16266
+ const pages = yield Promise.all(chunks.map((chunk) => request(poolBalancesAtBlocksQuery(chunk), { poolId }, cancellation.signal).then((page) => ({ chunk, page }))));
16267
+ const balances = new Map();
16268
+ for (const { chunk, page } of pages) {
16269
+ for (const block of chunk) {
16270
+ const state = page[blockAlias(block)];
16271
+ if (!state) {
16272
+ throw new Error(`getPoolTvlHistory: the subgraph has no state for the pool at block ${String(block)}`);
16273
+ }
16274
+ balances.set(block, state.balance);
16275
+ }
16276
+ }
16277
+ return { balances, requests: chunks.length };
16278
+ });
16279
+ }
16280
+ const EXTERNAL_TVL_INTERFACE = new utils$2.Interface(KasuPoolExternalTVLAbi__factory.abi);
16281
+ function hasCode(code) {
16282
+ if (typeof code !== 'string' || !/^0x[0-9a-fA-F]*$/.test(code)) {
16283
+ throw new Error(`getPoolTvlHistory: getCode returned ${String(code)}, not hex`);
16284
+ }
16285
+ return code !== '0x';
16286
+ }
16287
+ /**
16288
+ * The real reader: each read goes to the history RPCs IN ORDER until one
16289
+ * answers, and the whole chain is retried `attempts` times with back-off.
16290
+ * After the last attempt the last error propagates — never a value. Each
16291
+ * attempt first checks the cancellation, so a timed-out call stops here.
16292
+ */
16293
+ function createOffChainTvlReader({ externalTvl, pool, rpcs, cancellation, attempts = TVL_HISTORY_RPC_ATTEMPTS, delayMs = TVL_HISTORY_RPC_RETRY_DELAY_MS, sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)), }) {
16294
+ if (rpcs.length === 0) {
16295
+ throw new Error('getPoolTvlHistory: no history RPC to read from');
16296
+ }
16297
+ const data = EXTERNAL_TVL_INTERFACE.encodeFunctionData('externalTVLOfPool', [pool]);
16298
+ const read = (op) => __awaiter(this, void 0, void 0, function* () {
16299
+ let lastError;
16300
+ for (let attempt = 1; attempt <= attempts; attempt++) {
16301
+ for (const rpc of rpcs) {
16302
+ cancellation.throwIfCancelled();
16303
+ try {
16304
+ return yield op(rpc);
16305
+ }
16306
+ catch (error) {
16307
+ lastError = error;
16308
+ }
16309
+ }
16310
+ if (attempt < attempts) {
16311
+ yield sleep(delayMs * Math.pow(2, (attempt - 1)));
16312
+ }
16313
+ }
16314
+ cancellation.throwIfCancelled();
16315
+ throw lastError;
16316
+ });
16317
+ return {
16318
+ readAt: (block) => read((rpc) => __awaiter(this, void 0, void 0, function* () {
16319
+ const result = yield rpc.call({ to: externalTvl, data }, block);
16320
+ const [value] = EXTERNAL_TVL_INTERFACE.decodeFunctionResult('externalTVLOfPool', result);
16321
+ return value;
16322
+ })),
16323
+ hasCodeAt: (block) => read((rpc) => __awaiter(this, void 0, void 0, function* () { return hasCode(yield rpc.getCode(externalTvl, block)); })),
16324
+ poolHasCodeAt: (block) => read((rpc) => __awaiter(this, void 0, void 0, function* () { return hasCode(yield rpc.getCode(pool, block)); })),
16325
+ };
16326
+ }
16327
+ /**
16328
+ * Index of the first block (of `blocks`, ascending) at which the contract has
16329
+ * code; `blocks.length` when it has code at none. A deployed contract keeps
16330
+ * its code (it is a proxy, never self-destructed), so the answer is a single
16331
+ * boundary and a binary search finds it: ONE `getCode` when the contract
16332
+ * already existed at the oldest clearing, about log2(n) otherwise.
16333
+ */
16334
+ function firstBlockWithCode(blocks, hasCodeAt) {
16335
+ return __awaiter(this, void 0, void 0, function* () {
16336
+ if (blocks.length === 0)
16337
+ return 0;
16338
+ if (yield hasCodeAt(blocks[0]))
16339
+ return 0;
16340
+ const last = blocks.length - 1;
16341
+ if (!(yield hasCodeAt(blocks[last])))
16342
+ return blocks.length;
16343
+ let lo = 0; // no code
16344
+ let hi = last; // code
16345
+ while (hi - lo > 1) {
16346
+ const mid = Math.floor((lo + hi) / 2);
16347
+ if (yield hasCodeAt(blocks[mid]))
16348
+ hi = mid;
16349
+ else
16350
+ lo = mid;
16351
+ }
16352
+ return hi;
16353
+ });
16354
+ }
16355
+ /**
16356
+ * `fn` over `items`, at most `limit` in flight; rejects on the first failure
16357
+ * and starts nothing after it (or after `beforeEach` throws).
16358
+ */
16359
+ function mapWithConcurrency(items_1, limit_1, fn_1) {
16360
+ return __awaiter(this, arguments, void 0, function* (items, limit, fn, beforeEach = () => undefined) {
16361
+ const results = new Array(items.length);
16362
+ let next = 0;
16363
+ let failed = false;
16364
+ const worker = () => __awaiter(this, void 0, void 0, function* () {
16365
+ while (!failed && next < items.length) {
16366
+ const index = next++;
16367
+ try {
16368
+ beforeEach();
16369
+ results[index] = yield fn(items[index]);
16370
+ }
16371
+ catch (error) {
16372
+ failed = true;
16373
+ throw error;
16374
+ }
16375
+ }
16376
+ });
16377
+ yield Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
16378
+ return results;
16379
+ });
16380
+ }
16381
+ /**
16382
+ * Immutable past-block reads, per SDK instance. Keys carry the contract (or
16383
+ * subgraph), the pool and the block, so two deployments never collide.
16384
+ */
16385
+ class PastBlockCache {
16386
+ constructor() {
16387
+ this._entries = new Map();
16388
+ }
16389
+ get(key) {
16390
+ return this._entries.get(key);
16391
+ }
16392
+ set(key, value) {
16393
+ if (this._entries.size >= TVL_HISTORY_CACHE_LIMIT) {
16394
+ this._entries.clear();
16395
+ }
16396
+ this._entries.set(key, value);
16397
+ }
16398
+ get size() {
16399
+ return this._entries.size;
16400
+ }
16401
+ }
16402
+ /**
16403
+ * The off-chain TVL (BASE units) at each block, and — with `withLatest` — at
16404
+ * the latest block, all inside ONE bounded pool of reads.
16405
+ *
16406
+ * Before the contract existed there was nothing to record, so those blocks
16407
+ * are 0 — decided by `getCode`, never by catching a revert: an RPC that
16408
+ * refuses past state can surface that refusal as a "call exception". And a
16409
+ * "no code" answer is believed only if the same provider DOES show code for
16410
+ * the pool at the oldest and newest such block (it cleared there); otherwise
16411
+ * `PastStateUnavailableError`. Every other failed read throws.
16412
+ *
16413
+ * `cached` blocks (final, below the head) are answered from `cache` and
16414
+ * stored there after a successful read.
16415
+ */
16416
+ function fetchOffChainTvlAtBlocks(reader_1, blocks_1, _a) {
16417
+ return __awaiter(this, arguments, void 0, function* (reader, blocks, { withLatest = false, cancellation, cache, cacheKey, cacheable = () => false, }) {
16418
+ let rpcReads = 0;
16419
+ const cached = (kind, block, load) => {
16420
+ const key = cache && cacheKey && cacheable(block)
16421
+ ? cacheKey(kind, block)
16422
+ : undefined;
16423
+ const hit = key === undefined ? undefined : cache === null || cache === void 0 ? void 0 : cache.get(key);
16424
+ if (hit !== undefined)
16425
+ return Promise.resolve(hit);
16426
+ cancellation.throwIfCancelled();
16427
+ rpcReads += 1;
16428
+ return load().then((value) => {
16429
+ if (key !== undefined)
16430
+ cache === null || cache === void 0 ? void 0 : cache.set(key, value);
16431
+ return value;
16432
+ });
16433
+ };
16434
+ const deployedFrom = yield firstBlockWithCode(blocks, (block) => cached('code', block, () => reader.hasCodeAt(block)));
16435
+ if (deployedFrom > 0) {
16436
+ for (const block of new Set([blocks[0], blocks[deployedFrom - 1]])) {
16437
+ const poolDeployed = yield cached('pool', block, () => reader.poolHasCodeAt(block));
16438
+ if (!poolDeployed)
16439
+ throw new PastStateUnavailableError(block);
16440
+ }
16441
+ }
16442
+ const offChain = new Map();
16443
+ for (const block of blocks.slice(0, deployedFrom)) {
16444
+ offChain.set(block, ethers.BigNumber.from(0));
16445
+ }
16446
+ const targets = [...blocks.slice(deployedFrom)];
16447
+ if (withLatest)
16448
+ targets.push('latest');
16449
+ const values = yield mapWithConcurrency(targets, TVL_HISTORY_RPC_CONCURRENCY, (block) => {
16450
+ if (block !== 'latest') {
16451
+ return cached('tvl', block, () => reader.readAt(block));
16452
+ }
16453
+ // Never cached: "latest" moves.
16454
+ rpcReads += 1;
16455
+ return reader.readAt('latest');
16456
+ }, () => {
16457
+ cancellation.throwIfCancelled();
16458
+ });
16459
+ let latest = null;
16460
+ targets.forEach((block, i) => {
16461
+ if (block === 'latest')
16462
+ latest = values[i];
16463
+ else
16464
+ offChain.set(block, values[i]);
16465
+ });
16466
+ return { offChain, latest, rpcReads };
16467
+ });
16468
+ }
16469
+ /**
16470
+ * Pure: the reads → the history. One `clearing` point per clearing, ordered by
16471
+ * the block it finished in (then by epoch, for clearings that shared a block),
16472
+ * so the series is ascending in TIME even where epoch numbers jump; then the
16473
+ * `current` point. Each point is `poolTvl(onChain, offChain)` — the same
16474
+ * function `getPoolOverview` uses for `totalValueLocked.total`.
16475
+ */
16476
+ function buildPoolTvlHistory(read) {
16477
+ const offChainDisplay = (value) => utils$2.formatUnits(value, read.decimals);
16478
+ const entries = [...read.clearings]
16479
+ .sort((a, b) => a.block - b.block || a.epochId - b.epochId)
16480
+ .map((clearing) => {
16481
+ const balance = read.balances.get(clearing.block);
16482
+ const offChain = read.offChain.get(clearing.block);
16483
+ if (balance === undefined || offChain === undefined) {
16484
+ throw new Error(`getPoolTvlHistory: no reading at block ${String(clearing.block)}`);
16485
+ }
16486
+ return {
16487
+ epochId: clearing.epochId,
16488
+ timestamp: clearing.timestamp,
16489
+ tvl: poolTvl(balance, offChainDisplay(offChain)),
16490
+ kind: 'clearing',
16491
+ };
16492
+ });
16493
+ if (read.current) {
16494
+ entries.push({
16495
+ epochId: read.current.epochId,
16496
+ timestamp: read.current.timestamp,
16497
+ tvl: poolTvl(read.current.balance, offChainDisplay(read.current.offChain)),
16498
+ kind: 'current',
16499
+ });
16500
+ }
16501
+ return entries;
16502
+ }
16503
+ /** Ascending, de-duplicated clearing blocks: each past block is read ONCE. */
16504
+ function distinctBlocks(clearings) {
16505
+ return [...new Set(clearings.map((c) => c.block))].sort((a, b) => a - b);
16506
+ }
16507
+
16508
+ /** Thrown by `getPoolTvlHistory` when no provider that serves past state is configured. */
16509
+ const NO_HISTORY_PROVIDER_MESSAGE = 'getPoolTvlHistory: no history provider; pass historyProvider or set historyRpcUrls to RPCs that serve past state';
15912
16510
  class DataService {
15913
- constructor(_kasuConfig, signerOrProvider) {
16511
+ /**
16512
+ * @param historyProvider - a provider that SERVES PAST STATE (an archive
16513
+ * RPC), used only by `getPoolTvlHistory` for the off-chain TVL at old
16514
+ * blocks. Without one, providers are built on `SdkConfig.historyRpcUrls`
16515
+ * (tried in order); with neither, that one call throws
16516
+ * `NO_HISTORY_PROVIDER_MESSAGE`. Never the default provider: a pruned
16517
+ * node refuses old blocks.
16518
+ */
16519
+ constructor(_kasuConfig, signerOrProvider, historyProvider) {
15914
16520
  this._kasuConfig = _kasuConfig;
16521
+ /** Immutable past-block reads, kept for the life of this instance. */
16522
+ this._tvlHistoryCache = new PastBlockCache();
15915
16523
  this._externalTvlAbi = KasuPoolExternalTVLAbi__factory.connect(_kasuConfig.contracts.ExternalTVL, signerOrProvider);
16524
+ this._systemVariablesAbi = ISystemVariablesAbi__factory.connect(_kasuConfig.contracts.SystemVariables, signerOrProvider);
15916
16525
  this._graph = new GraphQLClient(_kasuConfig.subgraphUrl);
16526
+ this._explicitHistoryProvider = historyProvider;
15917
16527
  this._directus = createDirectusClient(_kasuConfig.directusUrl);
15918
16528
  }
15919
16529
  getUrlFromFile(fileName) {
@@ -15993,6 +16603,194 @@ class DataService {
15993
16603
  return tvlMap;
15994
16604
  });
15995
16605
  }
16606
+ /**
16607
+ * The pool's TVL — the total lent into it by everyone — at every cycle
16608
+ * boundary, then as of now: the data behind a TVL chart.
16609
+ *
16610
+ * One `clearing` point per FINISHED clearing, read at the block it
16611
+ * finished in (`LendingPoolClearing.endBlockNumber`), then one `current`
16612
+ * point. Every point is `poolTvl(onChain, offChain)`, the definition
16613
+ * `getPoolOverview` uses for `totalValueLocked.total`:
16614
+ *
16615
+ * - on-chain: `lendingPool.balance` from the subgraph, read with
16616
+ * time-travel queries (`block: { number }`);
16617
+ * - off-chain: `ExternalTVL.externalTVLOfPool(pool)` at the same block,
16618
+ * through the HISTORY providers (see the constructor). 0 before the
16619
+ * contract was deployed (decided by `getCode`, and only if the same
16620
+ * provider shows the pool's own code there); any failed read throws.
16621
+ *
16622
+ * The `current` point mixes two sources: the subgraph's latest balance
16623
+ * (and its latest block time) and `externalTVLOfPool` at `latest` on the
16624
+ * history provider. `getPoolOverview` reads the same two, the off-chain
16625
+ * one on the default provider, so the last point equals
16626
+ * `totalValueLocked.total` when both reads succeed. They can differ when
16627
+ * `getPoolOverview`'s off-chain read FAILS — it then falls back to 0 —
16628
+ * or when the figure moves between the two calls.
16629
+ *
16630
+ * Units: `tvl` in display units of the deployment's own stable token
16631
+ * (USDC on Base and XDC USDC, AUDD on XDC AUDD); `timestamp` in unix
16632
+ * seconds. Ascending in time. See `PoolTvlHistoryEntry`.
16633
+ *
16634
+ * Cost: one subgraph request for the clearings (per thousand), one per
16635
+ * hundred distinct uncached clearing blocks, a `currentEpochNumber` read;
16636
+ * then, on the history providers, one `externalTVLOfPool` per distinct
16637
+ * uncached block plus the latest, and one to about log2(n) `getCode`s —
16638
+ * at most `TVL_HISTORY_RPC_CONCURRENCY` in flight. Final blocks are
16639
+ * cached on this instance, so a second call reads only what is new.
16640
+ * `timeoutMs` (default 30 s) bounds the whole call.
16641
+ *
16642
+ * `[]` for a pool the subgraph does not know, for a window with no epoch
16643
+ * in it, and on a LITE deployment whose subgraph lacks the entities. On a
16644
+ * full deployment that schema error throws, like every network, indexing
16645
+ * or past-state failure.
16646
+ *
16647
+ * @throws RangeError / TypeError on a malformed option or a pool id that
16648
+ * is not an address, and `NO_HISTORY_PROVIDER_MESSAGE` when the
16649
+ * deployment has an `ExternalTVL` contract but no history provider — all
16650
+ * before any network read. `PoolTvlHistoryTimeoutError` /
16651
+ * `PoolTvlHistoryAbortedError` on the deadline or the signal,
16652
+ * `PastStateUnavailableError` from a pruned history provider.
16653
+ */
16654
+ getPoolTvlHistory(poolId_1) {
16655
+ return __awaiter(this, arguments, void 0, function* (poolId, options = {}) {
16656
+ var _a;
16657
+ validatePoolTvlHistoryOptions(options);
16658
+ if (!utils$2.isAddress(poolId)) {
16659
+ throw new TypeError(`getPoolTvlHistory: poolId must be an address, got ${String(poolId)}`);
16660
+ }
16661
+ const pool = poolId.toLowerCase();
16662
+ const externalTvl = this._externalTvlAddress();
16663
+ const rpcs = externalTvl ? this._historyRpcs() : [];
16664
+ if (externalTvl && rpcs.length === 0) {
16665
+ throw new Error(NO_HISTORY_PROVIDER_MESSAGE);
16666
+ }
16667
+ return yield runCancellable((_a = options.timeoutMs) !== null && _a !== void 0 ? _a : TVL_HISTORY_DEFAULT_TIMEOUT_MS, options.signal, (cancellation) => this._readPoolTvlHistory(pool, options, externalTvl
16668
+ ? this._createOffChainTvlReader({
16669
+ externalTvl,
16670
+ pool,
16671
+ rpcs,
16672
+ cancellation,
16673
+ })
16674
+ : null, externalTvl !== null && externalTvl !== void 0 ? externalTvl : '', cancellation));
16675
+ });
16676
+ }
16677
+ _readPoolTvlHistory(pool, options, reader, externalTvl, cancellation) {
16678
+ return __awaiter(this, void 0, void 0, function* () {
16679
+ var _a, _b, _c;
16680
+ const graph = (document, variables, signal) => this._graph.request({ document, variables, signal });
16681
+ const scope = options.fromEpoch === undefined && options.count !== undefined
16682
+ ? { kind: 'newest', count: options.count }
16683
+ : { kind: 'from', fromEpoch: (_a = options.fromEpoch) !== null && _a !== void 0 ? _a : 0 };
16684
+ try {
16685
+ const [currentEpochNumber, { clearings, snapshot }] = yield Promise.all([
16686
+ this._systemVariablesAbi.currentEpochNumber(),
16687
+ fetchPoolClearings(graph, pool, scope, cancellation),
16688
+ ]);
16689
+ const currentEpoch = currentEpochNumber.toNumber();
16690
+ const window = resolvePoolTvlHistoryWindow(options, currentEpoch);
16691
+ const poolNow = snapshot.pool;
16692
+ if (!window || !poolNow)
16693
+ return [];
16694
+ const head = (_b = snapshot.meta) === null || _b === void 0 ? void 0 : _b.block;
16695
+ if (!head || typeof head.timestamp !== 'number') {
16696
+ throw new Error('getPoolTvlHistory: the subgraph returned no latest block time');
16697
+ }
16698
+ const inWindow = clearings.filter((c) => c.epochId >= window.fromEpoch &&
16699
+ c.epochId <= window.toEpoch);
16700
+ const blocks = distinctBlocks(inWindow);
16701
+ const withCurrent = window.toEpoch >= currentEpoch;
16702
+ const cacheable = (block) => block <= head.number - TVL_HISTORY_CACHE_DEPTH;
16703
+ const [balances, offChainRead] = yield Promise.all([
16704
+ this._balancesAtBlocks(graph, pool, blocks, cacheable, cancellation),
16705
+ reader
16706
+ ? fetchOffChainTvlAtBlocks(reader, blocks, {
16707
+ withLatest: withCurrent,
16708
+ cancellation,
16709
+ cache: this._tvlHistoryCache,
16710
+ cacheKey: (kind, block) => `${kind}|${externalTvl}|${pool}|${String(block)}`,
16711
+ cacheable,
16712
+ })
16713
+ : {
16714
+ offChain: new Map(blocks.map((b) => [b, ethers.BigNumber.from(0)])),
16715
+ latest: null,
16716
+ },
16717
+ ]);
16718
+ return buildPoolTvlHistory({
16719
+ clearings: inWindow,
16720
+ balances,
16721
+ offChain: offChainRead.offChain,
16722
+ current: withCurrent
16723
+ ? {
16724
+ epochId: currentEpoch,
16725
+ timestamp: head.timestamp,
16726
+ balance: poolNow.balance,
16727
+ offChain: (_c = offChainRead.latest) !== null && _c !== void 0 ? _c : ethers.BigNumber.from(0),
16728
+ }
16729
+ : null,
16730
+ decimals: this._kasuConfig.stableAssetDecimals,
16731
+ });
16732
+ }
16733
+ catch (error) {
16734
+ if (isEmptyHistoryError(error, this._kasuConfig.isLiteDeployment)) {
16735
+ return [];
16736
+ }
16737
+ throw error;
16738
+ }
16739
+ });
16740
+ }
16741
+ /** The pool's balance at each block: cached final blocks, then one batch. */
16742
+ _balancesAtBlocks(graph, pool, blocks, cacheable, cancellation) {
16743
+ return __awaiter(this, void 0, void 0, function* () {
16744
+ const key = (block) => `balance|${this._kasuConfig.subgraphUrl}|${pool}|${String(block)}`;
16745
+ const balances = new Map();
16746
+ const missing = [];
16747
+ for (const block of blocks) {
16748
+ const hit = cacheable(block)
16749
+ ? this._tvlHistoryCache.get(key(block))
16750
+ : undefined;
16751
+ if (hit === undefined)
16752
+ missing.push(block);
16753
+ else
16754
+ balances.set(block, hit);
16755
+ }
16756
+ if (missing.length > 0) {
16757
+ const read = yield fetchPoolBalancesAtBlocks(graph, pool, missing, cancellation);
16758
+ for (const [block, balance] of read.balances) {
16759
+ balances.set(block, balance);
16760
+ if (cacheable(block))
16761
+ this._tvlHistoryCache.set(key(block), balance);
16762
+ }
16763
+ }
16764
+ return balances;
16765
+ });
16766
+ }
16767
+ /** `null` when the deployment has no `ExternalTVL` contract. */
16768
+ _externalTvlAddress() {
16769
+ const address = this._kasuConfig.contracts.ExternalTVL;
16770
+ if (!address || /^0x0{40}$/i.test(address))
16771
+ return null;
16772
+ return address.toLowerCase();
16773
+ }
16774
+ /** Seam for tests: the real reader over the history RPCs. */
16775
+ _createOffChainTvlReader(args) {
16776
+ return createOffChainTvlReader(args);
16777
+ }
16778
+ /**
16779
+ * The history RPCs, in fallback order: the explicit provider, else ONE
16780
+ * provider per `historyRpcUrls` entry — built here and nowhere else,
16781
+ * pinned to `SdkConfig.chainId` when known, each with a per-call timeout.
16782
+ * Built once per instance.
16783
+ */
16784
+ _historyRpcs() {
16785
+ if (this._explicitHistoryProvider)
16786
+ return [this._explicitHistoryProvider];
16787
+ if (!this._builtHistoryProviders) {
16788
+ // Through the `ethers` namespace, so the consumer's own build
16789
+ // (Node or browser) is used — see `defaultProvider` in the facade.
16790
+ this._builtHistoryProviders = this._kasuConfig.historyRpcUrls.map((url) => new ethers.providers.StaticJsonRpcProvider({ url, timeout: TVL_HISTORY_RPC_TIMEOUT_MS }, this._kasuConfig.chainId));
16791
+ }
16792
+ return this._builtHistoryProviders;
16793
+ }
15996
16794
  getPoolOverview(epochId, id_in) {
15997
16795
  return __awaiter(this, void 0, void 0, function* () {
15998
16796
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
@@ -16133,8 +16931,8 @@ class DataService {
16133
16931
  subheading: lendingPoolDirectus === null || lendingPoolDirectus === void 0 ? void 0 : lendingPoolDirectus.subheading,
16134
16932
  operatingSince: lendingPoolDirectus === null || lendingPoolDirectus === void 0 ? void 0 : lendingPoolDirectus.operatingSince,
16135
16933
  totalValueLocked: {
16136
- total: (parseFloat(lendingPoolSubgraph.balance) +
16137
- parseFloat(offChainTvl)).toString(),
16934
+ // The one TVL definition, shared with getPoolTvlHistory.
16935
+ total: poolTvl(lendingPoolSubgraph.balance, offChainTvl).toString(),
16138
16936
  offchain: offChainTvl,
16139
16937
  },
16140
16938
  loansUnderManagement: (_q = lendingPoolDirectus === null || lendingPoolDirectus === void 0 ? void 0 : lendingPoolDirectus.loansUnderManagement) !== null && _q !== void 0 ? _q : '0',
@@ -18295,35 +19093,6 @@ function buildYieldHistory(read, window, clock) {
18295
19093
  a.poolId.localeCompare(b.poolId) ||
18296
19094
  a.trancheId.localeCompare(b.trancheId));
18297
19095
  }
18298
- /**
18299
- * A GraphQL VALIDATION error naming an entity, field or argument this
18300
- * deployment's subgraph does not have. A network error, a timeout or an
18301
- * indexing error is not one.
18302
- */
18303
- function isMissingEntityError(error) {
18304
- if (typeof error !== 'object' || error === null)
18305
- return false;
18306
- const response = error.response;
18307
- const errors = response === null || response === void 0 ? void 0 : response.errors;
18308
- if (!Array.isArray(errors) || errors.length === 0)
18309
- return false;
18310
- return errors.every((e) => {
18311
- const message = e === null || e === void 0 ? void 0 : e.message;
18312
- return (typeof message === 'string' &&
18313
- /has no field|cannot query field|unknown (type|field|argument)|no such (type|field)/i.test(message));
18314
- });
18315
- }
18316
- /**
18317
- * The only failure `getYieldHistory` turns into `[]`: a missing-schema error
18318
- * on a LITE deployment, whose subgraph may predate the entities. On a full
18319
- * deployment the entities are part of the schema, so the same error means a
18320
- * broken query or subgraph and THROWS — an empty chart there would hide it.
18321
- * Everything else (network, timeout, indexing) throws on every deployment, so
18322
- * a consumer can tell "no history here" from "history unavailable".
18323
- */
18324
- function isEmptyHistoryError(error, liteDeployment) {
18325
- return liteDeployment && isMissingEntityError(error);
18326
- }
18327
19096
 
18328
19097
  class Portfolio {
18329
19098
  constructor(_kasuConfig, signerOrProvider) {
@@ -24767,11 +25536,11 @@ class Swapper {
24767
25536
  }
24768
25537
 
24769
25538
  class KasuSdk {
24770
- constructor(config, signerOrProvider) {
25539
+ constructor(config, signerOrProvider, options = {}) {
24771
25540
  this._graphClient = new GraphQLClient(config.subgraphUrl);
24772
25541
  this.Locking = new KSULocking(config, signerOrProvider);
24773
25542
  this.UserLending = new UserLending(config, signerOrProvider);
24774
- this.DataService = new DataService(config, signerOrProvider);
25543
+ this.DataService = new DataService(config, signerOrProvider, options.historyProvider);
24775
25544
  this.Portfolio = new Portfolio(config, signerOrProvider);
24776
25545
  this.Swapper = new Swapper();
24777
25546
  }
@@ -24779,7 +25548,7 @@ class KasuSdk {
24779
25548
 
24780
25549
  class SdkConfig {
24781
25550
  constructor(options) {
24782
- var _a, _b, _c, _d;
25551
+ var _a, _b, _c, _d, _e;
24783
25552
  this.subgraphUrl = options.subgraphUrl;
24784
25553
  this.contracts = options.contracts;
24785
25554
  this.directusUrl = (_a = options.directusUrl) !== null && _a !== void 0 ? _a : '';
@@ -24794,6 +25563,8 @@ class SdkConfig {
24794
25563
  this.isLiteDeployment = (_b = options.isLiteDeployment) !== null && _b !== void 0 ? _b : false;
24795
25564
  this.poolMetadataMapping = (_c = options.poolMetadataMapping) !== null && _c !== void 0 ? _c : {};
24796
25565
  this.stableAssetDecimals = (_d = options.stableAssetDecimals) !== null && _d !== void 0 ? _d : 6;
25566
+ this.historyRpcUrls = [...((_e = options.historyRpcUrls) !== null && _e !== void 0 ? _e : [])];
25567
+ this.chainId = options.chainId;
24797
25568
  }
24798
25569
  }
24799
25570
 
@@ -25100,6 +25871,27 @@ const XDC_RPC_URLS = [
25100
25871
  'https://rpc.xinfin.network',
25101
25872
  'https://rpc.primenumbers.xyz/',
25102
25873
  ];
25874
+ /**
25875
+ * XDC RPCs that serve PAST state, for `getPoolTvlHistory` (each verified
25876
+ * with `externalTVLOfPool` at old blocks, CORS `*`), in fallback order. Not
25877
+ * `rpc.xdcrpc.com`, which refuses older blocks, and never `rpc.xdc.org` /
25878
+ * `erpc.xdc.org`. Public and rate-limited.
25879
+ */
25880
+ const XDC_HISTORY_RPC_URLS = [
25881
+ 'https://rpc.primenumbers.xyz/',
25882
+ 'https://rpc.ankr.com/xdc',
25883
+ ];
25884
+ /**
25885
+ * Base RPCs that serve PAST state, in fallback order (each verified with
25886
+ * `externalTVLOfPool` at old blocks; all answer CORS). Not
25887
+ * `base-rpc.publicnode.com`, the default read RPC, which refuses old state.
25888
+ * Public and rate-limited.
25889
+ */
25890
+ const BASE_HISTORY_RPC_URLS = [
25891
+ 'https://mainnet.base.org',
25892
+ 'https://base.drpc.org',
25893
+ 'https://1rpc.io/base',
25894
+ ];
25103
25895
  /**
25104
25896
  * Built-in chain configurations for Kasu-supported networks.
25105
25897
  *
@@ -25143,6 +25935,7 @@ const CHAIN_CONFIGS = {
25143
25935
  'https://base-rpc.publicnode.com',
25144
25936
  'https://mainnet.base.org',
25145
25937
  ],
25938
+ historyRpcUrls: BASE_HISTORY_RPC_URLS,
25146
25939
  blockExplorerUrl: 'https://basescan.org',
25147
25940
  },
25148
25941
  xdc: {
@@ -25179,6 +25972,7 @@ const CHAIN_CONFIGS = {
25179
25972
  currencyCode: 'AUD',
25180
25973
  },
25181
25974
  rpcUrls: XDC_RPC_URLS,
25975
+ historyRpcUrls: XDC_HISTORY_RPC_URLS,
25182
25976
  blockExplorerUrl: 'https://xdcscan.com',
25183
25977
  },
25184
25978
  'xdc-usdc': {
@@ -25211,6 +26005,7 @@ const CHAIN_CONFIGS = {
25211
26005
  currencyCode: 'USD',
25212
26006
  },
25213
26007
  rpcUrls: XDC_RPC_URLS,
26008
+ historyRpcUrls: XDC_HISTORY_RPC_URLS,
25214
26009
  blockExplorerUrl: 'https://xdcscan.com',
25215
26010
  },
25216
26011
  plume: {
@@ -28322,6 +29117,28 @@ class StrategiesFacade {
28322
29117
  return yield this._dataService.getPerformanceFee();
28323
29118
  });
28324
29119
  }
29120
+ /**
29121
+ * The strategy's TVL (total lent by everyone) at every cycle boundary,
29122
+ * then as of now — the data behind a TVL chart. Ascending in time; the
29123
+ * last point (`kind: 'current'`) equals `strategy.tvl.total` when both
29124
+ * calls' reads succeed (see `PoolTvlHistoryEntry`).
29125
+ *
29126
+ * `tvl` is in display units of the deployment's OWN stable token (USDC on
29127
+ * Base and XDC USDC, AUDD on XDC AUDD); `timestamp` is unix SECONDS.
29128
+ * `{ count }` = the newest `count` epochs, `{ fromEpoch }` = from that
29129
+ * epoch on; `timeoutMs` (default 30 s) and `signal` bound the call. A
29130
+ * malformed option throws before any network read. See
29131
+ * `DataService.getPoolTvlHistory` for the reads, the cost and the errors.
29132
+ *
29133
+ * ```ts
29134
+ * const points = await kasu.strategies.getPoolTvlHistory(poolId, { count: 52 });
29135
+ * ```
29136
+ */
29137
+ getPoolTvlHistory(poolId_1) {
29138
+ return __awaiter(this, arguments, void 0, function* (poolId, options = {}) {
29139
+ return yield this._dataService.getPoolTvlHistory(poolId, options);
29140
+ });
29141
+ }
28325
29142
  /**
28326
29143
  * Fetch a single strategy by pool ID. Returns `null` if not found.
28327
29144
  */
@@ -28923,12 +29740,13 @@ class PortfolioFacade {
28923
29740
  * ```
28924
29741
  */
28925
29742
  class Kasu {
28926
- constructor(sdk, chainConfig, signerOrProvider, configOverrides) {
29743
+ constructor(sdk, chainConfig, signerOrProvider, configOverrides, historyProvider) {
28927
29744
  var _a, _b;
28928
29745
  this._sdk = sdk;
28929
29746
  this._chainConfig = chainConfig;
28930
29747
  this._signerOrProvider = signerOrProvider;
28931
29748
  this._configOverrides = configOverrides;
29749
+ this._historyProvider = historyProvider;
28932
29750
  this.strategies = new StrategiesFacade(sdk.DataService, sdk.UserLending, normaliseMapping((_a = configOverrides.poolMetadataMapping) !== null && _a !== void 0 ? _a : chainConfig.poolMetadataMapping));
28933
29751
  // Derived ONCE and handed to every facade that needs it. Each of them
28934
29752
  // re-deriving `Signer.isSigner` is how two facades come to disagree
@@ -28966,7 +29784,7 @@ class Kasu {
28966
29784
  * ```
28967
29785
  */
28968
29786
  static create(options) {
28969
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
29787
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
28970
29788
  const chainConfig = resolveChainConfig(options.chain);
28971
29789
  const overrides = (_a = options.configOverrides) !== null && _a !== void 0 ? _a : {};
28972
29790
  const sdkConfig = new SdkConfig({
@@ -28981,12 +29799,18 @@ class Kasu {
28981
29799
  // of 6. Every live deployment happens to be 6dp today, so the
28982
29800
  // default was right by luck; the next one need not be.
28983
29801
  stableAssetDecimals: (_h = overrides.stableAssetDecimals) !== null && _h !== void 0 ? _h : (_j = stableAssetOf(chainConfig)) === null || _j === void 0 ? void 0 : _j.decimals,
29802
+ historyRpcUrls: (_k = overrides.historyRpcUrls) !== null && _k !== void 0 ? _k : chainConfig.historyRpcUrls,
29803
+ chainId: (_l = overrides.chainId) !== null && _l !== void 0 ? _l : chainConfig.chainId,
28984
29804
  });
28985
- const signerOrProvider = (_k = options.signerOrProvider) !== null && _k !== void 0 ? _k : defaultProvider(chainConfig, typeof options.chain === 'string'
29805
+ const signerOrProvider = (_m = options.signerOrProvider) !== null && _m !== void 0 ? _m : defaultProvider(chainConfig, typeof options.chain === 'string'
28986
29806
  ? options.chain
28987
29807
  : chainConfig.name);
28988
- const sdk = new KasuSdk(sdkConfig, signerOrProvider);
28989
- return new Kasu(sdk, chainConfig, signerOrProvider, overrides);
29808
+ // An explicit history provider wins; otherwise DataService builds
29809
+ // them from `historyRpcUrls`, pinned to `chainId` (the one place).
29810
+ const sdk = new KasuSdk(sdkConfig, signerOrProvider, {
29811
+ historyProvider: options.historyProvider,
29812
+ });
29813
+ return new Kasu(sdk, chainConfig, signerOrProvider, overrides, options.historyProvider);
28990
29814
  }
28991
29815
  /**
28992
29816
  * A NEW instance bound to `signer`, with the same chain config and the same
@@ -29006,6 +29830,7 @@ class Kasu {
29006
29830
  chain: this._chainConfig,
29007
29831
  signerOrProvider: signer,
29008
29832
  configOverrides: this._configOverrides,
29833
+ historyProvider: this._historyProvider,
29009
29834
  });
29010
29835
  }
29011
29836
  /**
@@ -29817,9 +30642,13 @@ exports.MIN_LENDING_AMOUNT_FALLBACK = MIN_LENDING_AMOUNT_FALLBACK;
29817
30642
  exports.MIN_TRANCHE_CAPACITY = MIN_TRANCHE_CAPACITY;
29818
30643
  exports.NOT_A_WITHDRAWAL_CALL_MESSAGE = NOT_A_WITHDRAWAL_CALL_MESSAGE;
29819
30644
  exports.NO_DIRECTUS_URL_MESSAGE = NO_DIRECTUS_URL_MESSAGE;
30645
+ exports.NO_HISTORY_PROVIDER_MESSAGE = NO_HISTORY_PROVIDER_MESSAGE;
29820
30646
  exports.NO_KYC_PARAMS_MESSAGE = NO_KYC_PARAMS_MESSAGE;
29821
30647
  exports.NO_SPENDER_MESSAGE = NO_SPENDER_MESSAGE;
29822
30648
  exports.NO_WITHDRAWAL_ENCODER_MESSAGE = NO_WITHDRAWAL_ENCODER_MESSAGE;
30649
+ exports.PastStateUnavailableError = PastStateUnavailableError;
30650
+ exports.PoolTvlHistoryAbortedError = PoolTvlHistoryAbortedError;
30651
+ exports.PoolTvlHistoryTimeoutError = PoolTvlHistoryTimeoutError;
29823
30652
  exports.PortfolioFacade = PortfolioFacade;
29824
30653
  exports.READ_ONLY_MESSAGE = READ_ONLY_MESSAGE;
29825
30654
  exports.RIXON_CAPITAL = RIXON_CAPITAL;
@@ -29898,6 +30727,7 @@ exports.pickHighestYieldTranche = pickHighestYieldTranche;
29898
30727
  exports.pickShortDescription = pickShortDescription;
29899
30728
  exports.poolAllTranchesFull = poolAllTranchesFull;
29900
30729
  exports.poolMaxApy = poolMaxApy;
30730
+ exports.poolTvl = poolTvl;
29901
30731
  exports.projectedAnnualEarnings = projectedAnnualEarnings;
29902
30732
  exports.proveDepositInReceipt = proveDepositInReceipt;
29903
30733
  exports.proveUserOperationInReceipt = proveUserOperationInReceipt;