Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './tasks/updateIpfsFolder'
import './tasks/cancelProposal'
import './tasks/withdrawTreasury'
import './tasks/airdrop'
import './tasks/getAvgNewBlockTime'

dotent.config()

Expand Down Expand Up @@ -45,14 +46,14 @@ const config: HardhatUserConfig = {
url: 'https://public-node.rsk.co/',
...(typeof process.env.MAINNET_DEPLOYER_MNEMONIC !== 'undefined'
? {
accounts: {
mnemonic: process.env.MAINNET_DEPLOYER_MNEMONIC,
path: derivationPath,
},
}
accounts: {
mnemonic: process.env.MAINNET_DEPLOYER_MNEMONIC,
path: derivationPath,
},
}
: {
accounts,
}),
accounts,
}),
},
},
etherscan: {
Expand Down
34 changes: 34 additions & 0 deletions tasks/getAvgNewBlockTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { task } from 'hardhat/config'
import { HardhatRuntimeEnvironment } from 'hardhat/types'

task('average-block-time', 'Calculates the average time for adding new blocks')
.addParam('referenceBlockQuantity', 'Number of previous blocks to be considered for the calculation')
.setAction(async ({ referenceBlockQuantity }, hre: HardhatRuntimeEnvironment) => {
const provider = hre.ethers.provider
const currentBlock = await provider.getBlock('latest')

if (!currentBlock) {
console.log('Error fetching the current block.')
return
}

const referenceQuantity = parseInt(referenceBlockQuantity, 10)
const previousBlockNumber = currentBlock.number - referenceQuantity

if (previousBlockNumber < 0) {
console.log('The reference block quantity is too high relative to the current block number.')
return
}

const previousBlock = await provider.getBlock(previousBlockNumber)

if (!previousBlock) {
console.log('Error fetching the previous block.')
return
}

const timeDiff = currentBlock.timestamp - previousBlock.timestamp
const averageBlockTime = timeDiff / referenceQuantity

console.log(`Average time for adding new blocks: ${averageBlockTime.toFixed(2)} seconds`)
})