All files / src/ChainHistoryProvider BlockfrostChainHistoryProvider.ts

85.84% Statements 194/226
61.47% Branches 75/122
88.17% Functions 82/93
85.57% Lines 172/201

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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912  19x                 19x                           19x 19x 19x           19x     206x                                                                     19x                                   20x 20x 20x       20x 20x 20x 20x             215x 5x 5x 5x               5x               5x                           215x 5x 5x 5x                         215x   10x 5x   5x 20x     10x       5x 5x                   5x 5x                                     218x   3x       215x         218x   3x 3x 3x     215x 215x         5x 5x                       5x 5x                                                                     215x   5x             5x   15x 20x         215x 215x   215x 215x                             430x     5x 5x       5x             218x   5x 5x   5x 20x 20x   5x 5x   5x       5x   5x     5x   5x               10x           215x   215x             215x   215x 215x   215x       215x                                       215x                           218x   218x 8x 8x       218x                 218x   218x 218x   3x     3x     3x       3x                                     3x                         218x 218x   218x             218x 218x 218x   218x 226x   226x   226x     218x                                                                           17x   219x 219x   218x 218x 218x 218x                           13x 13x   13x   219x 2x 217x                   5x               5x   5x   7x               5x               8x         8x                         8x 8x 8x 8x   8x                         8x   7x     8x                   8x   1x     8x                   8x 8x     8x                 8x   8x   8x         8x   8x               8x 8x   8x           8x               13x   13x 13x       13x           13x   13x                                           218x                                                     218x               215x                                   16x 16x 16x 16x                           15x   15x       1x     15x     16x         16x                                 7x                           7x                             1x                                                  
// eslint-disable-next-line jsdoc/check-param-names
import {
  BlockfrostClient,
  BlockfrostProvider,
  BlockfrostToCore,
  BlockfrostTransactionContent,
  blockfrostMetadataToTxMetadata,
  fetchSequentially,
  isBlockfrostNotFoundError
} from '../blockfrost';
import {
  BlocksByIdsArgs,
  Cardano,
  ChainHistoryProvider,
  NetworkInfoProvider,
  Paginated,
  ProviderError,
  ProviderFailure,
  Serialization,
  TransactionsByAddressesArgs,
  TransactionsByIdsArgs,
  createSlotEpochCalc
} from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { extractCredentials, minimizeCredentialSet } from '../credentialUtils';
import omit from 'lodash/omit.js';
import uniq from 'lodash/uniq.js';
import type { Cache } from '@cardano-sdk/util';
import type { Responses } from '@blockfrost/blockfrost-js';
import type { Schemas } from '@blockfrost/blockfrost-js/lib/types/open-api';
 
type WithCertIndex<T> = T & { cert_index: number };
export const DB_MAX_SAFE_INTEGER = 2_147_483_647;
 
type BlockfrostTx = Pick<Responses['address_transactions_content'][0], 'block_height' | 'tx_index'>;
const compareTx = (a: BlockfrostTx, b: BlockfrostTx) => a.block_height - b.block_height || a.tx_index - b.tx_index;
 
/** Options for fetching transactions with pagination and filtering. */
interface FetchTransactionsOptions {
  /** Pagination options for controlling page size and order */
  pagination?: {
    /** Number of results per page */
    count: number;
    /** Sort order (ascending or descending) */
    order?: 'asc' | 'desc';
    /** Page number to fetch */
    page: number;
  };
  /** Block range filters for limiting results by block height */
  blockRange?: {
    /** Lower bound for block height (inclusive) */
    lowerBound?: number;
    /** Upper bound for block height (inclusive) */
    upperBound?: number;
  };
  /** Maximum number of transactions to fetch across all pages */
  limit?: number;
}
 
interface BlockfrostChainHistoryProviderOptions {
  queryTxsByCredentials?: boolean;
}
 
interface BlockfrostChainHistoryProviderDependencies {
  cache: Cache<Cardano.HydratedTx>;
  client: BlockfrostClient;
  networkInfoProvider: NetworkInfoProvider;
  logger: Logger;
}
 
export class BlockfrostChainHistoryProvider extends BlockfrostProvider implements ChainHistoryProvider {
  private readonly cache: Cache<Cardano.HydratedTx>;
  private networkInfoProvider: NetworkInfoProvider;
  // Feature flag to enable credential-based transaction fetching (used in transactionsByAddresses)
  protected readonly queryTxsByCredentials: boolean;
 
  // Overload 1: Old signature (backward compatibility)
  constructor(dependencies: BlockfrostChainHistoryProviderDependencies);
 
  // Overload 2: New signature with options
  constructor(options: BlockfrostChainHistoryProviderOptions, dependencies: BlockfrostChainHistoryProviderDependencies);
 
