Skip to content

Commit 24478d9

Browse files
Add module for UTxO type and operations
1 parent ab6fc23 commit 24478d9

File tree

10 files changed

+102
-28
lines changed

10 files changed

+102
-28
lines changed

cardano-api/cardano-api.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ library
9494
Cardano.Api.Ledger.Lens
9595
Cardano.Api.Network
9696
Cardano.Api.Shelley
97+
Cardano.Api.Tx.UTxO
9798

9899
build-depends:
99100
FailT,
@@ -244,6 +245,7 @@ library
244245
Cardano.Api.Internal.StakePoolMetadata
245246
Cardano.Api.Internal.Tx.Body
246247
Cardano.Api.Internal.Tx.Compatible
248+
Cardano.Api.Internal.Tx.UTxO
247249
Cardano.Api.Internal.TxIn
248250
Cardano.Api.Internal.TxMetadata
249251
Cardano.Api.Internal.Utils

cardano-api/src/Cardano/Api.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ import Cardano.Api.Internal.SerialiseUsing
11421142
import Cardano.Api.Internal.StakePoolMetadata
11431143
import Cardano.Api.Internal.Tx.Body
11441144
import Cardano.Api.Internal.Tx.Sign
1145+
import Cardano.Api.Internal.Tx.UTxO
11451146
import Cardano.Api.Internal.TxMetadata
11461147
import Cardano.Api.Internal.Utils
11471148
import Cardano.Api.Internal.Value

cardano-api/src/Cardano/Api/Internal/Convenience/Construction.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Cardano.Api.Internal.ProtocolParameters
2222
import Cardano.Api.Internal.Query
2323
import Cardano.Api.Internal.Tx.Body
2424
import Cardano.Api.Internal.Tx.Sign
25+
import Cardano.Api.Internal.Tx.UTxO (UTxO (..))
2526
import Cardano.Api.Internal.Utils
2627

2728
import qualified Cardano.Ledger.Api as L

cardano-api/src/Cardano/Api/Internal/Convenience/Query.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import Cardano.Api.Internal.ProtocolParameters
3535
import Cardano.Api.Internal.Query
3636
import Cardano.Api.Internal.Query.Expr
3737
import Cardano.Api.Internal.Tx.Body
38+
import Cardano.Api.Internal.Tx.UTxO
3839
import Cardano.Api.Internal.Utils
3940

4041
import qualified Cardano.Ledger.Api as L

cardano-api/src/Cardano/Api/Internal/Fees.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import Cardano.Api.Internal.Query
6767
import Cardano.Api.Internal.Script
6868
import Cardano.Api.Internal.Tx.Body
6969
import Cardano.Api.Internal.Tx.Sign
70+
import Cardano.Api.Internal.Tx.UTxO
7071
import Cardano.Api.Internal.Value
7172
import qualified Cardano.Api.Ledger.Lens as A
7273

