2022-01-18 09:37:04 +01:00
|
|
|
import { PoolInfo, PoolStats } from '../mempool.interfaces';
|
|
|
|
import BlocksRepository, { EmptyBlocks } from '../repositories/BlocksRepository';
|
|
|
|
import PoolsRepository from '../repositories/PoolsRepository';
|
|
|
|
import bitcoinClient from './bitcoin/bitcoin-client';
|
|
|
|
import BitcoinApi from './bitcoin/bitcoin-api';
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
class Mining {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate high level overview of the pool ranks and general stats
|
|
|
|
*/
|
2022-01-18 09:37:04 +01:00
|
|
|
public async $getPoolsStats(interval: string = '100 YEAR') : Promise<object> {
|
|
|
|
const poolsStatistics = {};
|
2022-01-06 11:59:33 +01:00
|
|
|
|
2022-01-18 09:37:04 +01:00
|
|
|
const blockHeightTip = await bitcoinClient.getBlockCount();
|
|
|
|
const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(120, blockHeightTip);
|
2022-01-06 11:59:33 +01:00
|
|
|
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
|
|
|
|
const blockCount: number = await BlocksRepository.$blockCount(interval);
|
|
|
|
const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$countEmptyBlocks(interval);
|
|
|
|
|
2022-01-18 09:37:04 +01:00
|
|
|
const poolsStats: PoolStats[] = [];
|
2022-01-06 11:59:33 +01:00
|
|
|
let rank = 1;
|
|
|
|
|
|
|
|
poolsInfo.forEach((poolInfo: PoolInfo) => {
|
2022-01-18 09:37:04 +01:00
|
|
|
const poolStat: PoolStats = {
|
2022-01-06 11:59:33 +01:00
|
|
|
poolId: poolInfo.poolId, // mysql row id
|
|
|
|
name: poolInfo.name,
|
|
|
|
link: poolInfo.link,
|
|
|
|
blockCount: poolInfo.blockCount,
|
|
|
|
rank: rank++,
|
|
|
|
emptyBlocks: 0,
|
|
|
|
}
|
|
|
|
for (let i = 0; i < emptyBlocks.length; ++i) {
|
|
|
|
if (emptyBlocks[i].poolId === poolInfo.poolId) {
|
|
|
|
poolStat.emptyBlocks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
poolsStats.push(poolStat);
|
2022-01-18 09:37:04 +01:00
|
|
|
});
|
2022-01-06 11:59:33 +01:00
|
|
|
|
2022-01-18 09:37:04 +01:00
|
|
|
poolsStatistics['blockCount'] = blockCount;
|
|
|
|
poolsStatistics['lastEstimatedHashrate'] = lastBlockHashrate;
|
|
|
|
poolsStatistics['pools'] = poolsStats;
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
return poolsStatistics;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new Mining();
|