Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/managers/RelayerBalanceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ import type {
IViemClientManager,
LoggerInterface,
} from '@concero/operator-utils';
import { WebClient } from '@slack/web-api';
import { formatUnits } from 'viem';

interface RelayerBalanceManagerConfig extends BalanceManagerConfig {
defaultMinBalance: bigint;
minBalances?: Record<string, bigint>;
slackChannelId: string;
slackBotToken: string;
slackIntervalMs: number;
}

export class RelayerBalanceManager extends BalanceManager {
private readonly defaultMinBalance: bigint;
private readonly minBalances: Record<string, bigint>;
private readonly slackChannelId: string;
private readonly slackBotToken: string;
private readonly slackIntervalMs: number;

private constructor(
logger: LoggerInterface,
Expand All @@ -25,6 +33,9 @@ export class RelayerBalanceManager extends BalanceManager {
super(logger, viemClientManager, txReader, config);
this.defaultMinBalance = config.defaultMinBalance;
this.minBalances = config.minBalances ?? {};
this.slackChannelId = config.slackChannelId || '';
this.slackBotToken = config.slackBotToken || '';
this.slackIntervalMs = config.slackIntervalMs || 60 * 60 * 1000;
}

public static createInstance(
Expand All @@ -46,6 +57,9 @@ export class RelayerBalanceManager extends BalanceManager {
// Wait for initial balances to be populated before starting watchers
await this.initializeBalances();
this.beginWatching();

await this.checkAndNotifyInsufficientBalance();
this.startPeriodicBalanceChecks();
}

/**
Expand Down Expand Up @@ -89,4 +103,50 @@ export class RelayerBalanceManager extends BalanceManager {
private registerNativeTokenWatch(network: ConceroNetwork): void {
this.registerToken(network, 'NATIVE', '0x0000000000000000000000000000000000000000' as any);
}

private startPeriodicBalanceChecks(): void {
setInterval(async () => {
try {
await this.checkAndNotifyInsufficientBalance();
} catch (error) {
this.logger.error('Error during periodic balance check:', error);
}
}, this.slackIntervalMs);
}

private async checkAndNotifyInsufficientBalance() {
for (const network of this.activeNetworks) {
const currentBalance = this.getNativeBalance(network.name);
const minRequired = this.getMinBalanceForNetwork(network.name);

if (currentBalance < minRequired) {
const message = `RelayerBalanceManager: \n Insufficient gas on ${network.name} (chain ID: ${network.id}). \n Minimum required: ${formatUnits(minRequired, 18)}, \n Actual balance: ${formatUnits(currentBalance, 18)}`;

await this.notifyInSlackAboutMinBalance(message);
}
}
}

private async notifyInSlackAboutMinBalance(message: string) {
try {
if (!this.slackChannelId || !this.slackBotToken) {
this.logger.warn('Slack channel ID or bot token is not set');
return;
}
const webClient = new WebClient(this.slackBotToken);

const res = await webClient.chat.postMessage({
channel: this.slackChannelId,
text: message,
});

if (!res.ok) {
this.logger.error(`Failed to send message to slack: ${res.error}`);
} else {
this.logger.debug(`Slack notification sent successfully: ${message}`);
}
} catch (error) {
this.logger.error('Error sending Slack notification:', error);
}
}
}
3 changes: 3 additions & 0 deletions src/utils/initializeManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ export async function initializeManagers(): Promise<void> {
defaultMinBalance: globalConfig.BALANCE_MANAGER.DEFAULT_MIN_BALANCE,
minBalances: globalConfig.BALANCE_MANAGER.MIN_BALANCES,
pollingIntervalMs: globalConfig.BALANCE_MANAGER.POLLING_INTERVAL_MS,
slackChannelId: globalConfig.NOTIFICATIONS.SLACK.MONITORING_SYSTEM_CHANNEL_ID || '',
slackBotToken: globalConfig.NOTIFICATIONS.SLACK.BOT_TOKEN || '',
slackIntervalMs: globalConfig.NOTIFICATIONS.INTERVAL,
},
);

Expand Down