handle observable errors

This commit is contained in:
MrTuxedo 2023-04-22 11:38:11 -07:00
parent 34f7f23ae8
commit ef228254b3
3 changed files with 19 additions and 5 deletions

8
package-lock.json generated
View File

@ -12,10 +12,12 @@
"discord.js": "^14.9.0", "discord.js": "^14.9.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"ethers": "^6.3.0", "ethers": "^6.3.0",
"lodash": "^4.17.21",
"redis": "^4.6.5", "redis": "^4.6.5",
"rxjs": "^7.8.0" "rxjs": "^7.8.0"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash": "^4.14.194",
"typescript": "^4.9.5" "typescript": "^4.9.5"
} }
}, },
@ -197,6 +199,12 @@
"resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz", "resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz",
"integrity": "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==" "integrity": "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A=="
}, },
"node_modules/@types/lodash": {
"version": "4.14.194",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.194.tgz",
"integrity": "sha512-r22s9tAS7imvBt2lyHC9B8AGwWnXaYb1tY09oyLkXDs4vArpYJzw09nj8MLx5VfciBPGIb+ZwG0ssYnEPJxn/g==",
"dev": true
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "18.15.11", "version": "18.15.11",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.11.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.11.tgz",

View File

@ -14,10 +14,12 @@
"discord.js": "^14.9.0", "discord.js": "^14.9.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"ethers": "^6.3.0", "ethers": "^6.3.0",
"lodash": "^4.17.21",
"redis": "^4.6.5", "redis": "^4.6.5",
"rxjs": "^7.8.0" "rxjs": "^7.8.0"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash": "^4.14.194",
"typescript": "^4.9.5" "typescript": "^4.9.5"
} }
} }

View File

@ -1,7 +1,8 @@
import 'dotenv/config.js'; import 'dotenv/config.js';
import { WebSocketProvider, formatUnits } from 'ethers'; import { WebSocketProvider, formatUnits } from 'ethers';
import { Observable } from 'rxjs'; import { sortBy } from 'lodash';
import { map, scan } from 'rxjs/operators'; import { Observable, throwError } from 'rxjs';
import { catchError, map, scan, retry } from 'rxjs/operators';
import { GasPrices } from '../types/gasPrices'; import { GasPrices } from '../types/gasPrices';
@ -16,13 +17,14 @@ const blockGasPricesObservable = new Observable<GasPrices>((observer) => {
if (!block) throw new Error(`Error fetching block! ${blockNumber}`); if (!block) throw new Error(`Error fetching block! ${blockNumber}`);
const gasPrices = block.prefetchedTransactions.map((tx) => tx.gasPrice).sort(); const gasPrices = sortBy(block.prefetchedTransactions, ['gasPrice']).map(x => x.gasPrice);
const fast = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.9)], "gwei")); const fast = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.9)], "gwei"));
const average = Number(formatUnits(gasPrices[Math.floor(gasPrices.length / 2)], "gwei")); const average = Number(formatUnits(gasPrices[Math.floor(gasPrices.length / 2)], "gwei"));
const slow = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.05)], "gwei")); const slow = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.05)], "gwei"));
// Log averages every 10 blocks // Log averages every 10 blocks
if (blockNumber % 10 == 0) console.log(`Found new block data for ${blockNumber}! Base average transaction cost: ${slow} Gwei`) if (blockNumber % 10 == 0) console.log(`Found new block data for ${blockNumber}!
Gas price spreads: ${fast.toFixed(2)} 🚶 ${average.toFixed(2)} 🐢 ${slow.toFixed(2)} Gwei`)
observer.next({ fast, average, slow } as GasPrices); observer.next({ fast, average, slow } as GasPrices);
} catch (error) { } catch (error) {
@ -46,7 +48,9 @@ const averageGasPricesObservable = blockGasPricesObservable.pipe(
average: Math.round(average / blocks.length), average: Math.round(average / blocks.length),
slow: Math.round(slow / blocks.length), slow: Math.round(slow / blocks.length),
} as GasPrices; } as GasPrices;
}) }),
catchError(err => throwError(() => new Error(err))),
retry(2)
); );
export { averageGasPricesObservable }; export { averageGasPricesObservable };