@gearbox-protocol/sdk 13.5.0 → 13.6.0-apy-plugin.1

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 (34) hide show
  1. package/dist/cjs/plugins/pools-history/ApyPlugin.js +328 -0
  2. package/dist/cjs/plugins/pools-history/apy-cache.js +120 -0
  3. package/dist/cjs/plugins/pools-history/apy-parser.js +169 -0
  4. package/dist/cjs/plugins/pools-history/constants.js +31 -0
  5. package/dist/cjs/plugins/pools-history/index.js +12 -2
  6. package/dist/cjs/plugins/pools-history/pool-apy-types.js +16 -0
  7. package/dist/cjs/plugins/pools-history/pool-apy-utils.js +141 -0
  8. package/dist/cjs/rewards/rewards/extra-apy.js +10 -8
  9. package/dist/cjs/sdk/GearboxSDK.js +11 -0
  10. package/dist/esm/plugins/pools-history/ApyPlugin.js +317 -0
  11. package/dist/esm/plugins/pools-history/apy-cache.js +86 -0
  12. package/dist/esm/plugins/pools-history/apy-parser.js +143 -0
  13. package/dist/esm/plugins/pools-history/constants.js +6 -0
  14. package/dist/esm/plugins/pools-history/index.js +6 -1
  15. package/dist/esm/plugins/pools-history/pool-apy-types.js +0 -0
  16. package/dist/esm/plugins/pools-history/pool-apy-utils.js +113 -0
  17. package/dist/esm/rewards/rewards/extra-apy.js +10 -8
  18. package/dist/esm/sdk/GearboxSDK.js +11 -0
  19. package/dist/types/plugins/pools-history/ApyPlugin.d.ts +70 -0
  20. package/dist/types/plugins/pools-history/apy-cache.d.ts +28 -0
  21. package/dist/types/plugins/pools-history/apy-parser.d.ts +5 -0
  22. package/dist/types/plugins/pools-history/constants.d.ts +2 -0
  23. package/dist/types/plugins/pools-history/index.d.ts +6 -1
  24. package/dist/types/plugins/pools-history/pool-apy-types.d.ts +39 -0
  25. package/dist/types/plugins/pools-history/pool-apy-utils.d.ts +71 -0
  26. package/dist/types/plugins/pools-history/types.d.ts +28 -0
  27. package/dist/types/rewards/rewards/api.d.ts +10 -1
  28. package/dist/types/rewards/rewards/common.d.ts +0 -10
  29. package/dist/types/rewards/rewards/extra-apy.d.ts +4 -6
  30. package/dist/types/sdk/GearboxSDK.d.ts +19 -0
  31. package/package.json +1 -1
  32. package/dist/cjs/plugins/pools-history/Pools7DAgoPlugin.js +0 -108
  33. package/dist/esm/plugins/pools-history/Pools7DAgoPlugin.js +0 -90
  34. package/dist/types/plugins/pools-history/Pools7DAgoPlugin.d.ts +0 -20
