All files / src/Utxo/DbSyncUtxoProvider mappers.ts

84% Statements 42/50
72% Branches 18/25
100% Functions 3/3
83.33% Lines 40/48

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 13136x                 36x 36x 36x               36x   12x     6x         6x 6x   3x         3x         3x   3x         3x         3x                                     12x                 36x   10x 197x 197x 197x 12x 12x 12x 12x 12x 12x   12x   185x 185x           185x 10x   185x 10x   185x   185x 17x       185x                 197x   10x    
import {
  Cardano,
  Serialization,
  SerializationError,
  SerializationFailure,
  createUtxoId,
  jsonToNativeScript
} from '@cardano-sdk/core';
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob, isNotNil } from '@cardano-sdk/util';
import { ReferenceScriptType, UtxoModel } from './types';
import { generateAssetId } from './util';
 
/**
 * Parse the reference script from the UtxoModel
 *
 * @param model The UtxoModel from dbSync.
 * @returns The reference Script.
 */
const parseReferenceScript = (model: UtxoModel): Cardano.Script => {
  let script: Cardano.Script;
  switch (model.reference_script_type) {
    case ReferenceScriptType.Timelock:
    case ReferenceScriptType.Multisig:
      Iif (!isNotNil(model.reference_script_json))
        throw new SerializationError(
          SerializationFailure.InvalidScript,
          'Unexpected error deserializing Native script. Data is null'
        );
      script = jsonToNativeScript(model.reference_script_json);
      break;
    case ReferenceScriptType.PlutusV1:
      Iif (!isNotNil(model.reference_script_bytes))
        throw new SerializationError(
          SerializationFailure.InvalidScript,
          'Unexpected error deserializing PlutusV1 script. Data is null'
        );
      script = {
        __type: Cardano.ScriptType.Plutus,
        bytes: model.reference_script_bytes as unknown as HexBlob,
        version: Cardano.PlutusLanguageVersion.V1
      };
      break;
    case ReferenceScriptType.PlutusV2:
      Iif (!isNotNil(model.reference_script_bytes))
        throw new SerializationError(
          SerializationFailure.InvalidScript,
          'Unexpected error deserializing PlutusV2 script. Data is null'
        );
      script = {
        __type: Cardano.ScriptType.Plutus,
        bytes: model.reference_script_bytes as unknown as HexBlob,
        version: Cardano.PlutusLanguageVersion.V2
      };
      break;
    case ReferenceScriptType.PlutusV3:
      Iif (!isNotNil(model.reference_script_bytes))
        throw new SerializationError(
          SerializationFailure.InvalidScript,
          'Unexpected error deserializing PlutusV2 script. Data is null'
        );
      script = {
        __type: Cardano.ScriptType.Plutus,
        bytes: model.reference_script_bytes as unknown as HexBlob,
        version: Cardano.PlutusLanguageVersion.V3
      };
      break;
    default:
      throw new SerializationError(
        SerializationFailure.InvalidScriptType,
        `Unknown script type ${model.reference_script_type}`
      );
  }
  return script;
};
 
/**
 * Transform DB results into indexed core UTxO
 *
 * @param {UtxoModel[]} utxosModels  UTxO query rows
 * @returns {Cardano.Utxo[]} an array of core UTxO objects
 */
export const utxosToCore = (utxosModels: UtxoModel[]): Cardano.Utxo[] => {
  // eslint-disable-next-line complexity
  const utxosMap = utxosModels.reduce((utxos, current) => {
    const utxoId = createUtxoId(current.tx_id, current.index);
    const utxo = utxos.get(utxoId);
    if (utxo) {
      const txIn = utxo[0];
      const txOut = utxo[1];
      if (isNotNil(current.asset_name) && current.asset_policy && current.asset_quantity) {
        const newAssets = txOut.value.assets || new Map<Cardano.AssetId, bigint>();
        newAssets.set(generateAssetId(current.asset_policy, current.asset_name), BigInt(current.asset_quantity));
        txOut.value.assets = newAssets;
      }
      utxos.set(utxoId, [txIn, txOut]);
    } else {
      const address = current.address as unknown as Cardano.PaymentAddress;
      const txOut: Cardano.TxOut = {
        address,
        value: {
          coins: BigInt(current.coins)
        }
      };
      if (isNotNil(current.inline_datum)) {
        txOut.datum = Serialization.PlutusData.fromCbor(HexBlob(current.inline_datum)).toCore();
      }
      if (isNotNil(current.data_hash)) {
        txOut.datumHash = current.data_hash as unknown as Hash32ByteBase16;
      }
      if (isNotNil(current.reference_script_type)) txOut.scriptReference = parseReferenceScript(current);
 
      if (isNotNil(current.asset_name) && current.asset_policy && current.asset_quantity) {
        txOut.value.assets = new Map<Cardano.AssetId, bigint>([
          [generateAssetId(current.asset_policy, current.asset_name), BigInt(current.asset_quantity)]
        ]);
      }
      utxos.set(utxoId, [
        {
          address,
          index: current.index,
          txId: current.tx_id as unknown as Cardano.TransactionId
        },
        txOut
      ]);
    }
    return utxos;
  }, new Map<string, Cardano.Utxo>());
  return [...utxosMap.values()];
};