From d83649cbfec283f7c77b490f19ff40e2be284a88 Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 23 Jan 2025 11:12:05 +1000 Subject: [PATCH 1/9] bestDiff notifications --- src/ORM/utils/NumberSuffix.ts | 23 ++++++++++++++++++++ src/models/StratumV1Client.ts | 3 +++ src/services/discord.service.ts | 22 +++++++++++++++++++ src/services/notification.service.ts | 5 +++++ src/services/telegram.service.ts | 32 +++++++++++++++++++++++++--- 5 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/ORM/utils/NumberSuffix.ts diff --git a/src/ORM/utils/NumberSuffix.ts b/src/ORM/utils/NumberSuffix.ts new file mode 100644 index 00000000..8438347e --- /dev/null +++ b/src/ORM/utils/NumberSuffix.ts @@ -0,0 +1,23 @@ +import { ValueTransformer } from 'typeorm'; + +export class NumberSuffix implements ValueTransformer { + + to(value: number): string { + + const suffixes = ['', 'k', 'M', 'G', 'T', 'P', 'E']; + + if (value == null || value < 0) { + return '0'; + } + + let power = Math.floor(Math.log10(value) / 3); + if (power < 0) { + power = 0; + } + const scaledValue = value / Math.pow(1000, power); + const suffix = suffixes[power]; + + return scaledValue.toFixed(2) + suffix; + } + + } \ No newline at end of file diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index bd2efc95..58a8e81a 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -541,6 +541,9 @@ export class StratumV1Client { } if (submissionDifficulty > this.entity.bestDifficulty) { + + await this.notificationService.notifySubscribersBestDiff(this.clientAuthorization.address, submissionDifficulty); + await this.clientService.updateBestDifficulty(this.extraNonceAndSessionId, submissionDifficulty); this.entity.bestDifficulty = submissionDifficulty; if (submissionDifficulty > (await this.addressSettingsService.getSettings(this.clientAuthorization.address, true)).bestDifficulty) { diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index 999076a9..ee3d15d5 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -2,6 +2,7 @@ import { Injectable, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { Block } from 'bitcoinjs-lib'; import { Client, Collection, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder, TextChannel } from 'discord.js'; +import { NumberSuffix } from './NumberSuffix'; interface IDiscordCommand { data: SlashCommandBuilder; @@ -28,6 +29,7 @@ export class DiscordService implements OnModuleInit { private clientId: string; private guildId: string; private channelId: string; + private diffNotificaitons: string; private bot: Client; private commandCollection: Collection; @@ -56,6 +58,8 @@ export class DiscordService implements OnModuleInit { }); this.bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] }); this.bot.login(this.token); + + this.diffNotificaitons = this.configService.get('DISCORD_DIFF_NOTIFICATIONS') || 'false'; } } @@ -135,4 +139,22 @@ export class DiscordService implements OnModuleInit { channel.send(`Block Found! Result: ${message}, Height: ${height}`); } } + + public async notifySubscribersBestDiff(submissionDifficulty: number) { + + if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { + if (this.bot == null) { + return; + } + + if (this.diffNotificaitons.toLowerCase() == 'false') { + return; + } + + const guild = await this.bot.guilds.fetch(this.guildId); + const channel = await guild.channels.fetch(this.channelId) as TextChannel; + channel.send(`New Best Diff! Result: ${NumberSuffix.to(submissionDifficulty)}`); + } + + } } \ No newline at end of file diff --git a/src/services/notification.service.ts b/src/services/notification.service.ts index f643426b..a48eeafe 100644 --- a/src/services/notification.service.ts +++ b/src/services/notification.service.ts @@ -21,4 +21,9 @@ export class NotificationService implements OnModuleInit { await this.discordService.notifySubscribersBlockFound(height, block, message); await this.telegramService.notifySubscribersBlockFound(address, height, block, message); } + + public async notifySubscribersBestDiff(address: string, submissionDifficulty: number) { + await this.discordService.notifySubscribersBestDiff(submissionDifficulty); + await this.telegramService.notifySubscribersBestDiff(address, submissionDifficulty); + } } \ No newline at end of file diff --git a/src/services/telegram.service.ts b/src/services/telegram.service.ts index ba3e5d00..1987ed3f 100644 --- a/src/services/telegram.service.ts +++ b/src/services/telegram.service.ts @@ -3,6 +3,7 @@ import { ConfigService } from '@nestjs/config'; import { validate } from 'bitcoin-address-validation'; import { Block } from 'bitcoinjs-lib'; import * as TelegramBot from 'node-telegram-bot-api'; +import { NumberSuffix } from './NumberSuffix'; import { TelegramSubscriptionsService } from '../ORM/telegram-subscriptions/telegram-subscriptions.service'; @@ -11,19 +12,22 @@ import { TelegramSubscriptionsService } from '../ORM/telegram-subscriptions/tele export class TelegramService implements OnModuleInit { private bot: TelegramBot; + private diffNotificaitons: string; constructor( private readonly configService: ConfigService, private readonly telegramSubscriptionsService: TelegramSubscriptionsService ) { const token: string | null = this.configService.get('TELEGRAM_BOT_TOKEN'); + if (token == null || token.length < 1) { return; } this.bot = new TelegramBot(token, { polling: true }); - console.log('Telegram bot init'); + console.log('Telegram bot init'); + this.diffNotificaitons = this.configService.get('TELEGRAM_DIFF_NOTIFICATIONS') || 'false'; } async onModuleInit(): Promise { @@ -38,8 +42,15 @@ export class TelegramService implements OnModuleInit { this.bot.sendMessage(msg.chat.id, "Invalid address."); return; } - await this.telegramSubscriptionsService.saveSubscription(msg.chat.id, address); - this.bot.sendMessage(msg.chat.id, "Subscribed!"); + + const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); + if (subscribers.count == 0) { + await this.telegramSubscriptionsService.saveSubscription(msg.chat.id, address); + this.bot.sendMessage(msg.chat.id, "Subscribed!"); + } + else { + this.bot.sendMessage(msg.chat.id, "Already Subscribed!"); + } }); this.bot.onText(/\/start/, (msg) => { @@ -61,4 +72,19 @@ export class TelegramService implements OnModuleInit { this.bot.sendMessage(subscriber.telegramChatId, `Block Found! Result: ${message}, Height: ${height}`); }); } + + public async notifySubscribersBestDiff(address: string, submissionDifficulty: number) { + if (this.bot == null) { + return; + } + if (this.diffNotificaitons.toLowerCase() == 'false') { + return; + } + + const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); + subscribers.forEach(subscriber => { + this.bot.sendMessage(subscriber.telegramChatId, `New Best Diff! Result: ${NumberSuffix.to(submissionDifficulty)}`); + }); + } + } \ No newline at end of file From c95067d107acd32770044f4d0eb5f9bce8f4d599 Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 23 Jan 2025 11:34:48 +1000 Subject: [PATCH 2/9] bug fixes --- src/ORM/utils/NumberSuffix.ts | 5 ++++- src/services/telegram.service.ts | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ORM/utils/NumberSuffix.ts b/src/ORM/utils/NumberSuffix.ts index 8438347e..b18f5286 100644 --- a/src/ORM/utils/NumberSuffix.ts +++ b/src/ORM/utils/NumberSuffix.ts @@ -19,5 +19,8 @@ export class NumberSuffix implements ValueTransformer { return scaledValue.toFixed(2) + suffix; } - + from(value: any): number { + //never called + return 1; + } } \ No newline at end of file diff --git a/src/services/telegram.service.ts b/src/services/telegram.service.ts index 1987ed3f..cda269e9 100644 --- a/src/services/telegram.service.ts +++ b/src/services/telegram.service.ts @@ -44,7 +44,7 @@ export class TelegramService implements OnModuleInit { } const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); - if (subscribers.count == 0) { + if (subscribers.Count() == 0) { await this.telegramSubscriptionsService.saveSubscription(msg.chat.id, address); this.bot.sendMessage(msg.chat.id, "Subscribed!"); } From b7f9b66beb7147228a070fc9ce734737a7bf3b93 Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 23 Jan 2025 11:38:42 +1000 Subject: [PATCH 3/9] bug fixes --- src/ORM/utils/NumberSuffix.ts | 2 +- src/services/discord.service.ts | 2 +- src/services/telegram.service.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ORM/utils/NumberSuffix.ts b/src/ORM/utils/NumberSuffix.ts index b18f5286..fb85b4e6 100644 --- a/src/ORM/utils/NumberSuffix.ts +++ b/src/ORM/utils/NumberSuffix.ts @@ -20,7 +20,7 @@ export class NumberSuffix implements ValueTransformer { return scaledValue.toFixed(2) + suffix; } from(value: any): number { - //never called + //never return 1; } } \ No newline at end of file diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index ee3d15d5..d862b9d6 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -2,7 +2,7 @@ import { Injectable, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { Block } from 'bitcoinjs-lib'; import { Client, Collection, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder, TextChannel } from 'discord.js'; -import { NumberSuffix } from './NumberSuffix'; +import { NumberSuffix } from '../ORM/utils/NumberSuffix'; interface IDiscordCommand { data: SlashCommandBuilder; diff --git a/src/services/telegram.service.ts b/src/services/telegram.service.ts index cda269e9..04915efd 100644 --- a/src/services/telegram.service.ts +++ b/src/services/telegram.service.ts @@ -3,7 +3,7 @@ import { ConfigService } from '@nestjs/config'; import { validate } from 'bitcoin-address-validation'; import { Block } from 'bitcoinjs-lib'; import * as TelegramBot from 'node-telegram-bot-api'; -import { NumberSuffix } from './NumberSuffix'; +import { NumberSuffix } from '../ORM/utils/NumberSuffix'; import { TelegramSubscriptionsService } from '../ORM/telegram-subscriptions/telegram-subscriptions.service'; @@ -44,7 +44,7 @@ export class TelegramService implements OnModuleInit { } const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); - if (subscribers.Count() == 0) { + if (subscribers.length == 0) { await this.telegramSubscriptionsService.saveSubscription(msg.chat.id, address); this.bot.sendMessage(msg.chat.id, "Subscribed!"); } From ef506ebf36aa16551cf1c7de0ca6352bbf297693 Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 23 Jan 2025 12:11:15 +1000 Subject: [PATCH 4/9] bug fixes --- src/ORM/utils/NumberSuffix.ts | 12 +++++------- src/services/discord.service.ts | 4 ++-- src/services/telegram.service.ts | 5 +++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/ORM/utils/NumberSuffix.ts b/src/ORM/utils/NumberSuffix.ts index fb85b4e6..daa72840 100644 --- a/src/ORM/utils/NumberSuffix.ts +++ b/src/ORM/utils/NumberSuffix.ts @@ -1,8 +1,10 @@ -import { ValueTransformer } from 'typeorm'; -export class NumberSuffix implements ValueTransformer { +import { Injectable } from '@nestjs/common'; - to(value: number): string { +@Injectable() +export class NumberSuffix { + + public Convert(value: number): string { const suffixes = ['', 'k', 'M', 'G', 'T', 'P', 'E']; @@ -19,8 +21,4 @@ export class NumberSuffix implements ValueTransformer { return scaledValue.toFixed(2) + suffix; } - from(value: any): number { - //never - return 1; - } } \ No newline at end of file diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index d862b9d6..5bc7e84c 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -35,7 +35,7 @@ export class DiscordService implements OnModuleInit { private commandCollection: Collection; - constructor(private readonly configService: ConfigService) { + constructor(private readonly configService: ConfigService, private readonly numberSuffix: NumberSuffix) { if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { this.token = this.configService.get('DISCORD_BOT_TOKEN'); this.clientId = this.configService.get('DISCORD_BOT_CLIENTID'); @@ -153,7 +153,7 @@ export class DiscordService implements OnModuleInit { const guild = await this.bot.guilds.fetch(this.guildId); const channel = await guild.channels.fetch(this.channelId) as TextChannel; - channel.send(`New Best Diff! Result: ${NumberSuffix.to(submissionDifficulty)}`); + channel.send(`New Best Diff! Result: ${this.numberSuffix.Convert(submissionDifficulty)}`); } } diff --git a/src/services/telegram.service.ts b/src/services/telegram.service.ts index 04915efd..cbe17f2b 100644 --- a/src/services/telegram.service.ts +++ b/src/services/telegram.service.ts @@ -16,7 +16,8 @@ export class TelegramService implements OnModuleInit { constructor( private readonly configService: ConfigService, - private readonly telegramSubscriptionsService: TelegramSubscriptionsService + private readonly telegramSubscriptionsService: TelegramSubscriptionsService, + private readonly numberSuffix: NumberSuffix ) { const token: string | null = this.configService.get('TELEGRAM_BOT_TOKEN'); @@ -83,7 +84,7 @@ export class TelegramService implements OnModuleInit { const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); subscribers.forEach(subscriber => { - this.bot.sendMessage(subscriber.telegramChatId, `New Best Diff! Result: ${NumberSuffix.to(submissionDifficulty)}`); + this.bot.sendMessage(subscriber.telegramChatId, `New Best Diff! Result: ${this.numberSuffix.Convert(submissionDifficulty)}`); }); } From 78bc69d5c6626afabf373f563c33247fb201cdb7 Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 23 Jan 2025 12:15:15 +1000 Subject: [PATCH 5/9] z --- src/app.module.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index e1a94c18..92aee2ff 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -24,6 +24,7 @@ import { NotificationService } from './services/notification.service'; import { StratumV1JobsService } from './services/stratum-v1-jobs.service'; import { StratumV1Service } from './services/stratum-v1.service'; import { TelegramService } from './services/telegram.service'; +import { NumberSuffix } from './ORM/utils/NumberSuffix'; const ORMModules = [ @@ -32,7 +33,8 @@ const ORMModules = [ AddressSettingsModule, TelegramSubscriptionsModule, BlocksModule, - RpcBlocksModule + RpcBlocksModule, + NumberSuffix ] @Module({ @@ -68,7 +70,8 @@ const ORMModules = [ BitcoinAddressValidator, StratumV1JobsService, BTCPayService, - BraiinsService + BraiinsService, + NumberSuffix ], }) export class AppModule { From 3e4d609aa12bbc4e8e4cdc3bdd0bc78006250c7b Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 23 Jan 2025 12:18:25 +1000 Subject: [PATCH 6/9] z --- src/app.module.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index 92aee2ff..9d0570db 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -70,8 +70,7 @@ const ORMModules = [ BitcoinAddressValidator, StratumV1JobsService, BTCPayService, - BraiinsService, - NumberSuffix + BraiinsService ], }) export class AppModule { From b0248808c1eb1c3bd02c7e8992baf461e4ad6686 Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 23 Jan 2025 12:24:53 +1000 Subject: [PATCH 7/9] a --- src/ORM/utils/NumberSuffix.ts | 37 +++++++++++++++----------------- src/app.module.ts | 3 +-- src/services/discord.service.ts | 6 ++++-- src/services/telegram.service.ts | 8 ++++--- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/ORM/utils/NumberSuffix.ts b/src/ORM/utils/NumberSuffix.ts index daa72840..d3f193b5 100644 --- a/src/ORM/utils/NumberSuffix.ts +++ b/src/ORM/utils/NumberSuffix.ts @@ -1,24 +1,21 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() export class NumberSuffix { - public Convert(value: number): string { - - const suffixes = ['', 'k', 'M', 'G', 'T', 'P', 'E']; - - if (value == null || value < 0) { - return '0'; - } - - let power = Math.floor(Math.log10(value) / 3); - if (power < 0) { - power = 0; - } - const scaledValue = value / Math.pow(1000, power); - const suffix = suffixes[power]; - - return scaledValue.toFixed(2) + suffix; + to(value: number): string { + + const suffixes = ['', 'k', 'M', 'G', 'T', 'P', 'E']; + + if (value == null || value < 0) { + return '0'; } - } \ No newline at end of file + + let power = Math.floor(Math.log10(value) / 3); + if (power < 0) { + power = 0; + } + const scaledValue = value / Math.pow(1000, power); + const suffix = suffixes[power]; + + return scaledValue.toFixed(2) + suffix; + } +} \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index 9d0570db..0ada96a0 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -33,8 +33,7 @@ const ORMModules = [ AddressSettingsModule, TelegramSubscriptionsModule, BlocksModule, - RpcBlocksModule, - NumberSuffix + RpcBlocksModule ] @Module({ diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index 5bc7e84c..e8077066 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -30,12 +30,13 @@ export class DiscordService implements OnModuleInit { private guildId: string; private channelId: string; private diffNotificaitons: string; + private numberSuffix: NumberSuffix; private bot: Client; private commandCollection: Collection; - constructor(private readonly configService: ConfigService, private readonly numberSuffix: NumberSuffix) { + constructor(private readonly configService: ConfigService) { if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { this.token = this.configService.get('DISCORD_BOT_TOKEN'); this.clientId = this.configService.get('DISCORD_BOT_CLIENTID'); @@ -59,6 +60,7 @@ export class DiscordService implements OnModuleInit { this.bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] }); this.bot.login(this.token); + this.numberSuffix = new NumberSuffix(); this.diffNotificaitons = this.configService.get('DISCORD_DIFF_NOTIFICATIONS') || 'false'; } } @@ -153,7 +155,7 @@ export class DiscordService implements OnModuleInit { const guild = await this.bot.guilds.fetch(this.guildId); const channel = await guild.channels.fetch(this.channelId) as TextChannel; - channel.send(`New Best Diff! Result: ${this.numberSuffix.Convert(submissionDifficulty)}`); + channel.send(`New Best Diff! Result: ${this.numberSuffix.to(submissionDifficulty)}`); } } diff --git a/src/services/telegram.service.ts b/src/services/telegram.service.ts index cbe17f2b..25773662 100644 --- a/src/services/telegram.service.ts +++ b/src/services/telegram.service.ts @@ -13,11 +13,11 @@ export class TelegramService implements OnModuleInit { private bot: TelegramBot; private diffNotificaitons: string; + private numberSuffix: NumberSuffix; constructor( private readonly configService: ConfigService, - private readonly telegramSubscriptionsService: TelegramSubscriptionsService, - private readonly numberSuffix: NumberSuffix + private readonly telegramSubscriptionsService: TelegramSubscriptionsService ) { const token: string | null = this.configService.get('TELEGRAM_BOT_TOKEN'); @@ -28,6 +28,8 @@ export class TelegramService implements OnModuleInit { console.log('Telegram bot init'); + this.numberSuffix = new NumberSuffix(); + this.diffNotificaitons = this.configService.get('TELEGRAM_DIFF_NOTIFICATIONS') || 'false'; } @@ -84,7 +86,7 @@ export class TelegramService implements OnModuleInit { const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); subscribers.forEach(subscriber => { - this.bot.sendMessage(subscriber.telegramChatId, `New Best Diff! Result: ${this.numberSuffix.Convert(submissionDifficulty)}`); + this.bot.sendMessage(subscriber.telegramChatId, `New Best Diff! Result: ${this.numberSuffix.to(submissionDifficulty)}`); }); } From 634ffbfc3eae4d7bf6b46e2ead2d14ae9784433c Mon Sep 17 00:00:00 2001 From: Benjamin Wilson Date: Sat, 1 Feb 2025 19:22:05 -0500 Subject: [PATCH 8/9] few enhancements --- src/app.module.ts | 1 - src/services/discord.service.ts | 12 ++++-------- src/services/telegram.service.ts | 24 +++++++++++++----------- src/{ORM => }/utils/NumberSuffix.ts | 2 +- 4 files changed, 18 insertions(+), 21 deletions(-) rename src/{ORM => }/utils/NumberSuffix.ts (82%) diff --git a/src/app.module.ts b/src/app.module.ts index 0ada96a0..e1a94c18 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -24,7 +24,6 @@ import { NotificationService } from './services/notification.service'; import { StratumV1JobsService } from './services/stratum-v1-jobs.service'; import { StratumV1Service } from './services/stratum-v1.service'; import { TelegramService } from './services/telegram.service'; -import { NumberSuffix } from './ORM/utils/NumberSuffix'; const ORMModules = [ diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index e8077066..d8a41f73 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -2,7 +2,7 @@ import { Injectable, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { Block } from 'bitcoinjs-lib'; import { Client, Collection, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder, TextChannel } from 'discord.js'; -import { NumberSuffix } from '../ORM/utils/NumberSuffix'; +import { NumberSuffix } from '../utils/NumberSuffix'; interface IDiscordCommand { data: SlashCommandBuilder; @@ -29,7 +29,7 @@ export class DiscordService implements OnModuleInit { private clientId: string; private guildId: string; private channelId: string; - private diffNotificaitons: string; + private diffNotifications: boolean; private numberSuffix: NumberSuffix; private bot: Client; @@ -61,7 +61,7 @@ export class DiscordService implements OnModuleInit { this.bot.login(this.token); this.numberSuffix = new NumberSuffix(); - this.diffNotificaitons = this.configService.get('DISCORD_DIFF_NOTIFICATIONS') || 'false'; + this.diffNotifications = (this.configService.get('DISCORD_DIFF_NOTIFICATIONS').toLowerCase() == 'true') || false; } } @@ -145,11 +145,7 @@ export class DiscordService implements OnModuleInit { public async notifySubscribersBestDiff(submissionDifficulty: number) { if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { - if (this.bot == null) { - return; - } - - if (this.diffNotificaitons.toLowerCase() == 'false') { + if (this.bot == null || this.diffNotifications == false) { return; } diff --git a/src/services/telegram.service.ts b/src/services/telegram.service.ts index 25773662..19e1281a 100644 --- a/src/services/telegram.service.ts +++ b/src/services/telegram.service.ts @@ -3,7 +3,7 @@ import { ConfigService } from '@nestjs/config'; import { validate } from 'bitcoin-address-validation'; import { Block } from 'bitcoinjs-lib'; import * as TelegramBot from 'node-telegram-bot-api'; -import { NumberSuffix } from '../ORM/utils/NumberSuffix'; +import { NumberSuffix } from '../utils/NumberSuffix'; import { TelegramSubscriptionsService } from '../ORM/telegram-subscriptions/telegram-subscriptions.service'; @@ -12,7 +12,7 @@ import { TelegramSubscriptionsService } from '../ORM/telegram-subscriptions/tele export class TelegramService implements OnModuleInit { private bot: TelegramBot; - private diffNotificaitons: string; + private diffNotifications: boolean; private numberSuffix: NumberSuffix; constructor( @@ -30,7 +30,7 @@ export class TelegramService implements OnModuleInit { this.numberSuffix = new NumberSuffix(); - this.diffNotificaitons = this.configService.get('TELEGRAM_DIFF_NOTIFICATIONS') || 'false'; + this.diffNotifications = (this.configService.get('TELEGRAM_DIFF_NOTIFICATIONS').toLowerCase() == 'true') || false; } async onModuleInit(): Promise { @@ -71,23 +71,25 @@ export class TelegramService implements OnModuleInit { } const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); - subscribers.forEach(subscriber => { - this.bot.sendMessage(subscriber.telegramChatId, `Block Found! Result: ${message}, Height: ${height}`); + + const subscriberMessages = subscribers.map(subscriber =>{ + return this.bot.sendMessage(subscriber.telegramChatId, `Block Found! Result: ${message}, Height: ${height}`); }); + + Promise.all(subscriberMessages).then(); } public async notifySubscribersBestDiff(address: string, submissionDifficulty: number) { - if (this.bot == null) { - return; - } - if (this.diffNotificaitons.toLowerCase() == 'false') { + if (this.bot == null || this.diffNotifications == false) { return; } const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); - subscribers.forEach(subscriber => { - this.bot.sendMessage(subscriber.telegramChatId, `New Best Diff! Result: ${this.numberSuffix.to(submissionDifficulty)}`); + const subscriberMessages = subscribers.map(subscriber => { + return this.bot.sendMessage(subscriber.telegramChatId, `New Best Diff! Result: ${this.numberSuffix.to(submissionDifficulty)}`); }); + + Promise.all(subscriberMessages).then(); } } \ No newline at end of file diff --git a/src/ORM/utils/NumberSuffix.ts b/src/utils/NumberSuffix.ts similarity index 82% rename from src/ORM/utils/NumberSuffix.ts rename to src/utils/NumberSuffix.ts index d3f193b5..78fd4405 100644 --- a/src/ORM/utils/NumberSuffix.ts +++ b/src/utils/NumberSuffix.ts @@ -3,7 +3,7 @@ export class NumberSuffix { to(value: number): string { - const suffixes = ['', 'k', 'M', 'G', 'T', 'P', 'E']; + const suffixes = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q']; if (value == null || value < 0) { return '0'; From 5cc706d36a73d5ac10ac21899b4fe96c78f8cf18 Mon Sep 17 00:00:00 2001 From: swordfish6975 Date: Thu, 8 May 2025 12:45:43 +1000 Subject: [PATCH 9/9] Update StratumV1Client.ts Make it way less spamy --- src/models/StratumV1Client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 58a8e81a..83818bff 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -542,12 +542,13 @@ export class StratumV1Client { if (submissionDifficulty > this.entity.bestDifficulty) { - await this.notificationService.notifySubscribersBestDiff(this.clientAuthorization.address, submissionDifficulty); + await this.clientService.updateBestDifficulty(this.extraNonceAndSessionId, submissionDifficulty); this.entity.bestDifficulty = submissionDifficulty; if (submissionDifficulty > (await this.addressSettingsService.getSettings(this.clientAuthorization.address, true)).bestDifficulty) { await this.addressSettingsService.updateBestDifficulty(this.clientAuthorization.address, submissionDifficulty, this.entity.userAgent); + await this.notificationService.notifySubscribersBestDiff(this.clientAuthorization.address, submissionDifficulty); } }