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

CC BY-SA 4.0
2023-04-18T19:23:55.660Z
thanks, I wasn't aware that's what it's doing behind the scenes.
zing
CC BY-SA 4.0
2023-04-17T07:42:38.050Z
2023-04-17T07:42:38.050Z
Nikhil Shetty
<p>I want to use a custom type for one of my policy parameters, which will be applied by a tool like Lucid.<br />
Example of param:</p>
<pre><code>data CoinParams = CoinParams {
    policyOwner :: PubKeyHash,
    tokenName :: TokenName
}
</code></pre>
<p>My policy will then look like this:</p>
<pre><code>policy :: CoinParams -&gt; () -&gt; ScriptContext -&gt; Bool
policy ... = ...
</code></pre>
<p>Before compilation I wrap the policy:</p>
<pre><code>                    vvv
wrappedPolicy :: BuiltinData -&gt; BuiltinData -&gt; BuiltinData -&gt; ()
wrappedPolicy params = wrapPolicy (policy $ unsafeFromBuiltinData params)
               ^^                                     ^^^           ^^
</code></pre>
<p>But this won't work because there is no <code>UnsafeFromData CoinParams</code> instance(required to use <code>unsafeFromBuiltInData</code>). 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</p>

<haskell><validator><builtindata>
UnsafeFromData instance for custom type
# UnsafeFromData instance for custom type

(Asked by: username_0 on 2023-04-16)

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

**Tags:** haskell, validator, builtindata

---

## 1 answers

### Answer by username_1 on 2023-04-17. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_0**: thanks, I wasn't aware that's what it's doing behind the scenes.  

---

