Skip to content

Commit 9210f30

Browse files
committed
refactor: reorganize balance tracker files and consolidate interfaces into a single location
1 parent 8ca483b commit 9210f30

File tree

4 files changed

+102
-111
lines changed

4 files changed

+102
-111
lines changed

apps/price_pusher/src/balance-tracker/index.ts renamed to apps/price_pusher/src/balance-tracker.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
// Export the interfaces
2-
export * from "./interface";
3-
4-
// Export chain-specific implementations
5-
export * from "./evm";
6-
// export * from "./aptos";
7-
8-
// Factory function to create the appropriate balance tracker based on the chain
9-
import { PricePusherMetrics } from "../metrics";
1+
import { PricePusherMetrics } from "./metrics";
102
import { Logger } from "pino";
11-
import { DurationInSeconds } from "../utils";
3+
import { DurationInSeconds } from "./utils";
124
import { IBalanceTracker } from "./interface";
13-
import { EvmBalanceTracker } from "./evm";
14-
import { SuperWalletClient } from "../evm/super-wallet";
5+
import { EvmBalanceTracker } from "./evm/balance-tracker";
6+
import { SuperWalletClient } from "./evm/super-wallet";
157

168
/**
179
* Parameters for creating an EVM balance tracker

apps/price_pusher/src/balance-tracker/interface.ts

Lines changed: 0 additions & 97 deletions
This file was deleted.

apps/price_pusher/src/balance-tracker/evm.ts renamed to apps/price_pusher/src/evm/balance-tracker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { SuperWalletClient } from "../evm/super-wallet";
2-
import { BaseBalanceTracker, BaseBalanceTrackerConfig } from "./interface";
1+
import { SuperWalletClient } from "./super-wallet";
2+
import { BaseBalanceTracker, BaseBalanceTrackerConfig } from "../interface";
33

44
/**
55
* EVM-specific configuration for balance tracker

apps/price_pusher/src/interface.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { HexString, UnixTimestamp } from "@pythnetwork/hermes-client";
22
import { DurationInSeconds } from "./utils";
3+
import { Logger } from "pino";
4+
import { PricePusherMetrics } from "./metrics";
35

46
export type PriceItem = {
57
id: HexString;
@@ -82,3 +84,97 @@ export interface IPricePusher {
8284
pubTimesToPush: UnixTimestamp[],
8385
): Promise<void>;
8486
}
87+
88+
/**
89+
* Common configuration properties for all balance trackers
90+
*/
91+
export interface BaseBalanceTrackerConfig {
92+
/** Address of the wallet to track */
93+
address: string;
94+
/** Name/ID of the network/chain */
95+
network: string;
96+
/** How often to update the balance */
97+
updateInterval: DurationInSeconds;
98+
/** Metrics instance to report balance updates */
99+
metrics: PricePusherMetrics;
100+
/** Logger instance */
101+
logger: Logger;
102+
}
103+
104+
/**
105+
* Interface for all balance trackers to implement
106+
* Each chain will have its own implementation of this interface
107+
*/
108+
export interface IBalanceTracker {
109+
/**
110+
* Start tracking the wallet balance
111+
*/
112+
start(): Promise<void>;
113+
114+
/**
115+
* Stop tracking the wallet balance
116+
*/
117+
stop(): void;
118+
}
119+
120+
/**
121+
* Abstract base class that implements common functionality for all balance trackers
122+
*/
123+
export abstract class BaseBalanceTracker implements IBalanceTracker {
124+
protected address: string;
125+
protected network: string;
126+
protected updateInterval: DurationInSeconds;
127+
protected metrics: PricePusherMetrics;
128+
protected logger: Logger;
129+
protected isRunning: boolean = false;
130+
131+
constructor(config: BaseBalanceTrackerConfig) {
132+
this.address = config.address;
133+
this.network = config.network;
134+
this.updateInterval = config.updateInterval;
135+
this.metrics = config.metrics;
136+
this.logger = config.logger;
137+
}
138+
139+
public async start(): Promise<void> {
140+
if (this.isRunning) {
141+
return;
142+
}
143+
144+
this.isRunning = true;
145+
146+
// Initial balance update
147+
await this.updateBalance();
148+
149+
// Start the update loop
150+
this.startUpdateLoop();
151+
}
152+
153+
private async startUpdateLoop(): Promise<void> {
154+
// We're using dynamic import to avoid circular dependencies
155+
const { sleep } = await import("./utils");
156+
157+
// Run in a loop to regularly update the balance
158+
for (;;) {
159+
// Wait first, since we already did the initial update in start()
160+
await sleep(this.updateInterval * 1000);
161+
162+
// Only continue if we're still running
163+
if (!this.isRunning) {
164+
break;
165+
}
166+
167+
await this.updateBalance();
168+
}
169+
}
170+
171+
/**
172+
* Chain-specific balance update implementation
173+
* Each chain will implement this method differently
174+
*/
175+
protected abstract updateBalance(): Promise<void>;
176+
177+
public stop(): void {
178+
this.isRunning = false;
179+
}
180+
}

0 commit comments

Comments
 (0)