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