/
UTxO.hs
57 lines (50 loc) · 1.92 KB
/
UTxO.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Hydra.Cardano.Api.UTxO where
import Hydra.Cardano.Api.Prelude
import Hydra.Cardano.Api.TxId (toLedgerTxId)
import Hydra.Cardano.Api.TxIn (fromLedgerTxIn, toLedgerTxIn)
import Hydra.Cardano.Api.TxOut (fromLedgerTxOut, toLedgerTxOut)
import qualified Cardano.Api.UTxO as UTxO
import qualified Cardano.Ledger.Babbage.TxBody as Ledger
import qualified Cardano.Ledger.BaseTypes as Ledger
import qualified Cardano.Ledger.Shelley.UTxO as Ledger
import qualified Cardano.Ledger.TxIn as Ledger
import qualified Data.Map as Map
import Data.String (IsString (..))
import qualified Data.Text as Text
import Data.Foldable (toList)
-- | Get a human-readable pretty text representation of a UTxO.
renderUTxO :: IsString str => UTxO -> str
renderUTxO =
fromString . Text.unpack . Text.intercalate "\n" . fmap UTxO.render . UTxO.pairs
-- | Construct a UTxO from a transaction. This constructs artificial `TxIn`
-- (a.k.a output reference) from the transaction itself, zipping them to the
-- outputs they correspond to.
utxoFromTx :: Tx Era -> UTxO
utxoFromTx (Tx body@(ShelleyTxBody _ ledgerBody _ _ _ _) _) =
let txOuts = toList $ Ledger.outputs' ledgerBody
txIns =
[ Ledger.TxIn (toLedgerTxId $ getTxId body) ix
| ix <- [Ledger.TxIx 0 .. toEnum (length txOuts)]
]
in fromLedgerUTxO $ Ledger.UTxO $ Map.fromList $ zip txIns txOuts
-- * Type Conversions
toLedgerUTxO :: UTxO -> Ledger.UTxO LedgerEra
toLedgerUTxO =
Ledger.UTxO . Map.foldMapWithKey fn . UTxO.toMap
where
fn ::
TxIn ->
TxOut CtxUTxO Era ->
Map (Ledger.TxIn StandardCrypto) (Ledger.TxOut LedgerEra)
fn i o =
Map.singleton (toLedgerTxIn i) (toLedgerTxOut o)
fromLedgerUTxO :: Ledger.UTxO LedgerEra -> UTxO
fromLedgerUTxO =
UTxO . Map.foldMapWithKey fn . Ledger.unUTxO
where
fn ::
Ledger.TxIn StandardCrypto ->
Ledger.TxOut LedgerEra ->
Map TxIn (TxOut CtxUTxO Era)
fn i o =
Map.singleton (fromLedgerTxIn i) (fromLedgerTxOut o)