<p>I should have used <code>--tx-out-datum-embed-file</code> instead of <code>--tx-out-datum-hash-file</code>.  I did not understand data/embedded data/inlined data.  Below is my understanding:</p>
<ul>
<li><p>In regular case one transaction attaches a hash (with <code>--tx-out-datum-hash-*</code>) to an output sent to a script address.  The transaction that consumes that output needs the actual data that is provided by <code>--tx-in-datum-*</code>.</p>
</li>
<li><p>Sometimes a script in the transaction that creates an output also needs the actual data present in the transaction (this is my case).  Then it should use <code>--tx-out-datum-embed-*</code>.  In this case the datum is added to <code>txIinfoData</code> in the transaction context that produces the transaction.  The script in the transaction that consumes that output also needs the actual data in the transaction context (<code>--tx-in-datum-*</code>) so there are two instances of the data both in the transaction that creates the output and in the transaction that consumes it.</p>
</li>
<li><p>So in this case inlined data could be used: data is attached to the producer transaction with <code>--tx-out-inline-datum-*</code> and the consuming transaction has access to it if <code>--tx-in-inline-datum-present</code> is specified for that output.</p>
</li>
</ul>

CC BY-SA 4.0
2024-06-20T07:53:46.883Z
2024-06-20T08:10:58.967Z
2024-06-20T08:10:58.967Z
amakarov
<p>I create a transaction with this command:</p>
<pre><code>cardano-cli latest transaction build \
    --testnet-magic 2 \
    --out-file &quot;$tx_unsigned_name&quot; \
    --tx-in &quot;$nft_utxo&quot; \
    --tx-in-datum-file data.json \
    --tx-in-redeemer-file redeemer.json \
    --tx-in-script-file contract_code.txt \
    --tx-in $utxo \
    --tx-in-collateral $collateral_utxo \
    --change-address &quot;$wallet_address&quot; \
    --tx-out &quot;$(cat contract_address.txt)+${nft}&quot; \
    --tx-out-datum-hash-file data_new.json \
    --invalid-hereafter $deadline
</code></pre>
<p>and expect that the <code>ctx.transaction.datums</code> has an entry <code>(h, d)</code> where <code>h</code> is hash of <code>data_new.json</code> and <code>d</code> is the data from that file.  But I printed the dict and it contains only the data from <code>data.json</code> file.</p>
<p><a href="https://github.com/input-output-hk/plutus-pioneer-program/blob/8cbc68d146c82cc95d9be44c30e049aba3774003/code/week01/src/Week01/EnglishAuction.hs#L160" rel="nofollow noreferrer">Here</a> is the line from plutus-pioneer-program code that searches the <code>datums</code> entry for the output data so I thought that should work.</p>
<p>The question is, how can I access the output data from the contract script?</p>
<p>Update:</p>
<p><a href="https://github.com/Plutonomicon/plutonomicon/blob/main/fundamentals.md" rel="nofollow noreferrer">Plutonomicon</a> says:</p>
<blockquote>
<p>The datums for the inputs are always contained in txInfoData. Anything
else may not be present. If you in your script depend on the datum for
an output being available, you must make sure when submitting the
transaction that you include the datum in txInfoData.</p>
</blockquote>
<p>So, how can I include the datum to <code>txIinfoData</code>?  One possibility is to use some additional input, but is there more straightforward solution?</p>

CC BY-SA 4.0
2024-06-19T17:27:39.050Z
Just realized that maybe I should use `--tx-out-datum-embed-file` instead of `--tx-out-datum-hash-file`.  I can not check this right now
amakarov
<smart-contract><transaction><aiken>
Access to output data from the onchain script
# Access to output data from the onchain script

(Asked by: username_0 on 2024-06-18)

I create a transaction with this command:
cardano-cli latest transaction build \
    --testnet-magic 2 \
    --out-file "$tx_unsigned_name" \
    --tx-in "$nft_utxo" \
    --tx-in-datum-file data.json \
    --tx-in-redeemer-file redeemer.json \
    --tx-in-script-file contract_code.txt \
    --tx-in $utxo \
    --tx-in-collateral $collateral_utxo \
    --change-address "$wallet_address" \
    --tx-out "$(cat contract_address.txt)+${nft}" \
    --tx-out-datum-hash-file data_new.json \
    --invalid-hereafter $deadline

and expect that the ctx.transaction.datums has an entry (h, d) where h is hash of data_new.json and d is the data from that file.  But I printed the dict and it contains only the data from data.json file.
Here is the line from plutus-pioneer-program code that searches the datums entry for the output data so I thought that should work.
The question is, how can I access the output data from the contract script?
Update:
Plutonomicon says:

The datums for the inputs are always contained in txInfoData. Anything
else may not be present. If you in your script depend on the datum for
an output being available, you must make sure when submitting the
transaction that you include the datum in txInfoData.

So, how can I include the datum to txIinfoData?  One possibility is to use some additional input, but is there more straightforward solution?

**Tags:** smart-contract, transaction, aiken
> **Comments:**
> **username_0**: Just realized that maybe I should use `--tx-out-datum-embed-file` instead of `--tx-out-datum-hash-file`.  I can not check this right now  

---

## 1 answers

### Answer by username_0 on 2024-06-20. Score: 0 | Accepted answer

I should have used `--tx-out-datum-embed-file` instead of `--tx-out-datum-hash-file`. I did not understand data/embedded data/inlined data. Below is my understanding:

* In regular case one transaction attaches a hash (with `--tx-out-datum-hash-*`) to an output sent to a script address. The transaction that consumes that output needs the actual data that is provided by `--tx-in-datum-*`.
* Sometimes a script in the transaction that creates an output also needs the actual data present in the transaction (this is my case). Then it should use `--tx-out-datum-embed-*`. In this case the datum is added to `txIinfoData` in the transaction context that produces the transaction. The script in the transaction that consumes that output also needs the actual data in the transaction context (`--tx-in-datum-*`) so there are two instances of the data both in the transaction that creates the output and in the transaction that consumes it.
* So in this case inlined data could be used: data is attached to the producer transaction with `--tx-out-inline-datum-*` and the consuming transaction has access to it if `--tx-in-inline-datum-present` is specified for that output.


---

