diff --git a/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs b/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs index bf092ceeef..806ca854ee 100644 --- a/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs +++ b/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs @@ -6,12 +6,24 @@ module Cardano.Api.Internal.Tx.UTxO where -import Cardano.Api.Internal.Eon.ShelleyBasedEra (IsShelleyBasedEra) +import Cardano.Api.Internal.Eon.ShelleyBasedEra + ( IsShelleyBasedEra + , ShelleyBasedEra + , ShelleyLedgerEra + ) import Cardano.Api.Internal.Eras.Core (IsCardanoEra) -import Cardano.Api.Internal.Tx.Body (CtxUTxO, TxOut (..)) +import Cardano.Api.Internal.Tx.Body + ( CtxUTxO + , TxOut (..) + , fromShelleyTxIn + , fromShelleyTxOut + , toShelleyTxIn + , toShelleyTxOut + ) import Cardano.Api.Internal.TxIn (TxIn (..)) import Cardano.Ledger.Babbage () +import Cardano.Ledger.Shelley.UTxO qualified as Ledger import Data.Aeson (FromJSON (..), ToJSON (..)) import Data.Aeson qualified as Aeson @@ -74,6 +86,10 @@ filterWithKey fn = UTxO . Map.filterWithKey fn . unUTxO inputSet :: UTxO era -> Set TxIn inputSet = Map.keysSet . unUTxO +-- | Get the UTxO output set. +txOutputs :: UTxO era -> [TxOut CtxUTxO era] +txOutputs = Map.elems . unUTxO + -- | Remove the right hand side from the left hand side. difference :: UTxO era -> UTxO era -> UTxO era difference a b = UTxO $ Map.difference (unUTxO a) (unUTxO b) @@ -85,3 +101,19 @@ fromList = UTxO . Map.fromList -- | Convert to a list of key/value pairs. toList :: UTxO era -> [(TxIn, TxOut CtxUTxO era)] toList (UTxO xs) = Map.toList xs + +-- | Convert from a `cardano-api` `UTxO` to a `cardano-ledger` UTxO. +toShelleyUTxO :: ShelleyBasedEra era -> UTxO era -> Ledger.UTxO (ShelleyLedgerEra era) +toShelleyUTxO sbe = + Ledger.UTxO . Map.foldMapWithKey f . unUTxO + where + f i o = + Map.singleton (toShelleyTxIn i) (toShelleyTxOut sbe o) + +-- | Convert from a `cardano-ledger` `UTxO` to a `cardano-api` UTxO. +fromShelleyUTxO :: ShelleyBasedEra era -> Ledger.UTxO (ShelleyLedgerEra era) -> UTxO era +fromShelleyUTxO sbe = + UTxO . Map.foldMapWithKey f . Ledger.unUTxO + where + f i o = + Map.singleton (fromShelleyTxIn i) (fromShelleyTxOut sbe o) diff --git a/cardano-api/src/Cardano/Api/Tx/UTxO.hs b/cardano-api/src/Cardano/Api/Tx/UTxO.hs index 798427c13b..51126eafcd 100644 --- a/cardano-api/src/Cardano/Api/Tx/UTxO.hs +++ b/cardano-api/src/Cardano/Api/Tx/UTxO.hs @@ -6,9 +6,12 @@ module Cardano.Api.Tx.UTxO , UTxO.filter , UTxO.filterWithKey , UTxO.inputSet + , UTxO.txOutputs , UTxO.difference , UTxO.fromList , UTxO.toList + , UTxO.fromShelleyUTxO + , UTxO.toShelleyUTxO ) where