-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add module for UTxO type and operations #710
Open
locallycompact
wants to merge
1
commit into
IntersectMBO:master
Choose a base branch
from
locallycompact:lc/utxo-module
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−27
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Cardano.Api.Query.UTxO where | ||
|
||
import Cardano.Api.Eon.ShelleyBasedEra (IsShelleyBasedEra) | ||
import Cardano.Api.Eras.Core (IsCardanoEra) | ||
import Cardano.Api.Tx.Body (CtxUTxO, TxOut (..)) | ||
import Cardano.Api.TxIn (TxIn (..)) | ||
|
||
import Cardano.Ledger.Babbage () | ||
|
||
import Data.Aeson (FromJSON (..), ToJSON (..)) | ||
import qualified Data.Aeson as Aeson | ||
import qualified Data.Aeson.KeyMap as KeyMap | ||
import Data.Aeson.Types (Parser) | ||
import qualified Data.HashMap.Strict as HashMap | ||
import Data.Map (Map) | ||
import qualified Data.Map as Map | ||
import Data.Set (Set) | ||
import Data.Text (Text) | ||
import GHC.Exts (IsList (..)) | ||
|
||
newtype UTxO era = UTxO {unUTxO :: Map TxIn (TxOut CtxUTxO era)} | ||
locallycompact marked this conversation as resolved.
Show resolved
Hide resolved
|
||
deriving stock (Eq, Show) | ||
deriving newtype (Semigroup, Monoid, IsList) | ||
|
||
instance IsCardanoEra era => ToJSON (UTxO era) where | ||
toJSON (UTxO m) = toJSON m | ||
toEncoding (UTxO m) = toEncoding m | ||
|
||
instance | ||
IsShelleyBasedEra era | ||
=> FromJSON (UTxO era) | ||
where | ||
parseJSON = Aeson.withObject "UTxO" $ \hm -> do | ||
let l = HashMap.toList $ KeyMap.toHashMapText hm | ||
res <- mapM toTxIn l | ||
pure . UTxO $ Map.fromList res | ||
where | ||
toTxIn :: (Text, Aeson.Value) -> Parser (TxIn, TxOut CtxUTxO era) | ||
toTxIn (txinText, txOutVal) = do | ||
(,) | ||
<$> parseJSON (Aeson.String txinText) | ||
<*> parseJSON txOutVal | ||
|
||
-- | Infix version of `difference`. | ||
(\\) :: UTxO era -> UTxO era -> UTxO era | ||
a \\ b = difference a b | ||
|
||
-- | Create an empty `UTxO`. | ||
empty :: UTxO era | ||
empty = UTxO Map.empty | ||
|
||
-- | Create a `UTxO` from a single unspent transaction output. | ||
singleton :: TxIn -> TxOut CtxUTxO era -> UTxO era | ||
singleton i o = UTxO $ Map.singleton i o | ||
|
||
-- | Same as `lookup`. | ||
resolve :: TxIn -> UTxO era -> Maybe (TxOut CtxUTxO era) | ||
resolve = Cardano.Api.Query.UTxO.lookup | ||
|
||
-- | Find a 'TxOut' for a given 'TxIn'. | ||
lookup :: TxIn -> UTxO era -> Maybe (TxOut CtxUTxO era) | ||
lookup k = Map.lookup k . unUTxO | ||
|
||
-- | Filter all `TxOut` that satisfy the predicate. | ||
filter :: (TxOut CtxUTxO era -> Bool) -> UTxO era -> UTxO era | ||
filter fn = UTxO . Map.filter fn . unUTxO | ||
|
||
-- | Filter all UTxO to only include 'out's satisfying given predicate. | ||
filterWithKey :: (TxIn -> TxOut CtxUTxO era -> Bool) -> UTxO era -> UTxO era | ||
filterWithKey fn = UTxO . Map.filterWithKey fn . unUTxO | ||
|
||
-- | Get the 'UTxO domain input's set | ||
inputSet :: UTxO (TxOut CtxUTxO era) -> Set TxIn | ||
inputSet = Map.keysSet . 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Cardano.Api.UTxO (module Cardano.Api.Query.UTxO) where | ||
|
||
import Cardano.Api.Query.UTxO |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
UndecidableInstances
is needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@carbolymer Do you believe this should not be needed?