All files / test/Asset/fixtures FixtureBuilder.ts

93.33% Statements 28/30
75% Branches 6/8
100% Functions 5/5
93.33% Lines 28/30

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  4x 4x       4x   4x 4x     4x             4x 9x 9x     9x 9x       1x 1x       1x       14x   14x 14x 10x     14x   14x 14x   14x       14x 14x 14x 14x 14x   14x                              
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as Queries from './queries';
import { Asset, Cardano, ProviderUtil } from '@cardano-sdk/core';
import { LastMintTxModel } from '../../../src';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
import { bufferToHexString } from '@cardano-sdk/util';
 
export enum AssetWith {
  CIP25Metadata = 'CIP25Metadata'
}
 
export class AssetData {
  id: Cardano.AssetId;
  name: Cardano.AssetName;
  policyId: Cardano.PolicyId;
  metadata: any;
}
 
export class AssetFixtureBuilder {
  #db: Pool;
  #logger: Logger;
 
  constructor(db: Pool, logger: Logger) {
    this.#db = db;
    this.#logger = logger;
  }
 
  public async queryLastMintTx(policyId: Cardano.PolicyId, name: Cardano.AssetName) {
    this.#logger.debug('About to query last nft mint tx for asset', { name, policyId });
    const result: QueryResult<LastMintTxModel> = await this.#db.query(Queries.findLastNftMintTx, [
      Buffer.from(policyId, 'hex'),
      Buffer.from(name, 'hex')
    ]);
    return result.rows[0];
  }
 
  public async getAssets(desiredQty: number, options?: { with?: AssetWith[] }): Promise<AssetData[]> {
    this.#logger.debug(`About to fetch up to ${desiredQty} assets`);
 
    let query = Queries.withoutMetadata;
    if (options?.with?.includes(AssetWith.CIP25Metadata)) {
      query = Queries.withCIP25Metadata;
    }
 
    const result: QueryResult<{ policy: Buffer; name: Buffer; json: any }> = await this.#db.query(query, [desiredQty]);
 
    const resultsQty = result.rows.length;
    Iif (result.rows.length === 0) {
      throw new Error('No assets found');
    } else Iif (resultsQty < desiredQty) {
      this.#logger.warn(`${desiredQty} assets desired, only ${resultsQty} results found`);
    }
 
    return result.rows.map(({ policy, name, json }) => {
      const hexPolicy = bufferToHexString(policy);
      const hexName = bufferToHexString(name);
      const policyId = hexPolicy as unknown as Cardano.PolicyId;
      const assetName = hexName as unknown as Cardano.AssetName;
 
      return {
        id: Cardano.AssetId(`${hexPolicy}${hexName}`),
        metadata: json
          ? Asset.NftMetadata.fromMetadatum(
              { name: assetName, policyId },
              new Map<bigint, Cardano.Metadatum>([[721n, ProviderUtil.jsonToMetadatum(json)]]),
              this.#logger
            )
          : null,
        name: assetName,
        policyId
      };
    });
  }
}