Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions cardano-api/src/Cardano/Api/Tx/Internal/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -586,31 +586,35 @@ instance IsShelleyBasedEra era => FromJSON (TxOut CtxTx era) where
alonzoTxOutParser
:: AlonzoEraOnwards era -> Aeson.Object -> Aeson.Parser (TxOut CtxTx era)
alonzoTxOutParser w o = do
-- Parse optional datum fields (both hash and JSON value may be present)
mDatumHash <- o .:? "datumhash"
mDatumVal <- o .:? "datum"
case (mDatumVal, mDatumHash) of
(Nothing, Nothing) ->
TxOut
<$> o .: "address"
<*> o .: "value"
<*> return TxOutDatumNone
<*> return ReferenceScriptNone
(Just dVal, Just{}) -> do
case scriptDataJsonToHashable ScriptDataJsonDetailedSchema dVal of
Left e -> fail $ "Error parsing ScriptData: " <> show e
Right hashableData ->
TxOut
<$> o .: "address"
<*> o .: "value"
<*> return (TxOutSupplementalDatum w hashableData)
<*> return ReferenceScriptNone
(Nothing, Just dHash) ->
TxOut
<$> o .: "address"
<*> o .: "value"
<*> return (TxOutDatumHash w dHash)
<*> return ReferenceScriptNone
(Just _dVal, Nothing) -> fail "Only datum JSON was found, this should not be possible."
mDatumJson <- o .:? "datum"

-- Parse required fields common to all TxOuts
parsedAddress <- o .: "address"
parsedValue <- o .: "value"

-- Parse datum based on which fields are present
txOutDatum <-
case (mDatumJson, mDatumHash) of
-- No datum information provided
(Nothing, Nothing) ->
pure TxOutDatumNone
-- Only datum hash provided (datum not included in tx)
(Nothing, Just dHash) ->
pure (TxOutDatumHash w dHash)
-- Both hash and JSON provided (supplemental datum in tx body)
(Just datumJson, Just{}) -> do
case scriptDataJsonToHashable ScriptDataJsonDetailedSchema datumJson of
Left e -> fail $ "Error parsing ScriptData: " <> show e
Right hashableData ->
pure (TxOutSupplementalDatum w hashableData)
-- Only JSON provided without hash (invalid state)
(Just _datumJson, Nothing) ->
fail "Only datum JSON was found, this should not be possible."

-- Note: ReferenceScript is always None for Alonzo-era parsing
pure $ TxOut parsedAddress parsedValue txOutDatum ReferenceScriptNone

instance IsShelleyBasedEra era => FromJSON (TxOut CtxUTxO era) where
parseJSON = withObject "TxOut" $ \o -> do
Expand Down
Loading