@@ -0,0 +1,143 @@
1
+ import { PERCENTAGE_FACTOR } from "../../sdk/index.js";
2
+ function numberToAPY(baseApy) {
3
+ return Math.round(baseApy * Number(PERCENTAGE_FACTOR));
4
+ }
5
+ function parseGearStats(output) {
6
+ const d = output.gearApy?.status === "ok" ? output.gearApy.data : void 0;
7
+ if (!d) return null;
8
+ return {
9
+ base: numberToAPY(d.base ?? 0),
10
+ crv: numberToAPY(d.crv ?? 0),
11
+ gear: numberToAPY(d.gear ?? 0),
12
+ gearPrice: d.gearPrice ?? 0
13
+ };
14
+ }
15
+ function parseNetworkApy(apyResp, poolResp) {
16
+ const baseAPYList = {};
17
+ const extraCollateralAPYList = {};
18
+ const basePointsList = {};
19
+ const extraCollateralPointsList = {};
20
+ const tokenExtraRewardsList = {};
21
+ const apyData = apyResp?.status === "ok" ? apyResp.data : void 0;
22
+ for (const d of apyData ?? []) {
23
+ const tokenAddress = d.address.toLowerCase();
24
+ const tokenSymbol = d.symbol;
25
+ const apy = d.rewards.apy?.[0];
26
+ if (apy) {
27
+ baseAPYList[tokenAddress] = numberToAPY(apy.value ?? 0);
28
+ }
29
+ const points = d.rewards.points?.[0];
30
+ if (points) {
31
+ basePointsList[tokenAddress] = {
32
+ address: tokenAddress,
33
+ symbol: tokenSymbol,
34
+ rewards: points.rewards.map((p) => ({
35
+ ...p,
36
+ multiplier: p.multiplier === "soon" ? p.multiplier : BigInt(p.multiplier || 0)
37
+ })),
38
+ debtRewards: points.debtRewards?.map((p) => ({
39
+ ...p,
40
+ cm: (p.cm || "").toLowerCase(),
41
+ multiplier: p.multiplier === "soon" ? p.multiplier : BigInt(p.multiplier || 0)
42
+ }))
43
+ };
44
+ }
45
+ const extraRewards = d.rewards.extraRewards;
46
+ if (extraRewards && extraRewards.length > 0) {
47
+ tokenExtraRewardsList[tokenAddress] = extraRewards.map((r) => ({
48
+ address: tokenAddress,
49
+ symbol: r.rewardSymbol,
50
+ rewardToken: r.rewardToken.toLowerCase(),
51
+ rewardSymbol: r.rewardSymbol,
52
+ token: r.rewardToken.toLowerCase(),
53
+ finished: BigInt(r.finished || 0),
54
+ duration: BigInt(r.duration || 0),
55
+ reward: BigInt(r.reward || 0),
56
+ balance: BigInt(r.balance || 0)
57
+ }));
58
+ }
59
+ const ecApy = d.rewards.extraCollateralAPY;
60
+ if (ecApy && ecApy.length > 0) {
61
+ for (const ea of ecApy) {
62
+ const pool = ea.pool.toLowerCase();
63
+ if (!extraCollateralAPYList[pool]) extraCollateralAPYList[pool] = {};
64
+ extraCollateralAPYList[pool][tokenAddress] = {
65
+ ...ea,
66
+ address: tokenAddress,
67
+ symbol: tokenSymbol,
68
+ pool,
69
+ value: numberToAPY(ea.value ?? 0)
70
+ };
71
+ }
72
+ }
73
+ const ecPoints = d.rewards.extraCollateralPoints;
74
+ if (ecPoints && ecPoints.length > 0) {
75
+ for (const ea of ecPoints) {
76
+ const pool = ea.pool.toLowerCase();
77
+ if (!extraCollateralPointsList[pool])
78
+ extraCollateralPointsList[pool] = {};
79
+ extraCollateralPointsList[pool][tokenAddress] = {
80
+ address: tokenAddress,
81
+ symbol: tokenSymbol,
82
+ rewards: ea.rewards.map((p) => ({
83
+ ...p,
84
+ multiplier: p.multiplier === "soon" ? p.multiplier : BigInt(p.multiplier || 0)
85
+ })),
86
+ debtRewards: ea.debtRewards?.map((p) => ({
87
+ ...p,
88
+ cm: (p.cm || "").toLowerCase(),
89
+ multiplier: p.multiplier === "soon" ? p.multiplier : BigInt(p.multiplier || 0)
90
+ }))
91
+ };
92
+ }
93
+ }
94
+ }
95
+ const poolRewardsList = {};
96
+ const poolExternalAPYList = {};
97
+ const poolExtraAPYList = {};
98
+ const poolData = poolResp?.status === "ok" ? poolResp.data : void 0;
99
+ for (const r of poolData ?? []) {
100
+ const pool = (r.pool || "").toLowerCase();
101
+ const points = r.rewards.points;
102
+ if (points && points.length > 0) {
103
+ poolRewardsList[pool] = points.map((p) => ({
104
+ ...p,
105
+ token: p.token.toLowerCase(),
106
+ pool,
107
+ amount: BigInt(p.amount || 0)
108
+ }));
109
+ }
110
+ const externalAPY = r.rewards.externalAPY;
111
+ if (externalAPY && externalAPY.length > 0) {
112
+ poolExternalAPYList[pool] = externalAPY.map((ex) => ({ ...ex, pool }));
113
+ }
114
+ const extraAPY = r.rewards.extraAPY;
115
+ if (extraAPY && extraAPY.length > 0) {
116
+ poolExtraAPYList[extraAPY[0].token.toLowerCase()] = extraAPY.map((ex) => ({
117
+ ...ex,
118
+ token: ex.token.toLowerCase(),
119
+ rewardToken: ex.rewardToken.toLowerCase()
120
+ }));
121
+ }
122
+ }
123
+ const allResponses = [apyResp, poolResp];
124
+ const allErrors = allResponses.filter((r) => r?.status === "error");
125
+ if (allResponses.length > 0 && allErrors.length === allResponses.length) {
126
+ return void 0;
127
+ }
128
+ return {
129
+ apyList: Object.keys(baseAPYList).length > 0 ? baseAPYList : void 0,
130
+ extraCollateralAPYList: Object.keys(extraCollateralAPYList).length > 0 ? extraCollateralAPYList : void 0,
131
+ pointsList: Object.keys(basePointsList).length > 0 ? basePointsList : void 0,
132
+ extraCollateralPointsList: Object.keys(extraCollateralPointsList).length > 0 ? extraCollateralPointsList : void 0,
133
+ poolRewardsList: Object.keys(poolRewardsList).length > 0 ? poolRewardsList : void 0,
134
+ tokenExtraRewardsList: Object.keys(tokenExtraRewardsList).length > 0 ? tokenExtraRewardsList : void 0,
135
+ poolExternalAPYList: Object.keys(poolExternalAPYList).length > 0 ? poolExternalAPYList : void 0,
136
+ poolExtraAPYList: Object.keys(poolExtraAPYList).length > 0 ? poolExtraAPYList : void 0
137
+ };
138
+ }
139
+ export {
140
+ numberToAPY,
141
+ parseGearStats,
142
+ parseNetworkApy
143
+ };
@@ -0,0 +1,6 @@
1
+ const APY_STATE_CACHE_URL = "https://state-cache.gearbox.foundation/apy-server/latest.json";
2
+ const DEFAULT_APY_INTERVAL_MS = 10 * 60 * 1e3;
3
+ export {
4
+ APY_STATE_CACHE_URL,
5
+ DEFAULT_APY_INTERVAL_MS
6
+ };
@@ -1,2 +1,7 @@
1
- export * from "./Pools7DAgoPlugin.js";
1
+ export * from "./ApyPlugin.js";
2
+ export * from "./apy-cache.js";
3
+ export * from "./apy-parser.js";
4
+ export * from "./constants.js";
5
+ export * from "./pool-apy-types.js";
6
+ export * from "./pool-apy-utils.js";
2
7
  export * from "./types.js";
