All files / src/ChainHistory/DbSyncChainHistory DbSyncChainHistoryProvider.ts

95.52% Statements 64/67
85.71% Branches 18/21
92.3% Functions 12/13
98.27% Lines 57/58

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200  40x   40x                   40x 40x 40x     40x 40x 40x                           40x 2x 2x 2x           2x 2x 2x 2x               9x 1x             8x               8x 8x   8x           8x 8x 8x   8x       13x 1x               12x 12x       19x 19x         19x                           18x                             18x 43x 178x 43x 327x 43x 43x       43x                                   5x 1x             4x 4x 4x 4x   6x 4x 4x 4x   3x 3x         3x 5x           5x        
/* eslint-disable sonarjs/no-nested-template-literals */
import * as Queries from './queries';
import { BlockModel, BlockOutputModel, TipModel, TxModel } from './types';
import {
  BlocksByIdsArgs,
  Cardano,
  ChainHistoryProvider,
  Paginated,
  ProviderError,
  ProviderFailure,
  TransactionsByAddressesArgs,
  TransactionsByIdsArgs
} from '@cardano-sdk/core';
import { ChainHistoryBuilder } from './ChainHistoryBuilder';
import { DB_MAX_SAFE_INTEGER } from './queries';
import { DbSyncProvider, DbSyncProviderDependencies } from '../../util/DbSyncProvider';
import { QueryResult } from 'pg';
import { TxMetadataService } from '../../Metadata';
import { hexStringToBuffer } from '@cardano-sdk/util';
import { mapBlock, mapTxAlonzo, mapTxIn, mapTxOut } from './mappers';
import orderBy from 'lodash/orderBy.js';
 
/** Properties that are need to create DbSyncChainHistoryProvider */
export interface ChainHistoryProviderProps {
  /** Pagination page size limit used for provider methods constraint. */
  paginationPageSizeLimit: number;
}
 
/** Dependencies that are need to create DbSyncChainHistoryProvider */
export interface ChainHistoryProviderDependencies extends DbSyncProviderDependencies {
  /** The TxMetadataService to retrieve transactions metadata by hashes. */
  metadataService: TxMetadataService;
}
 
export class DbSyncChainHistoryProvider extends DbSyncProvider() implements ChainHistoryProvider {
  #paginationPageSizeLimit: number;
  #builder: ChainHistoryBuilder;
  #metadataService: TxMetadataService;
 
  constructor(
    { paginationPageSizeLimit }: ChainHistoryProviderProps,
    { cache, dbPools, cardanoNode, metadataService, logger }: ChainHistoryProviderDependencies
  ) {
    super({ cache, cardanoNode, dbPools, logger });
    this.#builder = new ChainHistoryBuilder(dbPools.main, logger);
    this.#metadataService = metadataService;
    this.#paginationPageSizeLimit = paginationPageSizeLimit;
  }
 
  public async transactionsByAddresses({
    addresses,
    pagination,
    blockRange
  }: TransactionsByAddressesArgs): Promise<Paginated<Cardano.HydratedTx>> {
    if (addresses.length > this.#paginationPageSizeLimit) {
      throw new ProviderError(
        ProviderFailure.BadRequest,
        undefined,
        `Addresses count of ${addresses.length} can not be greater than ${this.#paginationPageSizeLimit}`
      );
    }
 
    Iif (pagination.limit > this.#paginationPageSizeLimit) {
      throw new ProviderError(
        ProviderFailure.BadRequest,
        undefined,
        `Page size of ${pagination.limit} can not be greater than ${this.#paginationPageSizeLimit}`
      );
    }
 
    const lowerBound = blockRange?.lowerBound ?? 0;
    const upperBound = blockRange?.upperBound ?? DB_MAX_SAFE_INTEGER;
 
    this.logger.debug(
      `About to find transactions of addresses ${addresses} ${
        blockRange?.lowerBound ? `since block ${lowerBound}` : ''
      } ${blockRange?.upperBound ? `and before ${upperBound}` : ''}`
    );
 
    const allIds = await this.#builder.queryTxIdsByAddresses(addresses, blockRange);
    const totalResultCount = allIds.length;
    const ids = allIds.splice(pagination.startAt, pagination.limit);
 
    return { pageResults: totalResultCount ? await this.transactionsByIds(ids) : [], totalResultCount };
  }
 
