import 'dotenv/config.js'; import { GasPrices } from '../types/gasPrices' import Web3 from 'web3'; const rpcUrl = process.env.RPC_URL || 'ws://localhost:8545'; // Create a new web3 instance const web3 = new Web3(new Web3.providers.WebsocketProvider(rpcUrl, { reconnect: { auto: true, delay: 5000, maxAttempts: 5, onTimeout: false } })); // Get the current gas price in gwei async function getGasPrice() { const gasPrice = await web3.eth.getGasPrice(); return Number(gasPrice); } const getGasPricesInGwei = async (): Promise => { const gweiFromWei = (priceInWei: number): number => Number(web3.utils.fromWei(`${Math.round(priceInWei)}`, 'gwei')); try { const gasPrice = await getGasPrice() const fastPrice = gasPrice * 1.2; const slowPrice = gasPrice * 0.8; const gasPrices = { fast: gweiFromWei(fastPrice), average: gweiFromWei(gasPrice), slow: gweiFromWei(slowPrice), }; return Promise.resolve(gasPrices); // await redisClient.set('gas-prices', JSON.stringify(gasPrices)); } catch (error) { console.log(error); return Promise.reject(error); } }; // Export the getCurrentGasPrice function export { getGasPricesInGwei };