<p>The offline code is dependente of plutus-apps and this project lags behind the nodes features.
Look at diferente tags/branches for plutus-apps...</p>

CC BY-SA 4.0
2022-10-24T09:05:04.870Z
2022-10-24T09:05:04.870Z
Cristovao Morgado
<p>I tried to use the inline datum of a reference input in the minting policy below:</p>
<pre><code>{-# INLINABLE validateMinting2 #-}
validateMinting2 :: TxOutRef -&gt; BuiltinData -&gt; BuiltinData -&gt; ()
validateMinting2 oref _ rawCtx =
  check $
    if not hasUsedUTXO
      then not $ cdEndContract contractData
      else True 

  where
    ctx = unsafeFromBuiltinData rawCtx
    symbol = ownCurrencySymbol ctx
    info = scriptContextTxInfo ctx
    inputs = txInfoInputs info
    contractData = case filter f $ map m $ txInfoReferenceInputs info of
      [TxOut{txOutDatum}] -&gt; case txOutDatum of
        NoOutputDatum -&gt; traceError &quot;NoDat&quot;
        OutputDatumHash _ -&gt; traceError &quot;Hash&quot;
        OutputDatum (Datum d) -&gt; case fromBuiltinData @ContractData d of
          Just dat -&gt; dat
          _        -&gt; traceError &quot;nothing&quot;
      _   -&gt; traceError &quot;nope&quot;
      where
        f TxOut{txOutValue} = valueOf txOutValue symbol contractDataTokenName == 1
        m inInfo = txInInfoResolved inInfo

    hasUsedUTXO :: Bool
    hasUsedUTXO
      | [_] &lt;- filter ((oref ==) . txInInfoOutRef) inputs = True
      | otherwise                                         = False

</code></pre>
<p>When I use the cli to mint with reference input (<code>--read-only-tx-in-reference</code>) everything works as expected and the inline datum can be used, but when I try to write offchain code for this it fails with:
<code>Contract instance stopped with error: &quot;WalletContractError (ValidationError (ScriptFailure (EvaluationError [\&quot;Hash\&quot;] \&quot;CekEvaluationFailure: An error has occurred:  User error:\\nThe machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.\&quot;)))&quot;</code></p>
<p>This error is shown when I use the following offchain code:</p>
<pre><code>  pkh &lt;- ownFirstPaymentPubKeyHash 
  outRef &lt;- getUnspentOutput
  o      &lt;- fromJust &lt;$&gt; unspentTxOutFromRef outRef
  let
    policy     = mintValidator outRef
    symbol     = scriptCurrencySymbol policy
    mintValue  = Value.singleton symbol contractDataTokenName 1 &lt;&gt; Value.singleton symbol chunkCreationTokenName 1 &lt;&gt; Value.singleton symbol (chunkTokenName 0) 1
    l          = Constraints.unspentOutputs (Map.singleton outRef o) &lt;&gt; Constraints.plutusV2MintingPolicy policy
    constr     = Constraints.mustMintValueWithRedeemer (Redeemer $ toBuiltinData ()) mintValue &lt;&gt;
                 Constraints.mustSpendPubKeyOutput outRef &lt;&gt;
                 Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ ChunkDatum (Address (PubKeyCredential $ unPaymentPubKeyHash pkh) Nothing)  &quot;&quot;) (Value.singleton symbol chunkCreationTokenName 1 &lt;&gt; Value.singleton symbol (chunkTokenName 0) 1) &lt;&gt;
                 Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ ContractData [] [] 100000 False) (Value.singleton symbol contractDataTokenName 1)
-- ... create, sign, submit ...
  ciTransaction &lt;- fmap (txOutRefMap . fromJust) $ txFromTxId $ getCardanoTxId signed
  let
    fVal tokenName (citxo, _) = valueOf (citoValue citxo) symbol tokenName == 1
    [dataRef] = Map.keys $ Map.filter (fVal contractDataTokenName) ciTransaction
    [chunkRef] = Map.keys $ Map.filter (fVal (chunkTokenName 0)) ciTransaction
  
  dataOut      &lt;- fromJust &lt;$&gt; unspentTxOutFromRef dataRef
  chunkOut     &lt;- fromJust &lt;$&gt; unspentTxOutFromRef chunkRef
  addr &lt;- ownAddress
  let
    dat = Datum $ toBuiltinData $ ContractData [] [] 100000 False
    mintValue2 = Value.singleton symbol (otherTokenName 0 0) 1
    l2          = Constraints.plutusV2MintingPolicy policy &lt;&gt; Constraints.unspentOutputs (Map.singleton chunkRef chunkOut &lt;&gt; Map.singleton dataRef dataOut) &lt;&gt; Constraints.otherData dat
    constr2     = Constraints.mustReferenceOutput dataRef &lt;&gt; Constraints.mustMintValueWithRedeemer (Redeemer $ toBuiltinData ()) mintValue2 &lt;&gt; mustSpendPubKeyOutput chunkRef &lt;&gt;
                  Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ OtherDatum 0 0 0 addr 2000000) (Value.singleton &quot;&quot; &quot;&quot; 2000000 &lt;&gt; mintValue2) &lt;&gt;
                  Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ ChunkDatum (Address (PubKeyCredential $ unPaymentPubKeyHash pkh) Nothing)  &quot;&quot;) (Value.singleton symbol chunkCreationTokenName 1 &lt;&gt; Value.singleton symbol (chunkTokenName 0) 1)
