<p>Did you try to have at least two utxos and use one for the mint and one different for collateral?</p>
<p>--tx-in $(cat oref1.tmp) <br />
--tx-in-collateral $(cat oref2.tmp)</p>

CC BY-SA 4.0
2023-02-19T19:53:42.643Z
Yes.  It didn't help.  The problem seems to be that the `TxOutRef` stored onchain has some extra structure.
a_juggler
CC BY-SA 4.0
2023-02-20T02:18:59.203Z
Mhhh don't believe that, the TxOutRef is just a hash and an Index.
https://playground.plutus.iohkdev.io/doc/haddock/plutus-ledger/html/Ledger.html#t:TxOutRef
Manu Padilla
CC BY-SA 4.0
2023-02-19T19:21:14.940Z
2023-02-19T19:21:14.940Z
Manu Padilla
<p>Ok, I think I have it...</p>
<pre><code>oref_1 = TxOutRef
  { txOutRefId = TxId &quot;9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c&quot;
  , txOutRefIdx = 1
  }
</code></pre>
<p>This is not doing what you expect... you must do some conversions, if not its converting your hex string into something else.</p>
<p>You can do like this:</p>
<pre><code>import qualified Data.Maybe as DataMaybe(fromJust)
import qualified Data.Text as DataText
import qualified PlutusTx.Builtins.Class as TxBuiltinsClass
import qualified Prelude as P
import qualified Text.Hex as TextHex

stringToStrictText :: P.String -&gt; DataText.Text
stringToStrictText = DataText.pack

txOutRef = TextHex.decodeHex P.$ stringToStrictText &quot;9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c&quot;
txOutRef' = TxBuiltinsClass.toBuiltin P.$ DataMaybe.fromJust txOutRef

