import { Provider } from '@ethersproject/providers'; import { BigNumber, Signer } from 'ethers'; import { YieldHistoryEntry } from '../../domain/yield-history'; import { SdkConfig } from '../../sdk-config'; import { TrancheConfigurationSubgraphResult, TrancheSubgraphResult } from '../DataService/subgraph-types'; import { PoolOverview } from '../DataService/types'; import { LockingSummarySubgraphResult, SystemVariables } from '../Locking/types'; import { PortfolioLendingPool, PortfolioRewards, PortfolioSummary, UserLendingPoolTrancheFixedTermDepositLock } from './types'; import { YieldHistoryOptions } from './yield-history'; export declare class Portfolio { private _kasuConfig; private _lockingService; private _dataService; private _userLendingService; private readonly _graph; private _systemVariablesAbi; private _userLoyaltyRewardsAbi; private _kasuNftContract; readonly _signerOrProvider: Signer | Provider; private _isLiteDeployment; constructor(_kasuConfig: SdkConfig, signerOrProvider: Signer | Provider); private computeYieldMetrics; /** * Calculate aggregate yield metrics for a set of portfolio lending pools. * Accepts pre-fetched pool data and returns totals without performing any network calls. */ calculateYieldMetrics(portfolioLendingPools: PortfolioLendingPool[]): { totalInvestments: number; totalYieldEarned: number; weightedApy: number; weeklyYieldEarnings: number; }; /** * Calculate the user's weekly protocol fee share (in stable asset units) given tranche * balances/configs, system variables and locking summary. All inputs are expected to be pre-fetched. */ calculateWeeklyProtocolFees(params: { trancheBalances: TrancheSubgraphResult; trancheConfigurations: TrancheConfigurationSubgraphResult; systemVariables: SystemVariables; lockingSummary: LockingSummarySubgraphResult; userRksuAmount: string; }): number; /** * Calculate the user's weekly KASU bonus rewards (KASU) based on current deposits, * loyalty level, reward rate, and KSU epoch price. Inputs should be pre-fetched. */ calculateWeeklyKsuBonusRewards(params: { userAddress: string; userRksuAmount: string; userDepositAmounts: [BigNumber, BigNumber]; ksuEpochPrice: { price: BigNumber; decimals: number; }; }): Promise; getPortfolioRewards(userAddress: string): Promise; getUserNfts(userAddress: string): Promise; getPortfolioSummary(userAddress: string, portfolioLendingPools: PortfolioLendingPool[], currentEpoch: string): Promise; getPortfolioLendingData(userAddress: string, poolOverviews: PoolOverview[], currentEpoch: string, provider?: Provider): Promise; /** * The lender's settled yield per cycle (epoch), per pool and tranche — * what the chain credited at each close, NET of the performance fee * (`domain/yield-history.ts`), in the deployment's own stable token. Each * entry runs the same `epochTrancheYield` as `getPortfolioLendingData`'s * last-epoch figures: a tranche's `yieldEarnings.lastEpoch` plus its * locks' equals its entry for the epoch of the tranche's most recently * APPLIED interest (older than the last cycle for a pool that has not * cleared since; no entry, and a figure of 0, when the lender held * nothing at that epoch). * * Sum entries with `aggregateYieldHistory` (all strategies, or one with * `{ poolId }`). Only epochs in which the lender held shares appear — a * new lender gets `[]`. Pools in `UNUSED_LENDING_POOL_IDS` are left out, * as in `lendingPortfolioQuery`; Directus enablement is NOT applied. * * Cost: three parallel `SystemVariables` reads and the positions query in * one round, then one subgraph request per page of the longest series * (1000 rows a page) — normally two subgraph round trips in all, never one * per epoch — alongside one `LendingPool.userBalance` call per pool at * the subgraph's block. A pool whose ledger credits more than the wallet * holds at that block (shares moved away outside a withdrawal) is left * out; if the node says it cannot serve that block, `latest` is read * and nothing is. Any other failure of those reads (a rate limit, a * network error) THROWS: the history needs a working RPC. * * On a LITE deployment, a subgraph that lacks the entities (a validation * error naming them) yields `[]`; on a full deployment that error throws, * like every other failure (`isEmptyHistoryError`). * * @throws RangeError on a malformed `fromEpoch` / `count`, before any * network read. */ getYieldHistory(userAddress: string, options?: YieldHistoryOptions): Promise; /** * Read the chain at the subgraph's block, so the chain and the ledger * describe the same state: after every clearing the subgraph runs a few * blocks behind, and a `latest` read would then disagree with an * honest ledger (interest moved every balance, deposits were accepted) * and book that difference as a ledger fault. * * Only when the node says it cannot serve that block (pruned, or behind * the subgraph: `isBlockUnavailableError`) does it fall back to `latest`, * and it says so (`blockTag: undefined`): unpinned figures are never * compared with the ledger, and `getPortfolioLendingData` marks the pools * `chainPinned: false`. Any other failure — a rate limit, a timeout, a * network error — is thrown, never turned into an unpinned read. */ private readChainAt; /** * One pool's balances: the ledger's (the subgraph counts a lender's * shares as accepted deposits − accepted withdrawals ± top-ups) checked * against the chain's `LendingPool.userBalance` at the SAME block. * * The ledger does not follow shares that move between wallets any other * way (an administrative move of a position is the case seen on chain): * the receiving wallet goes negative as it withdraws, the sending one * keeps shares it no longer holds. Where the two disagree at that block — * or any ledger balance is negative — the tranche balances are read from * the chain at that block (`userActiveAssets`, one call per tranche), the * tranche's ledger `yieldEarned` moves by the same difference, and the * pool carries `ledgerMismatch`. Unpinned (the node could not serve the * block), only a negative ledger balance is corrected. A pool that agrees * — every ordinary lender — costs nothing extra. * * Lifetime figures stay ledger figures: they cannot tell moved principal * from interest. */ private reconcilePool; /** * The last-epoch figures of positions whose newest interest is OLDER than * the recent window. Pools clear independently, and some clear only when * the originator repays, so a pool's last cycle can be weeks back: it is * priced at THAT epoch, from a history read of just those positions over * just those epochs, through the same `positionEpochYield` — so it equals * the history's newest bar. * * A lender who has since left the tranche (no ledger shares now) gets no * figure: an old pool's last cycle is not theirs to show. Costs nothing * when no position qualifies. */ private staleLastEpochYields; /** * A money figure as a plain decimal string. Below a millionth of a token * (the stable asset's own precision) is float noise, never money, so it * is `'0'` — not `'1e-7'`, which `parseUnits` rejects, nor `'-3e-12'`. A * small negative remainder (above −0.0001) is `'0'` as well. */ precisionToString(val: number): string; calculateFixedTermDepositEarnings(fixedTermDepositLock: UserLendingPoolTrancheFixedTermDepositLock, totalTrancheBalance: string, totalTrancheShares: string): number; }