Skip to content

Commit

Permalink
Merge branch 'account-balances'
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulgalimov committed Jul 14, 2023
2 parents 61f2735 + 3b944e7 commit 9580c13
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/telegram/balances.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import fetch from 'node-fetch';
import { ConfigService } from '@nestjs/config';
import {
Expand Down Expand Up @@ -28,7 +28,9 @@ const markdownTemplate = `{index}) {name} - *{balance}*
\`{address}\``;

@Injectable()
export class BalancesService {
export class BalancesService implements OnModuleInit {
private logger = new Logger(BalancesService.name);

private balanceConfig: BalancesConfig;
private slackConfig: SlackConfig;
private adminUsers: number[];
Expand All @@ -45,13 +47,15 @@ export class BalancesService {
this.slackConfig.channelName
) {
this.web = new WebClient(this.slackConfig.token);
}
}

setInterval(async () => {
await this.checkBalances();
}, 5 * 3_600_000);
async onModuleInit(): Promise<void> {
setInterval(async () => {
await this.checkBalances();
}, 5 * 3_600_000);

this.checkBalances();
}
await this.checkBalances();
}

private async getContractAddress(
Expand Down Expand Up @@ -149,12 +153,15 @@ ${this.balancesToString(chainBalanceData.balances, htmlTemplate)}`;

private async checkBalances() {
const criticalValue = BigNumber(this.balanceConfig.criticalValue);
this.logger.log(`check balances, critical value: ${criticalValue}`);

const chainBalances = await Promise.all([
this.getBalances(this.balanceConfig.unique),
this.getBalances(this.balanceConfig.quartz),
]);

this.logger.log('chain balances', JSON.stringify(chainBalances, null, 2));

const criticalBalances = chainBalances
.map((chainBalance) => {
return {
Expand All @@ -180,7 +187,7 @@ ${this.balancesToString(chainBalanceData.balances, markdownTemplate)}`;
.join(' ');

try {
await this.web.chat.postMessage({
const postMessageResult = await this.web.chat.postMessage({
link_names: true,
text: `Not enough money on the account
Expand All @@ -190,8 +197,13 @@ ${message}
${mentions}`,
channel: this.slackConfig.channelName,
});
this.logger.log('post message result', postMessageResult);
} catch (err) {
console.log('err', err.message, JSON.stringify(err, null, 2));
this.logger.error(
'post message error',
err.message,
JSON.stringify(err, null, 2),
);
}
}
}

0 comments on commit 9580c13

Please sign in to comment.