All files / src/services WalletAssetProvider.ts

98.96% Statements 96/97
97.05% Branches 33/34
94.44% Functions 17/18
98.86% Lines 87/88

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 23443x     43x 43x                 43x           26x   26x 7x             7x 7x 7x         19x     43x             8x     8x 5x         8x     43x 8x 8x 8x                 8x   8x     43x 15x   15x   9x 8x 8x     43x         9x       9x 12x 12x       7x   5x       9x     43x 2x 2x 2x                   43x           9x 15x 15x   15x 2x     13x 3x     10x 2x 2x   2x 2x       10x               43x         17x   8x 8x   8x   8x 7x 7x   3x       8x     8x 1x 1x 1x                   7x   5x 1x     1x 1x       5x 5x   5x       9x 9x 15x 9x 9x   9x   9x 8x 14x 14x       9x              
import { Asset, AssetProvider, Cardano, GetAssetArgs, GetAssetsArgs, HealthCheckResponse } from '@cardano-sdk/core';
import { Assets } from '../types';
import { Logger } from 'ts-log';
import { Observable, firstValueFrom } from 'rxjs';
import { isNotNil } from '@cardano-sdk/util';
 
export interface AssetProviderContext {
  assetProvider: AssetProvider;
  assetInfo$: Observable<Assets>;
  tx?: Cardano.Tx;
  logger: Logger;
}
 
const tryCip68NftMetadata = (
  policyId: Cardano.PolicyId,
  name: Cardano.AssetName,
  tx: Cardano.Tx,
  logger: Logger
): Asset.NftMetadata | null => {
  const decoded = Asset.AssetNameLabel.decode(name);
 
  if (decoded?.label === Asset.AssetNameLabelNum.UserNFT) {
    const referenceAssetId = Cardano.AssetId.fromParts(
      policyId,
      Asset.AssetNameLabel.encode(decoded.content, Asset.AssetNameLabelNum.ReferenceNFT)
    );
 
    // TODO: It is possible that the reference NFT is not in one of the outputs of the transaction and was previously minted. We
    // need a way to find the reference NFT TxOut from the current active UTXO set on the network.
    for (const output of tx.body.outputs) {
      if (output.value.assets?.get(referenceAssetId)) {
        return Asset.NftMetadata.fromPlutusData(output.datum, logger);
      }
    }
  }
 
  return null;
};
 
const getNftMetadata = (
  name: Cardano.AssetName,
  policyId: Cardano.PolicyId,
  tx: Cardano.Tx,
  logger: Logger
): Asset.NftMetadata | null => {
  // First, try CIP-68
  let metadata = tryCip68NftMetadata(policyId, name, tx, logger);
 
  // If metadata is not found, try CIP-25
  if (!metadata) {
    metadata = tx.auxiliaryData?.blob
      ? Asset.NftMetadata.fromMetadatum({ name, policyId }, tx.auxiliaryData.blob, logger)
      : null;
  }
 
  return metadata;
};
 
const createAssetInfo = (assetId: Cardano.AssetId, amount: bigint, tx: Cardano.Tx, logger: Logger): Asset.AssetInfo => {
  const name = Cardano.AssetId.getAssetName(assetId);
  const policyId = Cardano.AssetId.getPolicyId(assetId);
  const assetInfo: Asset.AssetInfo = {
    assetId,
    fingerprint: Cardano.AssetFingerprint.fromParts(policyId, name),
    name,
    policyId,
    quantity: amount,
    supply: amount
  };
 
  assetInfo.nftMetadata = getNftMetadata(name, policyId, tx, logger);
 
  return assetInfo;
};
 
const getMintedAssetInfosFromTx = async (tx: Cardano.Tx, logger: Logger): Promise<Asset.AssetInfo[] | null> => {
  const mints = tx.body.mint;
 
  if (!mints) return null;
 
  return [...mints.entries()]
    .filter(([_, amount]) => amount > 0)
    .map(([assetId, amount]) => createAssetInfo(assetId, amount, tx, logger));
};
 
const fetchAssetsFromProvider = async (
  provider: AssetProvider,
  assetIds: Cardano.AssetId[],
  logger: Logger
): Promise<Asset.AssetInfo[]> => {
  const assetsFromProvider: Asset.AssetInfo[] = [];
 
  // We need to fetch assets one by one because the provider will throw if any of the assets requests to the getAssets endpoint is not found.
  // We want to fetch the ones we can and return a simplified AssetInfo for the ones we can't.
  for (const assetId of assetIds) {
    try {
      const fetchedAsset = await provider.getAsset({
        assetId,
        extraData: { nftMetadata: true, tokenMetadata: true }
      });
      assetsFromProvider.push(fetchedAsset);
    } catch (error) {
      logger.error(error);
    }
  }
 
  return assetsFromProvider;
};
 
