<p>You may need to update this part:
utxos &lt;- fundsAtAddressGeq gameAddress (Ada.lovelaceValueOf 1)</p>
<p>Try:
utxos &lt;- Map.take 1 &lt;$&gt; fundsAtAddressGeq gameAddress (Ada.lovelaceValueOf 1)</p>
<p>You need to change the import on top to
import PlutusTx.Prelude hiding (pure) so as to use &lt;$&gt;.</p>
<p>I have tried it using 4 wallets (somehow the Playground does not allow one wallet to make multiple calls) and it worked.</p>

CC BY-SA 4.0
2022-03-22T18:26:19.467Z
2022-04-17T00:25:03.290Z
2022-04-17T00:25:03.290Z
Genty SAMVURA
<p>I need to get only one UTxO from the script address, where its datum hash matches the redeemer hash. For example I have locked two UTxO with 10 Ada  and the DatumHash &quot;house&quot; at the script and only want to unlock one of them.</p>
<p>I use the guessing game example of the Plutus Playground, but it unlocks both UTxO when the Redeemer has the same hash:</p>
<pre><code>guess = endpoint @&quot;guess&quot; @GuessParams $ \(GuessParams theGuess) -&gt; do
    -- Wait for script to have a UTxO of a least 1 lovelace
    utxos &lt;- fundsAtAddressGeq gameAddress (Ada.lovelaceValueOf 1)

    let redeemer = clearString theGuess
        tx       = collectFromScript utxos redeemer

    let hashedSecretWord = findSecretWordValue utxos
        isCorrectSecretWord = fmap (`isGoodGuess` redeemer) hashedSecretWord == Just 

    void (submitTxConstraintsSpending gameInstance utxos tx)

-- | Find the secret word in the Datum of the UTxOs
findSecretWordValue :: Map TxOutRef ChainIndexTxOut -&gt; Maybe HashedString
findSecretWordValue =
  listToMaybe . catMaybes . Map.elems . Map.map secretWordValue

-- | Extract the secret word in the Datum of a given transaction output is possible
secretWordValue :: ChainIndexTxOut -&gt; Maybe HashedString
secretWordValue o = do
  Datum d &lt;- either (const Nothing) Just (_ciTxOutDatum o)
  PlutusTx.fromBuiltinData d
</code></pre>
<p>At which part do I have to change the code, so it only unlocks one UTxO (best case: a random choice of both)?</p>

<plutus><smart-contract><plutus-playground>
Select only one UTxO from script address
# Select only one UTxO from script address

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

I need to get only one UTxO from the script address, where its datum hash matches the redeemer hash. For example I have locked two UTxO with 10 Ada  and the DatumHash "house" at the script and only want to unlock one of them.
I use the guessing game example of the Plutus Playground, but it unlocks both UTxO when the Redeemer has the same hash:
guess = endpoint @"guess" @GuessParams $ \(GuessParams theGuess) -> do
    -- Wait for script to have a UTxO of a least 1 lovelace
    utxos <- fundsAtAddressGeq gameAddress (Ada.lovelaceValueOf 1)

    let redeemer = clearString theGuess
        tx       = collectFromScript utxos redeemer

    let hashedSecretWord = findSecretWordValue utxos
        isCorrectSecretWord = fmap (`isGoodGuess` redeemer) hashedSecretWord == Just 

    void (submitTxConstraintsSpending gameInstance utxos tx)

-- | Find the secret word in the Datum of the UTxOs
findSecretWordValue :: Map TxOutRef ChainIndexTxOut -> Maybe HashedString
findSecretWordValue =
  listToMaybe . catMaybes . Map.elems . Map.map secretWordValue

-- | Extract the secret word in the Datum of a given transaction output is possible
secretWordValue :: ChainIndexTxOut -> Maybe HashedString
secretWordValue o = do
  Datum d <- either (const Nothing) Just (_ciTxOutDatum o)
  PlutusTx.fromBuiltinData d

At which part do I have to change the code, so it only unlocks one UTxO (best case: a random choice of both)?

**Tags:** plutus, smart-contract, plutus-playground

---

## 1 answers

### Answer by username_1 on 2022-03-22. Score: 0

You may need to update this part:
utxos <- fundsAtAddressGeq gameAddress (Ada.lovelaceValueOf 1)

Try:
utxos <- Map.take 1 <$> fundsAtAddressGeq gameAddress (Ada.lovelaceValueOf 1)

You need to change the import on top to
import PlutusTx.Prelude hiding (pure) so as to use <$>.

I have tried it using 4 wallets (somehow the Playground does not allow one wallet to make multiple calls) and it worked.


---