  // Implementation signature
  constructor(
    optionsOrDependencies: BlockfrostChainHistoryProviderOptions | BlockfrostChainHistoryProviderDependencies,
    maybeDependencies?: BlockfrostChainHistoryProviderDependencies
  ) {
    // Detect which overload was used
    const isOldSignature = 'cache' in optionsOrDependencies;
    const options = isOldSignature ? {} : (optionsOrDependencies as BlockfrostChainHistoryProviderOptions);
    const dependencies = isOldSignature
      ? (optionsOrDependencies as BlockfrostChainHistoryProviderDependencies)
      : maybeDependencies!;
 
    super(dependencies.client, dependencies.logger);
    this.cache = dependencies.cache;
    this.networkInfoProvider = dependencies.networkInfoProvider;
    this.queryTxsByCredentials = options.queryTxsByCredentials ?? false;
  }
 
  protected async fetchRedeemers({
    hash,
    redeemer_count
  }: Responses['tx_content']): Promise<Cardano.Redeemer[] | undefined> {
    if (!redeemer_count) return;
    return this.request<Responses['tx_content_redeemers']>(`txs/${hash}/redeemers`).then((response) =>
      response.map(
        ({ purpose, script_hash, unit_mem, unit_steps, tx_index }): Cardano.Redeemer => ({
          data: Buffer.from(script_hash),
          executionUnits: {
            memory: Number.parseInt(unit_mem),
            steps: Number.parseInt(unit_steps)
          },
          index: tx_index,
          purpose: ((): Cardano.Redeemer['purpose'] => {
            switch (purpose) {
              case 'cert':
                return Cardano.RedeemerPurpose.certificate;
              case 'reward':
                return Cardano.RedeemerPurpose.withdrawal;
              case 'mint':
                return Cardano.RedeemerPurpose.mint;
              case 'spend':
                return Cardano.RedeemerPurpose.spend;
              default:
                return purpose;
            }
          })()
        })
      )
    );
  }
 
  protected async fetchWithdrawals({
    withdrawal_count,
    hash
  }: Responses['tx_content']): Promise<Cardano.Withdrawal[] | undefined> {
    if (!withdrawal_count) return;
    return this.request<Responses['tx_content_withdrawals']>(`txs/${hash}/withdrawals`).then((response) =>
      response.map(
        ({ address, amount }): Cardano.Withdrawal => ({
          quantity: BigInt(amount),
          stakeAddress: Cardano.RewardAccount(address)
        })
      )
    );
  }
 
  /** This method gathers mints by finding the amounts that doesn't exist in 'inputs' but exist in 'outputs'. */
  protected gatherMintsFromUtxos(
    { asset_mint_or_burn_count }: Responses['tx_content'],
    { inputs, outputs }: Responses['tx_content_utxo']
  ): Cardano.TokenMap | undefined {
    if (!asset_mint_or_burn_count) return;
 
    const outputAmounts = outputs.flatMap((o) => o.amount);
    const inputAmounts = inputs.flatMap((i) => i.amount);
 
    const amountDifference = outputAmounts.filter(
      (amount1) => !inputAmounts.some((amount2) => amount1.unit === amount2.unit)
    );
 
    return new Map(amountDifference.map((amount) => [Cardano.AssetId(amount.unit), BigInt(amount.quantity)]));
  }
 
  protected async fetchPoolRetireCerts(hash: string): Promise<WithCertIndex<Cardano.PoolRetirementCertificate>[]> {
    return this.request<Responses['tx_content_pool_retires']>(`txs/${hash}/pool_retires`).then((response) =>
      response.map(({ pool_id, retiring_epoch, cert_index }) => ({
        __typename: Cardano.CertificateType.PoolRetirement,
        cert_index,
        epoch: Cardano.EpochNo(retiring_epoch),
        poolId: Cardano.PoolId(pool_id)
      }))
    );
  }
 
  protected async fetchPoolUpdateCerts(hash: string): Promise<WithCertIndex<Cardano.PoolRegistrationCertificate>[]> {
    return this.request<Responses['tx_content_pool_certs']>(`txs/${hash}/pool_updates`).then((response) =>
      response.map(({ pool_id, cert_index, fixed_cost, margin_cost, pledge, reward_account, vrf_key }) => ({
        __typename: Cardano.CertificateType.PoolRegistration,
        cert_index,
        poolId: Cardano.PoolId(pool_id),
        poolParameters: {
          cost: BigInt(fixed_cost),
          id: pool_id as Cardano.PoolId,
          margin: Cardano.FractionUtils.toFraction(margin_cost),
          owners: [],
          pledge: BigInt(pledge),
          relays: [],
          rewardAccount: reward_account as Cardano.RewardAccount,
          vrf: vrf_key as Cardano.VrfVkHex
        }
      }))
    );
  }
 
