Skip to content

Add ToJSON/FromJSON instances for EraTxAuxData#5853

Open
koslambrou wants to merge 1 commit into
koslambrou/erascript-jsonfrom
koslambrou/eratxauxdata-json
Open

Add ToJSON/FromJSON instances for EraTxAuxData#5853
koslambrou wants to merge 1 commit into
koslambrou/erascript-jsonfrom
koslambrou/eratxauxdata-json

Conversation

@koslambrou
Copy link
Copy Markdown
Contributor

@koslambrou koslambrou commented May 20, 2026

Description

  • Add ToJSON/FromJSON and NFData as EraTxAuxData superclass constraints
  • Add ToJSON/FromJSON for Metadatum
  • Add ToJSON/FromJSON for Data era and PlutusBinary
  • Add FromJSON for PoolCert, ConwayGovCert, DijkstraDelegCert, DijkstraTxCert era
  • Add ToJSON/FromJSON for ShelleyTxAuxData, AllegraTxAuxData, AlonzoTxAuxData
  • Add round-trip JSON property test for TxAuxData era

Checklist

  • Commits in meaningful sequence and with useful messages.
  • Tests added or updated when needed.
  • CHANGELOG.md files updated for packages with externally visible changes.
    NOTE: New section is never added with the code changes. (See RELEASING.md).
  • Versions updated in .cabal and CHANGELOG.md files when necessary, according to the
    versioning process.
  • Version bounds in .cabal files updated when necessary.
    NOTE: If bounds change in a cabal file, that package itself must have a version increase. (See RELEASING.md).
  • Code formatted (use scripts/fourmolize.sh).
  • Cabal files formatted (use scripts/cabal-format.sh).
  • CDDL files are up to date (use scripts/gen-cddl.sh)
  • hie.yaml updated (use scripts/gen-hie.sh).
  • Self-reviewed the diff.

@koslambrou koslambrou force-pushed the koslambrou/eratxauxdata-json branch from e774714 to 7cb4022 Compare May 20, 2026 18:43

instance Typeable era => NoThunks (Data era)

instance ToJSON (Data era) where
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These instance implementations come from cardano-api analogous ScriptData datatype.

instance DecCBOR Metadatum where
decCBOR = decodeMetadatum

instance ToJSON Metadatum where
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These instance implementations come from cardano-api's analogous datatype TxMetadata

@koslambrou koslambrou force-pushed the koslambrou/erascript-json branch from e91e67d to 64fada4 Compare May 20, 2026 18:48
@koslambrou koslambrou force-pushed the koslambrou/eratxauxdata-json branch 2 times, most recently from edd3dc5 to 78c3261 Compare May 20, 2026 18:51
@koslambrou koslambrou force-pushed the koslambrou/erascript-json branch from 64fada4 to a015bf1 Compare May 20, 2026 18:56
@koslambrou koslambrou force-pushed the koslambrou/eratxauxdata-json branch 2 times, most recently from b22f263 to 6795448 Compare May 20, 2026 19:18
@koslambrou koslambrou marked this pull request as ready for review May 20, 2026 19:18
@koslambrou koslambrou requested a review from a team as a code owner May 20, 2026 19:18
Copy link
Copy Markdown
Collaborator

@lehins lehins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like good progress 👍

Comment thread eras/allegra/impl/src/Cardano/Ledger/Allegra/TxAuxData.hs Outdated
Comment thread eras/allegra/impl/src/Cardano/Ledger/Allegra/TxAuxData.hs Outdated
Comment thread eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxAuxData.hs Outdated
Comment thread libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/Data.hs
Comment thread libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/Data.hs Outdated
Comment thread libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/Language.hs Outdated
Comment thread libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/Language.hs Outdated
Comment thread libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/Language.hs Outdated
Comment thread libs/cardano-ledger-core/src/Cardano/Ledger/Metadata.hs Outdated
metadata <- o .: "metadata"
nativeScripts <- StrictSeq.fromList <$> o .: "nativeScripts"
plutusScriptsRaw <- o .: "plutusScripts"
let plutusScripts = Map.mapMaybe NE.nonEmpty plutusScriptsRaw
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so this by itself is not going to work, because it would allow addition of Plutus scripts of an unsupported version for a particular era. See the pattern synonym AlonzoTxAuxData. So, what we need to do is extract the construction logic into a mkAlonzoTxAuxData function with MonadFail constraint, then it would be usable here in FromJSON instance as well as in AlonzoTxAuxData pattern synonym with errorFail from FailT package.

Copy link
Copy Markdown
Contributor Author

@koslambrou koslambrou May 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

There's already a mkAlonzoTxAuxData and a mkBinaryPlutusScript which has MonadFail. So the following works:

  parseJSON = withObject "AlonzoTxAuxData" $ \o -> do
    metadata <- o .: "metadata"
    nativeScripts <- o .:? "nativeScripts" .!= mempty
    plutusScriptsLangMap <- o .:? "plutusScripts" .!= mempty
    plutusScripts <-
      fmap concat $ forM (Map.toList plutusScriptsLangMap) $ \(lang, plutusScripts) ->
        traverse (fmap PlutusScript . mkBinaryPlutusScript lang) plutusScripts
    pure $ mkAlonzoTxAuxData metadata $ fmap NativeScript nativeScripts <> plutusScripts

Are you satisfied with that?

@koslambrou koslambrou force-pushed the koslambrou/erascript-json branch 2 times, most recently from 16ca7eb to ca7aa7b Compare May 21, 2026 15:14
@koslambrou koslambrou force-pushed the koslambrou/eratxauxdata-json branch 2 times, most recently from 547933d to f654838 Compare May 22, 2026 14:57
* Add ToJSON/FromJSON and NFData as EraTxAuxData superclass constraints
* Add ToJSON/FromJSON for Metadatum
* Add ToJSON/FromJSON for Data era and PlutusBinary
* Add FromJSON for PoolCert, ConwayGovCert, DijkstraDelegCert, DijkstraTxCert era
* Add ToJSON/FromJSON for ShelleyTxAuxData, AllegraTxAuxData, AlonzoTxAuxData
* Add round-trip JSON property test for TxAuxData era
@koslambrou koslambrou force-pushed the koslambrou/eratxauxdata-json branch from f654838 to e45b5a9 Compare May 22, 2026 15:02
@koslambrou koslambrou requested a review from lehins May 22, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants