4

In gift.hs, I do not understand the logic for why in the lookups for the grab contract there is Constraints.otherScript validator. The grabber is the consumer of the UTXO and thus the burden of providing the validator is on him, not the provider. It seems that the other is the provider who by definition does not have a validator.

grab :: forall w s e. AsContractError e => Contract w s e ()
grab = do
   utxos <- utxoAt scrAddress
   let orefs   = fst <$> Map.toList utxos
      lookups  = Constraints.unspentOutputs utxos      <>
                 Constraints.otherScript validator -- ###### THIS LINE #####
      tx :: TxConstraints Void Void
      tx       = mconcat [mustSpendScriptOutput oref $ Redeemer $ I 17 | oref <- orefs]
   ledgerTx <- submitTxConstraintsWith @Void lookups tx
   void $ awaitTxConfirmed $ txId ledgerTx
   logInfo @String $ "collected gifts"
1

The grabber is the consumer of the UTXO and thus the burden of providing the validator is on him, not the provider.

Correct! And I feel like this might be where your misunderstanding is.

You are right that "the burden of providing the validator" is on the grabber. The grab and give functions are called offchain to construct txs to perform those actions. So the line in question is in fact the code where the grabber "provid[es] the validator."

The Constraits are a way to declaratively specify

  1. What kind of script and UTxOs you want to target with your tx (lookups)
  2. What kind of tx you want to send (tx)

When we include

Constraints.otherScript validator

we are just saying that we want to target our validator with your tx. Or, it might be more accurate to say, we will need our validator in order to perform the tx.


Admittedly the name of the function as well as the documentation doesn't shed too much light on what it's doing:

-- | A script lookups value with a validator script
otherScript :: Validator -> ScriptLookups a
otherScript vl =
    let addr = Address.scriptAddress vl in
    mempty { slOtherScripts = Map.singleton addr vl }
3
  • So otherScript really means ourScript? This is very confusing. Jul 20 at 17:33
  • 1
    That's not quite right. Maybe think of it as adding "another" script to the lookups. You could theoretically include multiple validators if you're spending outputs from different scripts in a single transaction. Remember, validators just check if you are allowed to spend outputs at a script address. Jul 20 at 17:53
  • So it seems that in this particular case for grabbing, since there is only one grabber (at a time), there is just one validator. And hence there is no ambiguity in simply stating otherScript because there's just one script out there altogether for this particular transaction. Jul 20 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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