Skip to content

Commit fafa16f

Browse files
committed
fix: add sdk sync state hook
1 parent fe86349 commit fafa16f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/sdk/GearboxSDK.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { PriceFeedRegister } from "./market/pricefeeds";
2727
import { RouterV3Contract } from "./router";
2828
import type { GearboxStateHuman, ILogger, MultiCall } from "./types";
2929
import { AddressMap, formatBN } from "./utils";
30+
import { Hooks } from "./utils/internal";
3031
import { detectNetwork } from "./utils/viem";
3132

3233
export interface SDKOptions {
@@ -58,7 +59,13 @@ export interface SyncStateOptions {
5859
timestamp: bigint;
5960
}
6061

62+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
63+
export type SDKHooks = {
64+
syncState: [];
65+
};
66+
6167
export class GearboxSDK {
68+
readonly #hooks = new Hooks<SDKHooks>();
6269
// Represents chain object
6370
readonly #provider: Provider;
6471

@@ -102,6 +109,9 @@ export class GearboxSDK {
102109
*/
103110
public readonly tokensMeta = new TokensMeta();
104111

112+
public addHook = this.#hooks.addHook.bind(this.#hooks);
113+
public removeHook = this.#hooks.removeHook.bind(this.#hooks);
114+
105115
public static async attach(
106116
options: SDKOptions &
107117
Partial<NetworkOptions> &
@@ -210,7 +220,7 @@ export class GearboxSDK {
210220
*/
211221
public parseFunctionData(address: Address, calldata: Hex): string {
212222
const contract = this.contracts.get(address);
213-
// todo: fallback to 4bytes directory
223+
// TODO: fallback to 4bytes directory
214224
return contract
215225
? contract.parseFunctionData(calldata)
216226
: `unknown: ${address}.${calldata.slice(0, 10)}`;
@@ -293,6 +303,7 @@ export class GearboxSDK {
293303

294304
this.#currentBlock = blockNumber;
295305
this.#timestamp = timestamp;
306+
await this.#hooks.triggerHooks("syncState");
296307
this.#syncing = false;
297308
this.logger?.debug(`synced state to block ${blockNumber}`);
298309
}

0 commit comments

Comments
 (0)