  async fetchCBOR(hash: string): Promise<string> {
    return this.request<Responses['tx_content_cbor']>(`txs/${hash}/cbor`)
      .then((response) => {
        if (response) return response.cbor;
        throw new Error('CBOR is null');
      })
      .catch((_error) => {
        throw new Error('CBOR fetch failed');
      });
  }
 
  protected async fetchDetailsFromCBOR(hash: string) {
    return this.fetchCBOR(hash)
      .then((cbor) => {
        const tx = Serialization.Transaction.fromCbor(Serialization.TxCBOR(cbor)).toCore();
        this.logger.debug('Fetched details from CBOR for tx', hash);
        return tx;
      })
      .catch((error) => {
        this.logger.warn('Failed to fetch details from CBOR for tx', hash, error);
        return null;
      });
  }
 
  protected async fetchMirCerts(hash: string): Promise<WithCertIndex<Cardano.MirCertificate>[]> {
    return this.request<Responses['tx_content_mirs']>(`txs/${hash}/mirs`).then((response) =>
      response.map(({ address, amount, cert_index, pot }) => ({
        __typename: Cardano.CertificateType.MIR,
        cert_index,
        kind: Cardano.MirCertificateKind.ToStakeCreds,
        pot: pot === 'reserve' ? Cardano.MirCertificatePot.Reserves : Cardano.MirCertificatePot.Treasury,
        quantity: BigInt(amount),
        rewardAccount: Cardano.RewardAccount(address)
      }))
    );
  }
 
  protected async fetchStakeCerts(hash: string): Promise<WithCertIndex<Cardano.StakeAddressCertificate>[]> {
    return this.request<Responses['tx_content_stake_addr']>(`txs/${hash}/stakes`).then((response) =>
      response.map(({ address, cert_index, registration }) => ({
        __typename: registration
          ? Cardano.CertificateType.StakeRegistration
          : Cardano.CertificateType.StakeDeregistration,
        cert_index,
        stakeCredential: {
          hash: Cardano.RewardAccount.toHash(Cardano.RewardAccount(address)),
          type: Cardano.CredentialType.KeyHash
        }
      }))
    );
  }
 
  protected async fetchDelegationCerts(hash: string): Promise<WithCertIndex<Cardano.StakeDelegationCertificate>[]> {
    return this.request<Responses['tx_content_delegations']>(`txs/${hash}/delegations`).then((response) =>
      response.map(({ address, pool_id, cert_index }) => ({
        __typename: Cardano.CertificateType.StakeDelegation,
        cert_index,
        poolId: Cardano.PoolId(pool_id),
        stakeCredential: {
          hash: Cardano.RewardAccount.toHash(Cardano.RewardAccount(address)),
          type: Cardano.CredentialType.KeyHash
        }
      }))
    );
  }
 
  protected async fetchCertificates({
    pool_retire_count,
    pool_update_count,
    mir_cert_count,
    stake_cert_count,
    delegation_count,
    hash
  }: Responses['tx_content']): Promise<Cardano.Certificate[] | undefined> {
    if (pool_retire_count + pool_update_count + mir_cert_count + stake_cert_count + delegation_count === 0) return;
 
    return Promise.all([
      pool_retire_count ? this.fetchPoolRetireCerts(hash) : [],
      pool_update_count ? this.fetchPoolUpdateCerts(hash) : [],
      mir_cert_count ? this.fetchMirCerts(hash) : [],
      stake_cert_count ? this.fetchStakeCerts(hash) : [],
      delegation_count ? this.fetchDelegationCerts(hash) : []
    ]).then((results) =>
      results
        .flat()
        .sort((a, b) => b.cert_index - a.cert_index)
        .map((cert) => cert as Cardano.Certificate)
    );
  }
 
  protected async fetchJsonMetadataAsAuxiliaryData(txHash: string): Promise<Cardano.AuxiliaryData | undefined> {
    const UNDEFINED = undefined;
    return this.request<Responses['tx_content_metadata']>(`txs/${txHash}/metadata`)
      .then((m) => {
        const metadata = blockfrostMetadataToTxMetadata(m);
        return metadata && metadata.size > 0
          ? {
              blob: metadata
            }
          : UNDEFINED;
      })
      .catch((error) => {
        Iif (isBlockfrostNotFoundError(error)) {
          return UNDEFINED;
        }
        throw error;
      });
  }
 
  // eslint-disable-next-line unicorn/consistent-function-scoping
  protected parseValidityInterval = (num: string | null) => Cardano.Slot(Number.parseInt(num || '')) || undefined;
 
  protected async fetchEpochNo(slotNo: Cardano.Slot) {
    const calc = await this.networkInfoProvider.eraSummaries().then(createSlotEpochCalc);
    return calc(slotNo);
  }
 
