Skip to content

Latest commit

 

History

History
96 lines (71 loc) · 3.72 KB

importing-scripts.md

File metadata and controls

96 lines (71 loc) · 3.72 KB

Importing scripts for use with CTL

Table of Contents

Exporting scripts from Plutus or Plutarch

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"
}

Using Plutonomy

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||])

Serializing Plutus scripts

PlutusTx

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.

Plutarch

You can use ply and ply-ctl.

plutarch-ctl-bridge

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.

Importing serialized scripts

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).