Skip to content

Type-level schemas that let one define PlutusData encodings for PureScript algebraic data types

Notifications You must be signed in to change notification settings

mlabs-haskell/purescript-cardano-plutus-data-schema

Repository files navigation

purescript-cardano-plutus-data-schema

This package implements type-level machinery that we use across the cardano-transaction-lib ecosystem to specify PlutusData encodings for arbitrary algebraic data types.

It is similar in spirit to PlutusTx.makeIsDataIndexed from Plutus, which is implemented in TemplateHaskell.

In PureScript, we couldn't use TemplateHaskell-style codegen due to the lack of it, and we couldn't rely on the ordering of record fields and constructors in ADTs when using Generic machinery, because PureScript always sorts them alphabetically. So, this module was invented to fix the ordering when deriving instances via Generic.

Example

A quick usage example (S and Z are for type-level Peano numbers):

data FType
  = F0
      { f0A :: BigInt
      }
  | F1
      { f1A :: Boolean
      , f1B :: Boolean
      , f1C :: Boolean
      }
  | F2
      { f2A :: BigInt
      , f2B :: FType
      }

instance
  HasPlutusSchema FType
    ( "F0" :=
          ( "f0A" := I BigInt
          :+ PNil)
       @@ Z

    :+ "F1" :=
          ( "f1A"  := I Boolean
          :+ "f1B" := I Boolean
          :+ "f1C" := I Boolean
          :+ PNil
          )
        @@ (S Z)

    :+ "F2" :=
          (  "f2A" := I BigInt
          :+ "f2B" := I FType
          :+ PNil
          )
        @@ (S (S Z))

    :+ PNil
    )


instance ToData FType where
  toData = genericToData

instance FromData FType where
  fromData = genericFromData

For more examples, see the test suite of purescript-plutus-types

Testing

The tests for this package are located in purescript-cardano-types

About

Type-level schemas that let one define PlutusData encodings for PureScript algebraic data types

Resources

Stars

Watchers

Forks

Packages