  protected async fetchEpochParameters(epochNo: Cardano.EpochNo): Promise<Schemas['epoch_param_content']> {
    return await this.request<Responses['epoch_param_content']>(`epochs/${epochNo}/parameters`);
  }
 
  protected async processCertificates(
    txContent: Schemas['tx_content'],
    certificates?: Cardano.Certificate[]
  ): Promise<Cardano.Certificate[] | undefined> {
    if (!certificates) return;
 
    const epochNo = await this.fetchEpochNo(Cardano.Slot(txContent.slot));
    const { pool_deposit, key_deposit } = await this.fetchEpochParameters(epochNo);
 
    return certificates.map((c) => {
      const cert = omit(c, 'cert_index') as Cardano.Certificate;
      switch (cert.__typename) {
        case Cardano.CertificateType.PoolRegistration: {
          cert.poolParameters.owners = [];
          cert.poolParameters.relays = [];
          const deposit =
            txContent.deposit === undefined || txContent.deposit === '' || txContent.deposit === '0'
              ? 0n
              : BigInt(pool_deposit);
 
          delete cert.poolParameters.metadataJson;
 
          return { ...cert, deposit };
        }
        case Cardano.CertificateType.StakeRegistration: {
          const deposit = BigInt(key_deposit);
 
          return { ...cert, __typename: Cardano.CertificateType.Registration, deposit };
        }
        case Cardano.CertificateType.StakeDeregistration: {
          const deposit = BigInt(key_deposit);
 
          return { ...cert, __typename: Cardano.CertificateType.Unregistration, deposit };
        }
        default:
          return cert;
      }
    });
  }
 
  protected async transactionDetailsUsingAPIs(txContent: Responses['tx_content']): Promise<Cardano.HydratedTx> {
    const id = Cardano.TransactionId(txContent.hash);
 
    const [certificates, withdrawals, utxos, auxiliaryData] = await Promise.all([
      this.fetchCertificates(txContent),
      this.fetchWithdrawals(txContent),
      this.fetchUtxos(id),
      this.fetchJsonMetadataAsAuxiliaryData(id)
    ]);
 
    const { inputs, outputs, collaterals } = this.transactionUtxos(utxos);
 
    const mintPreOrder = this.gatherMintsFromUtxos(txContent, utxos);
    const mint = mintPreOrder ? new Map([...mintPreOrder].sort()) : mintPreOrder;
 
    const inputSource: Cardano.InputSource = txContent.valid_contract
      ? Cardano.InputSource.inputs
      : Cardano.InputSource.collaterals;
 
    const body: Cardano.HydratedTxBody = this.mapTxBody(
      {
        certificates: await this.processCertificates(txContent, certificates),
        collateralReturn: undefined,
        collaterals,
        fee: BigInt(txContent.fees),
        inputs,
        mint,
        outputs,
        proposalProcedures: undefined,
        validityInterval: {
          invalidBefore: this.parseValidityInterval(txContent.invalid_before),
          invalidHereafter: this.parseValidityInterval(txContent.invalid_hereafter)
        },
        votingProcedures: undefined,
        withdrawals
      },
      inputSource
    );
 
    return {
      auxiliaryData,
      blockHeader: this.mapBlockHeader(txContent),
      body,
      id,
      index: txContent.index,
      inputSource,
      txSize: txContent.size,
      witness: this.witnessFromRedeemers(await this.fetchRedeemers(txContent))
    };
  }
 
  private witnessFromRedeemers(redeemers: Cardano.Redeemer[] | undefined): Cardano.Witness {
    // Although cbor has the data, this stub is used for compatibility with DbSyncChainHistoryProvider
    const stubRedeemerData = Buffer.from('not implemented');
 
    if (redeemers) {
      for (const redeemer of redeemers) {
        redeemer.data = stubRedeemerData;
      }
    }
 
    return {
      redeemers,
      signatures: new Map() // available in cbor, but skipped for compatibility with DbSyncChainHistoryProvider
    };
  }
 
