From 0d446af09c61aa1244a8627e7361ad51c713f5ed Mon Sep 17 00:00:00 2001
From: bezerrablockchain <bezerrablockchain@gmail.com>
Date: Mon, 4 Nov 2024 20:13:51 -0400
Subject: [PATCH] feat: created a task for calculate the avg time for new
 blocks added

---
 hardhat.config.ts           | 15 ++++++++-------
 tasks/getAvgNewBlockTime.ts | 38 +++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 7 deletions(-)
 create mode 100644 tasks/getAvgNewBlockTime.ts

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`);
+    });