1

I'm on homework 5 of the pioneer program. If I check Signed.hs:

{-# INLINABLE mkPolicy #-}
mkPolicy :: PaymentPubKeyHash -> () -> ScriptContext -> Bool
mkPolicy pkh () ctx = txSignedBy (scriptContextTxInfo ctx) $ unPaymentPubKeyHash pkh

policy :: PaymentPubKeyHash -> Scripts.MintingPolicy
policy pkh = mkMintingPolicyScript $
    $$(PlutusTx.compile [|| Scripts.wrapMintingPolicy . mkPolicy ||])
    `PlutusTx.applyCode`
    PlutusTx.liftCode pkh

curSymbol :: PaymentPubKeyHash -> CurrencySymbol
curSymbol = scriptCurrencySymbol . policy

For some reason, this works, although mkPolicy has 3 arguments and while compiling, there's only 1. It seems that () -> ScriptContext is dropped while wrapping the script (which kind a makes sense). This results in only one argument, the PaymentPubKeyHash.

However, when I try Homework1, I need 2 arguments, so I try this:

mkPolicy :: PaymentPubKeyHash -> POSIXTime -> () -> ScriptContext -> Bool
mkPolicy pkh deadline () ctx = 
   txSignedBy txInfo $ unPaymentPubKeyHash pkh &&
      to deadline `contains` txInfoValidRange txInfo
         where txInfo :: txInfo
               txInfo = scriptContextInfo ctx

policy :: PaymentPubKeyHash -> POSIXTime -> Scripts.MintingPolicy
policy pkh deadline = mkMintingPolicyScript $
   $$(PlutusTx.compile [|| Scripts.wrapMintingPolicy . mkPolicy ||])
   `PlutusTx.applyCode`
   PlutusTx.liftCode pkh
   `PlutusTx.applyCode`
   PlutusTx.liftCode deadline

curSymbol :: PaymentPubKeyHash -> POSIXTime -> CurrencySymbol
curSymbol pkh deadline = scriptCurrencySymbol . policy 

I would expect the Scripts.wrapMintingPolicy again drops () -> ScriptContext while compiling, making it using the first two PaymentPubKeyHash -> POSIXTime. However, when I try to compile this, I get an error:

src/Week05/Homework1.hs:51:56: error:
    • Couldn't match type ‘()’ with ‘ScriptContext’
      Expected type: PaymentPubKeyHash
                     -> POSIXTime -> ScriptContext -> Bool
        Actual type: PaymentPubKeyHash
                     -> POSIXTime -> () -> ScriptContext -> Bool
    • Probable cause: ‘mkPolicy’ is applied to too few arguments
      In the second argument of ‘(.)’, namely ‘mkPolicy’
      In the Template Haskell quotation
        [|| Scripts.wrapMintingPolicy . mkPolicy ||]
      In the first argument of ‘PlutusTx.compile’, namely
        ‘[|| Scripts.wrapMintingPolicy . mkPolicy ||]’
   |
51 |    $$(PlutusTx.compile [|| Scripts.wrapMintingPolicy . mkPolicy ||])
   |                                                        ^^^^^^^^
Failed, no modules loaded.

So I've got the arguments wrong. I fail to understand why in the example of Signed.hs it works fine (1 argument to mkPolicy) and why it doesn't work fine in the case of 2 arguments.

What am I missing? Thx.

== EDIT 14/02/2022

So, final solution, thanks to @Mihai Codescu

mkPolicy :: PaymentPubKeyHash -> POSIXTime -> () -> ScriptContext -> Bool
mkPolicy pkh deadline () ctx = 
   txSignedBy txInfo (unPaymentPubKeyHash pkh) &&
      to deadline `contains` txInfoValidRange txInfo
         where txInfo :: TxInfo
               txInfo = scriptContextTxInfo ctx

policy :: PaymentPubKeyHash -> POSIXTime -> Scripts.MintingPolicy
policy pkh deadline = mkMintingPolicyScript $
   $$(PlutusTx.compile [|| mintingPolicyScript ||])
   `PlutusTx.applyCode`
   PlutusTx.liftCode pkh
   `PlutusTx.applyCode`
   PlutusTx.liftCode deadline
      where 
         mintingPolicyScript :: PaymentPubKeyHash -> POSIXTime -> Scripts.WrappedMintingPolicyType
         mintingPolicyScript = \pkh' deadline' -> Scripts.wrapMintingPolicy $ mkPolicy pkh' deadline' 

curSymbol :: PaymentPubKeyHash -> POSIXTime -> CurrencySymbol
curSymbol = \pkh' deadline' -> scriptCurrencySymbol (policy pkh' deadline')

1 Answer 1

2

We have wrapMintingPolicy :: UnsafeFromData r => (r -> ScriptContext -> Bool) -> WrappedMintingPolicyType.

In the first case, we can spell out the details like:

policy :: PaymentPubKeyHash -> Scripts.MintingPolicy
policy pkh = mkMintingPolicyScript $
    $$(PlutusTx.compile [|| \pkh' -> Scripts.wrapMintingPolicy $  mkPolicy pkh' ||])
    `PlutusTx.applyCode`
    PlutusTx.liftCode pkh

This means we partially apply mkPolicy and we get something that we can apply wrapMintingPolicy to.

In the second case we want to have:

policy :: PaymentPubKeyHash -> POSIXTime -> Scripts.MintingPolicy
policy pkh deadline = mkMintingPolicyScript $
    $$(PlutusTx.compile [|| \pkh' deadline' -> Scripts.wrapMintingPolicy $  mkPolicy pkh' deadline' ||])
    `PlutusTx.applyCode`
    PlutusTx.liftCode pkh
    `PlutusTx.applyCode`
    PlutusTx.liftCode deadline

Here we can't use composition anymore: wrapMintingPolicy :: y -> z and mkPolicy :: a -> b -> y.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.