2

Let's say I have validator, which checks datum against two numbers, hash, time and a string and there are some funds locked at this script address and I would like to unlock these funds using cardano-cli --tx-in-datum-file... by providing datum file. Inside source code I have a custom data type like so:

data CustomDatum = CustomDatum
    { num1 :: Integer
    , num2 :: Integer
    , hash1 :: PubKeyHash
    , time1 :: POSIXTime
    , string1 :: String
    }
    deriving Show

PlutusTx.unstableMakeIsData ''VestingDatum

What are the rules to form correct datum file?

How should typed and not typed datum file look like to match my CustomDatum inside Plutus script?

How are values mapped between datum file and custom data inside Plutus script?

2 Answers 2

1

Here is an example of a haskell executable that will convert any of your custom data types to the right JSON format : https://github.com/OnChainLabs/Martify/blob/main/app/datum-json.hs

In this file we convert the type NFTSale which is defined like so :

data NFTSale = NFTSale
    { nSeller    :: !PubKeyHash
    , nPrice     :: !Integer
    , nCurrency  :: !CurrencySymbol
    , nToken     :: !TokenName
    } deriving (Generic, ToJSON, FromJSON)

But it works for any other data type.

0

I managed to make a working example with more than one parameter. I still don't understand underlying principles, but at least here is something to start working with:

data CustomDatum = CustomDatum
    { num1 :: Integer
    , num2 :: Integer
    }
    deriving Show

PlutusTx.makeIsDataIndexed ''CustomDatum [('CustomDatum, 0)]

-- mkValidator :: Datum -> Redeemer -> ScriptContext -> ()
{-# INLINABLE mkValidator #-}
mkValidator :: CustomDatum -> () -> ScriptContext -> Bool
mkValidator dat () ctx = traceIfFalse "Numbers are missing" numbersAreCorrect
  where
    numbersAreCorrect :: Bool
    numbersAreCorrect = ((num1 dat) == 24) && ((num2 dat) == 42)

Example datum

{
    "constructor":0,
    "fields":
    [
        {
            "int":24
        },
        {
            "int":42
        }
    ]
}

Example redeemer

{ "constructor": 0, "fields":[] }
1
  • And as for the PubKeyHash, "bytes":"25e1081f483086365f62fa37618ad6c4768864806fc874b56abe59d7" works
    – KugisMugis
    Nov 24, 2021 at 13:38

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.