All files / src/Rewards/DbSyncRewardProvider DbSyncRewardsProvider.ts

94.11% Statements 16/17
33.33% Branches 1/3
100% Functions 3/3
94.11% Lines 16/17

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 4840x 40x             40x 40x               40x 2x 2x           2x 2x 2x       6x             6x 6x     3x 3x 3x      
import { DbSyncProvider, DbSyncProviderDependencies } from '../../util/DbSyncProvider';
import {
  ProviderError,
  ProviderFailure,
  RewardAccountBalanceArgs,
  RewardsHistoryArgs,
  RewardsProvider
} from '@cardano-sdk/core';
import { RewardsBuilder } from './RewardsBuilder';
import { rewardsToCore } from './mappers';
 
/** Properties that are need to create DbSyncRewardsProvider */
export interface RewardsProviderProps {
  /** Pagination page size limit used for provider methods constraint. */
  paginationPageSizeLimit: number;
}
 
export class DbSyncRewardsProvider extends DbSyncProvider() implements RewardsProvider {
  #builder: RewardsBuilder;
  #paginationPageSizeLimit: number;
 
  constructor(
    { paginationPageSizeLimit }: RewardsProviderProps,
    { cache, dbPools, cardanoNode, logger }: DbSyncProviderDependencies
  ) {
    super({ cache, cardanoNode, dbPools, logger });
    this.#builder = new RewardsBuilder(dbPools.main, logger);
    this.#paginationPageSizeLimit = paginationPageSizeLimit;
  }
 
  public async rewardsHistory({ rewardAccounts, epochs }: RewardsHistoryArgs) {
    Iif (rewardAccounts.length > this.#paginationPageSizeLimit) {
      throw new ProviderError(
        ProviderFailure.BadRequest,
        undefined,
        `Reward accounts count of ${rewardAccounts.length} can not be greater than ${this.#paginationPageSizeLimit}`
      );
    }
    const rewards = await this.#builder.getRewardsHistory(rewardAccounts, epochs);
    return rewardsToCore(rewards);
  }
  public async rewardAccountBalance({ rewardAccount }: RewardAccountBalanceArgs) {
    this.logger.debug(`About to get balance of reward account ${rewardAccount}`);
    const balance = await this.#builder.getAccountBalance(rewardAccount);
    return BigInt(balance?.balance || '0');
  }
}