  protected async transactionDetailsUsingCBOR(
    txContent: Responses['tx_content']
  ): Promise<Cardano.HydratedTx | undefined> {
    const id = Cardano.TransactionId(txContent.hash);
 
    const txFromCBOR = await this.fetchDetailsFromCBOR(id);
    if (!txFromCBOR) return;
 
    const utxos = await this.request<Responses['tx_content_utxo']>(`txs/${id}/utxos`);
 
    // We can't use txFromCBOR.body.inputs since it misses HydratedTxIn.address
    const { inputs, outputs, collaterals } = this.transactionUtxos(utxos, txFromCBOR);
 
    // txFromCBOR.isValid can be also be used
    const inputSource: Cardano.InputSource = txContent.valid_contract
      ? Cardano.InputSource.inputs
      : Cardano.InputSource.collaterals;
 
    const body: Cardano.HydratedTxBody = this.mapTxBody(
      {
        certificates: await this.processCertificates(txContent, txFromCBOR.body.certificates),
        collateralReturn: txFromCBOR.body.collateralReturn,
        collaterals,
        fee: txFromCBOR.body.fee,
        inputs,
        mint: txFromCBOR.body.mint ? new Map([...txFromCBOR.body.mint].sort()) : undefined,
        outputs,
        proposalProcedures: txFromCBOR.body.proposalProcedures,
        validityInterval: txFromCBOR.body.validityInterval
          ? txFromCBOR.body.validityInterval
          : { invalidBefore: undefined, invalidHereafter: undefined },
        votingProcedures: txFromCBOR.body.votingProcedures,
        withdrawals: txFromCBOR.body.withdrawals
      },
      inputSource
    );
 
    return {
      auxiliaryData: txFromCBOR.auxiliaryData,
      blockHeader: this.mapBlockHeader(txContent),
      body,
      id,
      index: txContent.index,
      inputSource,
      txSize: txContent.size,
      witness: this.witnessFromRedeemers(txFromCBOR.witness.redeemers)
    };
  }
 
  protected async fetchTransaction(txId: Cardano.TransactionId): Promise<Cardano.HydratedTx> {
    try {
      const txContent = await this.request<Responses['tx_content']>(`txs/${txId.toString()}`);
 
      return (await this.transactionDetailsUsingCBOR(txContent)) ?? (await this.transactionDetailsUsingAPIs(txContent));
    } catch (error) {
      throw this.toProviderError(error);
    }
  }
 
  private transactionUtxos(utxoResponse: Responses['tx_content_utxo'], txContent?: Cardano.Tx) {
    const collaterals = utxoResponse.inputs.filter((input) => input.collateral).map(BlockfrostToCore.hydratedTxIn);
    const inputs = utxoResponse.inputs
      .filter((input) => !input.collateral && !input.reference)
      .map(BlockfrostToCore.hydratedTxIn);
    const outputPromises: Cardano.TxOut[] = utxoResponse.outputs
      .filter((output) => !output.collateral)
      .map((output) => {
        const foundScript = txContent?.body.outputs.find((o) => o.address === output.address);
 
        return BlockfrostToCore.txOut(output, foundScript);
      });
 
    return { collaterals, inputs, outputs: outputPromises };
  }
 
  public async blocksByHashes({ ids }: BlocksByIdsArgs): Promise<Cardano.ExtendedBlockInfo[]> {
    try {
      const responses = await Promise.all(
        ids.map((id) => this.request<Responses['block_content']>(`blocks/${id.toString()}`))
      );
      return responses.map((response) => {
        Iif (!response.epoch || !response.epoch_slot || !response.height || !response.slot || !response.block_vrf) {
          throw new ProviderError(ProviderFailure.Unknown, null, 'Queried unsupported block');
        }
        return {
          confirmations: response.confirmations,
          date: new Date(response.time * 1000),
          epoch: Cardano.EpochNo(response.epoch),
          epochSlot: response.epoch_slot,
          fees: BigInt(response.fees || '0'),
          header: {
            blockNo: Cardano.BlockNo(response.height),
            hash: Cardano.BlockId(response.hash),
            slot: Cardano.Slot(response.slot)
          },
          nextBlock: response.next_block ? Cardano.BlockId(response.next_block) : undefined,
          previousBlock: response.previous_block ? Cardano.BlockId(response.previous_block) : undefined,
          size: Cardano.BlockSize(response.size),
          slotLeader: Cardano.SlotLeader(response.slot_leader),
          totalOutput: BigInt(response.output || '0'),
          txCount: response.tx_count,
          vrf: Cardano.VrfVkBech32(response.block_vrf)
        };
      });
    } catch (error) {
      throw this.toProviderError(error);
    }
  }
 
  public async transactionsByHashes({ ids }: TransactionsByIdsArgs): Promise<Cardano.HydratedTx[]> {
    return Promise.all(
      ids.map(async (id) => {
        const cached = await this.cache.get(id);
        if (cached) return cached;
 
        try {
          const fetchedTransaction = await this.fetchTransaction(id);
          void this.cache.set(id, fetchedTransaction);
          return fetchedTransaction;
        } catch (error) {
          throw this.toProviderError(error);
        }
      })
    );
  }
 
