import { EpochTrancheYield, YieldHistoryEntry } from '../../domain/yield-history'; /** * Which settled epochs a history covers. Epochs are counted in the chain's * numbering (`SystemVariables.currentEpochNumber`). Only SETTLED epochs exist * in a history — the newest is `currentEpoch − 1`. * * - neither field: every settled epoch; * - `count` only: the newest `count` settled epochs; * - `fromEpoch` only: `fromEpoch` up to the newest settled epoch; * - both: `count` epochs starting at `fromEpoch`, capped at the newest settled. */ export interface YieldHistoryOptions { /** First epoch included. A non-negative integer. */ fromEpoch?: number; /** How many epochs. A positive integer. */ count?: number; } /** The resolved, inclusive epoch range; `null` when it holds no settled epoch. */ export interface YieldHistoryWindow { fromEpoch: number; toEpoch: number; } /** The subgraph returns at most this many rows per collection per request. */ export declare const YIELD_HISTORY_PAGE_SIZE = 1000; /** * A runaway guard, not a working limit: a collection this many pages long * (`MAX × PAGE_SIZE` rows for one lender) is a broken response, not a history. */ export declare const YIELD_HISTORY_MAX_PAGES = 100; /** * Throws `RangeError` on a malformed `fromEpoch` / `count`. `getYieldHistory` * calls it BEFORE any network read. */ export declare function validateYieldHistoryOptions(options: YieldHistoryOptions): void; export declare function resolveYieldHistoryWindow(options: YieldHistoryOptions, currentEpoch: number): YieldHistoryWindow | null; /** * Round trip 1: the lender's tranche positions and fixed-term locks. Same * pool exclusion and the same nested-list size (`$first`) as * `lendingPortfolioQuery`, so both read the same lock set. */ export declare const yieldHistoryPositionsQuery: string; /** * Round trip 2 onwards: the six per-epoch series, one page of each per * request, cursor-paginated by `id`. A series that is exhausted is skipped with * `@include`, so a history costs as many requests as its LONGEST series has * pages — normally one — never one per epoch. * * Share snapshots are read up to the window's end with no lower bound: the * value AS OF the first epoch may come from an update long before it. Each * carries its `timestamp`, and the window's clearings their start, because a * cycle's interest is paired with the shares in force when the clearing * APPLIED it (`sharesAtInterest`), which the epoch tag alone gets wrong for a * withdrawal accepted between clearings — so the lender's updates are read one * tag past the window (`$userSharesToEpoch`), where the subgraph puts it. * Every share exit between clearings is read too (`trancheSharesAtInterest` * rebuilds the tranche total from the previous clearing's snapshot). */ export declare const yieldHistoryPageQuery: string; export interface YieldHistoryPositionsResult { user: { lendingPoolUserDetails: { lendingPool: { id: string; }; lendingPoolTrancheUserDetails: { id: string; /** The ledger's share count (accepted deposits − withdrawals ± top-ups). */ shares?: string; tranche: { id: string; balance: string; shares: string; }; userLendingPoolTrancheFixedTermDepositLocks: { id: string; epochLockStart: string; epochLockEnd: string; initialTrancheShares: string; }[]; }[]; }[]; } | null; /** The block the subgraph answered at. */ _meta?: { block: { number: number; }; }; } /** One share snapshot: the value from `epochId` on, until the next one. */ export interface ShareUpdateRow { id: string; epochId: string; shareUpdatesIndex: number; shares: string; } /** A tranche or lender share snapshot, with the block time it was written at. */ export interface TimedShareUpdateRow extends ShareUpdateRow { /** Unix seconds. */ timestamp: string; } /** What the share selection reads from a snapshot — no entity id needed. */ export type TimedShareSnapshot = Pick; export interface InterestRow { id: string; epochId: string; epochInterestAmount: string; lendingPoolTranche: { id: string; }; } /** A pool's clearing for one epoch: its first transaction applied the interest. */ export interface ClearingRow { id: string; epochId: string; /** Unix seconds of the clearing's first transaction. */ startTimestamp: string; lendingPool: { id: string; }; } /** * Shares leaving a tranche (any lender): an accepted withdrawal or an * immediate one. Each carries the pool's clearing epoch at the time * (`epochClearingId`) — so one between clearings E − 1 and E is tagged E. */ export interface ShareExitRow { id: string; type: 'WithdrawalAccepted' | 'ImmediateWithdrawalExecuted'; epochId: string; /** Unix seconds. */ createdOn: string; sharesAmount: string | null; tranche: { id: string; }; } export interface YieldHistoryPageResult { interest?: InterestRow[]; trancheShares?: (TimedShareUpdateRow & { lendingPoolTranche: { id: string; }; })[]; userShares?: (TimedShareUpdateRow & { lendingPoolTrancheUserDetails: { id: string; }; })[]; lockShares?: (ShareUpdateRow & { userLendingPoolTrancheFixedTermDepositLock: { id: string; }; })[]; clearings?: ClearingRow[]; withdrawals?: ShareExitRow[]; } /** Everything `buildYieldHistory` reads, keyed by the subgraph's entity ids. */ export interface YieldHistoryRead { positions: YieldHistoryPositionsResult; interest: InterestRow[]; trancheShares: (TimedShareUpdateRow & { lendingPoolTranche: { id: string; }; })[]; userShares: (TimedShareUpdateRow & { lendingPoolTrancheUserDetails: { id: string; }; })[]; lockShares: (ShareUpdateRow & { userLendingPoolTrancheFixedTermDepositLock: { id: string; }; })[]; clearings: ClearingRow[]; withdrawals: ShareExitRow[]; } /** The chain's epoch clock: epoch E ends at `epochZeroStart + (E + 1) × duration`. */ export interface EpochClock { epochZeroStart: number; epochDuration: number; } type PageRequest = (variables: Record) => Promise; /** * Fetch the six series for the window, paginating each by `id` until a short * page. Returns the rows and how many requests it took. */ export declare function fetchYieldHistorySeries(request: PageRequest, ids: { pools: string[]; tranches: string[]; userTranches: string[]; locks: string[]; }, window: YieldHistoryWindow): Promise<{ rows: Omit; requests: number; }>; /** * The snapshot in force at `epoch`: among updates with `epochId ≤ epoch`, the * one with the highest `shareUpdatesIndex` — exactly what * `lendingPortfolioQuery` selects with `orderBy: shareUpdatesIndex, desc, * first: 1, where: { epochId_lt: epoch + 1 }`. `undefined` when none. */ export declare function sharesAsOf>(updates: readonly T[], epoch: number): T | undefined; /** * The snapshot in force when the clearing of `epoch` APPLIED that epoch's * interest — the shares the interest was actually spread over. * * A clearing applies interest FIRST (`ClearingCoordinator.doClearing`: the * `applyInterests` call before steps 1–5), then accepts deposits and * withdrawals. So an update is in force for epoch E's interest when it is * tagged `≤ E` (the subgraph tags every update written during clearing E, or * after it, `E + 1` or later), OR when it was written before clearing E began * (`timestamp < clearingStart`). The second test is the one that matters: a * withdrawal the pool manager accepts BETWEEN clearings * (`forceAcceptWithdrawalRequest`) is tagged `E + 1` by the subgraph, as if a * clearing had accepted it, yet the shares were gone before clearing E. * * Without a clearing start (none recorded) it is `sharesAsOf` — the tag alone. */ export declare function sharesAtInterest(updates: readonly T[], epoch: number, clearingStart: number | undefined): T | undefined; /** * The tranche's total shares when the clearing of `epoch` applied its * interest. * * The subgraph writes a tranche snapshot when a clearing ENDS (tagged with * the next epoch) and at each immediate withdrawal, but none when the manager * accepts a withdrawal between clearings — so the newest snapshot can predate * shares that had already left. Instead of guessing the order of events in a * block, the total is rebuilt from the one snapshot whose place is certain: * the FIRST one tagged `epoch`, written when clearing `epoch − 1` ended (the * tag itself starts there). Every share exit between that and clearing * `epoch` is tagged `epoch` (the pool's clearing epoch at the time), and none * can happen during a clearing (`notPendingClearing`, * `verifyClearingNotPending`), while deposits and top-ups only happen inside * one. So: that snapshot less every exit tagged `epoch` before the clearing's * start (the clearing's own acceptances come at or after it). * * Without a clearing start, or without a snapshot tagged `epoch` (a tranche's * first clearing), it is the snapshot in force by tag alone. */ export declare function trancheSharesAtInterest(snapshots: readonly TimedShareSnapshot[], exits: readonly Pick[], epoch: number, clearingStart: number | undefined): number; /** A fixed-term lock inside one lender's tranche position. */ export interface PositionLock { epochLockStart: string; epochLockEnd: string; initialTrancheShares: string; /** The lock's share snapshots (paired by tag: its tags are right). */ updates: readonly Pick[]; } /** Everything one lender's tranche position needs to price one epoch. */ export interface PositionSeries { trancheBalanceNow: number; trancheSharesNow: number; userUpdates: readonly TimedShareSnapshot[]; trancheUpdates: readonly TimedShareSnapshot[]; exits: readonly Pick[]; locks: readonly PositionLock[]; } /** * THE per-position, per-epoch selection both `getYieldHistory` and * `getPortfolioLendingData` run: the shares in force when clearing `epoch` * applied its interest (`sharesAtInterest`, `trancheSharesAtInterest`), each * lock's snapshots as of `epoch` and `epoch − 1`, then `epochTrancheYield`. */ export declare function positionEpochYield(series: PositionSeries, epoch: number, epochInterest: number, clearingStart: number | undefined): { yield: EpochTrancheYield; userShares: number; }; /** One lender's tranche position, as the positions and portfolio queries read it. */ export interface PositionRef { id: string; tranche: { id: string; balance: string; shares: string; }; userLendingPoolTrancheFixedTermDepositLocks: { id?: string; epochLockStart: string; epochLockEnd: string; initialTrancheShares: string; }[]; } /** A position's `PositionSeries` out of the fetched history series. */ export declare function positionSeriesFrom(rows: Omit, position: PositionRef): PositionSeries; /** `pool → epoch → clearing start`, from the clearing rows. */ export declare function clearingStarts(clearings: readonly ClearingRow[]): Map>; /** * Pure: the fetched series → one entry per pool, tranche and settled epoch in * which the lender held shares (or a lock earned a top-up). Each entry runs the * same `epochTrancheYield` as the portfolio's last-epoch figure, with the same * share selection (`sharesAtInterest`, `trancheSharesAtInterest`), so the * newest entry equals that figure. * * Sorted by `epochId`, then `poolId`, then `trancheId`. */ export declare function buildYieldHistory(read: YieldHistoryRead, window: YieldHistoryWindow, clock: EpochClock): YieldHistoryEntry[]; export { isEmptyHistoryError, isMissingEntityError } from '../shared';