Table of Contents
Usually projects use a Haskell binary called exporter that outputs a pre-compiler UPLC bundle into a file.
The output file should be a Cardano envelope:
{
"cborHex": "4e4d01000033222220051200120011",
"description": "always-succeeds",
"type": "PlutusScriptV2"
}
- An example of a Plutus exporter can be found here.
- For Plutarch, see the-plutus-scaffold's exporter.
It makes sense to use Plutonomy (an optimizer for UPLC) in the exporter:
import Plutonomy (aggressiveOptimizerOptions, optimizeUPLCWith)
script :: Script
script = fromCompiledCode $
optimizeUPLCWith aggressiveOptimizerOptions $$(PlutusTx.compile [||policy||])
Take alwaysSucceeds
as an example:
alwaysSucceeds :: BuiltinData -> BuiltinData -> BuiltinData -> ()
alwaysSucceeds _ _ _ = ()
alwaysSucceedsCompiled :: CompiledCode (BuiltinData -> BuiltinData -> ())
alwaysSucceedsCompiled = $$(PlutusTx.compile [|| alwaysSucceeds ||])
For older plutus commits use:
import Plutus.V1.Ledger.Api (fromCompiledCode)
import Codec.Serialise (serialise)
scriptToCBOR :: CompiledCode a -> ByteString
scriptToCBOR = B.toStrict . serialise . fromCompiledCode
and for newer (match on plutus-ledger-api import):
import PlutusLedgerApi.Common (serialiseCompiledCode)
import Data.ByteString.Short (fromShort)
scriptToCBOR = Short.fromShort . serialiseCompiledCode
Then save a script to a file:
import Cardano.Api (writeFileTextEnvelope, PlutusScriptV1, SerialiseAsCBOR (deserialiseFromCBOR), AsType (AsScript, AsPlutusScriptV1), Script)
main =
case deserialiseFromCBOR (AsScript AsPlutusScriptV1) $ scriptToCBOR alwaysSucceedsCompiled of
Left err -> print err
Right script ->
writeFileTextEnvelope @(Script PlutusScriptV1) "always-succeeds.plutus" (Just "My script") script
>>= either print return
Note that we specified plutus version.
You can use plutarch-ctl-bridge
to generate Purescript types from your Haskell type definitions and typed script wrappers from parametrized Plutarch scripts. See example module.
To use your own scripts, compile them to any subdirectory in the root of your project and either dynamically load them from file (NodeJS) or use your bundler to include them as fixtures into the bundle (instructions for WebPack, for esbuild).