-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created a task for calculate the avg time for new blocks added
- Loading branch information
1 parent
4aae0af
commit 0d446af
Showing
2 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}); |