handle observable errors
This commit is contained in:
parent
34f7f23ae8
commit
ef228254b3
8
package-lock.json
generated
8
package-lock.json
generated
@ -12,10 +12,12 @@
|
||||
"discord.js": "^14.9.0",
|
||||
"dotenv": "^16.0.3",
|
||||
"ethers": "^6.3.0",
|
||||
"lodash": "^4.17.21",
|
||||
"redis": "^4.6.5",
|
||||
"rxjs": "^7.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/lodash": "^4.14.194",
|
||||
"typescript": "^4.9.5"
|
||||
}
|
||||
},
|
||||
@ -197,6 +199,12 @@
|
||||
"resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz",
|
||||
"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": {
|
||||
"version": "18.15.11",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.11.tgz",
|
||||
|
@ -14,10 +14,12 @@
|
||||
"discord.js": "^14.9.0",
|
||||
"dotenv": "^16.0.3",
|
||||
"ethers": "^6.3.0",
|
||||
"lodash": "^4.17.21",
|
||||
"redis": "^4.6.5",
|
||||
"rxjs": "^7.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/lodash": "^4.14.194",
|
||||
"typescript": "^4.9.5"
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
import 'dotenv/config.js';
|
||||
import { WebSocketProvider, formatUnits } from 'ethers';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map, scan } from 'rxjs/operators';
|
||||
import { sortBy } from 'lodash';
|
||||
import { Observable, throwError } from 'rxjs';
|
||||
import { catchError, map, scan, retry } from 'rxjs/operators';
|
||||
|
||||
import { GasPrices } from '../types/gasPrices';
|
||||
|
||||
@ -16,13 +17,14 @@ const blockGasPricesObservable = new Observable<GasPrices>((observer) => {
|
||||
|
||||
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 average = Number(formatUnits(gasPrices[Math.floor(gasPrices.length / 2)], "gwei"));
|
||||
const slow = Number(formatUnits(gasPrices[Math.floor(gasPrices.length * 0.05)], "gwei"));
|
||||
|
||||
// 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);
|
||||
} catch (error) {
|
||||
@ -46,7 +48,9 @@ const averageGasPricesObservable = blockGasPricesObservable.pipe(
|
||||
average: Math.round(average / blocks.length),
|
||||
slow: Math.round(slow / blocks.length),
|
||||
} as GasPrices;
|
||||
})
|
||||
}),
|
||||
catchError(err => throwError(() => new Error(err))),
|
||||
retry(2)
|
||||
);
|
||||
|
||||
export { averageGasPricesObservable };
|
Loading…
Reference in New Issue
Block a user