All files / src/DRepProvider BlockfrostDRepProvider.ts

75% Statements 12/16
50% Branches 1/2
50% Functions 2/4
80% Lines 12/15

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  17x 17x       17x   1x       1x 1x 1x 1x 1x 1x 1x   1x                                  
import { BlockfrostClient } from '../blockfrost/BlockfrostClient';
import { BlockfrostProvider } from '../blockfrost/BlockfrostProvider';
import { Cardano, DRepInfo, DRepProvider, GetDRepInfoArgs, GetDRepsInfoArgs } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import type { Responses } from '@blockfrost/blockfrost-js';
 
export class BlockfrostDRepProvider extends BlockfrostProvider implements DRepProvider {
  constructor(client: BlockfrostClient, logger: Logger) {
    super(client, logger);
  }
 
  async getDRepInfo({ id }: GetDRepInfoArgs): Promise<DRepInfo> {
    try {
      const cip105DRepId = Cardano.DRepID.toCip105DRepID(id); // Blockfrost only supports CIP-105 DRep IDs
      const response = await this.request<Responses['drep']>(`governance/dreps/${cip105DRepId.toString()}`);
      const amount = BigInt(response.amount);
      const activeEpoch = response.active_epoch ? Cardano.EpochNo(response.active_epoch) : undefined;
      const active = response.active;
      const hasScript = response.has_script;
 
      return {
        active,
        activeEpoch,
        amount,
        hasScript,
        id
      };
    } catch (error) {
      this.logger.error('getDRep failed', id);
      throw this.toProviderError(error);
    }
  }
 
  getDRepsInfo({ ids }: GetDRepsInfoArgs): Promise<DRepInfo[]> {
    return Promise.all(ids.map((id) => this.getDRepInfo({ id })));
  }
}