  /** Processes and filters transaction results, returning deduplicated and sorted transaction IDs. */
  private processTransactionResults(
    transactions: BlockfrostTransactionContent[],
    blockRange: { lowerBound?: number; upperBound?: number },
    order?: 'asc' | 'desc'
  ): Cardano.TransactionId[] {
    const lowerBound = blockRange.lowerBound ?? 0;
    const upperBound = blockRange.upperBound ?? DB_MAX_SAFE_INTEGER;
 
    return uniq(
      transactions
        .filter(({ block_height }) => block_height >= lowerBound && block_height <= upperBound)
        .sort(order === 'desc' ? (a, b) => compareTx(b, a) : compareTx)
        .map(({ tx_hash }) => Cardano.TransactionId(tx_hash))
    );
  }
 
  /** Executes the legacy per-address transaction fetching implementation. */
  private async fetchTransactionsPerAddress(
    addresses: Cardano.PaymentAddress[],
    pagination: TransactionsByAddressesArgs['pagination'],
    blockRange: TransactionsByAddressesArgs['blockRange']
  ): Promise<BlockfrostTransactionContent[]> {
    const paginationOptions = pagination
      ? {
          count: pagination.limit,
          order: pagination.order,
          page: (pagination.startAt + pagination.limit) / pagination.limit
        }
      : undefined;
 
    const limit = pagination?.limit ?? DB_MAX_SAFE_INTEGER;
 
    const addressTransactions = await Promise.all(
      addresses.map((address) =>
        this.fetchTransactionsByAddress(address, {
          blockRange,
          limit,
          pagination: paginationOptions
        })
      )
    );
 
    return addressTransactions.flat();
  }
 
  /** Logs information about skipped addresses. */
  private logSkippedAddresses(skippedAddresses: {
    byron: Cardano.PaymentAddress[];
    pointer: Cardano.PaymentAddress[];
  }): void {
    Iif (skippedAddresses.byron.length > 0) {
      this.logger.info(
        `Found ${skippedAddresses.byron.length} Byron address(es), falling back to per-address fetching`
      );
    }
    Iif (skippedAddresses.pointer.length > 0) {
      this.logger.info(
        `Found ${skippedAddresses.pointer.length} Pointer address(es), falling back to per-address fetching`
      );
    }
  }
 
  /** Logs minimization statistics. */
  private logMinimizationStats(
    totalAddresses: number,
    minimized: { paymentCredentials: Map<unknown, unknown>; rewardAccounts: Map<unknown, unknown> },
    skippedAddresses: { byron: Cardano.PaymentAddress[]; pointer: Cardano.PaymentAddress[] }
  ): void {
    const paymentCredCount = minimized.paymentCredentials.size;
    const rewardAccountCount = minimized.rewardAccounts.size;
    const skippedCount = skippedAddresses.byron.length + skippedAddresses.pointer.length;
    const totalQueries = paymentCredCount + rewardAccountCount + skippedCount;
 
    this.logger.debug(
      `Minimized ${totalAddresses} address(es) to ${totalQueries} query/queries: ` +
        `${paymentCredCount} payment credential(s), ${rewardAccountCount} reward account(s), ${skippedCount} skipped address(es)`
    );
  }
 
  /** Fetches transactions for all payment credentials in parallel. */
  private async fetchAllByPaymentCredentials(
    credentials: Map<Cardano.PaymentCredential, Cardano.PaymentAddress[]>,
    pagination?: { count: number; order?: 'asc' | 'desc'; page: number },
    blockRange?: { lowerBound?: number; upperBound?: number },
    limit?: number
  ): Promise<BlockfrostTransactionContent[]> {
    const results = await Promise.all(
      [...credentials.keys()].map((credential) =>
        this.fetchTransactionsByPaymentCredential(credential, { blockRange, limit, pagination })
      )
    );
    return results.flat();
  }
 
  /** Fetches transactions for all reward accounts in parallel. */
  private async fetchAllByRewardAccounts(
    rewardAccounts: Map<Cardano.RewardAccount, Cardano.PaymentAddress[]>,
    pagination?: { count: number; order?: 'asc' | 'desc'; page: number },
    blockRange?: { lowerBound?: number; upperBound?: number },
    limit?: number
  ): Promise<BlockfrostTransactionContent[]> {
    const results = await Promise.all(
      [...rewardAccounts.keys()].map((rewardAccount) =>
        this.fetchTransactionsByRewardAccount(rewardAccount, { blockRange, limit, pagination })
      )
    );
    return results.flat();
  }
 
  /** Fetches transactions for skipped addresses (Byron and Pointer). */
  private async fetchSkippedAddresses(
    skippedAddresses: { byron: Cardano.PaymentAddress[]; pointer: Cardano.PaymentAddress[] },
    pagination?: { count: number; order?: 'asc' | 'desc'; page: number },
    blockRange?: { lowerBound?: number; upperBound?: number },
    limit?: number
  ): Promise<BlockfrostTransactionContent[]> {
    const allSkippedAddresses = [...skippedAddresses.byron, ...skippedAddresses.pointer];
    const results = await Promise.all(
      allSkippedAddresses.map((address) => this.fetchTransactionsByAddress(address, { blockRange, limit, pagination }))
    );
    return results.flat();
  }
 
