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 ?
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)
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.
_ciTxOutDatum
we match it using Right
and not Left
.
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.