API method for node fee histogram data
This commit is contained in:
		
							parent
							
								
									7bdde13b40
								
							
						
					
					
						commit
						f4df51dd21
					
				@ -129,6 +129,56 @@ class NodesApi {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async $getFeeHistogram(node_public_key: string): Promise<unknown> {
 | 
			
		||||
    try {
 | 
			
		||||
      const inQuery = `
 | 
			
		||||
        SELECT CASE WHEN fee_rate <= 10.0 THEN CEIL(fee_rate)
 | 
			
		||||
                    WHEN (fee_rate > 10.0 and fee_rate <= 100.0) THEN CEIL(fee_rate / 10.0) * 10.0
 | 
			
		||||
                    WHEN (fee_rate > 100.0 and fee_rate <= 1000.0) THEN CEIL(fee_rate / 100.0) * 100.0
 | 
			
		||||
                    WHEN fee_rate > 1000.0 THEN CEIL(fee_rate / 1000.0) * 1000.0
 | 
			
		||||
               END as bucket,
 | 
			
		||||
               count(short_id) as count,
 | 
			
		||||
               sum(capacity) as capacity
 | 
			
		||||
        FROM (
 | 
			
		||||
          SELECT CASE WHEN node1_public_key = ? THEN node2_fee_rate WHEN node2_public_key = ? THEN node1_fee_rate END as fee_rate,
 | 
			
		||||
                 short_id as short_id,
 | 
			
		||||
                 capacity as capacity
 | 
			
		||||
          FROM channels
 | 
			
		||||
          WHERE status = 1 AND (channels.node1_public_key = ? OR channels.node2_public_key = ?)
 | 
			
		||||
        ) as fee_rate_table
 | 
			
		||||
        GROUP BY bucket;
 | 
			
		||||
      `;
 | 
			
		||||
      const [inRows]: any[] = await DB.query(inQuery, [node_public_key, node_public_key, node_public_key, node_public_key]);
 | 
			
		||||
 | 
			
		||||
      const outQuery = `
 | 
			
		||||
        SELECT CASE WHEN fee_rate <= 10.0 THEN CEIL(fee_rate)
 | 
			
		||||
                    WHEN (fee_rate > 10.0 and fee_rate <= 100.0) THEN CEIL(fee_rate / 10.0) * 10.0
 | 
			
		||||
                    WHEN (fee_rate > 100.0 and fee_rate <= 1000.0) THEN CEIL(fee_rate / 100.0) * 100.0
 | 
			
		||||
                    WHEN fee_rate > 1000.0 THEN CEIL(fee_rate / 1000.0) * 1000.0
 | 
			
		||||
               END as bucket,
 | 
			
		||||
               count(short_id) as count,
 | 
			
		||||
               sum(capacity) as capacity
 | 
			
		||||
        FROM (
 | 
			
		||||
          SELECT CASE WHEN node1_public_key = ? THEN node1_fee_rate WHEN node2_public_key = ? THEN node2_fee_rate END as fee_rate,
 | 
			
		||||
                 short_id as short_id,
 | 
			
		||||
                 capacity as capacity
 | 
			
		||||
          FROM channels
 | 
			
		||||
          WHERE status = 1 AND (channels.node1_public_key = ? OR channels.node2_public_key = ?)
 | 
			
		||||
        ) as fee_rate_table
 | 
			
		||||
        GROUP BY bucket;
 | 
			
		||||
      `;
 | 
			
		||||
      const [outRows]: any[] = await DB.query(outQuery, [node_public_key, node_public_key, node_public_key, node_public_key]);
 | 
			
		||||
 | 
			
		||||
      return {
 | 
			
		||||
        incoming: inRows.length > 0 ? inRows : [],
 | 
			
		||||
        outgoing: outRows.length > 0 ? outRows : [],
 | 
			
		||||
      };
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      logger.err(`Cannot get node fee distribution for ${node_public_key}. Reason: ${(e instanceof Error ? e.message : e)}`);
 | 
			
		||||
      throw e;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async $getAllNodes(): Promise<any> {
 | 
			
		||||
    try {
 | 
			
		||||
      const query = `SELECT * FROM nodes`;
 | 
			
		||||
 | 
			
		||||
@ -20,6 +20,7 @@ class NodesRoutes {
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/rankings/connectivity', this.$getTopNodesByChannels)
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/rankings/age', this.$getOldestNodes)
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key/statistics', this.$getHistoricalNodeStats)
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key/fees/histogram', this.$getFeeHistogram)
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key', this.$getNode)
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/group/:name', this.$getNodeGroup)
 | 
			
		||||
    ;
 | 
			
		||||
@ -95,6 +96,22 @@ class NodesRoutes {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private async $getFeeHistogram(req: Request, res: Response) {
 | 
			
		||||
    try {
 | 
			
		||||
      const node = await nodesApi.$getFeeHistogram(req.params.public_key);
 | 
			
		||||
      if (!node) {
 | 
			
		||||
        res.status(404).send('Node not found');
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      res.header('Pragma', 'public');
 | 
			
		||||
      res.header('Cache-control', 'public');
 | 
			
		||||
      res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
 | 
			
		||||
      res.json(node);
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      res.status(500).send(e instanceof Error ? e.message : e);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private async $getNodesRanking(req: Request, res: Response): Promise<void> {
 | 
			
		||||
    try {
 | 
			
		||||
      const topCapacityNodes = await nodesApi.$getTopCapacityNodes(false);
 | 
			
		||||
 | 
			
		||||
@ -53,6 +53,10 @@ export class LightningApiService {
 | 
			
		||||
    return this.httpClient.get<any>(this.apiBasePath + '/api/v1/lightning/nodes/' + publicKey + '/statistics');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getNodeFeeHistogram$(publicKey: string): Observable<any> {
 | 
			
		||||
    return this.httpClient.get<any>(this.apiBasePath + '/api/v1/lightning/nodes/' + publicKey + '/fees/histogram');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getNodesRanking$(): Observable<INodesRanking> {
 | 
			
		||||
    return this.httpClient.get<INodesRanking>(this.apiBasePath + '/api/v1/lightning/nodes/rankings');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user