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",
"dotenv": "^16.0.3",
"redis": "^4.6.5",
"web3": "^0.20.7",
"web3-providers-ws": "^1.9.0"
"web3": "^1.9.0"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",

View File

@ -1,35 +1,37 @@
import 'dotenv/config.js';
import { GasPrices } from '../types/gasPrices'
const Web3 = require('web3');
const Web3WsProvider = require('web3-providers-ws');
import Web3 from 'web3';
const rpcUrl = process.env.RPC_URL || 'ws://localhost:8545';
// Create a new web3 instance
const web3 = new Web3(new Web3WsProvider(rpcUrl));
const web3 = new Web3(rpcUrl);
// 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 gweiFromWei = (priceInWei: string): number =>
Number(web3.utils.fromWei(priceInWei, 'gwei').toFixed(2));
const gweiFromWei = (priceInWei: number): number =>
Number(web3.utils.fromWei(`${Math.round(priceInWei)}`, 'gwei'));
try {
const [fastPrice, averagePrice, slowPrice] = await Promise.all([
web3.eth.getGasPrice(),
web3.eth.getGasPrice('average'),
web3.eth.getGasPrice('slow'),
]);
const gasPrice = await getGasPrice()
const fastPrice = gasPrice * 1.2;
const slowPrice = gasPrice * 0.8;
const gasPrices = {
fast: gweiFromWei(fastPrice),
average: gweiFromWei(averagePrice),
average: gweiFromWei(gasPrice),
slow: gweiFromWei(slowPrice),
};
return Promise.resolve(gasPrices);
// await redisClient.set('gas-prices', JSON.stringify(gasPrices));
} catch (error) {
console.error(`Error fetching gas prices: ${error}`);
console.log(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 { handleGasAlertCommand, handleGasCommand, handleGasPendingCommand } from '../handlers';
import { handleGasCommand } from '../handlers';
module.exports = {
data: new SlashCommandBuilder()
.setName('gas')
.setDescription('Set alerts for GWEI')
.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))),
.setDescription('Get current gas values'),
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);
}
}
};

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) : []));
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 user = await client.users.fetch(gasAlert.userId);

View File

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