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 ?

3 Answers 3


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)

  • I am getting type errors from using this.
    – szhang9921
    May 3, 2022 at 11:07
  • @szhang9921 Can you share your code? May 3, 2022 at 16:18
  • it works now , the type error was because i was parsing the final value wrong. Thanks
    – szhang9921
    May 4, 2022 at 16:31

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 enter image description here

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.

  • 1
    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
    Dec 15, 2022 at 15:04

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.