39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
import { Client, EmbedBuilder, TextChannel } from 'discord.js';
|
|
import { getGasPricesInGwei } from './blockchain';
|
|
import redisClient from './redis';
|
|
|
|
import { GasAlert } from '../types/gasAlert';
|
|
|
|
const createGasPriceChecker = (client: Client) => {
|
|
setInterval(async () => {
|
|
try {
|
|
const gasPrices = await getGasPricesInGwei();
|
|
|
|
const gasAlerts: GasAlert[] = await redisClient
|
|
.get('gas-alerts')
|
|
.then((value) => (value ? JSON.parse(value) : []));
|
|
|
|
gasAlerts.forEach(async (gasAlert) => {
|
|
if (gasPrices.fast <= gasAlert.threshold) {
|
|
const channel = await client.channels.fetch(gasAlert.channelId) as TextChannel;
|
|
const user = await client.users.fetch(gasAlert.userId);
|
|
|
|
channel.send({
|
|
embeds: [
|
|
new EmbedBuilder()
|
|
.setTitle('Gas price alert!')
|
|
.setDescription(`<@${gasAlert.userId}>!\n\nThe current gas prices have fallen below your alert threshold of ${gasAlert.threshold} Gwei. The current gas prices are: \n\n Fast: ${gasPrices.fast} Gwei \n\n Average: ${gasPrices.average} Gwei \n\n Slow: ${gasPrices.slow} Gwei`)
|
|
.setColor('#FF8C00')
|
|
],
|
|
target: user
|
|
})
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error(`Error checking gas prices: ${error}`);
|
|
}
|
|
}, 15000);
|
|
};
|
|
|
|
export { createGasPriceChecker };
|