Merge pull request #4111 from mempool/nymkappa/mining-pool-api
[mining] add /api/v1/pools API to list mining pools
This commit is contained in:
		
						commit
						8d7c04bcda
					
				@ -12,6 +12,7 @@ import PricesRepository from '../../repositories/PricesRepository';
 | 
			
		||||
class MiningRoutes {
 | 
			
		||||
  public initRoutes(app: Application) {
 | 
			
		||||
    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/pool/:slug/hashrate', this.$getPoolHistoricalHashrate)
 | 
			
		||||
      .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) {
 | 
			
		||||
    try {
 | 
			
		||||
      const stats = await mining.$getPoolsStats(req.params.interval);
 | 
			
		||||
 | 
			
		||||
@ -26,7 +26,7 @@ class Mining {
 | 
			
		||||
  /**
 | 
			
		||||
   * Get historical blocks health
 | 
			
		||||
   */
 | 
			
		||||
   public async $getBlocksHealthHistory(interval: string | null = null): Promise<any> {
 | 
			
		||||
  public async $getBlocksHealthHistory(interval: string | null = null): Promise<any> {
 | 
			
		||||
    return await BlocksAuditsRepository.$getBlocksHealthHistory(
 | 
			
		||||
      this.getTimeRange(interval),
 | 
			
		||||
      Common.getSqlInterval(interval)
 | 
			
		||||
@ -56,7 +56,7 @@ class Mining {
 | 
			
		||||
  /**
 | 
			
		||||
   * Get historical block fee rates percentiles
 | 
			
		||||
   */
 | 
			
		||||
   public async $getHistoricalBlockFeeRates(interval: string | null = null): Promise<any> {
 | 
			
		||||
  public async $getHistoricalBlockFeeRates(interval: string | null = null): Promise<any> {
 | 
			
		||||
    return await BlocksRepository.$getHistoricalBlockFeeRates(
 | 
			
		||||
      this.getTimeRange(interval),
 | 
			
		||||
      Common.getSqlInterval(interval)
 | 
			
		||||
@ -66,7 +66,7 @@ class Mining {
 | 
			
		||||
  /**
 | 
			
		||||
   * Get historical block sizes
 | 
			
		||||
   */
 | 
			
		||||
   public async $getHistoricalBlockSizes(interval: string | null = null): Promise<any> {
 | 
			
		||||
  public async $getHistoricalBlockSizes(interval: string | null = null): Promise<any> {
 | 
			
		||||
    return await BlocksRepository.$getHistoricalBlockSizes(
 | 
			
		||||
      this.getTimeRange(interval),
 | 
			
		||||
      Common.getSqlInterval(interval)
 | 
			
		||||
@ -76,7 +76,7 @@ class Mining {
 | 
			
		||||
  /**
 | 
			
		||||
   * Get historical block weights
 | 
			
		||||
   */
 | 
			
		||||
   public async $getHistoricalBlockWeights(interval: string | null = null): Promise<any> {
 | 
			
		||||
  public async $getHistoricalBlockWeights(interval: string | null = null): Promise<any> {
 | 
			
		||||
    return await BlocksRepository.$getHistoricalBlockWeights(
 | 
			
		||||
      this.getTimeRange(interval),
 | 
			
		||||
      Common.getSqlInterval(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 {
 | 
			
		||||
    date.setUTCHours(0);
 | 
			
		||||
    date.setUTCMinutes(0);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user