All files / src/Rewards/DbSyncRewardProvider RewardsBuilder.ts

100% Statements 14/14
75% Branches 3/4
100% Functions 4/4
100% Lines 13/13

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          35x   35x 3x 3x     3x 3x       5x 5x 5x       11x 11x 11x       11x      
import { AccountBalanceModel, RewardEpochModel } from './types';
import { Cardano } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
import { Range } from '@cardano-sdk/util';
import { findAccountBalance, findRewardsHistory } from './queries';
 
export class RewardsBuilder {
  #db: Pool;
  #logger: Logger;
 
  constructor(db: Pool, logger: Logger) {
    this.#db = db;
    this.#logger = logger;
  }
 
  public async getAccountBalance(rewardAccount: Cardano.RewardAccount) {
    this.#logger.debug('About to run findAccountBalance query');
    const result: QueryResult<AccountBalanceModel> = await this.#db.query(findAccountBalance, [rewardAccount]);
    return result.rows.length > 0 ? result.rows[0] : undefined;
  }
 
  public async getRewardsHistory(rewardAccounts: Cardano.RewardAccount[], epochs?: Range<Cardano.EpochNo>) {
    const params: (string[] | number)[] = [rewardAccounts.map((rewardAcc) => rewardAcc)];
    this.#logger.debug('About to run findRewardsHistory query');
    const result: QueryResult<RewardEpochModel> = await this.#db.query(
      findRewardsHistory(epochs?.lowerBound, epochs?.upperBound),
      params
    );
    return result.rows.length > 0 ? result.rows : [];
  }
}