Skip to content

Commit

Permalink
feat: new cronjobs for fees, mempool and block stats
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelMedeiros committed Jan 7, 2024
1 parent 01ea1fc commit fe17d48
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/domain/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,25 @@ export class BotService {
// cronjob list
const cronjobList = [
{
webhook: `${this.webhookUrl}/new-price`,
interval: '*/20 * * * * *',
type: 'price',
webhook: `${this.webhookUrl}/new-fees`,
interval: '0 * * * * *',
type: 'fees',
},
{
webhook: `${this.webhookUrl}/new-block`,
interval: '* * * * * *',
interval: '15 * * * * *',
type: 'block',
},
{
webhook: `${this.webhookUrl}/new-price`,
interval: '30 * * * * *',
type: 'price',
},
{
webhook: `${this.webhookUrl}/new-mempool`,
interval: '45 * * * * *',
type: 'mempool',
},
]

// add cronjob list
Expand Down
7 changes: 7 additions & 0 deletions src/domain/webhooks/dto/fees.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface FeesBodyDto {
fastestFee: number
halfHourFee: number
hourFee: number
economyFee: number
minimumFee: number
}
2 changes: 2 additions & 0 deletions src/domain/webhooks/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './alert-tx.dto'
export * from './block.dto'
export * from './price.dto'
export * from './message.dto'
export * from './fees.dto'
export * from './mempool.dto'
4 changes: 4 additions & 0 deletions src/domain/webhooks/dto/mempool.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface MempoolBodyDto {
count: number
vsize: number
}
12 changes: 12 additions & 0 deletions src/domain/webhooks/webhooks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
PriceBodyDto,
MessageParamsDto,
MessageResponseDto,
FeesBodyDto,
MempoolBodyDto,
} from './dto'
import { WebhooksService } from './webhooks.service'

Expand Down Expand Up @@ -44,6 +46,16 @@ export class WebhooksController {
return { message: 'NOK' }
}

@Post('/new-fees')
updateNewFees(@Body() feesDto: FeesBodyDto) {
this.webhooksService.updateNewFees(feesDto)
}

@Post('/new-mempool')
updateNewMempool(@Body() mempoolDto: MempoolBodyDto) {
this.webhooksService.updateNewMempool(mempoolDto)
}

@Post('/new-block')
updateNewBlock(@Body() blockDto: BlockBodyDto) {
this.webhooksService.updateNewBlock(blockDto)
Expand Down
41 changes: 35 additions & 6 deletions src/domain/webhooks/webhooks.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { InjectDiscordClient } from '@discord-nestjs/core'
import { Injectable, Logger } from '@nestjs/common'
import { Client, EmbedBuilder } from 'discord.js'
import { ActivityType, Client, EmbedBuilder } from 'discord.js'
import { progressBar } from 'src/utils'
import { createResponse } from 'src/utils/default-response'
import { NumbersService } from 'src/utils/numbers/numbers.service'
import { PriceBodyDto, BlockBodyDto, MessageResponseDto } from './dto'
import { PriceBodyDto, BlockBodyDto, MessageResponseDto, FeesBodyDto, MempoolBodyDto } from './dto'

@Injectable()
export class WebhooksService {
Expand Down Expand Up @@ -67,14 +67,41 @@ export class WebhooksService {
}
}

async updateNewMempool(mempool: MempoolBodyDto) {
try {
if (mempool && mempool.count) {
this.client.user.setActivity(` ${mempool.count} txs unconfirmed`, {
type: ActivityType.Watching,
})
this.logger.debug(`NEW WEBHOOK - mempool txs: ${mempool.count}`)
}
} catch (error) {
this.logger.error(`NEW WEBHOOK - mempool txs: ${error}`)
}
}
async updateNewBlock(block: BlockBodyDto) {
try {
if (block && block.height) {
this.client.user.setActivity(`New Block: ${block.height}`)
this.logger.debug(`NEW WEBHOOK - New Block: ${block.height}`)
this.client.user.setActivity(`${block.height} block height`, {
type: ActivityType.Watching,
})
this.logger.debug(`NEW WEBHOOK - Block: ${block.height}`)
}
} catch (error) {
this.logger.error(`NEW WEBHOOK - Block: ${error}`)
}
}

async updateNewFees(fees: FeesBodyDto) {
try {
if (fees.fastestFee) {
this.client.user.setActivity(`${fees.fastestFee} sats/vByte`, {
type: ActivityType.Watching,
})
this.logger.debug(`NEW WEBHOOK - New Fees: ${fees.fastestFee} sats/vByte`)
}
} catch (error) {
this.logger.error(`NEW WEBHOOK - New Block: ${error}`)
this.logger.error(`NEW WEBHOOK - New Fees: ${error}`)
}
}

Expand All @@ -86,7 +113,9 @@ export class WebhooksService {
const status = tickers.usd.priceChangePercent <= 0 ? 'dnd' : 'online'

this.client.user.setStatus(status)
this.client.user.setActivity(msg)
this.client.user.setActivity(msg, {
type: ActivityType.Playing,
})
this.logger.debug(`NEW WEBHOOK - New Price: ${msg}`)
} catch (error) {
this.logger.error(`NEW WEBHOOK - New Price: ${error}`)
Expand Down

0 comments on commit fe17d48

Please sign in to comment.