  /** Executes the optimized credential-based transaction fetching implementation. */
  private async fetchTransactionsByCredentials(
    addresses: Cardano.PaymentAddress[],
    pagination: TransactionsByAddressesArgs['pagination'],
    blockRange: TransactionsByAddressesArgs['blockRange']
  ): Promise<BlockfrostTransactionContent[]> {
    const addressGroups = extractCredentials(addresses);
 
    this.logSkippedAddresses(addressGroups.skippedAddresses);
 
    const minimized = minimizeCredentialSet({
      paymentCredentials: addressGroups.paymentCredentials,
      rewardAccounts: addressGroups.rewardAccounts
    });
 
    this.logMinimizationStats(addresses.length, minimized, addressGroups.skippedAddresses);
 
    const paginationOptions = pagination
      ? {
          count: Math.min(pagination.limit, 100), // Cap at Blockfrost's max page size
          order: pagination.order ?? 'asc',
          page: (pagination.startAt + pagination.limit) / pagination.limit
        }
      : undefined;
 
    const blockRangeOptions = { lowerBound: blockRange?.lowerBound, upperBound: blockRange?.upperBound };
    const limit = pagination?.limit ?? DB_MAX_SAFE_INTEGER;
 
    const [paymentTxs, rewardAccountTxs, skippedAddressTxs] = await Promise.all([
      this.fetchAllByPaymentCredentials(minimized.paymentCredentials, paginationOptions, blockRangeOptions, limit),
      this.fetchAllByRewardAccounts(minimized.rewardAccounts, paginationOptions, blockRangeOptions, limit),
      this.fetchSkippedAddresses(addressGroups.skippedAddresses, paginationOptions, blockRangeOptions, limit)
    ]);
 
    return [...paymentTxs, ...rewardAccountTxs, ...skippedAddressTxs];
  }
 
  public async transactionsByAddresses({
    addresses,
    pagination,
    blockRange
  }: TransactionsByAddressesArgs): Promise<Paginated<Cardano.HydratedTx>> {
    this.logger.debug(`transactionsByAddresses: ${JSON.stringify(blockRange)} ${JSON.stringify(addresses)}`);
 
    try {
      const allTransactions = this.queryTxsByCredentials
        ? await this.fetchTransactionsByCredentials(addresses, pagination, blockRange)
        : await this.fetchTransactionsPerAddress(addresses, pagination, blockRange);
 
      const dedupedSortedTransactionsIds = this.processTransactionResults(
        allTransactions,
        { lowerBound: blockRange?.lowerBound, upperBound: blockRange?.upperBound },
        pagination?.order
      );
 
      const pageResults = await this.transactionsByHashes({ ids: dedupedSortedTransactionsIds });
 
      return { pageResults, totalResultCount: dedupedSortedTransactionsIds.length };
    } catch (error) {
      throw this.toProviderError(error);
    }
  }
 
  private mapTxBody(
    {
      collateralReturn,
      collaterals,
      fee,
      inputs,
      outputs,
      mint,
      proposalProcedures,
      validityInterval,
      votingProcedures,
      withdrawals,
      certificates
    }: Cardano.HydratedTxBody,
    inputSource: Cardano.InputSource
  ) {
    return {
      ...(inputSource === Cardano.InputSource.collaterals
        ? {
            collateralReturn: outputs.length > 0 ? outputs[0] : undefined,
            collaterals: inputs,
            fee: BigInt(0),
            inputs: [],
            outputs: [],
            totalCollateral: fee
          }
        : {
            collateralReturn: collateralReturn ?? undefined,
            collaterals,
            fee,
            inputs,
            outputs
          }),
      certificates,
      mint,
      proposalProcedures,
      validityInterval,
      votingProcedures,
      withdrawals
    };
  }
 
  private mapBlockHeader({ block, block_height, slot }: Responses['tx_content']): Cardano.PartialBlockHeader {
    return {
      blockNo: Cardano.BlockNo(block_height),
      hash: Cardano.BlockId(block),
      slot: Cardano.Slot(slot)
    };
  }
 
  private fetchUtxos(id: Cardano.TransactionId): Promise<Schemas['tx_content_utxo']> {
    return this.request<Responses['tx_content_utxo']>(`txs/${id}/utxos`);
  }
 
