diff --git a/backend/package-lock.json b/backend/package-lock.json index 50f260804..815cee5bf 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -2820,8 +2820,7 @@ "ws": { "version": "7.4.6", "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.6.tgz", - "integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==", - "requires": {} + "integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==" }, "yallist": { "version": "4.0.0", diff --git a/backend/src/index.ts b/backend/src/index.ts index ec9c53cab..5381790ac 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -248,6 +248,7 @@ class Server { .get(config.MEMPOOL.API_URL_PREFIX + 'address/:address/txs', routes.getAddressTransactions) .get(config.MEMPOOL.API_URL_PREFIX + 'address/:address/txs/chain/:txId', routes.getAddressTransactions) .get(config.MEMPOOL.API_URL_PREFIX + 'address-prefix/:prefix', routes.getAddressPrefix) + .get(config.MEMPOOL.API_URL_PREFIX + 'difficulty-adjustment', routes.getDifficultyChange) ; } } diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 7d4bd844e..a40a23895 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -666,6 +666,46 @@ class Routes { public getTransactionOutspends(req: Request, res: Response) { res.status(501).send('Not implemented'); } + + public getDifficultyChange(req: Request, res: Response) { + try { + const now = new Date().getTime() / 1000; + const DATime=blocks.getLastDifficultyAdjustmentTime(); + const diff = now - DATime; + const blockHeight=blocks.getCurrentBlockHeight(); + const blocksInEpoch = blockHeight % 2016; + const estimatedBlocks = Math.round(diff / 60 / 10); + const difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100; + + const timeAvgDiff = difficultyChange * 0.1; + + let timeAvgMins = 10; + 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={ + progressPercent, + difficultyChange, + estimatedRetargetDate, + remainingBlocks, + remainingTime, + } + res.json(result); + + } catch (e) { + res.status(500).send(e.message || e); + } + } } export default new Routes(); diff --git a/frontend/angular.json b/frontend/angular.json index 3a2c2d94c..05b784aea 100644 --- a/frontend/angular.json +++ b/frontend/angular.json @@ -1,5 +1,8 @@ { "$schema": "./node_modules/@angular/cli/lib/config/schema.json", + "cli": { + "analytics": "ed0ff723-05da-4a22-b43d-692b797b5243" + }, "version": 1, "newProjectRoot": "projects", "projects": { diff --git a/frontend/src/app/components/api-docs/api-docs.component.html b/frontend/src/app/components/api-docs/api-docs.component.html index 5d4ab2faa..9c7fe0e65 100644 --- a/frontend/src/app/components/api-docs/api-docs.component.html +++ b/frontend/src/app/components/api-docs/api-docs.component.html @@ -823,6 +823,23 @@ +
  • + Difficulty + +
    + +
    +
    Description
    +
    Returns details about difficulty adjustment.
    +
    + +
    +
    +
  • +
    diff --git a/frontend/src/app/components/api-docs/api-docs.component.scss b/frontend/src/app/components/api-docs/api-docs.component.scss index c8a938e66..2fbadd123 100644 --- a/frontend/src/app/components/api-docs/api-docs.component.scss +++ b/frontend/src/app/components/api-docs/api-docs.component.scss @@ -69,4 +69,8 @@ li.nav-item { } .websocket { padding: 15px; +} + +.difficulty { + padding: 15px; } \ No newline at end of file diff --git a/frontend/src/app/components/api-docs/api-docs.component.ts b/frontend/src/app/components/api-docs/api-docs.component.ts index ce40a8adb..21ca2d2ae 100644 --- a/frontend/src/app/components/api-docs/api-docs.component.ts +++ b/frontend/src/app/components/api-docs/api-docs.component.ts @@ -1125,7 +1125,24 @@ responseSample: `{ curl: ``, }, responseSample: ``, - } + }, + + difficulty: { + codeSample: { + esModule:``, + commonJS:``, + curl: `curl -X GET "https://mempool.space/api/difficulty-adjustment"`, + }, + responseSample: `{ + progressPercent: 18.55392610846515, + difficultyChange: -1.2440503501069622, + estimatedRetargetDate: 1627400849.2000492, + remainingBlocks: 1642, + remainingTime: 997456.3840492539 + +}`, + }, + }; constructor( diff --git a/frontend/src/app/components/api-docs/code-template.component.scss b/frontend/src/app/components/api-docs/code-template.component.scss index 714f7325c..448ddc6aa 100644 --- a/frontend/src/app/components/api-docs/code-template.component.scss +++ b/frontend/src/app/components/api-docs/code-template.component.scss @@ -71,6 +71,10 @@ li.nav-item { padding: 15px; } +.difficulty { + padding: 15px; +} + .links { margin-bottom: 5px; a {