From 05936f82bde18f299b85289a99f5114b15a5893d Mon Sep 17 00:00:00 2001 From: Miguel Medeiros Date: Wed, 25 Aug 2021 21:14:01 -0300 Subject: [PATCH] Refactor getDifficultyChange endpoint. --- backend/src/routes.ts | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/backend/src/routes.ts b/backend/src/routes.ts index cf4caeec8..e6bd416bf 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -693,37 +693,50 @@ class Routes { public getDifficultyChange(req: Request, res: Response) { try { - const now = new Date().getTime() / 1000; const DATime = blocks.getLastDifficultyAdjustmentTime(); const previousRetarget = blocks.getPreviousDifficultyRetarget(); - const diff = now - DATime; const blockHeight = blocks.getCurrentBlockHeight(); + + const now = new Date().getTime() / 1000; + const diff = now - DATime; const blocksInEpoch = blockHeight % 2016; - const difficultyChange = (600 / (diff / blocksInEpoch) - 1) * 100; + const progressPercent = (blocksInEpoch >= 0) ? blocksInEpoch / 2016 * 100 : 100; + const remainingBlocks = 2016 - blocksInEpoch; + const nextRetargetHeight = blockHeight + remainingBlocks; + + let difficultyChange = 0; + if (blocksInEpoch > 0) { + difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100; + } + if (difficultyChange > 300) { + difficultyChange = 300; + } + if (difficultyChange < -75) { + difficultyChange = -75; + } const timeAvgDiff = difficultyChange * 0.1; let timeAvgMins = 10; - if (timeAvgDiff > 0 ){ + if (timeAvgDiff > 0) { timeAvgMins -= Math.abs(timeAvgDiff); } else { timeAvgMins += Math.abs(timeAvgDiff); } - const remainingBlocks = 2016 - blocksInEpoch; - const timeAvgSeconds = timeAvgMins * 60; - const remainingTime = remainingBlocks * timeAvgSeconds; - const estimatedRetargetDate = (remainingTime + now); - const totalTime = estimatedRetargetDate-DATime; - const progressPercent = 100 - ((remainingTime * 100) / totalTime); - - const result={ + const timeAvg = timeAvgMins * 60; + const remainingTime = remainingBlocks * timeAvg; + const estimatedRetargetDate = remainingTime + now; + + const result = { progressPercent, difficultyChange, estimatedRetargetDate, remainingBlocks, remainingTime, previousRetarget, + nextRetargetHeight, + timeAvg, } res.json(result);