Skip to content

Commit

Permalink
feat: Mattermost notifications (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 30, 2024
1 parent 2e3e1a3 commit 9e527c4
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -34,7 +34,7 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1

- name: Install Node.js dependencies
run: npm ci
run: npm ci --force

- name: Install Python dependencies
run: npm run python:install
Expand Down
2 changes: 1 addition & 1 deletion docker/boltz/Dockerfile
Expand Up @@ -22,7 +22,7 @@ WORKDIR /boltz-backend
RUN sed -i "/grpc-tools/d" package.json

RUN npm install -g npm
RUN npm install
RUN npm install --force
RUN npm run compile

FROM node:${NODE_VERSION} AS final
Expand Down
2 changes: 2 additions & 0 deletions lib/Config.ts
Expand Up @@ -128,6 +128,8 @@ type BackupConfig = {
};

type NotificationConfig = {
mattermostUrl?: string;

token: string;
channel: string;
channelAlerts?: string;
Expand Down
6 changes: 3 additions & 3 deletions lib/notifications/BalanceChecker.ts
Expand Up @@ -4,8 +4,8 @@ import Logger from '../Logger';
import { liquidSymbol } from '../consts/LiquidTypes';
import { Balances } from '../proto/boltzrpc_pb';
import Service from '../service/Service';
import DiscordClient from './DiscordClient';
import { Emojis } from './Markup';
import NotificationClient from './clients/NotificationClient';

enum BalanceType {
Wallet,
Expand Down Expand Up @@ -39,7 +39,7 @@ class BalanceChecker {
constructor(
private logger: Logger,
private service: Service,
private discord: DiscordClient,
private notificationClient: NotificationClient,
currencyConfigs: (BaseCurrencyConfig | undefined)[],
tokenConfigs: TokenConfig[],
) {
Expand Down Expand Up @@ -234,7 +234,7 @@ class BalanceChecker {
}

this.logger.warn(`Balance warning: ${message}`);
await this.discord.sendMessage(message, true);
await this.notificationClient.sendMessage(message, true);
};
}

Expand Down
62 changes: 36 additions & 26 deletions lib/notifications/CommandHandler.ts
Expand Up @@ -25,9 +25,9 @@ import FeeRepository from '../db/repositories/FeeRepository';
import ReverseSwapRepository from '../db/repositories/ReverseSwapRepository';
import SwapRepository from '../db/repositories/SwapRepository';
import Service from '../service/Service';
import DiscordClient from './DiscordClient';
import { codeBlock } from './Markup';
import OtpManager from './OtpManager';
import NotificationClient from './clients/NotificationClient';

enum Command {
Help = 'help',
Expand Down Expand Up @@ -67,7 +67,7 @@ class CommandHandler {
constructor(
private logger: Logger,
config: NotificationConfig,
private discord: DiscordClient,
private notificationClient: NotificationClient,
private service: Service,
private backupScheduler: BackupScheduler,
) {
Expand Down Expand Up @@ -203,7 +203,7 @@ class CommandHandler {

this.optManager = new OtpManager(this.logger, config);

this.discord.on('message', async (message: string) => {
this.notificationClient.on('message', async (message: string) => {
const args = message.split(' ');

// Get the command and remove the first argument from the array which is the command itself
Expand Down Expand Up @@ -234,13 +234,15 @@ class CommandHandler {
message += `\n**${command}**: ${info.description}`;
});

await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);
} else {
const command = args[0].toLowerCase();
const info = this.commands.get(command);

if (!info) {
await this.discord.sendMessage(`Could not find command: ${command}`);
await this.notificationClient.sendMessage(
`Could not find command: ${command}`,
);
return;
}

Expand All @@ -254,7 +256,7 @@ class CommandHandler {
}
}

await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);
}
};

Expand All @@ -264,7 +266,7 @@ class CommandHandler {
message += `\n**${asset}**: ${satoshisToSatcomma(sum)} ${asset}`;
});

await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);
};

private swapInfo = async (args: string[]) => {
Expand Down Expand Up @@ -325,7 +327,9 @@ class CommandHandler {
break;

default:
await this.discord.sendMessage(`Invalid parameter: ${args[0]}`);
await this.notificationClient.sendMessage(
`Invalid parameter: ${args[0]}`,
);
return;
}
} else {
Expand All @@ -338,7 +342,7 @@ class CommandHandler {
minMonth = date.getUTCMonth();
}

await this.discord.sendMessage(
await this.notificationClient.sendMessage(
`${codeBlock}${stringify(
await Stats.generate(minYear, minMonth),
)}${codeBlock}`,
Expand Down Expand Up @@ -371,7 +375,7 @@ class CommandHandler {
});
});

await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);
};

