From cea18e6a5fb6c34bbbc5255fb6af4e028448ce4b Mon Sep 17 00:00:00 2001 From: GooeyTuxedo Date: Mon, 17 Apr 2023 22:51:24 -0700 Subject: [PATCH] reconnect for ws --- src/blockchain.ts | 9 ++++++++- src/gasPriceChecker.ts | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/blockchain.ts b/src/blockchain.ts index 37744a4..2dcbb4e 100644 --- a/src/blockchain.ts +++ b/src/blockchain.ts @@ -5,7 +5,14 @@ import Web3 from 'web3'; const rpcUrl = process.env.RPC_URL || 'ws://localhost:8545'; // Create a new web3 instance -const web3 = new Web3(rpcUrl); +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() { diff --git a/src/gasPriceChecker.ts b/src/gasPriceChecker.ts index 3fc65f9..a25fb7f 100644 --- a/src/gasPriceChecker.ts +++ b/src/gasPriceChecker.ts @@ -1,10 +1,11 @@ -import { Client, EmbedBuilder, TextChannel } from 'discord.js'; +import { EmbedBuilder, TextChannel } from 'discord.js'; import { getGasPricesInGwei } from './blockchain'; import redisClient from './redis'; +import { DiscordClient } from './discordClient'; import { GasAlert } from '../types/gasAlert'; -const createGasPriceChecker = (client: Client) => { +const createGasPriceChecker = (client: DiscordClient) => { setInterval(async () => { try { const gasPrices = await getGasPricesInGwei();