1

I am trying to make a minting policy that fails after a certain deadline.

I have tried something like this

{-# INLINABLE mkTokenPolicy #-}
mkTokenPolicy :: TxOutRef -> TokenName -> Integer -> () -> ScriptContext -> Bool
mkTokenPolicy oref tn amt () ctx = traceIfFalse "Deadline reached"   checkDeadline         && --getinng error that oef isnt even used
                                   traceIfFalse "wrong amount minted" checkMintedAmount
  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    checkDeadline:: Bool
    checkDeadline = ivTo (from mDeadline) >  ivTo (txInfoValidRange info)                        
        where
            mDeadline ::  POSIXTime
            mDeadline =  1680120281000

    checkMintedAmount :: Bool
    checkMintedAmount = case flattenValue (txInfoMint info) of
        [(_, tn', amt')] -> tn' == tn && amt' == amt
        _                -> False 

and have also tried something like this

{-# INLINABLE mkTokenPolicy #-}
mkTokenPolicy :: TxOutRef -> TokenName -> Integer -> () -> ScriptContext -> Bool
mkTokenPolicy oref tn amt () ctx = traceIfFalse "Deadline reached"   checkDeadline         && --getinng error that oef isnt even used
                                   traceIfFalse "wrong amount minted" checkMintedAmount
  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    checkDeadline:: Bool
    checkDeadline = to mDeadline `contains` txInfoValidRange info                       
        where
            mDeadline ::  POSIXTime
            mDeadline =  1680120281000

    checkMintedAmount :: Bool
    checkMintedAmount = case flattenValue (txInfoMint info) of
        [(_, tn', amt')] -> tn' == tn && amt' == amt
        _                -> False 

However, no matter what I seem to do, checkDeadline always returns False. Does anyone have any thoughts on how I can fix this?

0

2 Answers 2

1

Make sure you are adding your time constraints in your off-chain code as well.

Here is an example of the constraints in the auction contract.

Background:

This can be kinda unintuitive to some people, but essentially the script doesn't know when it's executing, it just has the context from the transaction.

When you use Constraints.mustValidateIn X off chain, you are building a tx that is valid over X range. If you submit it outside of that range, it will fail before it ever gets to a validator script.

Say your script only accepts txs that are valid in range 5-10 and your tx is valid in range 1-10. Even if it occurs on 7, it will still fail the validator!

Script constraints are looking at the range a tx is valid, with the implicit assertion that it is being executed in that range (or else it wouldn't have made it to the script). That way, the script doesn't need to know which slot it is currently and can be deterministic. The non-deterministic stuff is cleanly separated from the pure scripts.

0

Can you provide the test? The code looks fine

Prelude Ledger Ledger.Interval > checkDeadline x = to (POSIXTime 1680120281000) `contains` x
Prelude Ledger Ledger.Interval > checkDeadline (to 1680120280000)
True

Maybe you haven't provided any time contraints in the transaction? If you don't then I think it defaults to the always interval. Then checkDeadline will fail.


This should be a comment really, but comments mess up the code

Your Answer

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

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