This doesn't work
mkPolicy oref () ctx = traceIfFalse "UTxO not consumed" hasUTxO &&
traceIfFalse "wrong amount minted" checkMintedAmount
where
info :: TxInfo
info = scriptContextTxInfo ctx
hasUTxO :: Bool
hasUTxO = any (\i -> txInInfoOutRef i == oref) $ txInfoInputs info
checkMintedAmount :: Bool
checkMintedAmount = case flattenValue (txInfoMint info) of
[(_, tn, amt)] -> tn == "" && amt == 1
_ -> False
and throws
GHC Core to PLC plugin: E042:Error: Unsupported feature: Type constructor: GHC.Prim.Char#
Context: Compiling type: GHC.Prim.Char#
Context: Compiling data constructor type: GHC.Types.C#
Context: Compiling type: GHC.Types.Char
Context: Compiling type: GHC.Base.String
Context: Compiling type: GHC.Base.String
-> Data.ByteString.Internal.ByteString
Context: Compiling definition of: Plutus.V1.Ledger.Value.$fIsStringTokenName1
Context: Compiling expr: Plutus.V1.Ledger.Value.$fIsStringTokenName1
Context: Compiling expr: Plutus.V1.Ledger.Value.$fIsStringTokenName1
((GHC.Types.[] @ GHC.Types.Char)
`cast` (Sym (Nth:2
(((<GHC.Base.String>_R
->_R Sym (PlutusTx.Builtins.Internal.N:BuiltinByteString[0]) ; Sym (Plutus.V1.Ledger.Value.N:TokenName[0])) ; Sym (Data.String.N:IsString[0]) <Plutus.V1.Ledger.Value.TokenName>_N) ; Data.String.N:IsString[0] <Plutus.V1.Ledger.Value.TokenName>_N))
:: GHC.Types.Coercible GHC.Base.String GHC.Base.String))
... (a lot of lines skipped here)
Context: Compiling definition of: Week05.Homework2.mkPolicy
Context: Compiling expr: Week05.Homework2.mkPolicy
Context: Compiling expr: Week05.Homework2.mkPolicy oref'_axTu
Context: Compiling expr: PlutusTx.Base.$
@ (() -> Plutus.V1.Ledger.Contexts.ScriptContext -> GHC.Types.Bool)
@ Ledger.Typed.Scripts.MonetaryPolicies.WrappedMintingPolicyType
(Ledger.Typed.Scripts.MonetaryPolicies.wrapMintingPolicy
@ () PlutusTx.IsData.Instances.$fUnsafeFromData())
(Week05.Homework2.mkPolicy oref'_axTu)
Context: Compiling expr: \ (oref'_axTu :: Plutus.V1.Ledger.Tx.TxOutRef) ->
PlutusTx.Base.$
@ (() -> Plutus.V1.Ledger.Contexts.ScriptContext -> GHC.Types.Bool)
@ Ledger.Typed.Scripts.MonetaryPolicies.WrappedMintingPolicyType
(Ledger.Typed.Scripts.MonetaryPolicies.wrapMintingPolicy
@ () PlutusTx.IsData.Instances.$fUnsafeFromData())
(Week05.Homework2.mkPolicy oref'_axTu)
Context: Compiling expr at "plutus-pioneer-program-week05-0.1.0.0-inplace:Week05.Homework2:(55,8)-(55,85)"
but this works
mkPolicy oref () ctx = traceIfFalse "UTxO not consumed" hasUTxO &&
traceIfFalse "wrong amount minted" checkMintedAmount
where
info :: TxInfo
info = scriptContextTxInfo ctx
emptyToken :: TokenName
emptyToken = TokenName ""
hasUTxO :: Bool
hasUTxO = any (\i -> txInInfoOutRef i == oref) $ txInfoInputs info
checkMintedAmount :: Bool
checkMintedAmount = case flattenValue (txInfoMint info) of
[(_, tn, amt)] -> tn == emptyToken && amt == 1
_ -> False
I assumed that {-# LANGUAGE OverloadedStrings #-}
would work as TokenName
is just a newtype
for PlutusTx.Builtins.Internal.BuiltinByteString
. From the repl
Prelude Week05.Homework2> :t Plutus.V1.Ledger.Value.TokenName
Plutus.V1.Ledger.Value.TokenName
:: PlutusTx.Builtins.Internal.BuiltinByteString
-> Plutus.V1.Ledger.Value.TokenName
Prelude Week05.Homework2> :i Plutus.V1.Ledger.Value.TokenName
type Plutus.V1.Ledger.Value.TokenName :: *
newtype Plutus.V1.Ledger.Value.TokenName
= Plutus.V1.Ledger.Value.TokenName {Plutus.V1.Ledger.Value.unTokenName :: PlutusTx.Builtins.Internal.BuiltinByteString}
-- Defined in `Plutus.V1.Ledger.Value'
instance Eq Plutus.V1.Ledger.Value.TokenName
-- Defined in `Plutus.V1.Ledger.Value'
instance Ord Plutus.V1.Ledger.Value.TokenName
-- Defined in `Plutus.V1.Ledger.Value'
instance Show Plutus.V1.Ledger.Value.TokenName
-- Defined in `Plutus.V1.Ledger.Value'
Edit #1
On reading this doc I realised that maybe TokenName
is not an instance of IsString
. I tried to implement it using:
instance IsString TokenName where
fromString = unTokenName
but I got this error
src/Week05/Homework2.hs:32:10: error:
Duplicate instance declarations:
instance IsString TokenName
-- Defined at src/Week05/Homework2.hs:32:10
instance IsString TokenName -- Defined in `Plutus.V1.Ledger.Value'
|
32 | instance IsString TokenName where
| ^^^^^^^^^^^^^^^^^^
Edit #2
The complete Homework file is here.