private lockedFunds = async () => {
Expand Down Expand Up @@ -422,7 +426,7 @@ class CommandHandler {
message += `\n\nTotal: ${satoshisToSatcomma(symbolTotal)}\n`;
}

await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);
};

private pendingSwaps = async () => {
Expand Down Expand Up @@ -458,11 +462,11 @@ class CommandHandler {
formatSwapIds(false);
formatSwapIds(true);

await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);
};

private getReferrals = async () => {
await this.discord.sendMessage(
await this.notificationClient.sendMessage(
`${codeBlock}${stringify(await ReferralStats.getReferralFees())}${codeBlock}`,
);
};
Expand All @@ -471,15 +475,19 @@ class CommandHandler {
try {
await this.backupScheduler.uploadDatabase(new Date());

await this.discord.sendMessage('Uploaded backup of Boltz database');
await this.notificationClient.sendMessage(
'Uploaded backup of Boltz database',
);
} catch (error) {
await this.discord.sendMessage(`Could not upload backup: ${error}`);
await this.notificationClient.sendMessage(
`Could not upload backup: ${error}`,
);
}
};

private getAddress = async (args: string[]) => {
const sendError = (error: any) => {
return this.discord.sendMessage(
return this.notificationClient.sendMessage(
`Could not get address: ${formatError(error)}`,
);
};
Expand All @@ -493,22 +501,22 @@ class CommandHandler {
const currency = args[0].toUpperCase();

const response = await this.service.getAddress(currency);
await this.discord.sendMessage(`\`${response}\``);
await this.notificationClient.sendMessage(`\`${response}\``);
} catch (error) {
await sendError(error);
}
};

private withdraw = async (args: string[]) => {
if (args.length !== 3 && args.length !== 4) {
await this.discord.sendMessage('Invalid number of arguments');
await this.notificationClient.sendMessage('Invalid number of arguments');
return;
}

const validToken = this.optManager.verify(args[0]);

if (!validToken) {
await this.discord.sendMessage('Invalid OTP token');
await this.notificationClient.sendMessage('Invalid OTP token');
return;
}

Expand All @@ -519,13 +527,13 @@ class CommandHandler {
try {
const response = await this.service.payInvoice(symbol, args[2]);

await this.discord.sendMessage(
await this.notificationClient.sendMessage(
`Paid lightning invoice\nPreimage: ${getHexString(
response.preimage,
)}`,
);
} catch (error) {
await this.discord.sendMessage(
await this.notificationClient.sendMessage(
`Could not pay lightning invoice: ${formatError(error)}`,
);
}
Expand All @@ -544,11 +552,11 @@ class CommandHandler {
address: args[2],
});

await this.discord.sendMessage(
await this.notificationClient.sendMessage(
`Sent transaction: ${response.transactionId}:${response.vout}`,
);
} catch (error) {
await this.discord.sendMessage(
await this.notificationClient.sendMessage(
`Could not send coins: ${formatError(error)}`,
);
}
Expand All @@ -563,7 +571,7 @@ class CommandHandler {
} Reverse Swaps`;

this.logger.info(message);
await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);
};

/*
Expand All @@ -590,7 +598,7 @@ class CommandHandler {
}
}

await this.discord.sendMessage(
await this.notificationClient.sendMessage(
`${name} \`${swap.id}\`:\n` +
`${codeBlock}${stringify(swap)}${
hasChannelCreation ? '\n' + stringify(channelCreation) : ''
Expand All @@ -599,7 +607,9 @@ class CommandHandler {
};

private sendCouldNotFindSwap = async (id: string) => {
await this.discord.sendMessage(`Could not find swap with id: ${id}`);
await this.notificationClient.sendMessage(
`Could not find swap with id: ${id}`,
);
};
}

Expand Down
6 changes: 3 additions & 3 deletions lib/notifications/DiskUsageChecker.ts
@@ -1,7 +1,7 @@
import { execFile } from 'child_process';
import Logger from '../Logger';
import DiscordClient from './DiscordClient';
import { Emojis } from './Markup';
import NotificationClient from './clients/NotificationClient';

type DiskUsage = {
total: number;
Expand All @@ -17,7 +17,7 @@ class DiskUsageChecker {

constructor(
private logger: Logger,
private discord: DiscordClient,
private notificationClient: NotificationClient,
) {}

public checkUsage = async (): Promise<void> => {
Expand All @@ -39,7 +39,7 @@ class DiskUsageChecker {
)} GB** available ${Emojis.RotatingLight}`;

this.logger.warn(message);
await this.discord.sendMessage(message);
await this.notificationClient.sendMessage(message);

this.alertSent = true;
}
Expand Down

0 comments on commit 9e527c4

Please sign in to comment.