Skip to content

Commit d97fc7f

Browse files
Add toShelleyUTxO and fromShelleyUTxO functions
1 parent 7b471fe commit d97fc7f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
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 (IsShelleyBasedEra, ShelleyBasedEra, ShelleyLedgerEra)
1010
import Cardano.Api.Internal.Eras.Core (IsCardanoEra)
11-
import Cardano.Api.Internal.Tx.Body (CtxUTxO, TxOut (..))
11+
import Cardano.Api.Internal.Tx.Body
12+
( CtxUTxO,
13+
TxOut(..),
14+
toShelleyTxOut,
15+
fromShelleyTxIn,
16+
fromShelleyTxOut,
17+
toShelleyTxIn )
1218
import Cardano.Api.Internal.TxIn (TxIn (..))
1319

1420
import Cardano.Ledger.Babbage ()
21+
import Cardano.Ledger.Shelley.UTxO qualified as Ledger
1522

1623
import Data.Aeson (FromJSON (..), ToJSON (..))
1724
import Data.Aeson qualified as Aeson
@@ -89,3 +96,19 @@ fromList = UTxO . Map.fromList
8996
-- | Convert to a list of key/value pairs.
9097
toList :: UTxO era -> [(TxIn, TxOut CtxUTxO era)]
9198
toList (UTxO xs) = Map.toList xs
99+
100+
-- | Convert from a `cardano-api` `UTxO` to a `cardano-ledger` UTxO.
101+
toShelleyUTxO :: ShelleyBasedEra era -> UTxO era -> Ledger.UTxO (ShelleyLedgerEra era)
102+
toShelleyUTxO sbe =
103+
Ledger.UTxO . Map.foldMapWithKey f . unUTxO
104+
where
105+
f i o =
106+
Map.singleton (toShelleyTxIn i) (toShelleyTxOut sbe o)
107+
108+
-- | Convert from a `cardano-ledger` `UTxO` to a `cardano-api` UTxO.
109+
fromShelleyUTxO :: ShelleyBasedEra era -> Ledger.UTxO (ShelleyLedgerEra era) -> UTxO era
110+
fromShelleyUTxO sbe =
111+
UTxO . Map.foldMapWithKey f . Ledger.unUTxO
112+
where
113+
f i o =
114+
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)