oref_1 = TxOutRef { txOutRefId = TxId txOutRef', txOutRefIdx = 1 }

</code></pre>
<p>For using TextHex you must add this in your cabal file:</p>
<pre><code>  build-depends: 
  hex-text
</code></pre>

CC BY-SA 4.0
2023-02-20T04:59:38.923Z
This works indeed.  Awesome!  As a side note, it is frustrating that it was so hard to deploy this simple example (taken from PPP week 5) to the blockchain.
a_juggler
CC BY-SA 4.0
2023-02-20T15:24:58.193Z
Yes, I known, to put all the parts in move in the beginning take some time, but the good part is that after that most of the rest is reuse them in different ways. Good Luck!
Manu Padilla
CC BY-SA 4.0
2023-02-21T09:48:14.080Z
You can also do it without `DataText.Text`, but using function from `Plutus.V1.Ledger.Bytes` instead. See https://github.com/input-output-hk/plutus-apps/blob/b220a4e4c861c7e09d9528839e81fbff85ebb2aa/plutus-e2e-tests/test/PlutusScripts/Helpers.hs#L19-L24
james
CC BY-SA 4.0
2023-02-20T02:36:57.943Z
2023-02-20T02:36:57.943Z
Manu Padilla
<p>UPDATE:
I'm making progress.  It is apparent the <code>TxOutRef</code> stored on-chain has some extra-structure.  For evidence I &quot;printed&quot; the <code>TxOutRef</code>'s of the 'utxo' hard-wired in my minting policy and the one stored onchain.  For the former/latter I uncommented the first/second comment in</p>
<pre class="lang-haskell prettyprint-override"><code>hasUTxO :: Bool
hasUTxO = case txInfoInputs info of
--  [i] -&gt; traceError . decodeUtf8 . getTxId . txOutRefId $ oref
--  [i] -&gt; traceError . decodeUtf8 . getTxId . txOutRefId . txInInfoOutRef $ i
  _   -&gt; traceError &quot;many inputs&quot;
</code></pre>
<p>The &quot;printouts&quot;, given by the transaction errors returned by <strong>cardano-cli</strong> were, respectively:</p>
<p>1.</p>
<pre class="lang-shell prettyprint-override"><code>Command failed: transaction build  Error: The following scripts have execution failures:
&lt;ommited stuff&gt;
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Script debugging logs: Redeemer decoded successfully
Script context decoded successfully
9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c
</code></pre>
<ol start="2">
<li></li>
</ol>
<pre class="lang-shell prettyprint-override"><code>Command failed: transaction build  Error: The following scripts have execution failures:
&lt;ommited stuff&gt;
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Caused by: [
  (builtin decodeUtf8)
  (con
    bytestring #9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c
  )
]
Script debugging logs: Redeemer decoded successfully
Script context decoded successfully
</code></pre>
<p>It is apparent that in 2. the relevant <code>txOutRefId</code> is buried inside some extra structure.  The question now is, how can I extract the <code>txOutRefId</code> from all this structure?</p>
<p>ORIGINAL POST:</p>
<p>This simple minting policy should serve to mint an NFT by consuming a UTxO parametrized by <code>TxOutRef</code>:</p>
<pre class="lang-haskell prettyprint-override"><code>{-# INLINABLE mkPolicy #-}
mkPolicy :: TxOutRef -&gt; () -&gt; ScriptContext -&gt; Bool
mkPolicy utxo () ctx = traceIfFalse &quot;UTxO not consumed&quot; hasUTxO
  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    hasUTxO :: Bool
    hasUTxO = any (\i -&gt; txInInfoOutRef i == utxo) $ txInfoInputs info
</code></pre>
<p>(To simplify, we don’t even check that the number of minted tokens is one, since that is a non-issue here.)</p>
<p>I have been unable to deploy a concrete instance of this minting policy using <strong>cardano-cli</strong>.  I now describe my procedure.  Hopefully somebody can point out where the mistake is!</p>
<p>The UPLC policy and corresponding currency symbol are obtained with:</p>
<pre class="lang-haskell prettyprint-override"><code>policy :: TxOutRef -&gt; MintingPolicy
policy oref = mkMintingPolicyScript $
  $$(PlutusTx.compile [|| mkUntypedMintingPolicy . mkPolicy ||])
  `PlutusTx.applyCode`
  PlutusTx.liftCode oref

curSymbol :: TxOutRef -&gt; CurrencySymbol
curSymbol = Scripts.scriptCurrencySymbol . policy
</code></pre>
<p>Meanwhile I have a wallet on Testnet PreProd with one UTxO:</p>
<pre class="lang-shell prettyprint-override"><code>[bash]$ cardano-cli query utxo --address $(cat wallet1.addr) --testnet-magic 1
                           TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c     1        9784666930 lovelace + TxOutDatumNone
</code></pre>
<p>so I define an instance of my minting policy with:</p>
<pre class="lang-haskell prettyprint-override"><code>oref_1 :: TxOutRef
oref_1 = TxOutRef
  { txOutRefId = TxId &quot;9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c&quot;
  , txOutRefIdx = 1
  }

policy_1 :: MintingPolicy
policy_1 = policy oref_1

curSymbol_1 :: CurrencySymbol
curSymbol_1 = curSymbol oref_1
</code></pre>
<p>(Note that I have enabled the language extension <code>OverloadedStrings</code>.)</p>
<p>Now I serialize the minting policy and currency symbol, as well as the unit redeemer, with:</p>
<pre class="lang-shell prettyprint-override"><code>Prelude Deploy SimpleMinting&gt; writePolicy &quot;ledger/mint_1.plutus&quot; policy_1
Right ()
Prelude Deploy SimpleMinting&gt; writeCS &quot;ledger/mint_1.hex&quot; curSymbol_1 
Prelude Deploy SimpleMinting&gt; writeUnit &quot;ledger/unit.json&quot;
</code></pre>
<p>where the commands <code>writePolicy</code>, <code>writeCS</code> and <code>writeUnit</code> are defined in this <a href="https://gist.github.com/ajuggler/18f128fe8ba6dc20c69a2de4ab8bde29" rel="nofollow noreferrer">Deploy.hs</a>.</p>
<p>Now, also inside directory 'ledger', I wrote the shell script 'mint.sh':</p>
<pre class="lang-shell prettyprint-override"><code>cardano-cli transaction build \
        --babbage-era \
        --testnet-magic 1 \
        --tx-in $(cat oref1.tmp) \
        --tx-in-collateral $(cat oref1.tmp) \
        --tx-out $(cat wallet1.addr)+&quot;2000000 lovelace&quot;+&quot;1 $(cat mint_1.hex).$(cat tokenName.hex)&quot; \
        --change-address $(cat wallet1.addr) \
        --mint &quot;1 $(cat mint_1.hex).$(cat tokenName.hex)&quot; \
        --mint-script-file mint_1.plutus \
        --mint-redeemer-file unit.json \
        --protocol-params-file protocol-params.json \
        --out-file tx.body
</code></pre>
<p>('tokenName.hex' was generated with <code>echo -n &quot;FUNKYTOKEN&quot; | xxd -ps &gt; tokenName.hex</code>.)</p>
<p>Let us verify that 'oref1.tmp' stores the correct <code>TxOutRef</code> as queried above with cardano-cli:</p>
<pre class="lang-shell prettyprint-override"><code>[bash]$ cat oref1.tmp ; echo
9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c#1
</code></pre>
<p>which is indeed correct.  So we try to build the transaction by executing:</p>
<pre class="lang-shell prettyprint-override"><code>[bash]$ ./mint.sh
Command failed: transaction build  Error: The following scripts have execution failures:
the script for policyId 0 (in the order of the PolicyIds) failed with: 
The Plutus script evaluation failed: An error has occurred:  User error:
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Script debugging logs: Redeemer decoded successfully
Script context decoded successfully
UTxO not consumed
PT5
</code></pre>
<p>The &quot;UTxO not consumed&quot; error message implies that my Minting Policy fails to recognize the <code>TxOutRef</code>.  I am really puzzled about what am I doing wrong.</p>
<p>For reference, here is the complete haskell code for the plutus minting policy: <a href="https://gist.github.com/ajuggler/06c5a4d1f23cc89376bbe5986ab558ae" rel="nofollow noreferrer">SimpleMinting.hs</a>.</p>

<plutus><cardano-cli><nft><minting><mintingpolicy>
Unable to deploy Plutus NFT Minting Policy parametrized by TxOutRef using cardano-cli
# Unable to deploy Plutus NFT Minting Policy parametrized by TxOutRef using cardano-cli

(Asked by: username_0 on 2023-02-18)

UPDATE:
I'm making progress.  It is apparent the TxOutRef stored on-chain has some extra-structure.  For evidence I "printed" the TxOutRef's of the 'utxo' hard-wired in my minting policy and the one stored onchain.  For the former/latter I uncommented the first/second comment in
hasUTxO :: Bool
hasUTxO = case txInfoInputs info of
--  [i] -> traceError . decodeUtf8 . getTxId . txOutRefId $ oref
--  [i] -> traceError . decodeUtf8 . getTxId . txOutRefId . txInInfoOutRef $ i
  _   -> traceError "many inputs"

The "printouts", given by the transaction errors returned by cardano-cli were, respectively:
1.
Command failed: transaction build  Error: The following scripts have execution failures:

The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Script debugging logs: Redeemer decoded successfully
Script context decoded successfully
9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c




Command failed: transaction build  Error: The following scripts have execution failures:

The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Caused by: [
  (builtin decodeUtf8)
  (con
    bytestring #9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c
  )
]
Script debugging logs: Redeemer decoded successfully
Script context decoded successfully

It is apparent that in 2. the relevant txOutRefId is buried inside some extra structure.  The question now is, how can I extract the txOutRefId from all this structure?
ORIGINAL POST:
This simple minting policy should serve to mint an NFT by consuming a UTxO parametrized by TxOutRef:
{-# INLINABLE mkPolicy #-}
mkPolicy :: TxOutRef -> () -> ScriptContext -> Bool
mkPolicy utxo () ctx = traceIfFalse "UTxO not consumed" hasUTxO
  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    hasUTxO :: Bool
    hasUTxO = any (\i -> txInInfoOutRef i == utxo) $ txInfoInputs info

(To simplify, we don’t even check that the number of minted tokens is one, since that is a non-issue here.)
I have been unable to deploy a concrete instance of this minting policy using cardano-cli.  I now describe my procedure.  Hopefully somebody can point out where the mistake is!
The UPLC policy and corresponding currency symbol are obtained with:
policy :: TxOutRef -> MintingPolicy
policy oref = mkMintingPolicyScript $
  $$(PlutusTx.compile [|| mkUntypedMintingPolicy . mkPolicy ||])
  `PlutusTx.applyCode`
  PlutusTx.liftCode oref

curSymbol :: TxOutRef -> CurrencySymbol
curSymbol = Scripts.scriptCurrencySymbol . policy

Meanwhile I have a wallet on Testnet PreProd with one UTxO:
[bash]$ cardano-cli query utxo --address $(cat wallet1.addr) --testnet-magic 1
                           TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c     1        9784666930 lovelace + TxOutDatumNone

so I define an instance of my minting policy with:
oref_1 :: TxOutRef
oref_1 = TxOutRef
  { txOutRefId = TxId "9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c"
  , txOutRefIdx = 1
  }

policy_1 :: MintingPolicy
policy_1 = policy oref_1

curSymbol_1 :: CurrencySymbol
curSymbol_1 = curSymbol oref_1

(Note that I have enabled the language extension OverloadedStrings.)
Now I serialize the minting policy and currency symbol, as well as the unit redeemer, with:
Prelude Deploy SimpleMinting> writePolicy "ledger/mint_1.plutus" policy_1
Right ()
Prelude Deploy SimpleMinting> writeCS "ledger/mint_1.hex" curSymbol_1 
Prelude Deploy SimpleMinting> writeUnit "ledger/unit.json"

where the commands writePolicy, writeCS and writeUnit are defined in this Deploy.hs.
Now, also inside directory 'ledger', I wrote the shell script 'mint.sh':
cardano-cli transaction build \
        --babbage-era \
        --testnet-magic 1 \
        --tx-in $(cat oref1.tmp) \
        --tx-in-collateral $(cat oref1.tmp) \
        --tx-out $(cat wallet1.addr)+"2000000 lovelace"+"1 $(cat mint_1.hex).$(cat tokenName.hex)" \
        --change-address $(cat wallet1.addr) \
        --mint "1 $(cat mint_1.hex).$(cat tokenName.hex)" \
        --mint-script-file mint_1.plutus \
        --mint-redeemer-file unit.json \
        --protocol-params-file protocol-params.json \
        --out-file tx.body

('tokenName.hex' was generated with echo -n "FUNKYTOKEN" | xxd -ps > tokenName.hex.)
Let us verify that 'oref1.tmp' stores the correct TxOutRef as queried above with cardano-cli:
[bash]$ cat oref1.tmp ; echo
9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c#1

which is indeed correct.  So we try to build the transaction by executing:
[bash]$ ./mint.sh
Command failed: transaction build  Error: The following scripts have execution failures:
the script for policyId 0 (in the order of the PolicyIds) failed with: 
The Plutus script evaluation failed: An error has occurred:  User error:
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Script debugging logs: Redeemer decoded successfully
Script context decoded successfully
UTxO not consumed
PT5

The "UTxO not consumed" error message implies that my Minting Policy fails to recognize the TxOutRef.  I am really puzzled about what am I doing wrong.
For reference, here is the complete haskell code for the plutus minting policy: SimpleMinting.hs.

**Tags:** plutus, cardano-cli, nft, minting, mintingpolicy

---

## 2 answers

### Answer by username_1 on 2023-02-20. Score: 1 | Accepted answer

Ok, I think I have it...
oref_1 = TxOutRef
  { txOutRefId = TxId "9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c"
  , txOutRefIdx = 1
  }

This is not doing what you expect... you must do some conversions, if not its converting your hex string into something else.
You can do like this:
import qualified Data.Maybe as DataMaybe(fromJust)
import qualified Data.Text as DataText
import qualified PlutusTx.Builtins.Class as TxBuiltinsClass
import qualified Prelude as P
import qualified Text.Hex as TextHex

stringToStrictText :: P.String -> DataText.Text
stringToStrictText = DataText.pack

txOutRef = TextHex.decodeHex P.$ stringToStrictText "9c087132a325f6483aca8398bab1a56eda1390e762984ba054c25cafd738486c"
txOutRef' = TxBuiltinsClass.toBuiltin P.$ DataMaybe.fromJust txOutRef

oref_1 = TxOutRef { txOutRefId = TxId txOutRef', txOutRefIdx = 1 }


For using TextHex you must add this in your cabal file:
  build-depends: 
  hex-text

> **Comments:**
> **username_0**: This works indeed.  Awesome!  As a side note, it is frustrating that it was so hard to deploy this simple example (taken from PPP week 5) to the blockchain.  
> **username_1**: Yes, I known, to put all the parts in move in the beginning take some time, but the good part is that after that most of the rest is reuse them in different ways. Good Luck!  
> **username_2**: You can also do it without `DataText.Text`, but using function from `Plutus.V1.Ledger.Bytes` instead. See https://github.com/input-output-hk/plutus-apps/blob/b220a4e4c861c7e09d9528839e81fbff85ebb2aa/plutus-e2e-tests/test/PlutusScripts/Helpers.hs#L19-L24  

---

### Answer by username_1 on 2023-02-19. Score: 0

Did you try to have at least two utxos and use one for the mint and one different for collateral?

--tx-in $(cat oref1.tmp)   
--tx-in-collateral $(cat oref2.tmp)

> **Comments:**
> **username_0**: Yes.  It didn't help.  The problem seems to be that the `TxOutRef` stored onchain has some extra structure.  
> **username_1**: Mhhh don't believe that, the TxOutRef is just a hash and an Index. https://playground.plutus.iohkdev.io/doc/haddock/plutus-ledger/html/Ledger.html#t:TxOutRef  

---