  /**
   * Helper to build query string with pagination and block range filters.
   *
   * @param baseEndpoint - The base endpoint (e.g., "addresses/xyz/transactions")
   * @param paginationQueryString - Pagination query string from fetchSequentially
   * @param blockRange - Optional block range filters
   * @param blockRange.lowerBound - Lower bound for block range
   * @param blockRange.upperBound - Upper bound for block range
   * @returns Complete query string with all parameters
   */
  private buildTransactionQueryString(
    baseEndpoint: string,
    paginationQueryString: string,
    blockRange?: { lowerBound?: number; upperBound?: number }
  ): string {
    let queryString = `${baseEndpoint}?${paginationQueryString}`;
    if (blockRange?.lowerBound) queryString += `&from=${blockRange.lowerBound.toString()}`;
    if (blockRange?.upperBound) queryString += `&to=${blockRange.upperBound.toString()}`;
    return queryString;
  }
 
  /**
   * Common method to fetch transactions with pagination and limits.
   *
   * @param endpoint - The base endpoint (e.g., 'addresses/xyz' or 'accounts/stake123')
   * @param options - Options for pagination, block range, and limit
   * @returns Promise resolving to array of transaction contents
   */
  private async fetchTransactionsWithPagination(
    endpoint: string,
    options: FetchTransactionsOptions
  ): Promise<BlockfrostTransactionContent[]> {
    const limit = options.limit ?? DB_MAX_SAFE_INTEGER;
 
    return fetchSequentially<{ tx_hash: string; tx_index: number; block_height: number }, BlockfrostTransactionContent>(
      {
        haveEnoughItems: options.blockRange?.lowerBound
          ? (transactions) =>
              (transactions.length > 0 &&
                transactions[transactions.length - 1].block_height < options.blockRange!.lowerBound!) ||
              transactions.length >= limit
          : (transactions) => transactions.length >= limit,
        paginationOptions: options.pagination,
        request: (paginationQueryString) => {
          const queryString = this.buildTransactionQueryString(
            `${endpoint}/transactions`,
            paginationQueryString,
            options.blockRange
          );
          return this.request<Responses['address_transactions_content']>(queryString);
        }
      }
    );
  }
 
  /**
   * Fetches transactions for a given address using the fetchSequentially pattern.
   *
   * @param address - The address to fetch transactions for
   * @param options - Options for pagination, block range, and limit
   * @returns Promise resolving to array of transaction contents
   */
  private async fetchTransactionsByAddress(
    address: Cardano.PaymentAddress,
    options: FetchTransactionsOptions
  ): Promise<BlockfrostTransactionContent[]> {
    return this.fetchTransactionsWithPagination(`addresses/${address}`, options);
  }
 
  /**
   * Fetches transactions for a payment credential (bech32: addr_vkh or script).
   *
   * @param credential - Payment credential as bech32 string
   * @param options - Options for pagination, block range, and limit
   * @returns Promise resolving to array of transaction contents
   */
  protected async fetchTransactionsByPaymentCredential(
    credential: Cardano.PaymentCredential,
    options: FetchTransactionsOptions
  ): Promise<BlockfrostTransactionContent[]> {
    return this.fetchTransactionsWithPagination(`addresses/${credential}`, options);
  }
 
  /**
   * Fetches transactions for a reward account (stake address, bech32: stake or stake_test).
   *
   * @param rewardAccount - Reward account (stake address) as bech32 string
   * @param options - Options for pagination, block range, and limit
   * @returns Promise resolving to array of transaction contents
   */
  protected async fetchTransactionsByRewardAccount(
    rewardAccount: Cardano.RewardAccount,
    options: FetchTransactionsOptions
  ): Promise<BlockfrostTransactionContent[]> {
    // Note: accounts/{stake_address}/transactions returns the same structure as address_transactions_content
    return this.fetchTransactionsWithPagination(`accounts/${rewardAccount}`, options);
  }
 
  /**
   * Merges and deduplicates transaction results from multiple sources (payment credentials, reward accounts, skipped addresses).
   *
   * @param paymentTxs Transactions from payment credential queries
   * @param rewardAccountTxs Transactions from reward account (stake address) queries
   * @param skippedAddressTxs Transactions from skipped address queries (Byron, Pointer)
   * @param order Sort order (asc or desc)
   * @returns Sorted and deduplicated array of transaction IDs
   */
  protected mergeAndDeduplicateTransactionResults(
    paymentTxs: BlockfrostTransactionContent[],
    rewardAccountTxs: BlockfrostTransactionContent[],
    skippedAddressTxs: BlockfrostTransactionContent[],
    order: 'asc' | 'desc' = 'asc'
  ): Cardano.TransactionId[] {
    const allTransactions = [...paymentTxs, ...rewardAccountTxs, ...skippedAddressTxs];
 
    const sortedTransactions = allTransactions.sort(order === 'desc' ? (a, b) => compareTx(b, a) : compareTx);
 
    return uniq(sortedTransactions.map(({ tx_hash }) => Cardano.TransactionId(tx_hash)));
  }
}