user4301: I'm currently going through week 5 of the plutus piooner lectures, and I have some questions about the NFT process. Here is a reference with the complete code, but here are the important parts:
The on-chain part makes sense to me. We want our NFT to be unique, so we take as input a UTxO (always is unique) and check that we're minting only one token:
mkPolicy :: TxOutRef -> TokenName -> ScriptContext -> Bool
mkPolicy oref tn ctx = traceIfFalse "UTxO not consumed"   hasUTxO           &&
                       traceIfFalse "wrong amount minted" checkMintedAmount
    where
    ...

The off-chain part is what brings me some confusion, so let's lay down what we want to achieve:

We need to build a transaction that spends a specified UTxO and mints only one token.

The most relevant part of the off-chain code is this:
let val     = Value.singleton (curSymbol oref tn) tn 1
    lookups = Constraints.monetaryPolicy (policy oref tn) <> Constraints.unspentOutputs utxos
    tx      = Constraints.mustForgeValue val <> Constraints.mustSpendPubKeyOutput oref


I understand that mustSpendPubKeyOutput makes sure that the transaction to be built contains the necessary UTxO (oref) so that the on-chain policy passes. What happens behind the scenes? If the constraint is not specified Plutus will just use it's own logic to grab the UTxO used for building this transaction? If the constraint is not specified, is there still a chance that the right UTxO will be used?

What we need is to build a transaction that spends the required UTxO. One way of doing this is what we done before by specifying a constraint. But what if we only pass one UTxO to the lookup? Then transaction will be forced to be built with that UTxO right? There are no other options to be picked. Well, I tried that and it fails, this very clearly proves my understanding is wrong, why?

So far in the course we never had to pass UTxOs on the lookups and Plutus was able to find them automatically. Why it's only in this specific scenario that we need?



user1231: 
Actually in this code Value.singleton (curSymbol oref tn) tn 1 you are only specifying the Value to be minted. To be more precise: curSymbol oref tn is the currency symbol of your NFT to be minted with two parameters: oref and tn. For example, you can create a minting policy without any params (Free.hs in lesson 05) and mint using Value.singleton curSymbol tn 1. That's not related to Constraints.mustSpendPubKeyOutput.
Because if you remove mustSpendPubKeyOutput that means, you will not spend any UTxO and if you check the onchain code, there is a validation ensuring the use of the UTxO.
Purpose to use a UTxO to create an NFT, it's because an NFT should be unique. To ensure that, we have to specify something unique in the currency symbol and all UTxO are uniques, if you spend a UTxO, you can never use it again. It's the best way to identify our NFT.


Here some concepts to be clear on how this works:

tx: specifies the transaction to be built. In one transaction many things can be achieved at same time: mint values, pay to pubkeys, spend UTxO's (from PubKeys or scripts).
lookups: in order to successfully validate a transaction (from onchain code), there should be a "guide" to specify where those UTxO are. We do that, using lookups.

So, we say to our onchain code: validate this transaction (tx) using these UTxOs (lookups).
submitTxConstraintsWith @Void lookups tx


4301: <p>I'm currently going through week 5 of the plutus piooner lectures, and I have some questions about the NFT process. <a href="https://plutus-pioneer-program.readthedocs.io/en/latest/pioneer/week5.html#example-3-nft" rel="nofollow noreferrer">Here</a> is a reference with the complete code, but here are the important parts:</p>
<p>The on-chain part makes sense to me. We want our NFT to be unique, so we take as input a UTxO (always is unique) and check that we're minting only one token:</p>
<pre><code>mkPolicy :: TxOutRef -&gt; TokenName -&gt; ScriptContext -&gt; Bool
mkPolicy oref tn ctx = traceIfFalse &quot;UTxO not consumed&quot;   hasUTxO           &amp;&amp;
                       traceIfFalse &quot;wrong amount minted&quot; checkMintedAmount
    where
    ...
</code></pre>
<p>The off-chain part is what brings me some confusion, so let's lay down what we want to achieve:</p>
<blockquote>
<p>We need to build a transaction that spends a specified UTxO and mints only one token.</p>
</blockquote>
<p>The most relevant part of the off-chain code is this:</p>
<pre><code>let val     = Value.singleton (curSymbol oref tn) tn 1
    lookups = Constraints.monetaryPolicy (policy oref tn) &lt;&gt; Constraints.unspentOutputs utxos
    tx      = Constraints.mustForgeValue val &lt;&gt; Constraints.mustSpendPubKeyOutput oref
</code></pre>
<ol>
<li><p>I understand that <code>mustSpendPubKeyOutput</code> makes sure that the transaction to be built contains the necessary UTxO (<code>oref</code>) so that the on-chain policy passes. What happens behind the scenes? If the constraint is not specified Plutus will just use it's own logic to grab the UTxO used for building this transaction? If the constraint is not specified, is there still a chance that the right UTxO will be used?</p>
</li>
<li><p>What we need is to build a transaction that spends the required UTxO. One way of doing this is what we done before by specifying a constraint. But what if we <strong>only pass one</strong> UTxO to the lookup? Then transaction will be <strong>forced</strong> to be built with that UTxO right? There are no other options to be picked. Well, I tried that and it fails, this very clearly proves my understanding is wrong, why?</p>
</li>
<li><p>So far in the course we never had to pass UTxOs on the lookups and Plutus was able to find them automatically. Why it's only in this specific scenario that we need?</p>
</li>
</ol>
 1231: <ol>
<li>Actually in this code <code>Value.singleton (curSymbol oref tn) tn 1</code> you are only specifying the Value to be minted. To be more precise: <code>curSymbol oref tn</code> is the currency symbol of your NFT to be minted with two parameters: oref and tn. For example, you can create a minting policy without any params (Free.hs in lesson 05) and mint using <code>Value.singleton curSymbol tn 1</code>. That's not related to <code>Constraints.mustSpendPubKeyOutput</code>.</li>
<li>Because if you remove <code>mustSpendPubKeyOutput</code> that means, you will not spend any UTxO and if you check the onchain code, there is a validation ensuring the use of the UTxO.</li>
<li>Purpose to use a UTxO to create an NFT, it's because an NFT should be unique. To ensure that, we have to specify something <strong>unique</strong> in the currency symbol and all UTxO are uniques, if you spend a UTxO, you can never use it again. It's the best way to identify our NFT.</li>
</ol>
<hr />
<p>Here some concepts to be clear on how this works:</p>
<ul>
<li>tx: specifies the transaction to be built. In one transaction many things can be achieved at same time: mint values, pay to pubkeys, spend UTxO's (from PubKeys or scripts).</li>
<li>lookups: in order to successfully validate a transaction (from onchain code), there should be a &quot;guide&quot; to specify where those UTxO are. We do that, using lookups.</li>
</ul>
<p>So, we say to our onchain code: validate this transaction (tx) using these UTxOs (lookups).</p>
<pre><code>submitTxConstraintsWith @Void lookups tx
</code></pre>