@@ -0,0 +1,113 @@
1
+ import { PERCENTAGE_FACTOR, RAY } from "../../sdk/constants/index.js";
2
+ import { formatBN, rayToNumber } from "../../sdk/utils/formatter.js";
3
+ const SCALE = 1000000n;
4
+ const WEEKS_PER_YEAR = 54n;
5
+ function getPoolExtraAPY(lookupAddresses, poolExtraAPYList) {
6
+ if (!poolExtraAPYList) return [];
7
+ const result = [];
8
+ for (const addr of lookupAddresses) {
9
+ const extra = poolExtraAPYList[addr.toLowerCase()];
10
+ if (extra) {
11
+ result.push(...extra);
12
+ }
13
+ }
14
+ return result;
15
+ }
16
+ function calculateSupplyApy7d(currentDieselRate, currentSupplyRate, dieselRate7DAgo) {
17
+ if (dieselRate7DAgo > currentDieselRate) {
18
+ return rayToNumber(currentSupplyRate * SCALE) / Number(PERCENTAGE_FACTOR);
19
+ }
20
+ const apy = (currentDieselRate * RAY / dieselRate7DAgo - RAY) * WEEKS_PER_YEAR;
21
+ return rayToNumber(apy * SCALE) / Number(PERCENTAGE_FACTOR);
22
+ }
23
+ function calculatePoolFullAPY({
24
+ depositAPY,
25
+ underlyingAPY,
26
+ extraAPY,
27
+ currentExternalList
28
+ }) {
29
+ const baseAPY = [
30
+ { type: "supplyAPY", apy: depositAPY },
31
+ ...underlyingAPY > 0 ? [
32
+ {
33
+ type: "tokenYield",
34
+ apy: Number(underlyingAPY) / Number(PERCENTAGE_FACTOR)
35
+ }
36
+ ] : []
37
+ ];
38
+ const filteredExtra = [...extraAPY ?? []].filter((r) => r.apy > 0);
39
+ const baseAPYTotal = baseAPY.reduce((s, r) => s + (r.apy || 0), 0);
40
+ const extraAPYTotal = filteredExtra.reduce((s, r) => s + (r.apy || 0), 0);
41
+ const total = baseAPYTotal + extraAPYTotal;
42
+ const externalAPY = resolveExternalAPY(currentExternalList, total);
43
+ return {
44
+ totalAPY: total,
45
+ baseAPY,
46
+ extraAPY: filteredExtra,
47
+ extraAPYTotal,
48
+ externalAPY
49
+ };
50
+ }
51
+ function resolveExternalAPY(list, baseTotal) {
52
+ const first = list?.[0];
53
+ if (!first) return void 0;
54
+ return {
55
+ ...first,
56
+ totalValue: baseTotal + first.value
57
+ };
58
+ }
59
+ function calculatePoolFullAPY7DAgo({
60
+ supplyAPY7DAgo,
61
+ depositAPY,
62
+ poolAPY
63
+ }) {
64
+ const {
65
+ baseAPY = [],
66
+ extraAPYTotal = 0,
67
+ extraAPY = [],
68
+ externalAPY
69
+ } = poolAPY ?? {};
70
+ const base = [
71
+ { apy: supplyAPY7DAgo || depositAPY, type: "supplyAPY" },
72
+ ...baseAPY.filter((r) => r.type !== "supplyAPY")
73
+ ];
74
+ const baseTotal = base.reduce((acc, r) => acc + r.apy, 0);
75
+ const total = baseTotal + extraAPYTotal;
76
+ return {
77
+ totalAPY: total,
78
+ extraAPYTotal,
79
+ baseAPY: base,
80
+ extraAPY,
81
+ externalAPY,
82
+ loading7DAgo: supplyAPY7DAgo === void 0
83
+ };
84
+ }
85
+ function calculatePoolPoints({
86
+ poolTokenSymbol,
87
+ points,
88
+ tokensList
89
+ }) {
90
+ return points?.map(({ info, points: pts }) => {
91
+ const { decimals = 18 } = tokensList.get(info.token) || {};
92
+ const amount = formatBN(pts, decimals);
93
+ const { name = "Points", duration } = info ?? {};
94
+ return {
95
+ reward: info,
96
+ name,
97
+ amount,
98
+ tokenTitle: poolTokenSymbol,
99
+ fullTip: [
100
+ `${amount} ${name}`,
101
+ ...duration ? [duration] : [],
102
+ ...poolTokenSymbol ? [poolTokenSymbol] : []
103
+ ].join(" per ")
104
+ };
105
+ });
106
+ }
107
+ export {
108
+ calculatePoolFullAPY,
109
+ calculatePoolFullAPY7DAgo,
110
+ calculatePoolPoints,
111
+ calculateSupplyApy7d,
112
+ getPoolExtraAPY
113
+ };
@@ -52,11 +52,13 @@ class PoolPointsAPI {
52
52
  tokensList
53
53
  }) {
54
54
  const r = pools.reduce((acc, p) => {
55
- const pointsInfo = poolRewards[p.address] || [];
55
+ const poolAddress = p.pool.pool.address.toLowerCase();
56
+ const pointsInfo = poolRewards[poolAddress] || [];
56
57
  const poolPointsList = pointsInfo.reduce(
57
58
  (acc2, pointsInfo2) => {
58
- const { address: tokenAddress } = tokensList[pointsInfo2.token] || {};
59
- const tokenBalance = totalTokenBalances[tokenAddress || ""];
59
+ const { addr: tokenAddress } = tokensList.get(pointsInfo2.token) || {};
60
+ const tokenAddressLower = (tokenAddress || "").toLowerCase();
61
+ const tokenBalance = totalTokenBalances[tokenAddressLower];
60
62
  const points = PoolPointsAPI.getPoolTokenPoints(
61
63
  tokenBalance,
62
64
  p,
@@ -74,22 +76,22 @@ class PoolPointsAPI {
74
76
  },
75
77
  []
76
78
  );
77
- acc[p.address] = poolPointsList;
79
+ acc[poolAddress] = poolPointsList;
78
80
  return acc;
79
81
  }, {});
80
82
  return r;
81
83
  }
82
84
  static getPoolTokenPoints(tokenBalanceInPool, pool, tokensList, pointsInfo) {
83
- if (pool.expectedLiquidity <= 0) return 0n;
85
+ if (pool.pool.pool.expectedLiquidity <= 0) return 0n;
84
86
  if (pointsInfo.estimation === "relative" && !tokenBalanceInPool)
85
87
  return null;
86
- const { decimals = 18 } = tokensList[pointsInfo.token] || {};
88
+ const { decimals = 18 } = tokensList.get(pointsInfo.token) || {};
87
89
  const targetFactor = 10n ** BigInt(decimals);
88
90
  const defaultPoints = pointsInfo.amount * targetFactor / PERCENTAGE_FACTOR;
89
91
  if (pointsInfo.estimation === "absolute") return defaultPoints;
90
- const { decimals: underlyingDecimals = 18 } = tokensList[pool.underlyingToken] || {};
92
+ const { decimals: underlyingDecimals = 18 } = tokensList.get(pool.pool.pool.underlying) || {};
91
93
  const underlyingFactor = 10n ** BigInt(underlyingDecimals);
92
- const points = (tokenBalanceInPool?.balance || 0n) * defaultPoints / (pool.expectedLiquidity * targetFactor / underlyingFactor);
94
+ const points = (tokenBalanceInPool?.balance || 0n) * defaultPoints / (pool.pool.pool.expectedLiquidity * targetFactor / underlyingFactor);
93
95
  return BigIntMath.min(points, defaultPoints);
94
96
  }
95
97
  }
@@ -101,6 +101,17 @@ class GearboxSDK extends ChainContractsRegister {
101
101
  * @see {@link SDKHooks} for available event names.
102
102
  **/
103
103
  removeHook = this.#hooks.removeHook.bind(this.#hooks);
104
+ /**
105
+ * Triggers the `pluginUpdate` hook.
106
+ *
107
+ * Intended to be called by plugins when they update their internal state
108
+ * outside of the normal `syncState`/`rehydrate` cycle (e.g. via an
109
+ * internal timer). Frontend listeners registered with
110
+ * `sdk.addHook("pluginUpdate", …)` will be notified.
111
+ **/
112
+ async triggerPluginUpdate(plugin) {
113
+ await this.#hooks.triggerHooks("pluginUpdate", { plugin });
114
+ }
104
115
  /**
105
116
  * Creates and initialises a new SDK instance by reading live on-chain state.
106
117
  *
@@ -0,0 +1,70 @@
1
+ import type { Address } from "viem";
2
+ import type { IGearboxSDKPlugin } from "../../sdk/index.js";
3
+ import { AddressMap, BasePlugin } from "../../sdk/index.js";
4
+ import type { GetPoolsAPYResult } from "./pool-apy-types.js";
5
+ import type { ApySnapshotState, Pool7DAgoState, Pools7DAgoStateHuman } from "./types.js";
6
+ export interface ApyPluginState {
7
+ pools7DAgo: Record<Address, Pool7DAgoState>;
8
+ apySnapshot: ApySnapshotState;
9
+ }
10
+ export interface ApyPluginConstructorOptions {
11
+ apyUrl?: string;
12
+ /** TTL for the shared HTTP cache in milliseconds (default: same as timer interval) */
13
+ cacheTtlMs?: number;
14
+ timer?: PluginTimerOptions;
15
+ }
16
+ export interface ApyPluginLoadOptions {
17
+ /**
18
+ * When `false` and the plugin is already loaded, skips the multicall that loads
19
+ * pool diesel rates at the ~7d-ago block; only the APY snapshot is refreshed.
20
+ * Ignored on the first load (both datasets are always fetched initially).
21
+ */
22
+ loadPools7DAgo?: boolean;
23
+ }
24
+ export interface PluginTimerOptions {
25
+ /** Polling interval in milliseconds (default: 10 minutes) */
26
+ intervalMs?: number;
27
+ /** Callback fired after each successful refresh */
28
+ onChange?: () => void;
29
+ /**
30
+ * When `true`, each tick also refreshes 7d-ago pool state on-chain.
31
+ */
32
+ refreshPools7DAgoOnTick?: boolean;
33
+ }
34
+ export declare class ApyPlugin extends BasePlugin<ApyPluginState> implements IGearboxSDKPlugin<ApyPluginState> {
35
+ #private;
36
+ /**
37
+ * When `true`, the timer is started eagerly during the `attach` phase
38
+ * rather than waiting for an explicit `load` call.
39
+ **/
40
+ readonly startTimerOnAttach: boolean;
41
+ constructor(loadOnAttach?: boolean, startTimerOnAttach?: boolean, options?: ApyPluginConstructorOptions);
42
+ attach(): Promise<void>;
43
+ load(force?: boolean, loadOptions?: ApyPluginLoadOptions): Promise<ApyPluginState>;
44
+ get loaded(): boolean;
45
+ /**
46
+ * @throws if plugin is not loaded
47
+ */
48
+ get pools7DAgo(): AddressMap<Pool7DAgoState>;
49
+ /**
50
+ * @throws if plugin is not loaded
51
+ */
52
+ get apySnapshot(): ApySnapshotState;
53
+ /**
54
+ * Computes per-pool APY (current + 7d-ago) and points for all markets.
55
+ *
56
+ * @throws if plugin is not loaded
57
+ */
58
+ getPoolsAPY(): GetPoolsAPYResult;
59
+ /**
60
+ * Starts a periodic timer that performs a full plugin state refresh.
61
+ * Only one timer can be active; calling again is a no-op.
62
+ * @returns Cleanup function that stops the timer.
63
+ */
64
+ startTimer(opts?: PluginTimerOptions): () => void;
65
+ stopTimer(): void;
66
+ syncState(): Promise<void>;
67
+ stateHuman(_?: boolean): Pools7DAgoStateHuman[];
68
+ get state(): ApyPluginState;
69
+ hydrate(state: ApyPluginState): void;
70
+ }
@@ -0,0 +1,28 @@
1
+ import type { Output } from "../../rewards/apy/index.js";
2
+ import type { ILogger } from "../../sdk/index.js";
3
+ /**
4
+ * Process-wide cache for the APY state-cache JSON.
5
+ *
6
+ * Multiple ApyPlugin instances (one per SDK / network) share the same
7
+ * cached HTTP response so that only **one** request is made per TTL window
8
+ * regardless of how many SDK instances exist.
9
+ *
10
+ * Concurrent callers that arrive while a fetch is already in flight
11
+ * are de-duplicated — they all await the same promise.
12
+ */
13
+ export declare class ApyOutputCache {
14
+ #private;
15
+ private constructor();
16
+ /**
17
+ * Returns a shared cache instance for the given URL.
18
+ * The same instance is reused across all callers with identical URL.
19
+ */
20
+ static get(url: string, ttlMs: number, logger?: ILogger): ApyOutputCache;
21
+ /**
22
+ * Returns cached Output if fresh, otherwise fetches from the network.
23
+ * Concurrent calls are de-duplicated.
24
+ */
25
+ fetch(): Promise<Output<string, string> | undefined>;
26
+ /** Evicts all cached entries. Mainly useful for tests. */
27
+ static clearAll(): void;
28
+ }
@@ -0,0 +1,5 @@
1
+ import type { DataResult, Output, PoolOutputDetails, TokenOutputDetails } from "../../rewards/apy/index.js";
2
+ import type { GearStats, NetworkApyData } from "./types.js";
3
+ export declare function numberToAPY(baseApy: number): number;
4
+ export declare function parseGearStats(output: Output<string, string>): GearStats | null;
5
+ export declare function parseNetworkApy(apyResp: DataResult<TokenOutputDetails<string>[]> | undefined, poolResp: DataResult<PoolOutputDetails<string>[]> | undefined): NetworkApyData | undefined;
@@ -0,0 +1,2 @@
1
+ export declare const APY_STATE_CACHE_URL = "https://state-cache.gearbox.foundation/apy-server/latest.json";
2
+ export declare const DEFAULT_APY_INTERVAL_MS: number;
@@ -1,2 +1,7 @@
1
- export * from "./Pools7DAgoPlugin.js";
1
+ export * from "./ApyPlugin.js";
2
+ export * from "./apy-cache.js";
3
+ export * from "./apy-parser.js";
4
+ export * from "./constants.js";
5
+ export * from "./pool-apy-types.js";
6
+ export * from "./pool-apy-utils.js";
2
7
  export * from "./types.js";
@@ -0,0 +1,39 @@
1
+ import type { Address } from "viem";
2
+ import type { ExternalApy as ExternalApySDK, PoolExtraApy, PoolPointsInfo } from "../../rewards/apy/index.js";
3
+ export interface PoolBaseAPY {
4
+ type: "supplyAPY" | "tokenYield";
5
+ apy: number;
6
+ }
7
+ /**
8
+ * External APY enriched with the cumulative `totalValue`
9
+ * (base + extra + external).
10
+ */
11
+ export interface PoolExternalAPY extends ExternalApySDK {
12
+ totalValue: number;
13
+ }
14
+ export interface PoolFullAPY {
15
+ totalAPY: number;
16
+ baseAPY: PoolBaseAPY[];
17
+ extraAPY: PoolExtraApy[];
18
+ extraAPYTotal: number;
19
+ externalAPY: PoolExternalAPY | undefined;
20
+ }
21
+ export interface PoolFullAPY7DAgo extends PoolFullAPY {
22
+ loading7DAgo: boolean;
23
+ }
24
+ export interface PoolPointsWithTips {
25
+ reward?: PoolPointsInfo<string>;
26
+ amount: string;
27
+ name: string;
28
+ tokenTitle?: string;
29
+ fullTip: string;
30
+ }
31
+ type AllPoolsAPY = Record<Address, PoolFullAPY>;
32
+ type AllPoolsAPY7DAgo = Record<Address, PoolFullAPY7DAgo>;
33
+ type AllPoolsPoints = Record<Address, PoolPointsWithTips[] | undefined>;
34
+ export interface GetPoolsAPYResult {
35
+ data: AllPoolsAPY;
36
+ data7DAgo: AllPoolsAPY7DAgo;
37
+ points: AllPoolsPoints;
38
+ }
39
+ export {};
@@ -0,0 +1,71 @@
1
+ import type { Address } from "viem";
2
+ import type { ExternalApy, PoolExtraApy } from "../../rewards/apy/index.js";
3
+ import type { PoolPointsBase } from "../../rewards/rewards/extra-apy.js";
4
+ import type { TokensMeta } from "../../sdk/index.js";
5
+ import type { PoolFullAPY, PoolFullAPY7DAgo, PoolPointsWithTips } from "./pool-apy-types.js";
6
+ /**
7
+ * Collects extra APY entries for the given token addresses from the
8
+ * `poolExtraAPYList` map.
9
+ *
10
+ * In the client, `lookupAddresses` = [pool.address, ...pool.stakedDieselToken].
11
+ * The caller is responsible for providing the correct set of addresses.
12
+ */
13
+ export declare function getPoolExtraAPY(lookupAddresses: Address[], poolExtraAPYList: Record<Address, PoolExtraApy[]> | undefined): PoolExtraApy[];
14
+ /**
15
+ * Computes annualized supply APY based on diesel rate change over ~7 days.
16
+ *
17
+ * Returns a value in "percentage" units (e.g. 5 ≈ 5 %).
18
+ * Falls back to current on-chain `supplyRate` if 7d-ago diesel rate is
19
+ * missing or higher than the current rate.
20
+ */
21
+ export declare function calculateSupplyApy7d(currentDieselRate: bigint, currentSupplyRate: bigint, dieselRate7DAgo: bigint): number;
22
+ interface CalculatePoolFullAPYProps {
23
+ depositAPY: number;
24
+ underlyingAPY: number;
25
+ extraAPY: PoolExtraApy[] | undefined;
26
+ currentExternalList: ExternalApy[] | undefined;
27
+ }
28
+ /**
29
+ * Aggregates all APY components for a single pool.
30
+ *
31
+ * `depositAPY` and returned values are in "percentage" units (5 ≈ 5 %).
32
+ * `underlyingAPY` comes from `apyList` (scaled by `PERCENTAGE_FACTOR`),
33
+ * so it is divided by `PERCENTAGE_FACTOR` here.
34
+ *
35
+ * External APY: first entry in `currentExternalList`, enriched with `totalValue`.
36
+ */
37
+ export declare function calculatePoolFullAPY({ depositAPY, underlyingAPY, extraAPY, currentExternalList, }: CalculatePoolFullAPYProps): PoolFullAPY;
38
+ interface CalculatePoolFullAPY7DAgoProps {
39
+ depositAPY: number;
40
+ /**
41
+ * Annualized supply APY from the ~7d diesel snapshot, in the same units as
42
+ * `depositAPY`.
43
+ *
44
+ * - `undefined` — 7d snapshot not loaded yet (`loading7DAgo: true`); supply
45
+ * row uses `depositAPY`.
46
+ * - `null` — snapshot loaded but no numeric 7d supply; supply row uses
47
+ * `depositAPY` (`||` semantics, same as client `pool7DAgo` with missing
48
+ * `supplyAPY7DAgo`).
49
+ * - `number` — explicit 7d supply; `0` falls back to `depositAPY` via `||`.
50
+ */
51
+ supplyAPY7DAgo?: number | null;
52
+ poolAPY?: PoolFullAPY | null;
53
+ }
54
+ /**
55
+ * Computes the 7-days-ago APY snapshot for a pool.
56
+ *
57
+ * `supplyAPY7DAgo` replaces the current deposit APY in the base component;
58
+ * extra & external APY are kept from the "current" calculation.
59
+ */
60
+ export declare function calculatePoolFullAPY7DAgo({ supplyAPY7DAgo, depositAPY, poolAPY, }: CalculatePoolFullAPY7DAgoProps): PoolFullAPY7DAgo;
61
+ interface CalculatePoolPointsProps {
62
+ poolTokenSymbol: string | undefined;
63
+ points: PoolPointsBase[Address] | undefined;
64
+ tokensList: TokensMeta;
65
+ }
66
+ /**
67
+ * Transforms raw `PoolPointsBase` entries into consumer-friendly
68
+ * `PoolPointsWithTips` items with formatted amounts and tooltip strings.
69
+ */
70
+ export declare function calculatePoolPoints({ poolTokenSymbol, points, tokensList, }: CalculatePoolPointsProps): PoolPointsWithTips[] | undefined;
71
+ export {};
@@ -1,4 +1,5 @@
1
1
  import type { Address } from "viem";
2
+ import type { ExternalApy, ExtraCollateralAPY, GearAPYDetails, PointsInfo, PoolExtraApy, PoolPointsInfo } from "../../rewards/apy/index.js";
2
3
  import type { BaseContractStateHuman } from "../../sdk/index.js";
3
4
  export interface Pool7DAgoState {
4
5
  pool: Address;
@@ -7,3 +8,30 @@ export interface Pool7DAgoState {
7
8
  export interface Pools7DAgoStateHuman extends BaseContractStateHuman {
8
9
  dieselRate: bigint;
9
10
  }
11
+ export interface FarmRewardInfo {
12
+ address: Address;
13
+ symbol: string;
14
+ rewardToken: Address;
15
+ rewardSymbol: string;
16
+ token: Address;
17
+ duration: bigint;
18
+ finished: bigint;
19
+ reward: bigint;
20
+ balance: bigint;
21
+ }
22
+ export interface NetworkApyData {
23
+ apyList: Record<Address, number> | undefined;
24
+ extraCollateralAPYList: Record<Address, Record<Address, ExtraCollateralAPY>> | undefined;
25
+ pointsList: Record<Address, PointsInfo<string>> | undefined;
26
+ extraCollateralPointsList: Record<Address, Record<Address, PointsInfo<string>>> | undefined;
27
+ poolRewardsList: Record<Address, Array<PoolPointsInfo<string>>> | undefined;
28
+ tokenExtraRewardsList: Record<Address, Array<FarmRewardInfo>> | undefined;
29
+ poolExternalAPYList: Record<Address, Array<ExternalApy>> | undefined;
30
+ poolExtraAPYList: Record<Address, Array<PoolExtraApy>> | undefined;
31
+ }
32
+ export type GearStats = Omit<GearAPYDetails, "lastUpdated">;
33
+ export interface ApySnapshotState {
34
+ apy: NetworkApyData;
35
+ gearStats: GearStats | null;
36
+ timestamp: string;
37
+ }
@@ -1,6 +1,15 @@
1
1
  import type { Address } from "viem";
2
2
  import { type NetworkType } from "../../sdk/index.js";
3
- import type { PoolData } from "./common.js";
3
+ export interface PoolData {
4
+ address: Address;
5
+ version: number;
6
+ underlyingToken: Address;
7
+ dieselRateRay: bigint;
8
+ dieselToken: Address;
9
+ stakedDieselToken: Address[];
10
+ stakedDieselToken_old: Address[];
11
+ expectedLiquidity: bigint;
12
+ }
4
13
  export interface GearboxExtraMerkleLmReward {
5
14
  pool: Address;
6
15
  poolToken: Address;
@@ -4,13 +4,3 @@ export interface TokenData {
4
4
  symbol: string;
5
5
  decimals: number;
6
6
  }
7
- export interface PoolData {
8
- address: Address;
9
- version: number;
10
- underlyingToken: Address;
11
- dieselRateRay: bigint;
12
- dieselToken: Address;
13
- stakedDieselToken: Address[];
14
- stakedDieselToken_old: Address[];
15
- expectedLiquidity: bigint;
16
- }
@@ -1,13 +1,12 @@
1
1
  import type { Address } from "viem";
2
- import { type Asset, type NetworkType } from "../../sdk/index.js";
2
+ import { type Asset, type MarketSuite, type NetworkType, type TokensMeta } from "../../sdk/index.js";
3
3
  import type { PoolPointsInfo } from "../index.js";
4
- import type { PoolData, TokenData } from "./common.js";
5
- type PartialPool = Pick<PoolData, "expectedLiquidity" | "underlyingToken" | "address">;
4
+ import type { TokenData } from "./common.js";
6
5
  export interface GetPointsByPoolProps {
7
6
  poolRewards: Record<Address, Array<PoolPointsInfo<string>>>;
8
7
  totalTokenBalances: Record<Address, Asset>;
9
- pools: Array<PartialPool>;
10
- tokensList: Record<Address, TokenData>;
8
+ pools: MarketSuite[];
9
+ tokensList: TokensMeta;
11
10
  }
12
11
  export interface GetTotalTokensOnProtocolProps {
13
12
  tokensToCheck: Array<Address>;
@@ -27,4 +26,3 @@ export declare class PoolPointsAPI {
27
26
  static getPointsByPool({ poolRewards, totalTokenBalances, pools, tokensList, }: GetPointsByPoolProps): PoolPointsBase;
28
27
  private static getPoolTokenPoints;
29
28
  }
30
- export {};