const createFallbackAsset = (assetId: Cardano.AssetId): Asset.AssetInfo => {
  const name = Cardano.AssetId.getAssetName(assetId);
  const policyId = Cardano.AssetId.getPolicyId(assetId);
  return {
    assetId,
    fingerprint: Cardano.AssetFingerprint.fromParts(policyId, name),
    name,
    policyId,
    quantity: 0n,
    supply: 0n
  };
};
 
const mergeAssets = (
  assetIds: Cardano.AssetId[],
  cachedAssetsInfo: Map<Cardano.AssetId, Asset.AssetInfo>,
  assetsFromProvider: Asset.AssetInfo[],
  mintedAssets: Asset.AssetInfo[] | null
): Asset.AssetInfo[] =>
  assetIds.map((assetId) => {
    const asset = cachedAssetsInfo.get(assetId) || assetsFromProvider.find((a) => a.assetId === assetId);
    const mintedAsset = mintedAssets?.find((info) => info.assetId === assetId);
 
    if (!asset && !mintedAsset) {
      return createFallbackAsset(assetId);
    }
 
    if (!asset && mintedAsset) {
      return mintedAsset;
    }
 
    if (asset && mintedAsset) {
      asset.supply += mintedAsset.supply;
      asset.quantity = asset.supply;
 
      if (mintedAsset.nftMetadata) {
        asset.nftMetadata = mintedAsset.nftMetadata;
      }
    }
 
    return asset!;
  });
 
/**
 * Creates a wallet asset provider. This provider will try to first fetch the asset from the local cache (assetInfo$),
 * then from the provider and finally from the transaction if it was minted in the transaction. If the asset can not be found
 * it will return a dummy AssetInfo with both supply and quantity set to 0.
 */
export const createWalletAssetProvider = ({
  assetProvider,
  assetInfo$,
  tx,
  logger
}: AssetProviderContext): AssetProvider => ({
  async getAsset({ assetId }: GetAssetArgs): Promise<Asset.AssetInfo> {
    const mintedAssets = tx ? await getMintedAssetInfosFromTx(tx, logger) : [];
    const cachedAssetsInfo = await firstValueFrom(assetInfo$);
 
    let asset = cachedAssetsInfo.get(assetId);
 
    if (!asset) {
      try {
        asset = await assetProvider.getAsset({ assetId, extraData: { nftMetadata: true, tokenMetadata: true } });
      } catch (error) {
        logger.error(error);
      }
    }
 
    const mintedAsset = mintedAssets?.find((info) => info.assetId === assetId);
 
    // Let's create dummy AssetInfo for the unresolved asset. This is probably better than throwing as the UI can still present it as regular token.
    if (!asset && !mintedAsset) {
      const name = Cardano.AssetId.getAssetName(assetId);
      const policyId = Cardano.AssetId.getPolicyId(assetId);
      return {
        assetId,
        fingerprint: Cardano.AssetFingerprint.fromParts(policyId, name),
        name,
        policyId,
        quantity: 0n,
        supply: 0n
      };
    }
 
    if (!asset) return mintedAsset!;
 
    if (mintedAsset) {
      asset.supply += mintedAsset.supply;
 
      // We give preference to the metadata in the transaction if preset as this would be the most up to date.
      if (mintedAsset.nftMetadata) {
        asset.nftMetadata = mintedAsset.nftMetadata;
      }
    }
 
    const cip68NftMetadata = tx ? tryCip68NftMetadata(asset.policyId, asset.name, tx, logger) : null;
    if (cip68NftMetadata) asset.nftMetadata = cip68NftMetadata;
 
    return asset;
  },
 
  async getAssets({ assetIds }: GetAssetsArgs): Promise<Asset.AssetInfo[]> {
    const cachedAssetsInfo = await firstValueFrom(assetInfo$);
    const mintedAssets = tx ? await getMintedAssetInfosFromTx(tx, logger) : [];
    const missingAssetIds = assetIds.filter((assetId) => !cachedAssetsInfo.has(assetId));
    const assetsFromProvider = await fetchAssetsFromProvider(assetProvider, missingAssetIds, logger);
    const mergedAssets = mergeAssets(assetIds, cachedAssetsInfo, assetsFromProvider, mintedAssets);
 
    const assets = mergedAssets.filter(isNotNil);
 
    if (tx) {
      for (const asset of assets) {
        const cip68NftMetadata = tryCip68NftMetadata(asset.policyId, asset.name, tx, logger);
        if (cip68NftMetadata) asset.nftMetadata = cip68NftMetadata;
      }
    }
 
    return mergedAssets.filter(isNotNil);
  },
 
  healthCheck(): Promise<HealthCheckResponse> {
    return assetProvider.healthCheck();
  }
});