Skip to content

Commit 8c94320

Browse files
committed
refactor(api): extract inline datum parsing into helper function
Eliminates ~50 lines of code duplication by extracting the repeated inline datum parsing logic from Babbage/Conway/Dijkstra cases into a single parseInlineDatum helper function. The refactored helper: - Parses both inlineDatumhash and inlineDatum fields - Validates that the datum matches its hash - Handles era-specific parsing differences between Babbage (scriptDataJsonToHashable) and Conway/Dijkstra (scriptDataFromJson) - Maintains identical behavior with all existing tests passing This consolidation improves maintainability by ensuring consistent error handling and validation across all Babbage+ eras.
1 parent bf43266 commit 8c94320

File tree

1 file changed

+31
-60
lines changed
  • cardano-api/src/Cardano/Api/Tx/Internal

1 file changed

+31
-60
lines changed

cardano-api/src/Cardano/Api/Tx/Internal/Output.hs

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -457,77 +457,48 @@ instance IsShelleyBasedEra era => FromJSON (TxOut CtxTx era) where
457457
ShelleyBasedEraAlonzo -> alonzoTxOutParser AlonzoEraOnwardsAlonzo o
458458
ShelleyBasedEraBabbage -> do
459459
alonzoTxOutInBabbage <- alonzoTxOutParser AlonzoEraOnwardsBabbage o
460-
461-
-- We check for the existence of inline datums
462-
inlineDatumHash <- o .:? "inlineDatumhash"
463-
inlineDatum <- o .:? "inlineDatum"
464-
mInlineDatum <-
465-
case (inlineDatum, inlineDatumHash) of
466-
(Just dVal, Just h) -> do
467-
case scriptDataJsonToHashable ScriptDataJsonDetailedSchema dVal of
468-
Left err ->
469-
fail $ "Error parsing TxOut JSON: " <> displayError err
470-
Right hashableData -> do
471-
if hashScriptDataBytes hashableData /= h
472-
then fail "Inline datum not equivalent to inline datum hash"
473-
else return $ TxOutDatumInline BabbageEraOnwardsBabbage hashableData
474-
(Nothing, Nothing) -> return TxOutDatumNone
475-
(_, _) ->
476-
fail
477-
"Should not be possible to create a tx output with either an inline datum hash or an inline datum"
478-
460+
mInlineDatum <- parseInlineDatum BabbageEraOnwardsBabbage o
479461
mReferenceScript <- o .:? "referenceScript"
480-
481462
reconcileBabbage alonzoTxOutInBabbage mInlineDatum mReferenceScript
482463
ShelleyBasedEraConway -> do
483464
alonzoTxOutInConway <- alonzoTxOutParser AlonzoEraOnwardsConway o
484-
485-
-- We check for the existence of inline datums
486-
inlineDatumHash <- o .:? "inlineDatumhash"
487-
inlineDatum <- o .:? "inlineDatum"
488-
mInlineDatum <-
489-
case (inlineDatum, inlineDatumHash) of
490-
(Just dVal, Just h) ->
491-
case scriptDataFromJson ScriptDataJsonDetailedSchema dVal of
492-
Left err ->
493-
fail $ "Error parsing TxOut JSON: " <> displayError err
494-
Right sData ->
495-
if hashScriptDataBytes sData /= h
496-
then fail "Inline datum not equivalent to inline datum hash"
497-
else return $ TxOutDatumInline BabbageEraOnwardsConway sData
498-
(Nothing, Nothing) -> return TxOutDatumNone
499-
(_, _) ->
500-
fail
501-
"Should not be possible to create a tx output with either an inline datum hash or an inline datum"
502-
465+
mInlineDatum <- parseInlineDatum BabbageEraOnwardsConway o
503466
mReferenceScript <- o .:? "referenceScript"
504-
505467
reconcileConway ConwayEraOnwardsConway alonzoTxOutInConway mInlineDatum mReferenceScript
506468
ShelleyBasedEraDijkstra -> do
507469
alonzoTxOutInConway <- alonzoTxOutParser AlonzoEraOnwardsDijkstra o
508-
509-
-- We check for the existence of inline datums
510-
inlineDatumHash <- o .:? "inlineDatumhash"
511-
inlineDatum <- o .:? "inlineDatum"
512-
mInlineDatum <-
513-
case (inlineDatum, inlineDatumHash) of
514-
(Just dVal, Just h) ->
515-
case scriptDataFromJson ScriptDataJsonDetailedSchema dVal of
516-
Left err ->
517-
fail $ "Error parsing TxOut JSON: " <> displayError err
518-
Right sData ->
519-
if hashScriptDataBytes sData /= h
520-
then fail "Inline datum not equivalent to inline datum hash"
521-
else return $ TxOutDatumInline BabbageEraOnwardsDijkstra sData
522-
(Nothing, Nothing) -> return TxOutDatumNone
523-
(_, _) ->
524-
fail
525-
"Should not be possible to create a tx output with either an inline datum hash or an inline datum"
526-
470+
mInlineDatum <- parseInlineDatum BabbageEraOnwardsDijkstra o
527471
mReferenceScript <- o .:? "referenceScript"
528-
529472
reconcileConway ConwayEraOnwardsDijkstra alonzoTxOutInConway mInlineDatum mReferenceScript
530473
where
474+
-- | Parse inline datum fields from JSON object
475+
-- Handles both inlineDatumhash and inlineDatum fields, validating they match
476+
parseInlineDatum
477+
:: BabbageEraOnwards era
478+
-> Aeson.Object
479+
-> Aeson.Parser (TxOutDatum CtxTx era)
480+
parseInlineDatum w o = do
481+
inlineDatumHash <- o .:? "inlineDatumhash"
482+
inlineDatum <- o .:? "inlineDatum"
483+
case (inlineDatum, inlineDatumHash) of
484+
(Just dVal, Just h) -> do
485+
sData <- case (convert w :: ShelleyBasedEra era) of
486+
ShelleyBasedEraBabbage ->
487+
case scriptDataJsonToHashable ScriptDataJsonDetailedSchema dVal of
488+
Left err -> fail $ "Error parsing TxOut JSON: " <> displayError err
489+
Right hashableData -> return hashableData
490+
_ ->
491+
case scriptDataFromJson ScriptDataJsonDetailedSchema dVal of
492+
Left err -> fail $ "Error parsing TxOut JSON: " <> displayError err
493+
Right scriptData -> return scriptData
494+
if hashScriptDataBytes sData /= h
495+
then fail "Inline datum not equivalent to inline datum hash"
496+
else return $ TxOutDatumInline w sData
497+
(Nothing, Nothing) -> return TxOutDatumNone
498+
(_, _) ->
499+
fail
500+
"Should not be possible to create a tx output with either an inline datum hash or an inline datum"
501+
531502
reconcileBabbage
532503
:: TxOut CtxTx BabbageEra
533504
-- \^ Alonzo era datum in Babbage era

0 commit comments

Comments
 (0)