Generate mining basic pool ranking (sorted by block found) for a specified timeframe
This commit is contained in:
53
backend/src/api/mining.ts
Normal file
53
backend/src/api/mining.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
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 {
|
||||
private bitcoinApi: BitcoinApi;
|
||||
|
||||
constructor() {
|
||||
this.bitcoinApi = new BitcoinApi(bitcoinClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate high level overview of the pool ranks and general stats
|
||||
*/
|
||||
public async $getPoolsStats(interval: string = "100 YEAR") : Promise<object> {
|
||||
let poolsStatistics = {};
|
||||
|
||||
const lastBlockHashrate = await this.bitcoinApi.$getEstimatedHashrate(717960);
|
||||
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
|
||||
const blockCount: number = await BlocksRepository.$blockCount(interval);
|
||||
const emptyBlocks: EmptyBlocks[] = await BlocksRepository.$countEmptyBlocks(interval);
|
||||
|
||||
let poolsStats: PoolStats[] = [];
|
||||
let rank = 1;
|
||||
|
||||
poolsInfo.forEach((poolInfo: PoolInfo) => {
|
||||
let 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["poolsStats"] = poolsStats;
|
||||
poolsStatistics["lastEstimatedHashrate"] = lastBlockHashrate;
|
||||
|
||||
return poolsStatistics;
|
||||
}
|
||||
}
|
||||
|
||||
export default new Mining();
|
||||
Reference in New Issue
Block a user