-- ... create, sign, submit ...
</code></pre>
<p>Why does the transaction that is created in offchain code not contain the inline datum but a hash?
Is this currently possible with plutus-apps or do I still have to wait for this feature to be implemented?</p>
<p>If this is not possible at the moment I have one additional question: Is there a way to profile the execution cost of a script with the cardano-cli? I only know of <code>--calculate-plutus-script-cost</code> which produces a json file of the cost, but I also want to know which part of the script cost how much, similar to the  <code>writeScriptsTo</code> way.</p>

<plutus><minting><offchain>
InlineDatum on ReferenceOutput in OffChain code
# InlineDatum on ReferenceOutput in OffChain code

(Asked by: username_0 on 2022-10-08)

I tried to use the inline datum of a reference input in the minting policy below:
{-# INLINABLE validateMinting2 #-}
validateMinting2 :: TxOutRef -> BuiltinData -> BuiltinData -> ()
validateMinting2 oref _ rawCtx =
  check $
    if not hasUsedUTXO
      then not $ cdEndContract contractData
      else True 

  where
    ctx = unsafeFromBuiltinData rawCtx
    symbol = ownCurrencySymbol ctx
    info = scriptContextTxInfo ctx
    inputs = txInfoInputs info
    contractData = case filter f $ map m $ txInfoReferenceInputs info of
      [TxOut{txOutDatum}] -> case txOutDatum of
        NoOutputDatum -> traceError "NoDat"
        OutputDatumHash _ -> traceError "Hash"
        OutputDatum (Datum d) -> case fromBuiltinData @ContractData d of
          Just dat -> dat
          _        -> traceError "nothing"
      _   -> traceError "nope"
      where
        f TxOut{txOutValue} = valueOf txOutValue symbol contractDataTokenName == 1
        m inInfo = txInInfoResolved inInfo

    hasUsedUTXO :: Bool
    hasUsedUTXO
      | [_] <- filter ((oref ==) . txInInfoOutRef) inputs = True
      | otherwise                                         = False


When I use the cli to mint with reference input (--read-only-tx-in-reference) everything works as expected and the inline datum can be used, but when I try to write offchain code for this it fails with:
Contract instance stopped with error: "WalletContractError (ValidationError (ScriptFailure (EvaluationError [\"Hash\"] \"CekEvaluationFailure: An error has occurred:  User error:\\nThe machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.\")))"
This error is shown when I use the following offchain code:
  pkh <- ownFirstPaymentPubKeyHash 
  outRef <- getUnspentOutput
  o      <- fromJust <$> unspentTxOutFromRef outRef
  let
    policy     = mintValidator outRef
    symbol     = scriptCurrencySymbol policy
    mintValue  = Value.singleton symbol contractDataTokenName 1 <> Value.singleton symbol chunkCreationTokenName 1 <> Value.singleton symbol (chunkTokenName 0) 1
    l          = Constraints.unspentOutputs (Map.singleton outRef o) <> Constraints.plutusV2MintingPolicy policy
    constr     = Constraints.mustMintValueWithRedeemer (Redeemer $ toBuiltinData ()) mintValue <>
                 Constraints.mustSpendPubKeyOutput outRef <>
                 Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ ChunkDatum (Address (PubKeyCredential $ unPaymentPubKeyHash pkh) Nothing)  "") (Value.singleton symbol chunkCreationTokenName 1 <> Value.singleton symbol (chunkTokenName 0) 1) <>
                 Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ ContractData [] [] 100000 False) (Value.singleton symbol contractDataTokenName 1)
-- ... create, sign, submit ...
  ciTransaction <- fmap (txOutRefMap . fromJust) $ txFromTxId $ getCardanoTxId signed
  let
    fVal tokenName (citxo, _) = valueOf (citoValue citxo) symbol tokenName == 1
    [dataRef] = Map.keys $ Map.filter (fVal contractDataTokenName) ciTransaction
    [chunkRef] = Map.keys $ Map.filter (fVal (chunkTokenName 0)) ciTransaction
  
  dataOut      <- fromJust <$> unspentTxOutFromRef dataRef
  chunkOut     <- fromJust <$> unspentTxOutFromRef chunkRef
  addr <- ownAddress
  let
    dat = Datum $ toBuiltinData $ ContractData [] [] 100000 False
    mintValue2 = Value.singleton symbol (otherTokenName 0 0) 1
    l2          = Constraints.plutusV2MintingPolicy policy <> Constraints.unspentOutputs (Map.singleton chunkRef chunkOut <> Map.singleton dataRef dataOut) <> Constraints.otherData dat
    constr2     = Constraints.mustReferenceOutput dataRef <> Constraints.mustMintValueWithRedeemer (Redeemer $ toBuiltinData ()) mintValue2 <> mustSpendPubKeyOutput chunkRef <>
                  Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ OtherDatum 0 0 0 addr 2000000) (Value.singleton "" "" 2000000 <> mintValue2) <>
                  Constraints.mustPayWithInlineDatumToPubKey pkh (Datum $ toBuiltinData $ ChunkDatum (Address (PubKeyCredential $ unPaymentPubKeyHash pkh) Nothing)  "") (Value.singleton symbol chunkCreationTokenName 1 <> Value.singleton symbol (chunkTokenName 0) 1)
-- ... create, sign, submit ...

Why does the transaction that is created in offchain code not contain the inline datum but a hash?
Is this currently possible with plutus-apps or do I still have to wait for this feature to be implemented?
If this is not possible at the moment I have one additional question: Is there a way to profile the execution cost of a script with the cardano-cli? I only know of --calculate-plutus-script-cost which produces a json file of the cost, but I also want to know which part of the script cost how much, similar to the  writeScriptsTo way.

**Tags:** plutus, minting, offchain

---

## 1 answers

### Answer by username_1 on 2022-10-24. Score: 0

The offline code is dependente of plutus-apps and this project lags behind the nodes features.
Look at diferente tags/branches for plutus-apps...


---

