2022-01-14 18:09:40 +09:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { map } from 'rxjs/operators';
|
2022-01-18 17:37:04 +09:00
|
|
|
import { PoolsStats, SinglePoolStats } from '../interfaces/node-api.interface';
|
2022-01-14 18:09:40 +09:00
|
|
|
import { ApiService } from '../services/api.service';
|
2022-01-18 17:37:04 +09:00
|
|
|
import { StateService } from './state.service';
|
|
|
|
|
|
|
|
export interface MiningUnits {
|
|
|
|
hashrateDivider: number,
|
|
|
|
hashrateUnit: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MiningStats {
|
|
|
|
lastEstimatedHashrate: string,
|
|
|
|
blockCount: number,
|
|
|
|
totalEmptyBlock: number,
|
|
|
|
totalEmptyBlockRatio: string,
|
|
|
|
pools: SinglePoolStats[],
|
|
|
|
miningUnits: MiningUnits,
|
|
|
|
}
|
2022-01-14 18:09:40 +09:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class MiningService {
|
|
|
|
|
|
|
|
constructor(
|
2022-01-18 17:37:04 +09:00
|
|
|
private stateService: StateService,
|
2022-01-14 18:09:40 +09:00
|
|
|
private apiService: ApiService,
|
|
|
|
) { }
|
|
|
|
|
|
|
|
public getMiningStats(interval: string): Observable<MiningStats> {
|
|
|
|
return this.apiService.listPools$(interval).pipe(
|
|
|
|
map(pools => this.generateMiningStats(pools))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:37:04 +09:00
|
|
|
/**
|
|
|
|
* Set the hashrate power of ten we want to display
|
|
|
|
*/
|
|
|
|
public getMiningUnits() : MiningUnits {
|
|
|
|
const powerTable = {
|
|
|
|
0: "H/s",
|
|
|
|
3: "kH/s",
|
|
|
|
6: "MH/s",
|
|
|
|
9: "GH/s",
|
|
|
|
12: "TH/s",
|
|
|
|
15: "PH/s",
|
|
|
|
18: "EH/s",
|
|
|
|
};
|
|
|
|
|
|
|
|
// I think it's fine to hardcode this since we don't have x1000 hashrate jump everyday
|
|
|
|
// If we want to support the mining dashboard for testnet, we can hardcode it too
|
|
|
|
let selectedPower = 15;
|
|
|
|
if (this.stateService.network === 'testnet') {
|
|
|
|
selectedPower = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
hashrateDivider: Math.pow(10, selectedPower),
|
|
|
|
hashrateUnit: powerTable[selectedPower],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-14 18:09:40 +09:00
|
|
|
private generateMiningStats(stats: PoolsStats) : MiningStats {
|
2022-01-18 17:37:04 +09:00
|
|
|
const miningUnits = this.getMiningUnits();
|
|
|
|
const hashrateDivider = miningUnits.hashrateDivider;
|
|
|
|
|
2022-01-14 18:09:40 +09:00
|
|
|
const totalEmptyBlock = Object.values(stats.pools).reduce((prev, cur) => {
|
|
|
|
return prev + cur.emptyBlocks;
|
|
|
|
}, 0);
|
|
|
|
const totalEmptyBlockRatio = (totalEmptyBlock / stats.blockCount * 100).toFixed(2);
|
|
|
|
const poolsStats = stats.pools.map((poolStat) => {
|
|
|
|
return {
|
|
|
|
share: (poolStat.blockCount / stats.blockCount * 100).toFixed(2),
|
2022-01-18 17:37:04 +09:00
|
|
|
lastEstimatedHashrate: (poolStat.blockCount / stats.blockCount * stats.lastEstimatedHashrate / hashrateDivider).toFixed(2),
|
2022-01-14 18:09:40 +09:00
|
|
|
emptyBlockRatio: (poolStat.emptyBlocks / poolStat.blockCount * 100).toFixed(2),
|
2022-01-17 15:34:34 +09:00
|
|
|
logo: `./resources/mining-pools/` + poolStat.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg',
|
2022-01-14 18:09:40 +09:00
|
|
|
...poolStat
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2022-01-18 17:37:04 +09:00
|
|
|
lastEstimatedHashrate: (stats.lastEstimatedHashrate / hashrateDivider).toFixed(2),
|
2022-01-14 18:09:40 +09:00
|
|
|
blockCount: stats.blockCount,
|
|
|
|
totalEmptyBlock: totalEmptyBlock,
|
|
|
|
totalEmptyBlockRatio: totalEmptyBlockRatio,
|
|
|
|
pools: poolsStats,
|
2022-01-18 17:37:04 +09:00
|
|
|
miningUnits: miningUnits,
|
2022-01-14 18:09:40 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|