<p>Your scenario fails because the way off-chain code was implemented: Once on-chain grab validation fails, the off-chain caller grab fails and the execution finishes. Why? - Because off-chain code is not handling errors.</p>
<p>How to make it work? - Replace function <code>endpoint</code> with these 2 functions.</p>
<pre><code>endpoints :: AsContractError e =&gt; Contract () GiftSchema e ()
endpoints = handleError handler (selectList [give', grab']) &gt;&gt; endpoints
  where
    give' = endpoint @&quot;give&quot; give
    grab' = endpoint @&quot;grab&quot; grab

handler :: AsContractError e =&gt; e -&gt; Contract w s e ()
handler _ = logError @String &quot;an error occurred&quot;
</code></pre>
<p>Note: If I'm not wrong, in lecture 4 Lars talks about error handling in off-chain code.</p>

CC BY-SA 4.0
2022-01-25T08:16:56.987Z
Follow up question: suppose the above scenario played out on the MAINNET.  Say Wim's code was v1, kindofdev's improvement is v2.

Can the v2 code grab those 20 ADA 'locked' up by the v1 code ?
My guess: yes, as long as you don't change the validator code, because that determines the script address. Am I right?
wmoco_6725
CC BY-SA 4.0
2022-01-25T09:57:15.717Z
Yes, that's correct. @wmoco_6725
kindofdev
CC BY-SA 4.0
2022-01-28T09:00:48.663Z
Thanks @kindofdev, finally got time to execute it and your solution works!
Wim Janssen
CC BY-SA 4.0
2022-01-23T23:31:29.877Z
2022-01-23T23:31:29.877Z
kindofdev
<p>I managed to get the homework transactions to work as expected (<em>happy</em>), so far so good...</p>
<p>I also tried the following scenario which did not work.</p>
<p><strong>Q1: Why doesn't it work?</strong></p>
<p><strong>Q2: How would it be possible to make it work!</strong></p>
<p>wallet 1: give 20 Ada
wait 1
wallet 2: grab false/true
wait 1
wallet 2: grab true/true
wait 1</p>
<p>As expected, the first grab is not handled, as it evaluates false
But the second grab is also not handled ?
The logtrace shows:</p>
<p><em>Slot 3: 00000000-0000-4000-8000-000000000002 {Wallet Wc30e}:
No requests handled ]</em></p>
<p>Why isn't it possible for the same wallet to &quot;correct&quot; its previous mistake and try again with acceptible parameters ?</p>
<p><a href="https://i.sstatic.net/6N34g.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6N34g.png" alt="playground actions resluting in no grab" /></a></p>

CC BY-SA 4.0
2022-01-23T22:30:37.013Z
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Community
<plutus-pioneer-program><plutus-playground>
Week 2 - Homework - valid validator call true/true after invalid call true/false from same wallet doesn't go through
# Week 2 - Homework - valid validator call true/true after invalid call true/false from same wallet doesn't go through

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

I managed to get the homework transactions to work as expected (*happy*), so far so good...

I also tried the following scenario which did not work.

**Q1: Why doesn't it work?**

**Q2: How would it be possible to make it work!**

wallet 1: give 20 Ada
wait 1
wallet 2: grab false/true
wait 1
wallet 2: grab true/true
wait 1

As expected, the first grab is not handled, as it evaluates false
But the second grab is also not handled ?
The logtrace shows:

*Slot 3: 00000000-0000-4000-8000-000000000002 {Wallet Wc30e}:
No requests handled ]*

Why isn't it possible for the same wallet to "correct" its previous mistake and try again with acceptible parameters ?

[![playground actions resluting in no grab](https://i.sstatic.net/6N34g.png)](https://i.sstatic.net/6N34g.png)

**Tags:** plutus-pioneer-program, plutus-playground
> **Comments:**
> **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  

---

## 1 answers

### Answer by username_2 on 2022-01-23. Score: 2 | Accepted answer

Your scenario fails because the way off-chain code was implemented: Once on-chain grab validation fails, the off-chain caller grab fails and the execution finishes. Why? - Because off-chain code is not handling errors.
How to make it work? - Replace function endpoint with these 2 functions.
endpoints :: AsContractError e => Contract () GiftSchema e ()
endpoints = handleError handler (selectList [give', grab']) >> endpoints
  where
    give' = endpoint @"give" give
    grab' = endpoint @"grab" grab

handler :: AsContractError e => e -> Contract w s e ()
handler _ = logError @String "an error occurred"

Note: If I'm not wrong, in lecture 4 Lars talks about error handling in off-chain code.

> **Comments:**
> **username_3**: Follow up question: suppose the above scenario played out on the MAINNET.  Say Wim's code was v1, kindofdev's improvement is v2.  Can the v2 code grab those 20 ADA 'locked' up by the v1 code ? My guess: yes, as long as you don't change the validator code, because that determines the script address. Am I right?  
> **username_2**: Yes, that's correct. @wmoco_6725  
> **username_0**: Thanks @kindofdev, finally got time to execute it and your solution works!  

---

