-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomScriptContext.hs
executable file
·129 lines (109 loc) · 5.5 KB
/
CustomScriptContext.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env cabal
{- --------------------------------------------------------------- -}
{- How to reduce the size of a minting policy script ? -}
{- -}
{- Use a wrapper that makes an untyped minting policy -}
{- from a typed one, such as `mkUntypedMintingPolicy` -}
{- defined in plutus-script-utils. -}
{- -}
{- To further reduce the size of the script, use -}
{- a custom `ScriptContext` with fewer type references, -}
{- by defaulting all unused fields to `BuiltinData`. -}
{- -}
{- However, a generalised wrapper using a parametric -}
{- script context constrained by `FromData p` does not work -}
{- with TemplateHaskell, if the `FromData` instances are -}
{- created with `makeIsDataIndexed`. -}
{- -}
{- This script shows the different `MintingPolicy` sizes -}
{- depending on the types in use. -}
{- -}
{- --------------------------------------------------------------- -}
{- cabal:
build-depends:
, base
, plutus-ledger-api
, plutus-script-utils
, plutus-tx
, plutus-tx-plugin
-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TemplateHaskell #-}
import Prelude ( IO, Eq, print )
import PlutusTx.Prelude ( Bool(..), ($), (.), check )
import PlutusTx qualified ( BuiltinData, UnsafeFromData(..), compile, makeIsDataIndexed )
import GHC.Generics ( Generic )
import Plutus.V1.Ledger.Api qualified
as Ledger ( MintingPolicy, mkMintingPolicyScript )
import Plutus.V1.Ledger.Contexts
as Ledger ( ScriptContext )
import Plutus.V1.Ledger.Crypto qualified
as Ledger ( PubKeyHash )
import Plutus.V1.Ledger.Scripts qualified
as Ledger ( scriptSize, unMintingPolicyScript )
import Plutus.V1.Ledger.Time qualified
as Ledger ( POSIXTimeRange )
import Plutus.Script.Utils.V1.Typed.Scripts qualified
as Scripts ( mkUntypedMintingPolicy )
{- --------------------------------------------------------------- -}
{- Custom ScriptContext -}
{- --------------------------------------------------------------- -}
data TxInfo = TxInfo
{ txInfoInputs :: PlutusTx.BuiltinData -- unused field
, txInfoOutputs :: PlutusTx.BuiltinData -- unused field
, txInfoFee :: PlutusTx.BuiltinData -- unused field
, txInfoMint :: PlutusTx.BuiltinData -- unused field
, txInfoDCert :: PlutusTx.BuiltinData -- unused field
, txInfoWdrl :: PlutusTx.BuiltinData -- unused field
, txInfoValidRange :: Ledger.POSIXTimeRange
, txInfoSignatories :: [Ledger.PubKeyHash]
, txInfoData :: PlutusTx.BuiltinData -- unused field
, txInfoId :: PlutusTx.BuiltinData -- unused field
}
deriving stock (Generic, Eq)
PlutusTx.makeIsDataIndexed ''TxInfo [('TxInfo, 0)]
data CustomScriptContext = CustomScriptContext
{ scriptContextTxInfo :: TxInfo
, scriptContextPurpose :: PlutusTx.BuiltinData -- unused field
}
deriving stock (Generic, Eq)
PlutusTx.makeIsDataIndexed ''CustomScriptContext [('CustomScriptContext, 0)]
{- --------------------------------------------------------------- -}
{- On-chain code -}
{- --------------------------------------------------------------- -}
{-# INLINABLE mkUntypedMintingPolicy #-}
mkUntypedMintingPolicy
:: (PlutusTx.UnsafeFromData r)
=> (r -> CustomScriptContext -> Bool)
-> PlutusTx.BuiltinData
-> PlutusTx.BuiltinData
-> ()
mkUntypedMintingPolicy f r p =
check $ f
(PlutusTx.unsafeFromBuiltinData r)
(PlutusTx.unsafeFromBuiltinData p)
{-# INLINABLE mkMintingPolicy #-}
mkMintingPolicy :: () -> ScriptContext -> Bool
mkMintingPolicy _ _ = True
mintingPolicy :: Ledger.MintingPolicy
mintingPolicy = Ledger.mkMintingPolicyScript
$$(PlutusTx.compile [||Scripts.mkUntypedMintingPolicy mkMintingPolicy||])
{-# INLINABLE mkMintingPolicyCustomScriptContext #-}
mkMintingPolicyCustomScriptContext :: () -> CustomScriptContext -> Bool
mkMintingPolicyCustomScriptContext _ _ = True
mintingPolicyCustomScriptContext :: Ledger.MintingPolicy
mintingPolicyCustomScriptContext = Ledger.mkMintingPolicyScript
$$(PlutusTx.compile [||mkUntypedMintingPolicy mkMintingPolicyCustomScriptContext||])
{- --------------------------------------------------------------- -}
{- Main code -}
{- --------------------------------------------------------------- -}
main :: IO ()
main = do
printSize mintingPolicy
printSize mintingPolicyCustomScriptContext
where
printSize = print . Ledger.scriptSize . Ledger.unMintingPolicyScript