All files / src/Blockfrost builder.ts

18.18% Statements 4/22
100% Branches 0/0
0% Functions 0/5
18.18% Lines 4/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                  35x 35x 35x   35x                                                                              
import {
  CurrentEpochModel,
  FirstUpdateAfterBlock,
  FirstUpdateAfterBlockModel,
  LastRetireModel,
  PoolsModel
} from './types';
import { Logger } from 'ts-log';
import { Pool } from 'pg';
import { contextLogger } from '@cardano-sdk/util';
import { findCurrentEpoch, findFirstUpdateAfterBlock, findLastRetire, findPools } from './queries';
import { mapFirstUpdateAfterBlock } from './mappers';
 
export class BlockfrostCacheBuilder {
  #db: Pool;
  #logger: Logger;
 
  constructor(db: Pool, logger: Logger) {
    this.#db = db;
    this.#logger = contextLogger(logger, 'builder');
  }
 
  async getCurrentEpoch() {
    this.#logger.debug('Going to query current epoch');
    const result = await this.#db.query<CurrentEpochModel>(findCurrentEpoch);
 
    return result.rows[0].epoch_no;
  }
 
  async getFirstUpdateAfterBlock(id: string, blockNo: number): Promise<FirstUpdateAfterBlock | undefined> {
    this.#logger.debug(`Going to query first update for pool ${id} after block ${blockNo}`);
    const result = await this.#db.query<FirstUpdateAfterBlockModel>(findFirstUpdateAfterBlock, [id, blockNo]);
 
    return result.rows.map(mapFirstUpdateAfterBlock)[0];
  }
 
  async getLastRetire(id: string): Promise<LastRetireModel | undefined> {
    this.#logger.debug(`Going to query last retire for pool ${id}`);
    const result = await this.#db.query<LastRetireModel>(findLastRetire, [id]);
 
    return result.rows[0];
  }
 
  async getPools(cacheTtl: number) {
    const since = Date.now() - cacheTtl * 60_000;
    this.#logger.debug('Going to query stake pools to be refreshed');
    const result = await this.#db.query<PoolsModel>(findPools, [since]);
    this.#logger.debug('Stake pools to refresh', result.rowCount);
 
    return result.rows;
  }
}