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';
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
class Mining {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
2022-02-08 10:28:53 +01:00
|
|
|
private getSqlInterval(interval: string | null): string | null {
|
|
|
|
switch (interval) {
|
|
|
|
case '24h': return '1 DAY';
|
|
|
|
case '3d': return '3 DAY';
|
|
|
|
case '1w': return '1 WEEK';
|
|
|
|
case '1m': return '1 MONTH';
|
|
|
|
case '3m': return '3 MONTH';
|
|
|
|
case '6m': return '6 MONTH';
|
|
|
|
case '1y': return '1 YEAR';
|
|
|
|
case '2y': return '2 YEAR';
|
|
|
|
case '3y': return '3 YEAR';
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 11:59:33 +01:00
|
|
|
/**
|
|
|
|
* Generate high level overview of the pool ranks and general stats
|
|
|
|
*/
|
2022-01-25 10:33:46 +01:00
|
|
|
public async $getPoolsStats(interval: string | null) : Promise<object> {
|
2022-02-08 10:28:53 +01:00
|
|
|
const sqlInterval = this.getSqlInterval(interval);
|
2022-01-25 10:33:46 +01:00
|
|
|
|
2022-01-18 09:37:04 +01:00
|
|
|
const poolsStatistics = {};
|
2022-01-06 11:59:33 +01:00
|
|
|
|
2022-01-25 10:33:46 +01:00
|
|
|
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(sqlInterval);
|
|
|
|
const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$countEmptyBlocks(sqlInterval);
|
2022-01-06 11:59:33 +01:00
|
|
|
|
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-25 10:33:46 +01:00
|
|
|
poolsStatistics['pools'] = poolsStats;
|
|
|
|
|
|
|
|
const oldestBlock = new Date(await BlocksRepository.$oldestBlockTimestamp());
|
|
|
|
poolsStatistics['oldestIndexedBlockTimestamp'] = oldestBlock.getTime();
|
|
|
|
|
|
|
|
const blockCount: number = await BlocksRepository.$blockCount(sqlInterval);
|
2022-01-18 09:37:04 +01:00
|
|
|
poolsStatistics['blockCount'] = blockCount;
|
2022-01-25 10:33:46 +01:00
|
|
|
|
|
|
|
const blockHeightTip = await bitcoinClient.getBlockCount();
|
|
|
|
const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(120, blockHeightTip);
|
2022-01-18 09:37:04 +01:00
|
|
|
poolsStatistics['lastEstimatedHashrate'] = lastBlockHashrate;
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
return poolsStatistics;
|
|
|
|
}
|
2022-02-08 10:28:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all mining pool stats for a pool
|
|
|
|
*/
|
|
|
|
public async $getPoolStat(interval: string | null, poolId: number): Promise<object> {
|
|
|
|
const pool = await PoolsRepository.$getPool(poolId);
|
|
|
|
if (!pool) {
|
|
|
|
throw new Error("This mining pool does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
const sqlInterval = this.getSqlInterval(interval);
|
|
|
|
const blocks = await BlocksRepository.$getBlocksByPool(sqlInterval, poolId);
|
|
|
|
|
|
|
|
return {
|
|
|
|
pool: pool,
|
|
|
|
blocks: blocks,
|
|
|
|
};
|
|
|
|
}
|
2022-01-06 11:59:33 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 10:33:46 +01:00
|
|
|
export default new Mining();
|