cardano-api/src/Cardano/Api/Internal/Plutus.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import Cardano.Api.Internal.Eon.AlonzoEraOnwards (AlonzoEraOnwards (..
1414
import Cardano.Api.Internal.Eon.Convert (convert)
1515
import Cardano.Api.Internal.Eon.ShelleyBasedEra (ShelleyLedgerEra)
1616
import Cardano.Api.Internal.Pretty (Pretty (pretty), docToText)
17-
import Cardano.Api.Internal.Query (UTxO, toLedgerUTxO)
17+
import Cardano.Api.Internal.Query (toLedgerUTxO)
1818
import qualified Cardano.Api.Internal.ReexposeLedger as L
1919
import Cardano.Api.Internal.Script (ScriptHash, fromShelleyScriptHash)
2020
import qualified Cardano.Api.Internal.Script as Api
2121
import Cardano.Api.Internal.Tx.Body (ScriptWitnessIndex (..), toScriptIndex)
2222
import Cardano.Api.Internal.Tx.Sign (Tx (..))
23+
import Cardano.Api.Internal.Tx.UTxO (UTxO)
2324

2425
import qualified Cardano.Ledger.Alonzo.Scripts as L
2526
import qualified Cardano.Ledger.Alonzo.UTxO as Alonzo

cardano-api/src/Cardano/Api/Internal/Query.hs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module Cardano.Api.Internal.Query
2222
, QueryInEra (..)
2323
, QueryInShelleyBasedEra (..)
2424
, QueryUTxOFilter (..)
25-
, UTxO (..)
2625
, UTxOInAnyEra (..)
2726

2827
-- * Internal conversion functions
@@ -80,6 +79,7 @@ import Cardano.Api.Internal.ProtocolParameters
8079
import Cardano.Api.Internal.Query.Types
8180
import qualified Cardano.Api.Internal.ReexposeLedger as Ledger
8281
import Cardano.Api.Internal.Tx.Body
82+
import Cardano.Api.Internal.Tx.UTxO
8383

8484
import qualified Cardano.Chain.Update.Validation.Interface as Byron.Update
8585
import qualified Cardano.Ledger.Api as L
@@ -117,10 +117,6 @@ import Ouroboros.Network.PeerSelection.LedgerPeers.Type (LedgerPeerSna
117117
import Ouroboros.Network.Protocol.LocalStateQuery.Client (Some (..))
118118

119119
import Control.Monad.Trans.Except
120-
import Data.Aeson (FromJSON (..), ToJSON (..), withObject)
121-
import qualified Data.Aeson as Aeson
122-
import qualified Data.Aeson.KeyMap as KeyMap
123-
import Data.Aeson.Types (Parser)
124120
import Data.Bifunctor (bimap, first)
125121
import qualified Data.ByteString.Lazy as LBS
126122
import Data.Either.Combinators (rightToMaybe)
@@ -374,9 +370,6 @@ instance NodeToClientVersionOf QueryUTxOFilter where
374370
newtype ByronUpdateState = ByronUpdateState Byron.Update.State
375371
deriving Show
376372

377-
newtype UTxO era = UTxO {unUTxO :: Map TxIn (TxOut CtxUTxO era)}
378-
deriving (Eq, Show)
379-
380373
data UTxOInAnyEra where
381374
UTxOInAnyEra
382375
:: CardanoEra era
@@ -385,25 +378,6 @@ data UTxOInAnyEra where
385378

386379
deriving instance Show UTxOInAnyEra
387380

388-
instance IsCardanoEra era => ToJSON (UTxO era) where
389-
toJSON (UTxO m) = toJSON m
390-
toEncoding (UTxO m) = toEncoding m
391-
392-
instance
393-
(IsShelleyBasedEra era, FromJSON (TxOut CtxUTxO era))
394-
=> FromJSON (UTxO era)
395-
where
396-
parseJSON = withObject "UTxO" $ \hm -> do
397-
let l = toList $ KeyMap.toHashMapText hm
398-
res <- mapM toTxIn l
399-
pure . UTxO $ fromList res
400-
where
401-
toTxIn :: (Text, Aeson.Value) -> Parser (TxIn, TxOut CtxUTxO era)
402-
toTxIn (txinText, txOutVal) = do
403-
(,)
404-
<$> parseJSON (Aeson.String txinText)
405-
<*> parseJSON txOutVal
406-
407381
newtype SerialisedDebugLedgerState era
408382
= SerialisedDebugLedgerState (Serialised (Shelley.NewEpochState (ShelleyLedgerEra era)))
409383

cardano-api/src/Cardano/Api/Internal/Query/Expr.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import Cardano.Api.Internal.NetworkId
5858
import Cardano.Api.Internal.ProtocolParameters
5959
import Cardano.Api.Internal.Query
6060
import qualified Cardano.Api.Internal.ReexposeLedger as Ledger
61+
import Cardano.Api.Internal.Tx.UTxO
6162

6263
import qualified Cardano.Ledger.Api as L
6364
import qualified Cardano.Ledger.Api.State.Query as L
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{-# LANGUAGE DerivingStrategies #-}
2+
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE UndecidableInstances #-}
6+
7+
module Cardano.Api.Internal.Tx.UTxO where
8+
9+
import Cardano.Api.Internal.Eon.ShelleyBasedEra (IsShelleyBasedEra)
10+
import Cardano.Api.Internal.Eras.Core (IsCardanoEra)
11+
import Cardano.Api.Internal.Tx.Body (CtxUTxO, TxOut (..))
12+
import Cardano.Api.Internal.TxIn (TxIn (..))
13+
14+
import Cardano.Ledger.Babbage ()
15+
16+
import Data.Aeson (FromJSON (..), ToJSON (..))
17+
import qualified Data.Aeson as Aeson
18+
import qualified Data.Aeson.KeyMap as KeyMap
19+
import Data.Aeson.Types (Parser)
20+
import Data.Map (Map)
21+
import qualified Data.Map as Map
22+
import Data.Set (Set)
23+
import Data.Text (Text)
24+
import GHC.Exts (IsList (..))
25+
26+
newtype UTxO era = UTxO {unUTxO :: Map TxIn (TxOut CtxUTxO era)}
27+
deriving stock (Eq, Show)
28+
deriving newtype (Semigroup, Monoid, IsList)
29+
30+
instance IsCardanoEra era => ToJSON (UTxO era) where
31+
toJSON (UTxO m) = toJSON m
32+
toEncoding (UTxO m) = toEncoding m
33+
34+
instance
35+
IsShelleyBasedEra era
36+
=> FromJSON (UTxO era)
37+
where
38+
parseJSON = Aeson.withObject "UTxO" $ \hm -> do
39+
let l = toList $ KeyMap.toHashMapText hm
40+
res <- mapM toTxIn l
41+
pure . UTxO $ Map.fromList res
42+
where
43+
toTxIn :: (Text, Aeson.Value) -> Parser (TxIn, TxOut CtxUTxO era)
44+
toTxIn (txinText, txOutVal) = do
45+
(,)
46+
<$> parseJSON (Aeson.String txinText)
47+
<*> parseJSON txOutVal
48+
49+
-- | Infix version of `difference`.
50+
(\\) :: UTxO era -> UTxO era -> UTxO era
51+
a \\ b = difference a b
52+
53+
-- | Create an empty `UTxO`.
54+
empty :: UTxO era
55+
empty = UTxO Map.empty
56+
57+
-- | Create a `UTxO` from a single unspent transaction output.
58+
singleton :: TxIn -> TxOut CtxUTxO era -> UTxO era
59+
singleton i o = UTxO $ Map.singleton i o
60+
61+
-- | Find a 'TxOut' for a given 'TxIn'.
62+
lookup :: TxIn -> UTxO era -> Maybe (TxOut CtxUTxO era)
63+
lookup k = Map.lookup k . unUTxO
64+
65+
-- | Filter all `TxOut` that satisfy the predicate.
66+
filter :: (TxOut CtxUTxO era -> Bool) -> UTxO era -> UTxO era
67+
filter fn = UTxO . Map.filter fn . unUTxO
68+
69+
-- | Filter all UTxO to only include 'out's satisfying given predicate.
70+
filterWithKey :: (TxIn -> TxOut CtxUTxO era -> Bool) -> UTxO era -> UTxO era
71+
filterWithKey fn = UTxO . Map.filterWithKey fn . unUTxO
72+
73+
-- | Get the 'UTxO domain input's set
74+
inputSet :: UTxO (TxOut CtxUTxO era) -> Set TxIn
75+
inputSet = Map.keysSet . unUTxO
76+
77+
-- | Remove the right hand side from the left hand side.
78+
difference :: UTxO era -> UTxO era -> UTxO era
79+
difference a b = UTxO $ Map.difference (unUTxO a) (unUTxO b)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Cardano.Api.Tx.UTxO
2+
( UTxO.UTxO (..)
3+
, UTxO.empty
4+
, UTxO.singleton
5+
, UTxO.lookup
6+
, UTxO.filter
7+
, UTxO.filterWithKey
8+
, UTxO.inputSet
9+
, UTxO.difference
10+
)
11+
where
12+
13+
import qualified Cardano.Api.Internal.Tx.UTxO as UTxO

0 commit comments

Comments
 (0)