@gearbox-protocol/sdk 13.6.0-apy-plugin.1 → 13.6.0-apy-plugin.3
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.
- package/dist/cjs/plugins/pools-history/ApyPlugin.js +3 -66
- package/dist/cjs/sdk/GearboxSDK.js +0 -11
- package/dist/esm/plugins/pools-history/ApyPlugin.js +3 -66
- package/dist/esm/sdk/GearboxSDK.js +0 -11
- package/dist/types/plugins/pools-history/ApyPlugin.d.ts +2 -26
- package/dist/types/plugins/pools-history/pool-apy-types.d.ts +2 -0
- package/dist/types/plugins/pools-history/pool-apy-utils.d.ts +4 -2
- package/dist/types/sdk/GearboxSDK.d.ts +0 -19
- package/package.json +1 -1
|
@@ -31,40 +31,15 @@ var import_apy_parser = require("./apy-parser.js");
|
|
|
31
31
|
var import_constants = require("./constants.js");
|
|
32
32
|
var import_pool_apy_utils = require("./pool-apy-utils.js");
|
|
33
33
|
const MAP_LABEL = "pools7DAgo";
|
|
34
|
-
const PLUGIN_KEY = "ApyPlugin";
|
|
35
34
|
class ApyPlugin extends import_sdk.BasePlugin {
|
|
36
|
-
#timerInterval;
|
|
37
35
|
#apyUrl;
|
|
38
36
|
#cacheTtlMs;
|
|
39
37
|
#pools7DAgo;
|
|
40
38
|
#apySnapshot;
|
|
41
|
-
|
|
42
|
-
* Default timer options
|
|
43
|
-
* @see PluginTimerOptions
|
|
44
|
-
*/
|
|
45
|
-
#defaultTimerOptions;
|
|
46
|
-
/**
|
|
47
|
-
* When `true`, the timer is started eagerly during the `attach` phase
|
|
48
|
-
* rather than waiting for an explicit `load` call.
|
|
49
|
-
**/
|
|
50
|
-
startTimerOnAttach;
|
|
51
|
-
constructor(loadOnAttach = false, startTimerOnAttach = false, options) {
|
|
39
|
+
constructor(loadOnAttach = false, options) {
|
|
52
40
|
super(loadOnAttach);
|
|
53
|
-
this.startTimerOnAttach = startTimerOnAttach;
|
|
54
41
|
this.#apyUrl = options?.apyUrl ?? import_constants.APY_STATE_CACHE_URL;
|
|
55
42
|
this.#cacheTtlMs = options?.cacheTtlMs ?? import_constants.DEFAULT_APY_INTERVAL_MS;
|
|
56
|
-
this.#defaultTimerOptions = options?.timer ?? {
|
|
57
|
-
refreshPools7DAgoOnTick: false,
|
|
58
|
-
intervalMs: import_constants.DEFAULT_APY_INTERVAL_MS,
|
|
59
|
-
onChange: () => {
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
async attach() {
|
|
64
|
-
await super.attach();
|
|
65
|
-
if (this.startTimerOnAttach) {
|
|
66
|
-
this.startTimer();
|
|
67
|
-
}
|
|
68
43
|
}
|
|
69
44
|
// ---------------------------------------------------------------------------
|
|
70
45
|
// Load — single entry point for all data (on-chain + state-cache)
|
|
@@ -203,51 +178,13 @@ class ApyPlugin extends import_sdk.BasePlugin {
|
|
|
203
178
|
tokensList: this.sdk.tokensMeta
|
|
204
179
|
});
|
|
205
180
|
}
|
|
206
|
-
return { data, data7DAgo, points };
|
|
207
|
-
}
|
|
208
|
-
// ---------------------------------------------------------------------------
|
|
209
|
-
// Periodic full refresh
|
|
210
|
-
// ---------------------------------------------------------------------------
|
|
211
|
-
/**
|
|
212
|
-
* Starts a periodic timer that performs a full plugin state refresh.
|
|
213
|
-
* Only one timer can be active; calling again is a no-op.
|
|
214
|
-
* @returns Cleanup function that stops the timer.
|
|
215
|
-
*/
|
|
216
|
-
startTimer(opts) {
|
|
217
|
-
if (this.#timerInterval) {
|
|
218
|
-
this.#logger?.debug("plugin timer already running");
|
|
219
|
-
return () => this.stopTimer();
|
|
220
|
-
}
|
|
221
|
-
const intervalMs = opts?.intervalMs ?? this.#defaultTimerOptions.intervalMs;
|
|
222
|
-
this.#logger?.debug(`starting plugin timer (interval: ${intervalMs}ms)`);
|
|
223
|
-
this.#timerInterval = setInterval(async () => {
|
|
224
|
-
try {
|
|
225
|
-
const prevTimestamp = this.#apySnapshot?.timestamp;
|
|
226
|
-
await this.load(true, {
|
|
227
|
-
loadPools7DAgo: opts?.refreshPools7DAgoOnTick ?? this.#defaultTimerOptions.refreshPools7DAgoOnTick
|
|
228
|
-
});
|
|
229
|
-
if (this.#apySnapshot?.timestamp !== prevTimestamp) {
|
|
230
|
-
opts?.onChange?.() ?? this.#defaultTimerOptions.onChange?.();
|
|
231
|
-
await this.sdk.triggerPluginUpdate(PLUGIN_KEY);
|
|
232
|
-
}
|
|
233
|
-
} catch (e) {
|
|
234
|
-
this.#logger?.error(e, "periodic refresh failed");
|
|
235
|
-
}
|
|
236
|
-
}, intervalMs);
|
|
237
|
-
return () => this.stopTimer();
|
|
238
|
-
}
|
|
239
|
-
stopTimer() {
|
|
240
|
-
if (this.#timerInterval) {
|
|
241
|
-
clearInterval(this.#timerInterval);
|
|
242
|
-
this.#timerInterval = void 0;
|
|
243
|
-
this.#logger?.debug("plugin timer stopped");
|
|
244
|
-
}
|
|
181
|
+
return { data, data7DAgo, pointsBase: poolPointsBase, points };
|
|
245
182
|
}
|
|
246
183
|
// ---------------------------------------------------------------------------
|
|
247
184
|
// Plugin lifecycle
|
|
248
185
|
// ---------------------------------------------------------------------------
|
|
249
186
|
async syncState() {
|
|
250
|
-
await this.load(
|
|
187
|
+
await this.load();
|
|
251
188
|
}
|
|
252
189
|
stateHuman(_) {
|
|
253
190
|
return this.pools7DAgo.values().flatMap((p) => ({
|
|
@@ -110,17 +110,6 @@ class GearboxSDK extends import_base.ChainContractsRegister {
|
|
|
110
110
|
* @see {@link SDKHooks} for available event names.
|
|
111
111
|
**/
|
|
112
112
|
removeHook = this.#hooks.removeHook.bind(this.#hooks);
|
|
113
|
-
/**
|
|
114
|
-
* Triggers the `pluginUpdate` hook.
|
|
115
|
-
*
|
|
116
|
-
* Intended to be called by plugins when they update their internal state
|
|
117
|
-
* outside of the normal `syncState`/`rehydrate` cycle (e.g. via an
|
|
118
|
-
* internal timer). Frontend listeners registered with
|
|
119
|
-
* `sdk.addHook("pluginUpdate", …)` will be notified.
|
|
120
|
-
**/
|
|
121
|
-
async triggerPluginUpdate(plugin) {
|
|
122
|
-
await this.#hooks.triggerHooks("pluginUpdate", { plugin });
|
|
123
|
-
}
|
|
124
113
|
/**
|
|
125
114
|
* Creates and initialises a new SDK instance by reading live on-chain state.
|
|
126
115
|
*
|
|
@@ -21,40 +21,15 @@ import {
|
|
|
21
21
|
getPoolExtraAPY
|
|
22
22
|
} from "./pool-apy-utils.js";
|
|
23
23
|
const MAP_LABEL = "pools7DAgo";
|
|
24
|
-
const PLUGIN_KEY = "ApyPlugin";
|
|
25
24
|
class ApyPlugin extends BasePlugin {
|
|
26
|
-
#timerInterval;
|
|
27
25
|
#apyUrl;
|
|
28
26
|
#cacheTtlMs;
|
|
29
27
|
#pools7DAgo;
|
|
30
28
|
#apySnapshot;
|
|
31
|
-
|
|
32
|
-
* Default timer options
|
|
33
|
-
* @see PluginTimerOptions
|
|
34
|
-
*/
|
|
35
|
-
#defaultTimerOptions;
|
|
36
|
-
/**
|
|
37
|
-
* When `true`, the timer is started eagerly during the `attach` phase
|
|
38
|
-
* rather than waiting for an explicit `load` call.
|
|
39
|
-
**/
|
|
40
|
-
startTimerOnAttach;
|
|
41
|
-
constructor(loadOnAttach = false, startTimerOnAttach = false, options) {
|
|
29
|
+
constructor(loadOnAttach = false, options) {
|
|
42
30
|
super(loadOnAttach);
|
|
43
|
-
this.startTimerOnAttach = startTimerOnAttach;
|
|
44
31
|
this.#apyUrl = options?.apyUrl ?? APY_STATE_CACHE_URL;
|
|
45
32
|
this.#cacheTtlMs = options?.cacheTtlMs ?? DEFAULT_APY_INTERVAL_MS;
|
|
46
|
-
this.#defaultTimerOptions = options?.timer ?? {
|
|
47
|
-
refreshPools7DAgoOnTick: false,
|
|
48
|
-
intervalMs: DEFAULT_APY_INTERVAL_MS,
|
|
49
|
-
onChange: () => {
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
async attach() {
|
|
54
|
-
await super.attach();
|
|
55
|
-
if (this.startTimerOnAttach) {
|
|
56
|
-
this.startTimer();
|
|
57
|
-
}
|
|
58
33
|
}
|
|
59
34
|
// ---------------------------------------------------------------------------
|
|
60
35
|
// Load — single entry point for all data (on-chain + state-cache)
|
|
@@ -193,51 +168,13 @@ class ApyPlugin extends BasePlugin {
|
|
|
193
168
|
tokensList: this.sdk.tokensMeta
|
|
194
169
|
});
|
|
195
170
|
}
|
|
196
|
-
return { data, data7DAgo, points };
|
|
197
|
-
}
|
|
198
|
-
// ---------------------------------------------------------------------------
|
|
199
|
-
// Periodic full refresh
|
|
200
|
-
// ---------------------------------------------------------------------------
|
|
201
|
-
/**
|
|
202
|
-
* Starts a periodic timer that performs a full plugin state refresh.
|
|
203
|
-
* Only one timer can be active; calling again is a no-op.
|
|
204
|
-
* @returns Cleanup function that stops the timer.
|
|
205
|
-
*/
|
|
206
|
-
startTimer(opts) {
|
|
207
|
-
if (this.#timerInterval) {
|
|
208
|
-
this.#logger?.debug("plugin timer already running");
|
|
209
|
-
return () => this.stopTimer();
|
|
210
|
-
}
|
|
211
|
-
const intervalMs = opts?.intervalMs ?? this.#defaultTimerOptions.intervalMs;
|
|
212
|
-
this.#logger?.debug(`starting plugin timer (interval: ${intervalMs}ms)`);
|
|
213
|
-
this.#timerInterval = setInterval(async () => {
|
|
214
|
-
try {
|
|
215
|
-
const prevTimestamp = this.#apySnapshot?.timestamp;
|
|
216
|
-
await this.load(true, {
|
|
217
|
-
loadPools7DAgo: opts?.refreshPools7DAgoOnTick ?? this.#defaultTimerOptions.refreshPools7DAgoOnTick
|
|
218
|
-
});
|
|
219
|
-
if (this.#apySnapshot?.timestamp !== prevTimestamp) {
|
|
220
|
-
opts?.onChange?.() ?? this.#defaultTimerOptions.onChange?.();
|
|
221
|
-
await this.sdk.triggerPluginUpdate(PLUGIN_KEY);
|
|
222
|
-
}
|
|
223
|
-
} catch (e) {
|
|
224
|
-
this.#logger?.error(e, "periodic refresh failed");
|
|
225
|
-
}
|
|
226
|
-
}, intervalMs);
|
|
227
|
-
return () => this.stopTimer();
|
|
228
|
-
}
|
|
229
|
-
stopTimer() {
|
|
230
|
-
if (this.#timerInterval) {
|
|
231
|
-
clearInterval(this.#timerInterval);
|
|
232
|
-
this.#timerInterval = void 0;
|
|
233
|
-
this.#logger?.debug("plugin timer stopped");
|
|
234
|
-
}
|
|
171
|
+
return { data, data7DAgo, pointsBase: poolPointsBase, points };
|
|
235
172
|
}
|
|
236
173
|
// ---------------------------------------------------------------------------
|
|
237
174
|
// Plugin lifecycle
|
|
238
175
|
// ---------------------------------------------------------------------------
|
|
239
176
|
async syncState() {
|
|
240
|
-
await this.load(
|
|
177
|
+
await this.load();
|
|
241
178
|
}
|
|
242
179
|
stateHuman(_) {
|
|
243
180
|
return this.pools7DAgo.values().flatMap((p) => ({
|
|
@@ -101,17 +101,6 @@ 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
|
-
}
|
|
115
104
|
/**
|
|
116
105
|
* Creates and initialises a new SDK instance by reading live on-chain state.
|
|
117
106
|
*
|
|
@@ -9,9 +9,8 @@ export interface ApyPluginState {
|
|
|
9
9
|
}
|
|
10
10
|
export interface ApyPluginConstructorOptions {
|
|
11
11
|
apyUrl?: string;
|
|
12
|
-
/** TTL for the shared HTTP cache in milliseconds (default:
|
|
12
|
+
/** TTL for the shared HTTP cache in milliseconds (default: 10 minutes, see `DEFAULT_APY_INTERVAL_MS`) */
|
|
13
13
|
cacheTtlMs?: number;
|
|
14
|
-
timer?: PluginTimerOptions;
|
|
15
14
|
}
|
|
16
15
|
export interface ApyPluginLoadOptions {
|
|
17
16
|
/**
|
|
@@ -21,25 +20,9 @@ export interface ApyPluginLoadOptions {
|
|
|
21
20
|
*/
|
|
22
21
|
loadPools7DAgo?: boolean;
|
|
23
22
|
}
|
|
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
23
|
export declare class ApyPlugin extends BasePlugin<ApyPluginState> implements IGearboxSDKPlugin<ApyPluginState> {
|
|
35
24
|
#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>;
|
|
25
|
+
constructor(loadOnAttach?: boolean, options?: ApyPluginConstructorOptions);
|
|
43
26
|
load(force?: boolean, loadOptions?: ApyPluginLoadOptions): Promise<ApyPluginState>;
|
|
44
27
|
get loaded(): boolean;
|
|
45
28
|
/**
|
|
@@ -56,13 +39,6 @@ export declare class ApyPlugin extends BasePlugin<ApyPluginState> implements IGe
|
|
|
56
39
|
* @throws if plugin is not loaded
|
|
57
40
|
*/
|
|
58
41
|
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
42
|
syncState(): Promise<void>;
|
|
67
43
|
stateHuman(_?: boolean): Pools7DAgoStateHuman[];
|
|
68
44
|
get state(): ApyPluginState;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
2
|
import type { ExternalApy as ExternalApySDK, PoolExtraApy, PoolPointsInfo } from "../../rewards/apy/index.js";
|
|
3
|
+
import type { PoolPointsBase } from "../../rewards/index.js";
|
|
3
4
|
export interface PoolBaseAPY {
|
|
4
5
|
type: "supplyAPY" | "tokenYield";
|
|
5
6
|
apy: number;
|
|
@@ -34,6 +35,7 @@ type AllPoolsPoints = Record<Address, PoolPointsWithTips[] | undefined>;
|
|
|
34
35
|
export interface GetPoolsAPYResult {
|
|
35
36
|
data: AllPoolsAPY;
|
|
36
37
|
data7DAgo: AllPoolsAPY7DAgo;
|
|
38
|
+
pointsBase: PoolPointsBase;
|
|
37
39
|
points: AllPoolsPoints;
|
|
38
40
|
}
|
|
39
41
|
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
2
|
import type { ExternalApy, PoolExtraApy } from "../../rewards/apy/index.js";
|
|
3
3
|
import type { PoolPointsBase } from "../../rewards/rewards/extra-apy.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { AddressMap } from "../../sdk/index.js";
|
|
5
5
|
import type { PoolFullAPY, PoolFullAPY7DAgo, PoolPointsWithTips } from "./pool-apy-types.js";
|
|
6
6
|
/**
|
|
7
7
|
* Collects extra APY entries for the given token addresses from the
|
|
@@ -61,7 +61,9 @@ export declare function calculatePoolFullAPY7DAgo({ supplyAPY7DAgo, depositAPY,
|
|
|
61
61
|
interface CalculatePoolPointsProps {
|
|
62
62
|
poolTokenSymbol: string | undefined;
|
|
63
63
|
points: PoolPointsBase[Address] | undefined;
|
|
64
|
-
tokensList:
|
|
64
|
+
tokensList: AddressMap<{
|
|
65
|
+
decimals: number;
|
|
66
|
+
}>;
|
|
65
67
|
}
|
|
66
68
|
/**
|
|
67
69
|
* Transforms raw `PoolPointsBase` entries into consumer-friendly
|
|
@@ -115,13 +115,6 @@ export interface SyncStateOptions {
|
|
|
115
115
|
**/
|
|
116
116
|
ignoreUpdateablePrices?: boolean;
|
|
117
117
|
}
|
|
118
|
-
/**
|
|
119
|
-
* Payload carried by the `pluginUpdate` hook.
|
|
120
|
-
**/
|
|
121
|
-
export interface PluginUpdateInfo {
|
|
122
|
-
/** Identifier of the plugin that triggered the update (e.g. `"pools7DAgo"`). */
|
|
123
|
-
plugin: string;
|
|
124
|
-
}
|
|
125
118
|
/**
|
|
126
119
|
* Hook event map for the SDK lifecycle.
|
|
127
120
|
*
|
|
@@ -130,13 +123,10 @@ export interface PluginUpdateInfo {
|
|
|
130
123
|
*
|
|
131
124
|
* - `syncState` — fired after {@link GearboxSDK.syncState} completes.
|
|
132
125
|
* - `rehydrate` — fired after {@link GearboxSDK.rehydrate} completes.
|
|
133
|
-
* - `pluginUpdate` — fired by a plugin when its internal state changes
|
|
134
|
-
* outside of the normal `syncState`/`rehydrate` cycle (e.g. timer tick).
|
|
135
126
|
**/
|
|
136
127
|
export type SDKHooks = {
|
|
137
128
|
syncState: [SyncStateOptions];
|
|
138
129
|
rehydrate: [SyncStateOptions];
|
|
139
|
-
pluginUpdate: [PluginUpdateInfo];
|
|
140
130
|
};
|
|
141
131
|
/**
|
|
142
132
|
* Main entry point for the Gearbox SDK.
|
|
@@ -185,15 +175,6 @@ export declare class GearboxSDK<const Plugins extends PluginsMap = {}> extends C
|
|
|
185
175
|
* @see {@link SDKHooks} for available event names.
|
|
186
176
|
**/
|
|
187
177
|
removeHook: <K extends keyof SDKHooks>(hookName: K, fn: (...args: SDKHooks[K]) => void | Promise<void>) => void;
|
|
188
|
-
/**
|
|
189
|
-
* Triggers the `pluginUpdate` hook.
|
|
190
|
-
*
|
|
191
|
-
* Intended to be called by plugins when they update their internal state
|
|
192
|
-
* outside of the normal `syncState`/`rehydrate` cycle (e.g. via an
|
|
193
|
-
* internal timer). Frontend listeners registered with
|
|
194
|
-
* `sdk.addHook("pluginUpdate", …)` will be notified.
|
|
195
|
-
**/
|
|
196
|
-
triggerPluginUpdate(plugin: string): Promise<void>;
|
|
197
178
|
/**
|
|
198
179
|
* Creates and initialises a new SDK instance by reading live on-chain state.
|
|
199
180
|
*
|