Skip to content

Commit 259c840

Browse files
Add toShelleyUTxO and fromShelleyUTxO functions
1 parent 78f8d9b commit 259c840

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

Lines changed: 30 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
@@ -89,3 +101,19 @@ fromList = UTxO . Map.fromList
89101
-- | Convert to a list of key/value pairs.
90102
toList :: UTxO era -> [(TxIn, TxOut CtxUTxO era)]
91103
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module Cardano.Api.Tx.UTxO
1010
, UTxO.difference
1111
, UTxO.fromList
1212
, UTxO.toList
13+
, UTxO.fromShelleyUTxO
14+
, UTxO.toShelleyUTxO
1315
)
1416
where
1517

0 commit comments

Comments
 (0)