|
1 | 1 | import { HexString, UnixTimestamp } from "@pythnetwork/hermes-client";
|
2 | 2 | import { DurationInSeconds } from "./utils";
|
| 3 | +import { Logger } from "pino"; |
| 4 | +import { PricePusherMetrics } from "./metrics"; |
3 | 5 |
|
4 | 6 | export type PriceItem = {
|
5 | 7 | id: HexString;
|
@@ -82,3 +84,97 @@ export interface IPricePusher {
|
82 | 84 | pubTimesToPush: UnixTimestamp[],
|
83 | 85 | ): Promise<void>;
|
84 | 86 | }
|
| 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