0

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

1 Answer 1

0

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"

2
  • 1
    Thank you so much I really appreciate the help, that solved it :) Sep 12, 2022 at 8:08
  • No prob, glad I could help. Would appreciate if you can mark my response as answer with the checkmark icon :).
    – Will
    Sep 12, 2022 at 8:09

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.