0

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)?

1 Answer 1

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.

Your Answer

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

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