0

I have two main actions that user can do. They both require sending utxos to a script address. The datum I want produced by these two actions are different. I want one of the datum like this

data VotingDatum = VotingDatum
  { projectPubKey :: PaymentPubKeyHash
  , amount :: Integer
  , fund :: Integer
  , paymentPubKey :: PaymentPubKeyHash
  }deriving (Show)

and the other one like this.

data StartDatum = StartDatum
 { startFund :: Integer
 , fundPrize :: Integer
 }deriving (Show)

I was thinking of putting these two in a single data type like this

data ContractDatum = ContractDatum
 { voteDatum :: Maybe VotingDatum
 , startDatum :: Maybe StartDatum
 }

How would i set the value of these fields if i did it like this. Can i do it like this ? I am currently setting the value of the types like this

start sp = do 
    let dat = StartDatum
                { startFund = spFund sp 
                , fundPrize = spAmount sp
                }

How can i have two different data types for the datum. How can i set the value of it , and how would i access the value of it.

1 Answer 1

1

You can combine the two like this

data DatumAction = VotingDatum | StartDatum

Then you can specify in you validator which case is used like this

mkValidator :: DatumAction -> redeemer -> ScriptContext -> Bool
mkValidator datumAction redeemer context = case datumAction of
    VotingDatum -> do something with type bool
    StartDatum  -> do something with type bool

Hope this helps, also see (1) and (2).

1
  • Yea , this is what i am looking for but I think i am missing something.It giving errors saying it couldn't match type StartDatum with type DatumAction and it couldnt match type VotingDatum with type Datum Action
    – szhang9921
    Commented Apr 15, 2022 at 13:42

Your Answer

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

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