<p>This is the code to deserialize the UTxOs of an address:</p>
<pre><code>for (const rawUtxo of rawUtxos) {
      const utxo = C.TransactionUnspentOutput.from_bytes(Buffer.from(rawUtxo, &quot;hex&quot;));
      const input = utxo.input();
      const txid = Buffer.from(input.transaction_id().to_bytes(), &quot;utf8&quot;).toString(&quot;hex&quot;);
      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 = &quot;&quot;;

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

          for (let j = 0; j &lt; K; j++) {
            const assetName = assetNames.get(j);
            const assetNameString = Buffer.from(assetName.name(), &quot;utf8&quot;).toString();
            const assetNameHex = Buffer.from(assetName.name(), &quot;utf8&quot;).toString(&quot;hex&quot;)
            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);
</code></pre>

CC BY-SA 4.0
2022-11-06T02:08:44.430Z
2022-11-06T02:08:44.430Z
Falcon Stakepool
<p>I'm getting the following error when trying to deserialize a UTxO obtained using <code>cardano-graphql</code>:</p>
<pre><code>Deserialization failed in TransactionUnspentOutput because: No variant matched
</code></pre>
<p>This is the code that it's failing:</p>
<pre><code>for (const rawUtxo of rawUtxos) {
      const utxo = CardanoWasm.TransactionUnspentOutput.from_bytes(
        Buffer.from(rawUtxo.transaction.hash, &quot;hex&quot;)
      )
      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()
</code></pre>
<p>What am I doing wrong?</p>

<cardano-serialization-lib><utxo><cardano-graphql>
What's the correct way of deserializing a UTxO?
# What's the correct way of deserializing a UTxO?

(Asked by: username_0 on 2022-05-31)

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?

**Tags:** cardano-serialization-lib, utxo, cardano-graphql

---

## 1 answers

### Answer by username_0 on 2022-11-06. Score: 0 | Accepted answer

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);


---

