<p>The cause is <code>mustPayToOtherScript</code> in the <code>give</code> <a href="https://github.com/Vortecsmaster/EmurgoAcademyPlutusExamples/blob/d4253139843b504d7237dc9fb050f676b730283a/V1/JustValidators/src/AlwaysSucceedandFail.hs#L74" rel="nofollow noreferrer">endpoint</a> where</p>
<pre><code>give :: AsContractError e =&gt; Integer -&gt; Contract w s e ()
give amount = do
  let tx = mustPayToOtherScript valHash (Datum $ Builtins.mkI 0) $ Ada.lovelaceValueOf amount 
  ledgerTx &lt;- submitTx tx                                                                         
  void $ awaitTxConfirmed $ getCardanoTxId ledgerTx                                                
  Plutus.Contract.logInfo @String $ printf &quot;made a gift of %d lovelace&quot; amount 
</code></pre>
<p>The error is fixed by replacing <code>mustPayToOtherScript</code> with <code>mustPayToOtherScriptWithDatumInTx</code>.</p>
<p>Get the answer <a href="https://github.com/mlabs-haskell/plutip/blob/7f2d59abd911dd11310404863cdedb2886902ebf/test/Spec/TestContract/AlwaysFail.hs#L45" rel="nofollow noreferrer">from mlabs-haskell/plutip</a>. Thanks to their warning:</p>
<blockquote>
<p>-- WARN: at the moment 'mustPayToOtherScript' causes 'DatumNotFound' error during constraints resolution</p>
</blockquote>

CC BY-SA 4.0
2022-12-24T00:45:50.913Z
2022-12-24T00:45:50.913Z
Adam
<p>This is how I spend script's utxos with the Emulator. This one <a href="https://github.com/Vortecsmaster/EmurgoAcademyPlutusExamples/blob/d4253139843b504d7237dc9fb050f676b730283a/V1/JustValidators/src/AlwaysSucceedandFail.hs#L81" rel="nofollow noreferrer">from this example</a>.</p>
<pre><code>grab :: forall w s e. AsContractError e =&gt; Contract w s e ()  
grab = do
  utxos &lt;- utxosAt scrAddress                                                                      
  let orefs   = fst &lt;$&gt; Map.toList utxos                                                           
    lookups = Constraints.unspentOutputs utxos &lt;&gt;                                           
              Constraints.plutusV1OtherScript validator                                          
    tx :: TxConstraints Void Void                                                            
    tx      = mconcat [mustSpendScriptOutput oref $ Redeemer $ Builtins.mkI 17 | oref &lt;- orefs]  
                                                                                                 
  ledgerTx &lt;- submitTxConstraintsWith @Void lookups tx                                             
  void $ awaitTxConfirmed $ getCardanoTxId ledgerTx                                                
  Plutus.Contract.logInfo @String $ &quot;collected gifts&quot;  
</code></pre>
<p>This is PlutusV1. This works as expected. But <a href="https://github.com/Vortecsmaster/EmurgoAcademyPlutusExamples/blob/d4253139843b504d7237dc9fb050f676b730283a/V2/JustValidators/src/AlwaysSucceedandFailV2.hs#L80" rel="nofollow noreferrer">with PlutusV2</a>, the <code>lookups</code> now is like:</p>
<pre><code>    lookups = Constraints.unspentOutputs utxos &lt;&gt;                                           
              Constraints.plutusV2OtherScript validator  
</code></pre>
<p>Note the version difference in <code>Constraints.plutusV2OtherScript validator</code>.</p>
<p>The rest of the code is the same except that this time the package is plutus-apps v1.0.0.</p>
<p>The V2 script compiles but when <code>grab</code> is called I get this error:</p>
<blockquote>
<p>*** CONTRACT STOPPED WITH ERROR: &quot;&quot;ConstraintResolutionContractError (DatumNotFound
03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314)&quot;&quot;</p>
</blockquote>
<p>I try <code>Constraints.otherScript (Versioned validator PlutusV2)</code>. Same error.</p>
<p>I try <code>Plutus.Trace</code> instead of <code>Plutus.Trace.Emulator</code>. Same error.</p>
<p>This is just one example. I see this &quot;DatumNotFound&quot; error in other projects when trying to work with plutus-apps v1.0.0.</p>
<p>How do I fix this. Or is it just a bug?</p>

<plutus><datum><plutus-v2><plutus-apps><emulator>
PlutusV2: Error when spending script output
# PlutusV2: Error when spending script output

(Asked by: username_0 on 2022-12-23)

This is how I spend script's utxos with the Emulator. This one from this example.
grab :: forall w s e. AsContractError e => Contract w s e ()  
grab = do
  utxos <- utxosAt scrAddress                                                                      
  let orefs   = fst <$> Map.toList utxos                                                           
    lookups = Constraints.unspentOutputs utxos <>                                           
              Constraints.plutusV1OtherScript validator                                          
    tx :: TxConstraints Void Void                                                            
    tx      = mconcat [mustSpendScriptOutput oref $ Redeemer $ Builtins.mkI 17 | oref <- orefs]  
                                                                                                 
  ledgerTx <- submitTxConstraintsWith @Void lookups tx                                             
  void $ awaitTxConfirmed $ getCardanoTxId ledgerTx                                                
  Plutus.Contract.logInfo @String $ "collected gifts"  

This is PlutusV1. This works as expected. But with PlutusV2, the lookups now is like:
    lookups = Constraints.unspentOutputs utxos <>                                           
              Constraints.plutusV2OtherScript validator  

Note the version difference in Constraints.plutusV2OtherScript validator.
The rest of the code is the same except that this time the package is plutus-apps v1.0.0.
The V2 script compiles but when grab is called I get this error:

*** CONTRACT STOPPED WITH ERROR: ""ConstraintResolutionContractError (DatumNotFound
03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314)""

I try Constraints.otherScript (Versioned validator PlutusV2). Same error.
I try Plutus.Trace instead of Plutus.Trace.Emulator. Same error.
This is just one example. I see this "DatumNotFound" error in other projects when trying to work with plutus-apps v1.0.0.
How do I fix this. Or is it just a bug?

**Tags:** plutus, datum, plutus-v2, plutus-apps, emulator

---

## 1 answers

### Answer by username_0 on 2022-12-24. Score: 0

The cause is mustPayToOtherScript in the give endpoint where
give :: AsContractError e => Integer -> Contract w s e ()
give amount = do
  let tx = mustPayToOtherScript valHash (Datum $ Builtins.mkI 0) $ Ada.lovelaceValueOf amount 
  ledgerTx <- submitTx tx                                                                         
  void $ awaitTxConfirmed $ getCardanoTxId ledgerTx                                                
  Plutus.Contract.logInfo @String $ printf "made a gift of %d lovelace" amount 

The error is fixed by replacing mustPayToOtherScript with mustPayToOtherScriptWithDatumInTx.
Get the answer from mlabs-haskell/plutip. Thanks to their warning:

-- WARN: at the moment 'mustPayToOtherScript' causes 'DatumNotFound' error during constraints resolution


---

