mirror of
https://github.com/mempool/mempool.git
synced 2025-03-15 12:20:28 +01:00
[mining] add /api/v1/mining/pools API to list mining pools
This commit is contained in:
parent
b977c4332f
commit
e4c17e5011
2 changed files with 42 additions and 4 deletions
|
@ -12,6 +12,7 @@ import PricesRepository from '../../repositories/PricesRepository';
|
||||||
class MiningRoutes {
|
class MiningRoutes {
|
||||||
public initRoutes(app: Application) {
|
public initRoutes(app: Application) {
|
||||||
app
|
app
|
||||||
|
.get(config.MEMPOOL.API_URL_PREFIX + 'mining/pools', this.$listPools)
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'mining/pools/:interval', this.$getPools)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'mining/pools/:interval', this.$getPools)
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'mining/pool/:slug/hashrate', this.$getPoolHistoricalHashrate)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'mining/pool/:slug/hashrate', this.$getPoolHistoricalHashrate)
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'mining/pool/:slug/blocks', this.$getPoolBlocks)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'mining/pool/:slug/blocks', this.$getPoolBlocks)
|
||||||
|
@ -88,6 +89,29 @@ class MiningRoutes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async $listPools(req: Request, res: Response): Promise<void> {
|
||||||
|
try {
|
||||||
|
res.header('Pragma', 'public');
|
||||||
|
res.header('Cache-control', 'public');
|
||||||
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
||||||
|
|
||||||
|
const pools = await mining.$listPools();
|
||||||
|
if (!pools) {
|
||||||
|
res.status(500).end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.header('X-total-count', pools.length.toString());
|
||||||
|
if (pools.length === 0) {
|
||||||
|
res.status(204).send();
|
||||||
|
} else {
|
||||||
|
res.json(pools);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
res.status(500).send(e instanceof Error ? e.message : e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async $getPools(req: Request, res: Response) {
|
private async $getPools(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const stats = await mining.$getPoolsStats(req.params.interval);
|
const stats = await mining.$getPoolsStats(req.params.interval);
|
||||||
|
|
|
@ -595,6 +595,20 @@ class Mining {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List existing mining pools
|
||||||
|
*/
|
||||||
|
public async $listPools(): Promise<{name: string, slug: string, unique_id: number}[] | null> {
|
||||||
|
const [rows] = await database.query(`
|
||||||
|
SELECT
|
||||||
|
name,
|
||||||
|
slug,
|
||||||
|
unique_id
|
||||||
|
FROM pools`
|
||||||
|
);
|
||||||
|
return rows as {name: string, slug: string, unique_id: number}[];
|
||||||
|
}
|
||||||
|
|
||||||
private getDateMidnight(date: Date): Date {
|
private getDateMidnight(date: Date): Date {
|
||||||
date.setUTCHours(0);
|
date.setUTCHours(0);
|
||||||
date.setUTCMinutes(0);
|
date.setUTCMinutes(0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue