diff --git a/hardhat.config.ts b/hardhat.config.ts index 6406e22..510be70 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -5,6 +5,7 @@ import './tasks/updateIpfsFolder' import './tasks/cancelProposal' import './tasks/withdrawTreasury' import './tasks/airdrop' +import './tasks/getAvgNewBlockTime' dotent.config() @@ -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: { diff --git a/tasks/getAvgNewBlockTime.ts b/tasks/getAvgNewBlockTime.ts new file mode 100644 index 0000000..9f81967 --- /dev/null +++ b/tasks/getAvgNewBlockTime.ts @@ -0,0 +1,38 @@ +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); + if (!currentBlock) { + console.log("Error fetching the current block."); + return; + } + 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`); + });