2

I'm getting the following error when trying to deserialize a UTxO obtained using cardano-graphql:

Deserialization failed in TransactionUnspentOutput because: No variant matched

This is the code that it's failing:

for (const rawUtxo of rawUtxos) {
      const utxo = CardanoWasm.TransactionUnspentOutput.from_bytes(
        Buffer.from(rawUtxo.transaction.hash, "hex")
      )
      const input = utxo.input()
      const txid = input.transaction_id().to_bytes().toString();
      const txindx = input.index()
      const output = utxo.output()
      const amount = output.amount().coin().to_str() // amount in lovelace
      const multiasset = output.amount().multiasset()

What am I doing wrong?

1 Answer 1

0

This is the code to deserialize the UTxOs of an address:

for (const rawUtxo of rawUtxos) {
      const utxo = C.TransactionUnspentOutput.from_bytes(Buffer.from(rawUtxo, "hex"));
      const input = utxo.input();
      const txid = Buffer.from(input.transaction_id().to_bytes(), "utf8").toString("hex");
      const txindx = showPtrInHex(input.index());   
      const output = utxo.output();
      const amount = output.amount().coin().to_str(); // ADA amount in lovelace
      const multiasset = output.amount().multiasset();
      let multiAssetStr = "";

      if (multiasset) {
        const keys = multiasset.keys() // policy Ids of thee multiasset
        const N = keys.len();
    
        for (let i = 0; i < N; i++) {
          const policyId = keys.get(i);
          const policyIdHex = Buffer.from(policyId.to_bytes(), "utf8").toString("hex");
          const assets = multiasset.get(policyId)
          const assetNames = assets.keys();
          const K = assetNames.len()

          for (let j = 0; j < K; j++) {
            const assetName = assetNames.get(j);
            const assetNameString = Buffer.from(assetName.name(), "utf8").toString();
            const assetNameHex = Buffer.from(assetName.name(), "utf8").toString("hex")
            const multiassetAmt = multiasset.get_asset(policyId, assetName)
            multiAssetStr += ` + ${multiassetAmt.to_str()} ${policyIdHex} . ${assetNameHex} (${assetNameString})`
          }
        }
      }
    
      const obj = {
        txid: txid,
        txindx: txindx,
        amount: amount,
        str: `${txid} # ${txindx} = ${amount}`,
        multiAssetStr: multiAssetStr,
        TransactionUnspentOutput: utxo
      }

      utxos.push(obj);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.