Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions cardano-api/src/Cardano/Api/Internal/Tx/UTxO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
3 changes: 3 additions & 0 deletions cardano-api/src/Cardano/Api/Tx/UTxO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading