<p>The ChainIndexTxOut type has a data field called <code>_ciTxOutValue :: Value</code> and another that is <code>_ciTxOutDatum :: Either DatumHash Datum</code> I'll show you how to get the value. Getting the datum is the same process with the extra step of having to extract it from the Either binding.</p>
<pre><code>utxos &lt;- utxosAt ...
list = Map.toList utxos
firstUtxo = head list
ci = snd firstUtxo
val = _ciTxOutVale ci
</code></pre>
<p>utxos is type Map(TxOutRef ChainIndexTxOut)</p>
<p>list is type [(TxOutRef, ChainIndexTxOut)]</p>
<p>firstUtxo is type (TxOutRef, ChainIndexTxOut)</p>
<p>ci is type ChainIndexTxOut</p>
<p>val is type Value</p>
<p>All those lines can be combined to <code>vals = _ciTxOutValue (snd (head (Map.toList utxos)))</code> or if you want the sum of all the of the values instead of just the value in the first utxo at the address, you can do <code>vals = _ciTxOutValue &lt;$&gt; (snd &lt;$&gt; Map.toList utxos)</code></p>

CC BY-SA 4.0
2022-05-03T11:07:25.947Z
I am getting type errors from using this.
szhang9921
CC BY-SA 4.0
2022-05-03T16:18:09.437Z
@szhang9921 Can you share your code?
Kyle Murphy
CC BY-SA 4.0
2022-05-04T16:31:47.607Z
it works now , the type error was because i was parsing the final value wrong. Thanks
szhang9921
CC BY-SA 4.0
2022-05-02T19:14:58.463Z
2022-05-02T19:25:43.077Z
2022-05-02T19:25:43.077Z
Kyle Murphy
<p>The upshot is that you might not find a datum, but instead get a hash of the datum (it depends).</p>
<p>When submitting a transaction, the full &quot;unhashed&quot; datum is not automatically attached to the output. This to save space and add use cased where the sender wants to keep the datum a secret. If it is not attached, you will not find it onchain, only the hash of the datum used. Reversing this is (computational) impossible by construction.</p>
<p>In the other case, where there is one attached, you can retrieve it. As stated by Kyle Murphy the constructor <code>ChainIndexTxOut</code><a href="https://playground.plutus.iohkdev.io/doc/haddock/plutus-ledger/html/Ledger.html#t:ChainIndexTxOut" rel="nofollow noreferrer">(1)</a> that resulted from the filter &quot;<code>findInitalAmount fundAddress</code>&quot; over all UTxO's sitting at the script address <code>scrAddress</code> contains all info known of these outputs fitting the condition you specified.</p>
<p>The function <code>utxosAt</code> returns a map of two fields, <code>Map TxOutRef ChainIndexTxOut</code> <a href="https://playground.plutus.iohkdev.io/doc/haddock/playground-common/html/Playground-Contract.html#v:utxosAt" rel="nofollow noreferrer">(2)</a>. Each <code>TxOutRef</code> contains the hash of the transaction that made this utxo and an associated id relative to the other outputs of that transaction (<code>TxOutRef TxId Integer</code>). Now the other field in the <code>Map</code> is the <code>ChainIndexTxOut</code> which can contain one of two things depending on the query. If you queried a script address, this will be
<a href="https://i.sstatic.net/41nuk.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/41nuk.png" alt="enter image description here" /></a></p>
<p>Note That the <code>_ciTxOutDatum</code> constructor holds a <code>Either DatumHash Datum</code>. So if the UTxO contains a datum you can retrieve, you can extract it with <code>Right</code> like below.</p>
<pre><code>getDatum :: ChainIndexTxOut -&gt; Maybe Datum
getDatum o = case _ciTxOutDatum o of
            Right  i               -&gt; Just i 
            Left   j               -&gt; Nothing
</code></pre>
<p>I hope this helps.</p>

CC BY-SA 4.0
2022-12-15T15:04:22.710Z
Great explanation. However, I believe your last sentence is inaccurate and in order to read the datum from the call to `_ciTxOutDatum` we match it using `Right` and not `Left`.
caike
CC BY-SA 4.0
2022-05-03T15:42:08.053Z
2022-12-15T15:21:49.577Z
2022-12-15T15:21:49.577Z
Fermat
<p>OffChain:
It's a datum hash - so there is no reverse lookup if you don't know how to construct datums for a given contract.
Datum values are usually provided by the consumer of a script UTxO.</p>
<p>OnChain:
If this is onchain code (which it does not look like), you can use a function called <code>findDatum</code> to lookup datums provided by the consumer of a script UTxO.</p>

CC BY-SA 4.0
2022-05-02T19:08:03.303Z
2022-05-02T19:08:03.303Z
Will
<p>I currently have a utxo that i am getting like this.</p>
<pre><code>initalMatchPool &lt;- Map.filter (findInitalAmount fundAddress) &lt;$&gt; utxosAt scrAddress
</code></pre>
<p>How do i get the value of the datum attached to this specific utxo ?</p>

<utxo><datum><datum-value>
Get Value of Datum from a UTXO
# Get Value of Datum from a UTXO

(Asked by: username_0 on 2022-05-02)

I currently have a utxo that i am getting like this.
initalMatchPool <- Map.filter (findInitalAmount fundAddress) <$> utxosAt scrAddress

How do i get the value of the datum attached to this specific utxo ?

**Tags:** utxo, datum, datum-value

---

## 3 answers

### Answer by username_1 on 2022-05-02. Score: 2 | Accepted answer

The ChainIndexTxOut type has a data field called _ciTxOutValue :: Value and another that is _ciTxOutDatum :: Either DatumHash Datum I'll show you how to get the value. Getting the datum is the same process with the extra step of having to extract it from the Either binding.
utxos <- utxosAt ...
list = Map.toList utxos
firstUtxo = head list
ci = snd firstUtxo
val = _ciTxOutVale ci

utxos is type Map(TxOutRef ChainIndexTxOut)
list is type [(TxOutRef, ChainIndexTxOut)]
firstUtxo is type (TxOutRef, ChainIndexTxOut)
ci is type ChainIndexTxOut
val is type Value
All those lines can be combined to vals = _ciTxOutValue (snd (head (Map.toList utxos))) or if you want the sum of all the of the values instead of just the value in the first utxo at the address, you can do vals = _ciTxOutValue <$> (snd <$> Map.toList utxos)

> **Comments:**
> **username_0**: I am getting type errors from using this.  
> **username_1**: @szhang9921 Can you share your code?  
> **username_0**: it works now , the type error was because i was parsing the final value wrong. Thanks  

---

### Answer by username_2 on 2022-05-03. Score: 3

The upshot is that you might not find a datum, but instead get a hash of the datum (it depends).
When submitting a transaction, the full "unhashed" datum is not automatically attached to the output. This to save space and add use cased where the sender wants to keep the datum a secret. If it is not attached, you will not find it onchain, only the hash of the datum used. Reversing this is (computational) impossible by construction.
In the other case, where there is one attached, you can retrieve it. As stated by Kyle Murphy the constructor ChainIndexTxOut(1) that resulted from the filter "findInitalAmount fundAddress" over all UTxO's sitting at the script address scrAddress contains all info known of these outputs fitting the condition you specified.
The function utxosAt returns a map of two fields, Map TxOutRef ChainIndexTxOut (2). Each TxOutRef contains the hash of the transaction that made this utxo and an associated id relative to the other outputs of that transaction (TxOutRef TxId Integer). Now the other field in the Map is the ChainIndexTxOut which can contain one of two things depending on the query. If you queried a script address, this will be

Note That the _ciTxOutDatum constructor holds a Either DatumHash Datum. So if the UTxO contains a datum you can retrieve, you can extract it with Right like below.
getDatum :: ChainIndexTxOut -> Maybe Datum
getDatum o = case _ciTxOutDatum o of
            Right  i               -> Just i 
            Left   j               -> Nothing

I hope this helps.

> **Comments:**
> **username_3**: Great explanation. However, I believe your last sentence is inaccurate and in order to read the datum from the call to `_ciTxOutDatum` we match it using `Right` and not `Left`.  

---

### Answer by username_4 on 2022-05-02. Score: 1

OffChain:
It's a datum hash - so there is no reverse lookup if you don't know how to construct datums for a given contract.
Datum values are usually provided by the consumer of a script UTxO.

OnChain:
If this is onchain code (which it does not look like), you can use a function called `findDatum` to lookup datums provided by the consumer of a script UTxO.


---

