Skip to content

Commit

Permalink
feat: created a task for calculate the avg time for new blocks added
Browse files Browse the repository at this point in the history
  • Loading branch information
bezerrablockchain committed Nov 5, 2024
1 parent 4aae0af commit 0d446af
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
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
38 changes: 38 additions & 0 deletions tasks/getAvgNewBlockTime.ts
Original file line number Diff line number Diff line change
@@ -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`);
});

0 comments on commit 0d446af

Please sign in to comment.