import { ILogger } from "../onchain/types/logger.js"; import { ChainId } from "../model/primitives.js"; import { Notice, NoticeSubject } from "../model/notices.js"; import { DataResponse } from "../model/response.js"; import "../model/index.js"; import { NetworkType } from "../onchain/chain/chains.js"; import { MultichainAttachOptions, MultichainSDK, MultichainSDKOptions } from "../onchain/MultichainSDK.js"; import "../onchain/index.js"; import { GearboxAPIOptions } from "../offchain/types.js"; import { GearboxAPI } from "../offchain/GearboxAPI.js"; import "../offchain/index.js"; import { IAnalyticsByMode } from "./analytics/types.js"; import { ILiquidationsByMode } from "./liquidations/types.js"; import { IOpportunities } from "./opportunities/types.js"; import { IPositions } from "./positions/types.js"; import { IPreviewByMode } from "./preview/types.js"; import { IRewardsByMode, RewardsKeys } from "./rewards/types.js"; //#region src/sdk/types.d.ts /** * Which sources a {@link GearboxSDK} reads from, and therefore which of its * methods exist. **/ export type Mode = "onchain" | "offchain" | "both"; /** * On-chain source of a {@link GearboxSDK}: either an SDK to reuse, which its * owner keeps attaching itself, or the options to build one with. **/ export type OnchainSource = MultichainSDK | PlainMultichainSDKOptions; /** * {@link MultichainSDKOptions} without plugin typing. **/ export type PlainMultichainSDKOptions = MultichainSDKOptions<{}>; /** * Off-chain source of a {@link GearboxSDK}: either a client to reuse or the * options to build one with. The chains come from * {@link GearboxSDKOptions.networks} either way. **/ export type OffchainSource = GearboxAPI | Omit; /** * Options for creating a {@link GearboxSDK}. * * @typeParam M - Mode, which decides which sources are required. **/ export interface GearboxSDKOptions { /** * Sources to read from, see {@link Mode}. **/ mode: M; /** * Chains the SDK covers, which every read of every source is scoped to. An * injected source covering a different set is rejected at construction. **/ networks: NetworkType[]; /** * On-chain source. Required in `onchain` and `both` mode. **/ onchain?: OnchainSource; /** * Off-chain source. Required in `offchain` and `both` mode. **/ offchain?: OffchainSource; /** * How many seconds the backend may lag the chain and still serve a chain in * `both` mode, see {@link DEFAULT_MAX_OFFCHAIN_LAG}. **/ maxOffchainLagSeconds?: number; /** * How old the loaded on-chain state may be, in seconds, before a read syncs * the chains it touches first, see {@link DEFAULT_MAX_STATE_AGE}. A read * inside the window is served from loaded state. **/ maxStateAgeSeconds?: number; /** * Options passed to {@link MultichainSDK.attach}, used only when the SDK * builds the on-chain source itself. **/ attach?: MultichainAttachOptions; /** * API keys for {@link IGearboxSDK.rewards}. **/ rewards?: RewardsKeys; logger?: ILogger; } /** * What a namespace asks of the on-chain state before it reads it: attached, * and no older than the SDK's `maxStateAgeSeconds` on the chains the read * touches. Owned by {@link GearboxSDK}, which shares one attach and one * in-flight sync per chain across every namespace. * * @internal **/ export type EnsureFreshChains = (chainIds?: readonly ChainId[]) => Promise; /** * What a {@link GearboxSDK} hands every namespace it builds. **/ export interface NamespaceOptions { /** * How far the backend may lag the chain, see * {@link GearboxSDKOptions.maxOffchainLagSeconds}. **/ maxOffchainLagSeconds: number; /** * Awaited before every on-chain leg of an async read, see * {@link EnsureFreshChains}. Absent when the SDK reads no chain. **/ ensureFresh?: EnsureFreshChains; logger?: ILogger; } /** * On-chain escape hatch per mode: absent when the SDK reads no chain. * * @internal **/ export interface OnchainByMode { onchain: MultichainSDK; offchain: undefined; both: MultichainSDK; } /** * The banners the backend attaches to a pool opportunity or a strategy * position, see {@link Notice}. **/ export type INotices = (subject: NoticeSubject) => Promise>; /** * `sdk.notices` per mode: a backend read, absent when the SDK reads no backend. **/ export interface INoticesByMode { onchain: undefined; offchain: INotices; both: INotices; } /** * Public contract of {@link GearboxSDK}: every namespace and sub-construction, * gated by {@link Mode}. Escape hatches `onchain` and `offchain` are not part * of this surface. * * @typeParam M - Mode the instance was built in. **/ export interface IGearboxSDK { /** * Sources this instance reads from. **/ readonly mode: M; /** * Chains this instance covers, which every read is scoped to. **/ readonly networks: readonly NetworkType[]; /** * Whether the on-chain source is ready to be read from. Always `true` in * `offchain` mode. **/ readonly attached: boolean; /** * Attaches the on-chain SDK when this instance owns one; a no-op in * `offchain` mode and when an already-attached SDK was injected. **/ attach(): Promise; /** Protocol-wide analytics. Absent in `onchain` mode. */ readonly analytics: IAnalyticsByMode[M]; /** * Namespace for pool and strategy opportunities. **/ readonly opportunities: IOpportunities; /** * Namespace for the positions a wallet holds. **/ readonly positions: IPositions; /** * Namespace for liquidatable credit accounts and delayed-withdrawal * positions a liquidator holds. Absent in `offchain` mode. **/ readonly liquidations: ILiquidationsByMode[M]; /** * Namespace for on-chain previews of raw operation calldata. Absent in * `offchain` mode. **/ readonly preview: IPreviewByMode[M]; /** * Namespace for claimable rewards. Absent in `offchain` mode. **/ readonly rewards: IRewardsByMode[M]; /** * The banners the backend attaches to a pool opportunity or a strategy * position, see {@link Notice}. Absent in `onchain` mode. **/ readonly notices: INoticesByMode[M]; } /** * Off-chain escape hatch per mode: absent when the SDK reads no backend. * * @internal **/ export interface OffchainByMode { onchain: undefined; offchain: GearboxAPI; both: GearboxAPI; } //#endregion