<p>Well the last error tells you that you don't have a <code>Maybe</code> but an <code>Either</code> type. You're trying to pattern match against a <code>Maybe</code> instead of <code>Either</code> at this point in your code:</p>
<pre><code>case txOutDatum (txOutTxOut o) of
   (Left _)  -&gt; ...
   (Right _) -&gt; ...
</code></pre>
<p>My guess is you actually want to do something like this:</p>
<pre><code>getMyDatum :: ChainIndexTxOut -&gt; MyDatum
getMyDatum o = case _ciTxOutDatum o of
    Left _ -&gt; traceError &quot;Unexpected Datumhash&quot;
    Right (Datum d) -&gt; do
        let parsedDatum = PlutusTx.fromBuiltinData d :: Maybe MyDatum
        case parsedDatum of
            Just s -&gt; s
            _ -&gt; traceError &quot;Unknown datum type&quot;

</code></pre>

CC BY-SA 4.0
2022-09-12T08:08:21.880Z
Thank you so much I really appreciate the help, that solved it :)
NaeriKailash
CC BY-SA 4.0
2022-09-12T08:09:28.013Z
No prob, glad I could help. Would appreciate if you can mark my response as answer with the checkmark icon :).
Will
CC BY-SA 4.0
2022-09-12T07:45:57.527Z
2022-09-12T07:45:57.527Z
Will
<p>I have this function that is expecting type <code>TxOutTx</code>, but it receives <code>ChainIndexTxOut</code>. I'm trying to convert to the type required but encountering issues</p>
<pre><code>getTradeDatum o = case txOutDatum (txOutTxOut o) of
    Just h -&gt; do
        let [(_,datum)] = P.filter (\(h',_) -&gt; h == h') (Map.toList (txData (txOutTxTx o)))
        let parsedDatum = PlutusTx.fromBuiltinData (getDatum datum) :: Maybe TradeDatum
        case parsedDatum of
            Just b -&gt; b
            _ -&gt; traceError &quot;expected datum&quot;
    _ -&gt; traceError &quot;expected datum&quot;
</code></pre>
<p>I get the error:</p>
<pre><code>    * Couldn't match expected type `TxOutTx'
                  with actual type `ChainIndexTxOut'
    * In the first argument of `txOutTxOut', namely `o'
      In the first argument of `txOutDatum', namely `(txOutTxOut o)'
      In the expression: txOutDatum (txOutTxOut o)
    |
586 | getTradeDatum o = case txOutDatum (txOutTxOut o) of
    |                               
</code></pre>
<p>I've tried the following solution, changing the single line below, because <code>_ciTxOutValue</code> has worked for me before:</p>
<pre><code>getTradeDatum o = case _ciTxOutDatum o of
</code></pre>
<p>But then I get this error:</p>
<pre><code> error:
    * Couldn't match expected type `Either DatumHash Datum'
                  with actual type `Maybe DatumHash'
    * In the pattern: Just h
</code></pre>

<smart-contract><plutus-playground><types>
Convert ChainIndexTxOut to TxOutTx
# Convert ChainIndexTxOut to TxOutTx

(Asked by: username_0 on 2022-09-12)

I have this function that is expecting type TxOutTx, but it receives ChainIndexTxOut. I'm trying to convert to the type required but encountering issues
getTradeDatum o = case txOutDatum (txOutTxOut o) of
    Just h -> do
        let [(_,datum)] = P.filter (\(h',_) -> h == h') (Map.toList (txData (txOutTxTx o)))
        let parsedDatum = PlutusTx.fromBuiltinData (getDatum datum) :: Maybe TradeDatum
        case parsedDatum of
            Just b -> b
            _ -> traceError "expected datum"
    _ -> traceError "expected datum"

I get the error:
    * Couldn't match expected type `TxOutTx'
                  with actual type `ChainIndexTxOut'
    * In the first argument of `txOutTxOut', namely `o'
      In the first argument of `txOutDatum', namely `(txOutTxOut o)'
      In the expression: txOutDatum (txOutTxOut o)
    |
586 | getTradeDatum o = case txOutDatum (txOutTxOut o) of
    |                               

I've tried the following solution, changing the single line below, because _ciTxOutValue has worked for me before:
getTradeDatum o = case _ciTxOutDatum o of

But then I get this error:
 error:
    * Couldn't match expected type `Either DatumHash Datum'
                  with actual type `Maybe DatumHash'
    * In the pattern: Just h

**Tags:** smart-contract, plutus-playground, types

---

## 1 answers

### Answer by username_1 on 2022-09-12. Score: 0 | Accepted answer

Well the last error tells you that you don't have a Maybe but an Either type. You're trying to pattern match against a Maybe instead of Either at this point in your code:
case txOutDatum (txOutTxOut o) of
   (Left _)  -> ...
   (Right _) -> ...

My guess is you actually want to do something like this:
getMyDatum :: ChainIndexTxOut -> MyDatum
getMyDatum o = case _ciTxOutDatum o of
    Left _ -> traceError "Unexpected Datumhash"
    Right (Datum d) -> do
        let parsedDatum = PlutusTx.fromBuiltinData d :: Maybe MyDatum
        case parsedDatum of
            Just s -> s
            _ -> traceError "Unknown datum type"

> **Comments:**
> **username_0**: Thank you so much I really appreciate the help, that solved it :)  
> **username_1**: No prob, glad I could help. Would appreciate if you can mark my response as answer with the checkmark icon :).  

---

