From 78f8d9bad73cab0bce8cd45aaef2be78ff814d2a Mon Sep 17 00:00:00 2001 From: Daniel Firth Date: Sun, 13 Apr 2025 15:07:36 +0000 Subject: [PATCH 1/2] Cardano.Api.Tx.UTxO: add txOutputs function --- cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs | 4 ++++ cardano-api/src/Cardano/Api/Tx/UTxO.hs | 1 + 2 files changed, 5 insertions(+) diff --git a/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs b/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs index bf092ceeef..28b5c43a7b 100644 --- a/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs +++ b/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs @@ -74,6 +74,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) diff --git a/cardano-api/src/Cardano/Api/Tx/UTxO.hs b/cardano-api/src/Cardano/Api/Tx/UTxO.hs index 798427c13b..1730cdb15f 100644 --- a/cardano-api/src/Cardano/Api/Tx/UTxO.hs +++ b/cardano-api/src/Cardano/Api/Tx/UTxO.hs @@ -6,6 +6,7 @@ module Cardano.Api.Tx.UTxO , UTxO.filter , UTxO.filterWithKey , UTxO.inputSet + , UTxO.txOutputs , UTxO.difference , UTxO.fromList , UTxO.toList From 259c840435794b3b208cff29fa92e591b0504c99 Mon Sep 17 00:00:00 2001 From: Daniel Firth Date: Mon, 14 Apr 2025 04:54:38 +0000 Subject: [PATCH 2/2] Add toShelleyUTxO and fromShelleyUTxO functions --- .../src/Cardano/Api/Internal/Tx/UTxO.hs | 32 +++++++++++++++++-- cardano-api/src/Cardano/Api/Tx/UTxO.hs | 2 ++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs b/cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs index 28b5c43a7b..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 @@ -89,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 1730cdb15f..51126eafcd 100644 --- a/cardano-api/src/Cardano/Api/Tx/UTxO.hs +++ b/cardano-api/src/Cardano/Api/Tx/UTxO.hs @@ -10,6 +10,8 @@ module Cardano.Api.Tx.UTxO , UTxO.difference , UTxO.fromList , UTxO.toList + , UTxO.fromShelleyUTxO + , UTxO.toShelleyUTxO ) where