Expose the difficulty adjustment information in an API #622 (#628)

* Added difficulty adjustment information API

* Added Difficulty API in API docs frontend

* Added link to API

* Updated the API implementation of difficulty-adjustment

* Updated API End Point in frontend

* Updated sample API response in frontend
This commit is contained in:
Priyansh
2021-07-19 03:15:45 +05:30
committed by GitHub
parent ad08c3a907
commit 9e0a5300b0
8 changed files with 88 additions and 3 deletions

View File

@@ -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",

View File

@@ -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)
;
}
}

View File

@@ -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();