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'; class Mining { constructor() { } /** * Generate high level overview of the pool ranks and general stats */ public async $getPoolsStats(interval: string = '100 YEAR') : Promise { const poolsStatistics = {}; const blockHeightTip = await bitcoinClient.getBlockCount(); const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(120, blockHeightTip); const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval); const blockCount: number = await BlocksRepository.$blockCount(interval); const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$countEmptyBlocks(interval); const poolsStats: PoolStats[] = []; let rank = 1; poolsInfo.forEach((poolInfo: PoolInfo) => { const poolStat: PoolStats = { 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); }); poolsStatistics['blockCount'] = blockCount; poolsStatistics['lastEstimatedHashrate'] = lastBlockHashrate; poolsStatistics['pools'] = poolsStats; return poolsStatistics; } } export default new Mining();