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 {
|
2022-01-21 11:17:36 +09:00
|
|
|
hashrateDivider: number;
|
|
|
|
hashrateUnit: string;
|
2022-01-18 17:37:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface MiningStats {
|
2022-01-21 11:17:36 +09:00
|
|
|
lastEstimatedHashrate: string;
|
|
|
|
blockCount: number;
|
|
|
|
totalEmptyBlock: number;
|
|
|
|
totalEmptyBlockRatio: string;
|
|
|
|
pools: SinglePoolStats[];
|
|
|
|
miningUnits: MiningUnits;
|
2022-01-25 18:33:46 +09:00
|
|
|
availableTimespanDay: number;
|
2022-01-18 17:37:04 +09:00
|
|
|
}
|
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
|
|
|
|
*/
|
2022-01-21 11:17:36 +09:00
|
|
|
public getMiningUnits(): MiningUnits {
|
2022-01-18 17:37:04 +09:00
|
|
|
const powerTable = {
|
2022-01-21 11:17:36 +09:00
|
|
|
0: 'H/s',
|
|
|
|
3: 'kH/s',
|
|
|
|
6: 'MH/s',
|
|
|
|
9: 'GH/s',
|
|
|
|
12: 'TH/s',
|
|
|
|
15: 'PH/s',
|
|
|
|
18: 'EH/s',
|
2022-01-18 17:37:04 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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-21 11:17:36 +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
|
2022-01-21 11:17:36 +09:00
|
|
|
};
|
2022-01-14 18:09:40 +09:00
|
|
|
});
|
|
|
|
|
2022-01-25 18:33:46 +09:00
|
|
|
const availableTimespanDay = (
|
2022-02-22 15:50:14 +09:00
|
|
|
(new Date().getTime() / 1000) - (stats.oldestIndexedBlockTimestamp)
|
2022-01-25 18:33:46 +09:00
|
|
|
) / 3600 / 24;
|
|
|
|
|
2022-01-14 18:09:40 +09:00
|
|
|
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-25 18:33:46 +09:00
|
|
|
availableTimespanDay: availableTimespanDay,
|
2022-01-14 18:09:40 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|