4

I'm trying to deploy a smart contract on cardano-cli after its implementation on plutus playground.

Imagine that I have:

data TradeDatum = StartBid | Bid TradeDetails | Offer TradeDetails 
    deriving (Generic, ToJSON, FromJSON)


data TradeDetails = TradeDetails
    { tradeOwner :: !PubKeyHash
    , budId :: !BuiltinByteString
    , requestedAmount :: !Integer
    } deriving (Generic, ToJSON, FromJSON)

How can I write this in an example JSON to submit on cardano-cli? I saw an example with int: 42,

{
  "constructor": 0,
  "fields": {int: 42}
}

but I really don't know.

  1. How can I define the different types?
  2. Inside the tradedetails, how can I define? and for example, how to express the pubkeyhash?

Can you give me an exmaple? There are some tutorials with the types of data that I can write inside the JSON (like int)?

Thanks.

2 Answers 2

5

Have a look at the following source Cardano-Api Haddock. Here you will find all about the conversion between Haskell data types and the Json format the cardano-cli uses.

I noticed that you used the marketplace of SpaceBudz as an example, so lets have a look at the other code in that repository. Notice the line 99.

print $ "Datum value: " <> encode (scriptDataToJson ScriptDataJsonDetailedSchema $ fromPlutusData (Plutus.toData offerDatum))

Here that function prints the offerDatum defined at line 85. You see here how the function from the cardano-api are used and then encoded to convert the datum to a json form? Here the <> symbol just means concatenate the two strings.

For a more detailed explanation of what is going on in this line have a look at the PPP cohort 3 lecture 3 from lars on youtube.

1
  • got it. thanks :)
    – pprs
    May 19, 2022 at 16:40
3

{ "constructor": 0, "fields": {int: 42} }

This isn't valid because fields must be an array. The index of the constructor and fields will match the order of types defined for the validator script. For a Bid you'd use constructor 0 with a single field which is a json representation of TradeDetails (bytestring, bytestring, integer). It is easier and safer to use scriptDataToJson.

2
  • got it. thanks :)
    – pprs
    May 19, 2022 at 16:40
  • This answer helps me, indirectly. Thanks
    – Adam
    Dec 8, 2022 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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