<p>Make sure you are adding your time constraints in your off-chain code as well.</p>
<p><a href="https://github.com/input-output-hk/plutus-pioneer-program/blob/28559d379df8b66c06d8fbd1e2a43f6a8351382a/code/week01/src/Week01/EnglishAuction.hs" rel="nofollow noreferrer">Here</a> is an example of the constraints in the auction contract.</p>
<p><strong>Background:</strong></p>
<p>This can be kinda unintuitive to some people, but essentially the script doesn't know <em>when</em> it's executing, it just has the context from the transaction.</p>
<p>When you use <code>Constraints.mustValidateIn X</code> 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.</p>
<p>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!</p>
<p>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.</p>

CC BY-SA 4.0
2022-03-30T18:44:56.567Z
2022-03-30T18:44:56.567Z
Mitchell Turner
<p>Can you provide the test?
The code looks fine</p>
<pre><code>Prelude Ledger Ledger.Interval &gt; checkDeadline x = to (POSIXTime 1680120281000) `contains` x
Prelude Ledger Ledger.Interval &gt; checkDeadline (to 1680120280000)
True
</code></pre>
<p>Maybe you haven't provided any time contraints in the transaction?
If you don't then I think it defaults to the <code>always</code> interval. Then <code>checkDeadline</code> will fail.</p>
<hr />
<p>This should be a comment really, but comments mess up the code</p>

CC BY-SA 4.0
2022-03-30T09:05:04.157Z
2022-03-30T09:05:04.157Z
waalge
<p>I am trying to make a minting policy that fails after a certain deadline.</p>
<p>I have tried something like this</p>
<pre><code>{-# INLINABLE mkTokenPolicy #-}
mkTokenPolicy :: TxOutRef -&gt; TokenName -&gt; Integer -&gt; () -&gt; ScriptContext -&gt; Bool
mkTokenPolicy oref tn amt () ctx = traceIfFalse &quot;Deadline reached&quot;   checkDeadline         &amp;&amp; --getinng error that oef isnt even used
                                   traceIfFalse &quot;wrong amount minted&quot; checkMintedAmount
  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    checkDeadline:: Bool
    checkDeadline = ivTo (from mDeadline) &gt;  ivTo (txInfoValidRange info)                        
        where
            mDeadline ::  POSIXTime
            mDeadline =  1680120281000

    checkMintedAmount :: Bool
    checkMintedAmount = case flattenValue (txInfoMint info) of
        [(_, tn', amt')] -&gt; tn' == tn &amp;&amp; amt' == amt
        _                -&gt; False 
</code></pre>
<p>and have also tried something like this</p>
<pre><code>{-# INLINABLE mkTokenPolicy #-}
mkTokenPolicy :: TxOutRef -&gt; TokenName -&gt; Integer -&gt; () -&gt; ScriptContext -&gt; Bool
mkTokenPolicy oref tn amt () ctx = traceIfFalse &quot;Deadline reached&quot;   checkDeadline         &amp;&amp; --getinng error that oef isnt even used
                                   traceIfFalse &quot;wrong amount minted&quot; 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')] -&gt; tn' == tn &amp;&amp; amt' == amt
        _                -&gt; False 
</code></pre>
<p>However, no matter what I seem to do, checkDeadline always returns False. Does anyone have any thoughts on how I can fix this?</p>

<plutus><plutus-pioneer-program><smart-contract><nft><script>
checkDeadline function always returning False
# checkDeadline function always returning False

(Asked by: username_0 on 2022-03-29)

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?

**Tags:** plutus, plutus-pioneer-program, smart-contract, nft, script

---

## 2 answers

### Answer by username_1 on 2022-03-30. Score: 1

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

[Here](https://github.com/input-output-hk/plutus-pioneer-program/blob/28559d379df8b66c06d8fbd1e2a43f6a8351382a/code/week01/src/Week01/EnglishAuction.hs) 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.


---

### Answer by username_2 on 2022-03-30. Score: 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


---

