3

By following the alonzo-testnet exercises, I'm right now having to build a transaction with cardano-cli. One of the arguments necessary to consume a script is the datum, another is the redeemer, both of which should be in a JSON file (or value).

Is there any reference of how Data should be expressed in JSON? Because I tried converting it with Data.Aeson in Haskell, but received the following error

JSON object does not match the schema.
Expected a single field named "int", "bytes", "string", "list" or "map".
Unexpected object field(s): {"contents":{"getPubKeyHash":"3e7181860513a17dfa44d69232429e5c328e79c748d4a176950c42b6"},"tag":"Collect"}

Also, is there any library to make my life easier, so I can be able to easily convert my redeemers into JSON?

2 Answers 2

1

Reading some old messages in discord, someone mentioned that I could use encode (scriptDataToJson ScriptDataJsonDetailedSchema $ fromPlutusData data), taken from here, so I made a redeemerJSON function defined as follows:

redeemerJSON :: DistributionRedeemer -> ByteString
redeemerJSON dr =
  encode (scriptDataToJson ScriptDataJsonDetailedSchema $ fromPlutusData (PlutusTx.toData dr))

Of course some imports are necessary, but I believe you can use this same approach for the datum and other script parameters too.

1
  • 1
    I would make it even more general to: haskell plutusDataToJSON :: PlutusTx.ToData a => a -> Data.ByteString.Lazy.Internal.ByteString plutusDataToJSON = encode . (scriptDataToJson ScriptDataJsonDetailedSchema) . fromPlutusData . Plutus.toData Nov 24, 2021 at 1:11
0

The Cardano-node github is the best resource that I have found

JSON conversion

Script data is similar to JSON but not exactly the same. It has some deliberate limitations such as no support for floating point numbers or special forms for null or boolean values. It also has limitations on the length of strings.

On the other hand, unlike JSON, it distinguishes between byte strings and text strings. It also supports any value as map keys rather than just string. It also supports alternatives / tagged unions, used for representing constructors for Plutus data values.

continues ....

Further extensive details in Cardano-node github file ScriptData.hs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.