66
77module Cardano.Api.Internal.Tx.UTxO where
88
9- import Cardano.Api.Internal.Eon.ShelleyBasedEra (IsShelleyBasedEra )
9+ import Cardano.Api.Internal.Eon.ShelleyBasedEra
10+ ( IsShelleyBasedEra
11+ , ShelleyBasedEra
12+ , ShelleyLedgerEra
13+ )
1014import Cardano.Api.Internal.Eras.Core (IsCardanoEra )
11- import Cardano.Api.Internal.Tx.Body (CtxUTxO , TxOut (.. ))
15+ import Cardano.Api.Internal.Tx.Body
16+ ( CtxUTxO
17+ , TxOut (.. )
18+ , fromShelleyTxIn
19+ , fromShelleyTxOut
20+ , toShelleyTxIn
21+ , toShelleyTxOut
22+ )
1223import Cardano.Api.Internal.TxIn (TxIn (.. ))
1324
1425import Cardano.Ledger.Babbage ()
26+ import Cardano.Ledger.Shelley.UTxO qualified as Ledger
1527
1628import Data.Aeson (FromJSON (.. ), ToJSON (.. ))
1729import Data.Aeson qualified as Aeson
@@ -74,6 +86,10 @@ filterWithKey fn = UTxO . Map.filterWithKey fn . unUTxO
7486inputSet :: UTxO era -> Set TxIn
7587inputSet = Map. keysSet . unUTxO
7688
89+ -- | Get the UTxO output set.
90+ txOutputs :: UTxO era -> [TxOut CtxUTxO era ]
91+ txOutputs = Map. elems . unUTxO
92+
7793-- | Remove the right hand side from the left hand side.
7894difference :: UTxO era -> UTxO era -> UTxO era
7995difference a b = UTxO $ Map. difference (unUTxO a) (unUTxO b)
@@ -85,3 +101,19 @@ fromList = UTxO . Map.fromList
85101-- | Convert to a list of key/value pairs.
86102toList :: UTxO era -> [(TxIn , TxOut CtxUTxO era )]
87103toList (UTxO xs) = Map. toList xs
104+
105+ -- | Convert from a `cardano-api` `UTxO` to a `cardano-ledger` UTxO.
106+ toShelleyUTxO :: ShelleyBasedEra era -> UTxO era -> Ledger. UTxO (ShelleyLedgerEra era )
107+ toShelleyUTxO sbe =
108+ Ledger. UTxO . Map. foldMapWithKey f . unUTxO
109+ where
110+ f i o =
111+ Map. singleton (toShelleyTxIn i) (toShelleyTxOut sbe o)
112+
113+ -- | Convert from a `cardano-ledger` `UTxO` to a `cardano-api` UTxO.
114+ fromShelleyUTxO :: ShelleyBasedEra era -> Ledger. UTxO (ShelleyLedgerEra era ) -> UTxO era
115+ fromShelleyUTxO sbe =
116+ UTxO . Map. foldMapWithKey f . Ledger. unUTxO
117+ where
118+ f i o =
119+ Map. singleton (fromShelleyTxIn i) (fromShelleyTxOut sbe o)
0 commit comments