progress but https instead of wss

This commit is contained in:
GooeyTuxedo 2023-04-17 21:15:10 -07:00
parent 3dd291f394
commit eb6b1fe0d1
8 changed files with 2885 additions and 91 deletions

2882
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,8 +14,7 @@
"discord.js": "^14.9.0", "discord.js": "^14.9.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"redis": "^4.6.5", "redis": "^4.6.5",
"web3": "^0.20.7", "web3": "^1.9.0"
"web3-providers-ws": "^1.9.0"
}, },
"devDependencies": { "devDependencies": {
"@types/dotenv": "^8.2.0", "@types/dotenv": "^8.2.0",

View File

@ -1,35 +1,37 @@
import 'dotenv/config.js'; import 'dotenv/config.js';
import { GasPrices } from '../types/gasPrices' import { GasPrices } from '../types/gasPrices'
const Web3 = require('web3'); import Web3 from 'web3';
const Web3WsProvider = require('web3-providers-ws');
const rpcUrl = process.env.RPC_URL || 'ws://localhost:8545'; const rpcUrl = process.env.RPC_URL || 'ws://localhost:8545';
// Create a new web3 instance // Create a new web3 instance
const web3 = new Web3(new Web3WsProvider(rpcUrl)); const web3 = new Web3(rpcUrl);
// Get the current gas price in gwei // Get the current gas price in gwei
async function getGasPrice() {
const gasPrice = await web3.eth.getGasPrice();
return Number(gasPrice);
}
const getGasPricesInGwei = async (): Promise<GasPrices> => { const getGasPricesInGwei = async (): Promise<GasPrices> => {
const gweiFromWei = (priceInWei: string): number => const gweiFromWei = (priceInWei: number): number =>
Number(web3.utils.fromWei(priceInWei, 'gwei').toFixed(2)); Number(web3.utils.fromWei(`${Math.round(priceInWei)}`, 'gwei'));
try { try {
const [fastPrice, averagePrice, slowPrice] = await Promise.all([ const gasPrice = await getGasPrice()
web3.eth.getGasPrice(), const fastPrice = gasPrice * 1.2;
web3.eth.getGasPrice('average'), const slowPrice = gasPrice * 0.8;
web3.eth.getGasPrice('slow'),
]);
const gasPrices = { const gasPrices = {
fast: gweiFromWei(fastPrice), fast: gweiFromWei(fastPrice),
average: gweiFromWei(averagePrice), average: gweiFromWei(gasPrice),
slow: gweiFromWei(slowPrice), slow: gweiFromWei(slowPrice),
}; };
return Promise.resolve(gasPrices); return Promise.resolve(gasPrices);
// await redisClient.set('gas-prices', JSON.stringify(gasPrices)); // await redisClient.set('gas-prices', JSON.stringify(gasPrices));
} catch (error) { } catch (error) {
console.error(`Error fetching gas prices: ${error}`); console.log(error);
return Promise.reject(error); return Promise.reject(error);
} }
}; };

16
src/commands/alert.ts Normal file
View File

@ -0,0 +1,16 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
import { handleGasAlertCommand, handleGasCommand, handleGasPendingCommand } from '../handlers';
module.exports = {
data: new SlashCommandBuilder()
.setName('alert')
.setDescription('Set gas alert for gwei threshold')
.addStringOption(option =>
option.setName('gwei')
.setDescription('gwei threshold')
.setRequired(true)),
async execute(interaction: ChatInputCommandInteraction) {
return await handleGasAlertCommand(interaction);
}
}

View File

@ -1,37 +1,12 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js'; import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
import { handleGasAlertCommand, handleGasCommand, handleGasPendingCommand } from '../handlers'; import { handleGasCommand } from '../handlers';
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('gas') .setName('gas')
.setDescription('Set alerts for GWEI') .setDescription('Get current gas values'),
.addSubcommand(subcommand =>
subcommand
.setName('pending')
.setDescription('List your current alert threshhold'))
.addSubcommand(subcommand =>
subcommand
.setName('alert')
.setDescription('List your current alert threshhold')
.addStringOption(option =>
option.setName('gwei')
.setDescription('gwei threshold')
.setRequired(true))),
async execute(interaction: ChatInputCommandInteraction) { async execute(interaction: ChatInputCommandInteraction) {
const subcommand = interaction.options.getSubcommand();
if (subcommand == 'pending' ) {
console.log(`Replying to command "/gas pending"`)
return await handleGasPendingCommand(interaction);
} else if (subcommand == 'gwei') {
return await handleGasAlertCommand(interaction);
} else {
return await handleGasCommand(interaction); return await handleGasCommand(interaction);
} }
}
}; };

View File

@ -0,0 +1,12 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
import { handleGasPendingCommand } from '../handlers';
module.exports = {
data: new SlashCommandBuilder()
.setName('pending-alert')
.setDescription('Get your gwei alert threshold'),
async execute(interaction: ChatInputCommandInteraction) {
return await handleGasPendingCommand(interaction);
}
}

View File

@ -14,7 +14,7 @@ const createGasPriceChecker = (client: Client) => {
.then((value) => (value ? JSON.parse(value) : [])); .then((value) => (value ? JSON.parse(value) : []));
gasAlerts.forEach(async (gasAlert) => { gasAlerts.forEach(async (gasAlert) => {
if (gasPrices.fast <= gasAlert.threshold) { if (gasPrices.average <= gasAlert.threshold) {
const channel = await client.channels.fetch(gasAlert.channelId) as TextChannel; const channel = await client.channels.fetch(gasAlert.channelId) as TextChannel;
const user = await client.users.fetch(gasAlert.userId); const user = await client.users.fetch(gasAlert.userId);

View File

@ -6,10 +6,10 @@ import { GasAlert } from '../types/gasAlert';
// Respond to the "/gas" command // Respond to the "/gas" command
const handleGasCommand = async (interaction: ChatInputCommandInteraction): Promise<void> => { const handleGasCommand = async (interaction: ChatInputCommandInteraction): Promise<void> => {
console.log(`Replying to command "/gas"`);
const gasPrices = await getGasPricesInGwei(); const gasPrices = await getGasPricesInGwei();
console.log(`Replying to command "/gas": \n${gasPrices}`);
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor('#0099ff') .setColor('#0099ff')
.setTitle('Current Gas Prices') .setTitle('Current Gas Prices')