Skip to content

Commit 3e55214

Browse files
authored
Merge pull request #812 from locallycompact/lc/utxo-outputs-function
Cardano.Api.Tx.UTxO: add txOutputs, toShelleyUTxO, fromShelleyUTxO
2 parents 457b7eb + 259c840 commit 3e55214

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@
66

77
module 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+
)
1014
import 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+
)
1223
import Cardano.Api.Internal.TxIn (TxIn (..))
1324

1425
import Cardano.Ledger.Babbage ()
26+
import Cardano.Ledger.Shelley.UTxO qualified as Ledger
1527

1628
import Data.Aeson (FromJSON (..), ToJSON (..))
1729
import Data.Aeson qualified as Aeson
@@ -74,6 +86,10 @@ filterWithKey fn = UTxO . Map.filterWithKey fn . unUTxO
7486
inputSet :: UTxO era -> Set TxIn
7587
inputSet = 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.
7894
difference :: UTxO era -> UTxO era -> UTxO era
7995
difference 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.
86102
toList :: UTxO era -> [(TxIn, TxOut CtxUTxO era)]
87103
toList (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)

cardano-api/src/Cardano/Api/Tx/UTxO.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ module Cardano.Api.Tx.UTxO
66
, UTxO.filter
77
, UTxO.filterWithKey
88
, UTxO.inputSet
9+
, UTxO.txOutputs
910
, UTxO.difference
1011
, UTxO.fromList
1112
, UTxO.toList
13+
, UTxO.fromShelleyUTxO
14+
, UTxO.toShelleyUTxO
1215
)
1316
where
1417

0 commit comments

Comments
 (0)