22{-# LANGUAGE FlexibleContexts #-}
33{-# LANGUAGE GeneralizedNewtypeDeriving #-}
44{-# LANGUAGE ScopedTypeVariables #-}
5- {-# LANGUAGE UndecidableInstances #-}
5+ {-# LANGUAGE StandaloneDeriving #-}
6+ {-# LANGUAGE TypeFamilies #-}
67
78module Cardano.Api.Internal.Tx.UTxO where
89
@@ -24,22 +25,25 @@ import Data.Aeson.KeyMap qualified as KeyMap
2425import Data.Aeson.Types (Parser )
2526import Data.Map (Map )
2627import Data.Map qualified as Map
28+ import Data.MonoTraversable
2729import Data.Set (Set )
2830import Data.Text (Text )
2931import GHC.Exts qualified as GHC
3032
3133newtype UTxO era = UTxO { unUTxO :: Map TxIn (TxOut CtxUTxO era )}
3234 deriving stock (Eq , Show )
33- deriving newtype (Semigroup , Monoid , GHC.IsList )
35+ deriving newtype (Semigroup , Monoid )
36+
37+ instance GHC. IsList (UTxO era ) where
38+ type Item (UTxO era ) = (TxIn , TxOut CtxUTxO era )
39+ fromList = UTxO . GHC. fromList
40+ toList = GHC. toList . unUTxO
3441
3542instance IsCardanoEra era => ToJSON (UTxO era ) where
3643 toJSON (UTxO m) = toJSON m
3744 toEncoding (UTxO m) = toEncoding m
3845
39- instance
40- IsShelleyBasedEra era
41- => FromJSON (UTxO era )
42- where
46+ instance IsShelleyBasedEra era => FromJSON (UTxO era ) where
4347 parseJSON = Aeson. withObject " UTxO" $ \ hm -> do
4448 let l = GHC. toList $ KeyMap. toHashMapText hm
4549 res <- mapM toTxIn l
@@ -51,6 +55,17 @@ instance
5155 <$> parseJSON (Aeson. String txinText)
5256 <*> parseJSON txOutVal
5357
58+ type instance Element (UTxO era ) = TxOut CtxUTxO era
59+
60+ instance MonoFunctor (UTxO era ) where
61+ omap f (UTxO utxos) = UTxO $ f <$> utxos
62+
63+ deriving newtype instance MonoFoldable (UTxO era )
64+
65+ instance MonoTraversable (UTxO era ) where
66+ otraverse = omapM
67+ omapM f (UTxO utxos) = UTxO <$> omapM f utxos
68+
5469-- | Infix version of `difference`.
5570(\\) :: UTxO era -> UTxO era -> UTxO era
5671a \\ b = difference a b
0 commit comments