Illegal type: Perhaps you intended to use DataKinds

611 Views Asked by At

Hi i am trying to compile my plutus script with cabal but I am getting this error with the cabal whenever i try to do cabal build or cabal repl i am not sure what's wrong with it pls help me out

Treasury.hs file

{-# LANGUAGE DeriveAnyClass        #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Stake.StackingTreasury where

import              Ledger              hiding (singleton)
import qualified Ledger.Typed.Scripts as Scripts
import              Ledger.Value        as Value
import              Ledger.Ada
import qualified    PlutusTx
import              PlutusTx.Prelude    hiding (Semigroup (..), unless)

import              Stake.StackingTypes

{-# INLINEABLE mkValidator #-}
mkValidator :: TreasuryParam -> WithdrawalDatum -> StackingDetails -> ScriptContext -> Bool
mkValidator tp dat b ctx =    traceIfFalse "Only Issuer can change Treasury"              signedByIssuer ||
                              traceIfFalse "Access token missing from input"              inputHasAuthToken &&
                              traceIfFalse "Access token missing from contract output"    contractOutputHasAuthToken &&
                              traceIfFalse "Output Value must match StackingDetails"      checkValueToStackingContract &&
                              traceIfFalse "Treasury must keep remaining tokens"          treasuryGetsTokensBack &&
                              traceIfFalse "redeemer is not datum"                        checkReIsOutDat

  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    signedByIssuer :: Bool
    signedByIssuer = txSignedBy info $ tTreasuryIssuerPkh tp

    -- Create a list of all CurrencySymbol in tx input
    inVals :: [CurrencySymbol]
    inVals = symbols $ valueSpent info

    -- Check that list of CurrencySymbols includes Auth CurrencySymbol
    inputHasAuthToken :: Bool
    inputHasAuthToken = tAccessTokenPolicyId tp `elem` inVals

    -- The Value to be included in Stacking Contract UTXO
    toStackingContract :: Value
    toStackingContract = valueLockedBy info (bountyContractHash tp)

    -- Check that the Auth Token is sent to Bounty Contract UTXO
    contractOutputHasAuthToken :: Bool
    contractOutputHasAuthToken = tAccessTokenPolicyId tp `elem` symbols toStackingContract

    -- Check that the Value sent to Contract UTXO matches what is specified in the Redeemer
    -- Note: For now, we can just remember to match Treasury Redeemer to Bounty Datum
    -- when we build transactions
    checkValueToStackingContract :: Bool
    checkValueToStackingContract =  valueOf toStackingContract (tBountyTokenPolicyId tp) (tBountyTokenName tp) >= tokenAmount b

    -- The UTXO input from Treasury
    ownInput :: TxOut
    ownInput = case findOwnInput ctx of
        Nothing -> traceError "treasury input missing"
        Just i  -> txInInfoResolved i

    -- The UTXO output back to Treasury
    ownOutput :: TxOut
    ownOutput = case getContinuingOutputs ctx of
        [o] -> o -- There must be exactly ONE output UTXO
        _   -> traceError "expected exactly one treasury output"

    -- Values of each
    treasuryInputValue :: Value
    treasuryInputValue = txOutValue ownInput

    treasuryOutputValue :: Value
    treasuryOutputValue = txOutValue ownOutput

    -- Compare Values from and to Treasury to make sure that Treasury gets the right value back.
    treasuryGetsLovelaceBack :: Bool
    treasuryGetsLovelaceBack = getLovelace ( fromValue treasuryInputValue) - getLovelace ( fromValue treasuryOutputValue) <= getLovelace ( fromValue toStackingContract)

    treasuryGetsTokensBack :: Bool
    treasuryGetsTokensBack = valueOf treasuryInputValue (tBountyTokenPolicyId tp) (tBountyTokenName tp) - valueOf treasuryOutputValue (tBountyTokenPolicyId tp) (tBountyTokenName tp) <= valueOf toStackingContract (tBountyTokenPolicyId tp) (tBountyTokenName tp)

    getStackingDatum :: Maybe StackingDatum
    getStackingDatum = let os = [ o | o <- txInfoOutputs info, txOutValue o == toStackingContract ] in
                  case os of
                    [o] -> stackingDatum o (`findDatum` info)
                    _   -> Nothing

    checkReIsOutDat :: Bool
    checkReIsOutDat = case getStackingDatum of
      Nothing -> False
      Just ns -> bedIssuerPkh   ns == issuerPkh b &&
                 bedContributorPkh ns == contributorPkh b &&
                 bedTokenAmount ns == tokenAmount b &&
                 bedExpirationTime ns == expirationTime b

-- typedValidator :: TreasuryParam -> TypedValidator TreasuryTypes
-- typedValidator tp =
--   mkTypedValidator @TreasuryTypes
--     $$(PlutusTx.compile [||mkValidator||]) `PlutusTx.applyCode` PlutusTx.liftCode tp
--     $$(PlutusTx.compile [||wrap||])
--   where
--     wrap = wrapValidator @WithdrawalDatum @StackingDetails

-- validator :: TreasuryParam -> Validator
-- validator = validatorScript . typedValidator

typedValidator :: TreasuryParam -> Scripts.TypedValidator TreasuryTypes
typedValidator tp = Scripts.mkTypedValidator @TreasuryTypes
    ($$(PlutusTx.compile [|| mkValidator ||])
        `PlutusTx.applyCode` PlutusTx.liftCode tp)
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @WithdrawalDatum @StackingDetails

validator :: TreasuryParam -> Validator
validator = Scripts.validatorScript . typedValidator

Stacking types file

{-# LANGUAGE DeriveAnyClass        #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE TypeFamilies #-}

module Stake.StackingTypes
    ( TreasuryParam (..)
    , WithdrawalDatum (..)
    , StackingDetails (..)
    , StackingAction (..)
    , StackingParam (..)
    , StackingDatum (..)
    , TreasuryTypes
    , StackingTypes
    , stackingDatum
    ) where

import              Data.Aeson                  (ToJSON, FromJSON)
import              GHC.Generics                (Generic)
import              Schema                      (ToSchema)
import              Ledger              hiding  (singleton)
import              Ledger.Typed.Scripts
import  qualified   PlutusTx
import              PlutusTx.Prelude    hiding  (Semigroup (..), unless)
import              Prelude                     (Show (..))
import  qualified   Prelude                 as  Pr

data TreasuryParam = TreasuryParam
    { tAccessTokenPolicyId   :: !CurrencySymbol
    , bountyContractHash     :: !ValidatorHash
    , tBountyTokenPolicyId   :: !CurrencySymbol
    , tBountyTokenName       :: !TokenName
    , tTreasuryIssuerPkh     :: !PubKeyHash
    } deriving (Pr.Eq, Pr.Ord, Show, Generic, ToJSON, FromJSON, ToSchema)

PlutusTx.makeLift ''TreasuryParam

data WithdrawalDatum = WithdrawalDatum
  { bountyCount     :: !Integer
  , treasuryKey     :: !PubKeyHash
  } deriving (Pr.Eq, Pr.Ord, Show, Generic, ToJSON, FromJSON, ToSchema)

PlutusTx.unstableMakeIsData ''WithdrawalDatum



-- Completion Status - not 0 or 1, but partial
-- Completion status as, for example a % of what is completed?

data StackingDetails = StackingDetails
  { issuerPkh           :: !PubKeyHash
  , contributorPkh      :: !PubKeyHash
  , tokenAmount         :: !Integer
  , expirationTime      :: !POSIXTime
  } deriving (Pr.Eq, Pr.Ord, Show, Generic, ToJSON, FromJSON, ToSchema)

instance Eq StackingDetails where
  {-# INLINABLE (==) #-}
  StackingDetails iP cP tA eT == StackingDetails iP' cP' tA' eT' =
    (iP == iP') && (cP == cP') && (tA == tA') && (eT == eT')

    -- Alternative way of comparisons
    -- a == b = (issuerPkh       a == issuerPkh      b) &&
    --          (contributorPkh  a == contributorPkh b) &&
    --          (lovelaceAmount  a == lovelaceAmount b) &&
    --          (expirationTime  a == expirationTime b)

PlutusTx.unstableMakeIsData ''StackingDetails
PlutusTx.makeLift ''StackingDetails

data TreasuryTypes
instance ValidatorTypes TreasuryTypes where
    type DatumType TreasuryTypes = WithdrawalDatum
    type RedeemerType TreasuryTypes = StackingDetails

-- BountyEscrow
-- INLINABLE to use On Chain
{-# INLINABLE stackingDatum #-}
stackingDatum :: TxOut -> (DatumHash -> Maybe Datum) -> Maybe StackingDatum
stackingDatum o f = do
    dh <- txOutDatum o
    Datum d <- f dh
    PlutusTx.fromBuiltinData d

data StackingDatum = StackingDatum
  { bedIssuerPkh           :: !PubKeyHash
  , bedContributorPkh      :: !PubKeyHash
  , bedTokenAmount         :: !Integer
  , bedExpirationTime      :: !POSIXTime
  } deriving (Pr.Eq, Pr.Ord, Show, Generic, ToJSON, FromJSON, ToSchema)

instance Eq StackingDatum where
  {-# INLINABLE (==) #-}
  StackingDatum bIP bCP  bTA bET == StackingDatum bIP' bCP'  bTA' bET' =
    (bIP == bIP') && (bCP == bCP')  && (bTA == bTA') && (bET == bET')

    -- Alternative way of comparisons
    -- a == b = (bedIssuerPkh       a == bedIssuerPkh      b) &&
    --          (bedContributorPkh  a == bedContributorPkh b) &&
    --          (bedLovelaceAmount  a == bedLovelaceAmount b) &&
    --          (bedTokenAmount     a == bedTokenAmount    b) &&
    --          (bedExpirationTime  a == bedExpirationTime b)

PlutusTx.unstableMakeIsData ''StackingDatum
PlutusTx.makeLift ''StackingDatum

data StackingParam = StackingParam
    { bountyTokenPolicyId     :: !CurrencySymbol
    , bountyTokenName         :: !TokenName
    , accessTokenPolicyId     :: !CurrencySymbol
    , treasuryIssuerPkh       :: !PubKeyHash
    } deriving (Pr.Eq, Pr.Ord, Show, Generic, ToJSON, FromJSON, ToSchema)

PlutusTx.makeLift ''StackingParam

data StackingAction = Cancel BuiltinByteString | Claim
  deriving Show

PlutusTx.makeIsDataIndexed ''StackingAction [('Cancel, 0), ('Claim, 1) ]
PlutusTx.makeLift ''StackingAction

data StackingTypes
instance ValidatorTypes StackingTypes where
    type DatumType StackingTypes = StackingDatum
    type RedeemerType StackingTypes = StackingAction

I am getting this error while trying to do cabal build or cabal repl

src/Stake/StackingTreasury.hs:115:9: error:
    • Illegal type: ‘"stacking-contract-1.0.0.0-inplace:Stake.StackingTreasury:(115,9)-(115,45)"’
        Perhaps you intended to use DataKinds
    • In the result of the splice:
        $PlutusTx.compile
           template-haskell-2.16.0.0:Language.Haskell.TH.Syntax.unsafeTExpCoerce
             [|| mkValidator ||]
      To see what the splice expanded to, use -ddump-splices
      In the Template Haskell splice
        $$(PlutusTx.compile [|| mkValidator ||])
      In the first argument of ‘PlutusTx.applyCode’, namely
        ‘$$(PlutusTx.compile [|| mkValidator ||])’
    |
115 |     ($$(PlutusTx.compile [|| mkValidator ||])
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pls help stuck here for few days I don't know what I am doing wrong . Here treasury contract will hold some tokens as rewards

1

There are 1 best solutions below

0
On

You need to enable the DataKinds language extension at the top of stacking.hs:

{-# LANGUAGE DataKinds #-}

-- ...rest of stacking.hs