-
Notifications
You must be signed in to change notification settings - Fork 3
/
analyzeBaseFeeSmooth.js
55 lines (44 loc) · 1.52 KB
/
analyzeBaseFeeSmooth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const ethers = require('ethers')
const compact = require('lodash.compact')
const BigNumber = require('bignumber.js')
const START_BLOCK = parseInt(process.env.START_BLOCK, 10)
const END_BLOCK = parseInt(process.env.END_BLOCK, 10)
const TOTAL_BLOCKS = END_BLOCK - START_BLOCK
async function main () {
const provider = new ethers.providers.WebSocketProvider(process.env.POLYGON_RPC_WS)
const baseFees = []
for (let i = START_BLOCK; i < END_BLOCK; i++) {
process.stdout.clearLine()
process.stdout.cursorTo(0)
process.stdout.write(chalk.blue.underline(`${i - START_BLOCK + 1}/${TOTAL_BLOCKS} Checking block ${i}`))
const block = await provider.getBlock(i)
baseFees.push([
block.number,
BigNumber(block.baseFeePerGas.toString())
])
}
const grouped = []
for (let i = 0; i < baseFees.length; i = i + 5) {
if (!baseFees[i]) continue
let fees = []
for (let j = 0; j < 5; j++) {
fees.push(baseFees[i + j][1])
}
fees = compact(fees)
const feeAvg = BigNumber.sum(...fees).div(fees.length).toFixed()
grouped.push([baseFees[i][0], feeAvg])
}
const baseFeesCsv = [
'block,avg5basefee',
...grouped.map(b => b.join(','))
].join('\n')
const fp = path.join(__dirname, 'out', 'basefeesmooth', `${START_BLOCK}-${END_BLOCK}.csv`)
fs.writeFileSync(fp, baseFeesCsv, 'utf-8')
console.log('')
console.log(chalk.green(`Done! Wrote to ${fp}`))
provider._websocket.close()
}
main()