All files / src/ChainHistory/BlockrostChainHistoryProvider BlockfrostChainHistoryProvider.ts

79.14% Statements 129/163
46.31% Branches 44/95
83.33% Functions 60/72
80.98% Lines 115/142

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    40x 40x               40x                         40x     40x               40x       4x 4x             2x 2x 2x 2x               2x               2x                           2x 2x 2x 2x                         2x   4x 2x   2x 8x     4x       2x 2x                   2x 2x                                                             4x   2x 2x 2x     2x 2x         2x 2x                       2x 2x                                                                     2x   2x             2x   6x 8x         2x 2x     2x 2x                             4x     2x 2x       2x             4x   2x 2x   2x 8x 8x   2x 2x   2x       2x   2x     2x   2x               4x           2x   2x             2x   2x 2x   2x       2x                                       2x                           4x   4x 4x 4x       4x                 4x   4x 4x   2x     2x     2x       2x                                     2x                         4x 4x   4x             4x 4x 4x   4x 8x   8x   8x     4x                                                                       4x 4x                       2x 2x 2x 2x   2x   2x                     2x               2x   2x 2x   2x     2x   2x                                           4x                                                     4x               2x      
// eslint-disable-next-line jsdoc/check-param-names
import * as Crypto from '@cardano-sdk/crypto';
import { BlockfrostProvider, BlockfrostProviderDependencies } from '../../util/BlockfrostProvider/BlockfrostProvider';
import {
  BlockfrostToCore,
  BlockfrostTransactionContent,
  blockfrostMetadataToTxMetadata,
  blockfrostToProviderError,
  fetchByAddressSequentially,
  isBlockfrostNotFoundError
} from '../../util';
import {
  BlocksByIdsArgs,
  Cardano,
  ChainHistoryProvider,
  NetworkInfoProvider,
  Paginated,
  ProviderError,
  ProviderFailure,
  Serialization,
  TransactionsByAddressesArgs,
  TransactionsByIdsArgs,
  createSlotEpochCalc
} from '@cardano-sdk/core';
import { DB_MAX_SAFE_INTEGER } from '../DbSyncChainHistory/queries';
import { Responses } from '@blockfrost/blockfrost-js';
import { Schemas } from '@blockfrost/blockfrost-js/lib/types/open-api';
import omit from 'lodash/omit.js';
 
type WithCertIndex<T> = T & { cert_index: number };
 
export interface BlockfrostChainHistoryProviderDependencies extends BlockfrostProviderDependencies {
  networkInfoProvider: NetworkInfoProvider;
}
 
export class BlockfrostChainHistoryProvider extends BlockfrostProvider implements ChainHistoryProvider {
  private networkInfoProvider: NetworkInfoProvider;
 
  constructor({ logger, blockfrost, networkInfoProvider }: BlockfrostChainHistoryProviderDependencies) {
    super({ blockfrost, logger });
    this.networkInfoProvider = networkInfoProvider;
  }
 
  protected async fetchRedeemers({
    hash,
    redeemer_count
  }: Responses['tx_content']): Promise<Cardano.Redeemer[] | undefined> {
    Iif (!redeemer_count) return;
    return this.blockfrost.txsRedeemers(hash).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> {
    Iif (!withdrawal_count) return;
    return this.blockfrost.txsWithdrawals(hash).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 {
    Iif (!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.blockfrost.txsPoolRetires(hash).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.blockfrost.txsPoolUpdates(hash).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.blockfrost
      .instance<Schemas['script_cbor']>(`txs/${hash}/cbor`)
      .then((response) => {
        Iif (response.body.cbor) return response.body.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.info('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.blockfrost.txsMirs(hash).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.blockfrost.txsStakes(hash).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)) as unknown as Crypto.Hash28ByteBase16,
          type: Cardano.CredentialType.KeyHash
        }
      }))
    );
  }
 
  protected async fetchDelegationCerts(hash: string): Promise<WithCertIndex<Cardano.StakeDelegationCertificate>[]> {
    return this.blockfrost.txsDelegations(hash).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)) as unknown as Crypto.Hash28ByteBase16,
          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> {
    Iif (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.blockfrost
      .txsMetadata(txHash)
      .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.blockfrost.epochsParameters(epochNo);
  }
 
  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: Schemas['tx_content_utxo'] = (await this.blockfrost.txsUtxos(id)) as Schemas['tx_content_utxo'];
 
    // 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.blockfrost.txs(txId.toString());
 
      return (await this.transactionDetailsUsingCBOR(txContent)) ?? (await this.transactionDetailsUsingAPIs(txContent));
    } catch (error) {
      throw blockfrostToProviderError(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.blockfrost.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 blockfrostToProviderError(error);
    }
  }
 
  public async transactionsByHashes({ ids }: TransactionsByIdsArgs): Promise<Cardano.HydratedTx[]> {
    try {
      return Promise.all(ids.map((id) => this.fetchTransaction(id)));
    } catch (error) {
      throw blockfrostToProviderError(error);
    }
  }
 
  // eslint-disable-next-line sonarjs/cognitive-complexity
  public async transactionsByAddresses({
    addresses,
    pagination,
    blockRange
  }: TransactionsByAddressesArgs): Promise<Paginated<Cardano.HydratedTx>> {
    this.logger.info(`transactionsByAddresses: ${JSON.stringify(blockRange)} ${JSON.stringify(addresses)}`);
    try {
      const lowerBound = blockRange?.lowerBound ?? 0;
      const upperBound = blockRange?.upperBound ?? DB_MAX_SAFE_INTEGER;
 
      const addressTransactions = await Promise.all(
        addresses.map(async (address) =>
          fetchByAddressSequentially<
            { tx_hash: string; tx_index: number; block_height: number },
            BlockfrostTransactionContent
          >({
            address,
            haveEnoughItems: blockRange?.lowerBound
              ? (transactions) =>
                  transactions.length > 0 &&
                  transactions[transactions.length - 1].block_height < blockRange!.lowerBound!
              : undefined,
            request: (addr: Cardano.PaymentAddress, paginationOptions) =>
              this.blockfrost.addressesTransactions(addr.toString(), paginationOptions, {
                from: blockRange?.lowerBound ? blockRange?.lowerBound.toString() : undefined,
                to: blockRange?.upperBound ? blockRange?.upperBound.toString() : undefined
              })
          })
        )
      );
 
      const allTransactions = addressTransactions.flat(1);
 
      const ids = allTransactions
        .filter(({ block_height }) => block_height >= lowerBound && block_height <= upperBound)
        .sort((a, b) => a.block_height - b.block_height || a.tx_index - b.tx_index)
        .map(({ tx_hash }) => Cardano.TransactionId(tx_hash))
        .splice(pagination.startAt, pagination.limit);
 
      const pageResults = await this.transactionsByHashes({ ids });
 
      return { pageResults, totalResultCount: allTransactions.length };
    } catch (error) {
      throw blockfrostToProviderError(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.blockfrost.txsUtxos(id);
  }
}