All files / src/Asset DbSyncNftMetadataService.ts

100% Statements 17/17
100% Branches 1/1
100% Functions 2/2
100% Lines 16/16

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 4136x 36x                           36x 8x 8x 8x     8x 8x 8x         6x   6x   4x   4x 4x 4x 4x      
import { Asset, Cardano } from '@cardano-sdk/core';
import { AssetBuilder } from './DbSyncAssetProvider';
import { AssetPolicyIdAndName, NftMetadataService } from './types';
import { Logger } from 'ts-log';
import { Pool } from 'pg';
import { TxMetadataService } from '../Metadata';
 
/** Dependencies that are need to create DbSyncNftMetadataService */
export interface DbSyncNftMetadataServiceDependencies {
  metadataService: TxMetadataService;
  db: Pool;
  logger: Logger;
}
 
/** NftMetadataService implementation using cardano-db-sync database as a source */
export class DbSyncNftMetadataService implements NftMetadataService {
  #builder: AssetBuilder;
  #logger: Logger;
  #metadataService: TxMetadataService;
 
  constructor({ db, logger, metadataService }: DbSyncNftMetadataServiceDependencies) {
    this.#builder = new AssetBuilder(db, logger);
    this.#logger = logger;
    this.#metadataService = metadataService;
  }
 
  async getNftMetadata(assetInfo: AssetPolicyIdAndName): Promise<Asset.NftMetadata | null> {
    // Perf: could query last mint tx metadata in 1 query instead of 2
    const lastMintedTx = await this.#builder.queryLastMintTx(assetInfo.policyId, assetInfo.name);
 
    if (!lastMintedTx) return null;
 
    const lastMintedTxId = lastMintedTx.tx_hash.toString('hex') as unknown as Cardano.TransactionId;
 
    this.#logger.debug('Querying tx metadata', lastMintedTxId);
    const metadatas = await this.#metadataService.queryTxMetadataByHashes([lastMintedTxId]);
    const metadata = metadatas.get(lastMintedTxId);
    return Asset.NftMetadata.fromMetadatum(assetInfo, metadata, this.#logger);
  }
}