  public async transactionsByHashes({ ids }: TransactionsByIdsArgs): Promise<Cardano.HydratedTx[]> {
    if (ids.length > this.#paginationPageSizeLimit) {
      throw new ProviderError(
        ProviderFailure.BadRequest,
        undefined,
        `Transaction ids count of ${ids.length} can not be greater than ${this.#paginationPageSizeLimit}`
      );
    }
 
    // Conversion tx.hash -> tx.id
    const txRecordIds = await this.#builder.queryTxRecordIdsByTxHashes(ids);
    return this.transactionsByIds(txRecordIds);
  }
 
  private async transactionsByIds(ids: string[]): Promise<Cardano.HydratedTx[]> {
    this.logger.debug('About to find transactions with ids:', ids);
    const txResults: QueryResult<TxModel> = await this.dbPools.main.query({
      name: 'transactions_by_ids',
      text: Queries.findTransactionsByIds,
      values: [ids]
    });
    if (txResults.rows.length === 0) return [];
 
    const [
      inputs,
      outputs,
      mints,
      withdrawals,
      redeemers,
      metadata,
      collaterals,
      certificates,
      collateralOutputs,
      votingProcedures,
      proposalProcedures
    ] = await Promise.all([
      this.#builder.queryTransactionInputsByIds(ids),
      this.#builder.queryTransactionOutputsByIds(ids),
      this.#builder.queryTxMintByIds(ids),
      this.#builder.queryWithdrawalsByTxIds(ids),
      this.#builder.queryRedeemersByIds(ids),
      // Missing witness datums
      this.#metadataService.queryTxMetadataByRecordIds(ids),
      this.#builder.queryTransactionInputsByIds(ids, true),
      this.#builder.queryCertificatesByIds(ids),
      this.#builder.queryTransactionOutputsByIds(ids, true),
      this.#builder.queryVotingProceduresByIds(ids),
      this.#builder.queryProposalProceduresByIds(ids)
    ]);
 
    return txResults.rows.map((tx) => {
      const txId = tx.id.toString('hex') as unknown as Cardano.TransactionId;
      const txInputs = inputs.filter((input) => input.txInputId === txId).map(mapTxIn);
      const txCollaterals = collaterals.filter((col) => col.txInputId === txId).map(mapTxIn);
      const txOutputs = orderBy(outputs.filter((output) => output.txId === txId).map(mapTxOut), ['index']);
      const txCollateralOutputs = collateralOutputs.filter((output) => output.txId === txId).map(mapTxOut);
      const inputSource: Cardano.InputSource = tx.valid_contract
        ? Cardano.InputSource.inputs
        : Cardano.InputSource.collaterals;
 
      return mapTxAlonzo(tx, {
        certificates: certificates.get(txId),
        collateralOutputs: txCollateralOutputs,
        collaterals: txCollaterals,
        inputSource,
        inputs: txInputs,
        metadata: metadata.get(txId),
        mint: mints.get(txId),
        outputs: txOutputs,
        proposalProcedures: proposalProcedures.get(txId),
        redeemers: redeemers.get(txId),
        votingProcedures: votingProcedures.get(txId),
        withdrawals: withdrawals.get(txId)
      });
    });
  }
 
  public async blocksByHashes({ ids }: BlocksByIdsArgs): Promise<Cardano.ExtendedBlockInfo[]> {
    if (ids.length > this.#paginationPageSizeLimit) {
      throw new ProviderError(
        ProviderFailure.BadRequest,
        undefined,
        `Block ids count of ${ids.length} can not be greater than ${this.#paginationPageSizeLimit}`
      );
    }
 
    this.logger.debug('About to find network tip');
    const tipResult: QueryResult<TipModel> = await this.dbPools.main.query(Queries.findTip);
    const tip: TipModel = tipResult.rows[0];
    Iif (!tip) return [];
 
    const byteIds = ids.map((id) => hexStringToBuffer(id));
    this.logger.debug('About to find blocks with hashes:', byteIds);
    const blocksResult: QueryResult<BlockModel> = await this.dbPools.main.query(Queries.findBlocksByHashes, [byteIds]);
    if (blocksResult.rows.length === 0) return [];
 
    this.logger.debug('About to find blocks outputs and fees for blocks:', byteIds);
    const outputResult: QueryResult<BlockOutputModel> = await this.dbPools.main.query(
      Queries.findBlocksOutputByHashes,
      [byteIds]
    );
 
    return blocksResult.rows.map((block) => {
      const blockOutput = outputResult.rows.find((output) => output.hash === block.hash) ?? {
        fees: '0',
        hash: block.hash,
        output: '0'
      };
 
      return mapBlock(block, blockOutput, tip);
    });
  }
}