0

I want to use a custom type for one of my policy parameters, which will be applied by a tool like Lucid.
Example of param:

data CoinParams = CoinParams {
    policyOwner :: PubKeyHash,
    tokenName :: TokenName
}

My policy will then look like this:

policy :: CoinParams -> () -> ScriptContext -> Bool
policy ... = ...

Before compilation I wrap the policy:

                    vvv
wrappedPolicy :: BuiltinData -> BuiltinData -> BuiltinData -> ()
wrappedPolicy params = wrapPolicy (policy $ unsafeFromBuiltinData params)
               ^^                                     ^^^           ^^

But this won't work because there is no UnsafeFromData CoinParams instance(required to use unsafeFromBuiltInData). I'm struggling to understand how to implement the instance because I'm not sure how to 'destructure' BuiltinData to map it to my custom type. Could anyone assist? Thanks

1 Answer 1

3

In order to programmatically generate "FromData" and "ToData" instances for custom data types we have the function unstableMakeIsData present in PlutusTx (https://github.com/input-output-hk/plutus/blob/master/plutus-tx/src/PlutusTx/IsData/TH.hs#L131). This is a TemplateHaskell function which takes the name of the type. In your case it will be like so: unstableMakeIsData ''CoinParams

Hope this helps.

1
  • thanks, I wasn't aware that's what it's doing behind the scenes.
    – zing
    Commented Apr 18, 2023 at 19:23

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.