1

I am trying to learn the basics of the PlutusTx module. In this module is a function applyCode which maps the signatures CompiledCodeIn uni fun (a -> b) -> CompiledCodeIn uni fun a -> CompiledCodeIn uni fun b. In my understanding this function is there to splice in inputs. So given the following validator

{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE NoImplicitPrelude   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE TypeOperators       #-}

module Modules.Contract where

import           Control.Monad       hiding (fmap)
import           Data.Map            as Map
import           Data.Text           (Text)
import           Data.Void           (Void)
import           Plutus.Contract
import           PlutusTx            as PlutusTx
import           PlutusTx.Prelude    as P hiding (Semigroup(..), unless)
import           Ledger              hiding (singleton)
import           Ledger.Constraints  as Constraints
import qualified Ledger.Scripts      as Scripts
import           Ledger.Ada          as Ada
import           Playground.Contract (printJson, printSchemas, ensureKnownCurrencies, stage)
import           Playground.TH       (mkKnownCurrencies, mkSchemaDefinitions)
import           Playground.Types    (KnownCurrency (..))
import           Prelude             (IO, Semigroup (..), String)
import           Text.Printf         (printf)

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
someList :: [Integer]
someList = [1,2,3]

someListBuiltin :: BuiltinData
someListBuiltin = PlutusTx.toBuiltinData someList

{-# INLINABLE mkValidator #-}
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> BuiltinData -> ()
mkValidator someListBuiltin _ r _
        | (someListBuiltin P.!! 1) == r = ()
        | otherwise                     = traceError "wrong redeemer"

Notice that I use BuiltinData instead of a typed version and I import the Plutus.Prelude as P. This simple validator checks if the redeemer is the first element in the list (so if r = (I 1) it would validate. Now how do I splice this pre determined list in the validator via template haskell? My attempt by using applyCode in infix style

validator :: Validator
validator = mkValidatorScript ($$(PlutusTx.compile [|| mkValidator ||]) `PlutusTx.applyCode` PlutusTx.liftCode someListBuiltin)

For a typed validator I got this to work.

9
  • What's the error you get? Jan 26 at 14:54
  • I managed to pin it down to the fact that the P.!! function is not applicable on BuiltinData. If one wants to script with BuiltinData, do you have to convert everything back to Data to use the Plutus.Prelude functions? I got the applyCode working now (it was a wrong module import).
    – Fermat
    Jan 26 at 15:03
  • Ah, it might be a better idea to use mkTypedValidatorScript which will do the conversions for you. Jan 26 at 15:47
  • I agree, but everything should be possible in the non-typed version as well right? Lars said in his lecture that the non-typed validator where smaller in size since the conversion is skipped. So thats why I wanted to learn the non-typed style too.
    – Fermat
    Jan 26 at 16:15
  • 1
    That's sadly not how it works, but I like how you're thinking! If you pattern match on List xs, you can then do xs !! 0. Jan 28 at 10:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy