Skip to content

Commit

Permalink
Implement query utxo in cardano-cli using the new api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jan 27, 2021
1 parent f293c6c commit e798870
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 129 deletions.
3 changes: 2 additions & 1 deletion cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ library
Cardano.Api.Protocol.Cardano
Cardano.Api.Protocol.Shelley
Cardano.Api.Protocol.Types
Cardano.Api.Query
Cardano.Api.Shelley.Genesis
Cardano.Api.TxInMode
Cardano.Api.TxSubmit
Expand Down Expand Up @@ -62,7 +63,7 @@ library
Cardano.Api.NetworkId
Cardano.Api.OperationalCertificate
-- TODO: move here Cardano.Api.ProtocolParameters
Cardano.Api.Query
-- TODO: move here Cardano.Api.Query
Cardano.Api.Script
Cardano.Api.SerialiseBech32
Cardano.Api.SerialiseCBOR
Expand Down
5 changes: 5 additions & 0 deletions cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ module Cardano.Api.Address (

import Prelude

import Data.Aeson (ToJSON (..))
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Base58 as Base58
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
Expand Down Expand Up @@ -312,6 +314,9 @@ data AddressInEra era where
-> Address addrtype
-> AddressInEra era

instance IsCardanoEra era => ToJSON (AddressInEra era) where
toJSON = Aeson.String . serialiseAddress

instance Eq (AddressInEra era) where
(==) (AddressInEra ByronAddressInAnyEra addr1)
(AddressInEra ByronAddressInAnyEra addr2) = addr1 == addr2
Expand Down
8 changes: 8 additions & 0 deletions cardano-api/src/Cardano/Api/Query.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}

Expand Down Expand Up @@ -29,6 +31,7 @@ module Cardano.Api.Query (
ProtocolState(..),
) where

import Data.Aeson (ToJSON)
import Data.Bifunctor (bimap)
import Data.Map (Map)
import qualified Data.Map as Map
Expand Down Expand Up @@ -160,6 +163,7 @@ newtype LedgerState era

newtype ProtocolState era
= ProtocolState (Serialised (Shelley.ChainDepState (Ledger.Crypto (ShelleyLedgerEra era))))
deriving newtype instance IsCardanoEra era => ToJSON (UTxO era)

toShelleyAddrSet :: CardanoEra era
-> Set AddressAny
Expand Down Expand Up @@ -191,6 +195,10 @@ fromUTxO eraConversion utxo =
let Shelley.UTxO sUtxo = utxo
in UTxO . Map.fromList . map (bimap fromShelleyTxIn (fromTxOut ShelleyBasedEraMary)) $ Map.toList sUtxo

--toUTxO :: ShelleyBasedEra era -> UTxO era -> Shelley.UTxO
--toUTxO = -- Left off here...perhaps we want to convert to API types instead for printing?
-- I.e instead of exposing the underlying ledger specs types, expose the api types.

fromShelleyPoolDistr :: Shelley.PoolDistr StandardCrypto
-> Map (Hash StakePoolKey) Rational
fromShelleyPoolDistr =
Expand Down
31 changes: 27 additions & 4 deletions cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -95,6 +96,9 @@ module Cardano.Api.TxBody (

import Prelude

import Data.Aeson (ToJSON (..))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (ToJSONKey (..), toJSONKeyText)
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS
Expand All @@ -106,7 +110,9 @@ import Data.Maybe (fromMaybe)
import qualified Data.Sequence.Strict as Seq
import qualified Data.Set as Set
import Data.String (IsString)
import qualified Data.Text as Text
import Data.Word (Word64)
import GHC.Generics

import Control.Monad (guard)

Expand Down Expand Up @@ -169,6 +175,8 @@ newtype TxId = TxId (Shelley.Hash StandardCrypto Shelley.EraIndependentTxBody)
deriving newtype (IsString)
-- We use the Shelley representation and convert the Byron one

deriving newtype instance ToJSON TxId

instance HasTypeProxy TxId where
data AsType TxId = AsTxId
proxyToAsType _ = AsTxId
Expand Down Expand Up @@ -224,11 +232,16 @@ getTxId (ShelleyTxBody era tx _) =
--

data TxIn = TxIn TxId TxIx
deriving (Eq, Ord, Show)
deriving (Eq, Generic, Ord, Show)

deriving instance ToJSON TxIn
instance ToJSONKey TxIn where
toJSONKey = toJSONKeyText (\txIn -> Text.pack $ show txIn)

newtype TxIx = TxIx Word
deriving stock (Eq, Ord, Show)
deriving newtype (Enum)
deriving newtype ToJSON

fromByronTxIn :: Byron.TxIn -> TxIn
fromByronTxIn (Byron.TxInUtxo txId index) =
Expand All @@ -255,8 +268,11 @@ fromShelleyTxIn (Shelley.TxIn txid txix) =
-- Transaction outputs
--

data TxOut era = TxOut (AddressInEra era) (TxOutValue era)
data TxOut era
= TxOut (AddressInEra era) (TxOutValue era)
deriving Generic

deriving instance IsCardanoEra era => ToJSON (TxOut era)
deriving instance Eq (TxOut era)
deriving instance Show (TxOut era)

Expand Down Expand Up @@ -330,6 +346,9 @@ data MultiAssetSupportedInEra era where
deriving instance Eq (MultiAssetSupportedInEra era)
deriving instance Show (MultiAssetSupportedInEra era)

instance ToJSON (MultiAssetSupportedInEra era) where
toJSON = Aeson.String . Text.pack . show

-- | A representation of whether the era supports only ada transactions.
--
-- Prior to the Mary era only ada transactions are supported. Multi-assets are
Expand All @@ -347,6 +366,9 @@ data OnlyAdaSupportedInEra era where
deriving instance Eq (OnlyAdaSupportedInEra era)
deriving instance Show (OnlyAdaSupportedInEra era)

instance ToJSON (OnlyAdaSupportedInEra era) where
toJSON = Aeson.String . Text.pack . show

multiAssetSupportedInEra :: CardanoEra era
-> Either (OnlyAdaSupportedInEra era)
(MultiAssetSupportedInEra era)
Expand Down Expand Up @@ -589,7 +611,8 @@ data TxOutValue era where

deriving instance Eq (TxOutValue era)
deriving instance Show (TxOutValue era)

deriving instance ToJSON (TxOutValue era)
deriving instance Generic (TxOutValue era)

-- ----------------------------------------------------------------------------
-- Transaction fees
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ data QueryCmd =
| QueryTip AnyConsensusModeParams NetworkId (Maybe OutputFile)
| QueryStakeDistribution AnyCardanoEra AnyConsensusModeParams NetworkId (Maybe OutputFile)
| QueryStakeAddressInfo AnyCardanoEra AnyConsensusModeParams StakeAddress NetworkId (Maybe OutputFile)
| QueryUTxO AnyCardanoEra Protocol QueryFilter NetworkId (Maybe OutputFile)
| QueryUTxO AnyCardanoEra AnyConsensusModeParams QueryFilter NetworkId (Maybe OutputFile)
| QueryLedgerState AnyCardanoEra Protocol NetworkId (Maybe OutputFile)
| QueryProtocolState AnyCardanoEra Protocol NetworkId (Maybe OutputFile)
deriving Show
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ pQueryCmd =
pQueryUTxO =
QueryUTxO
<$> pCardanoEra
<*> pProtocol
<*> pConsensusModeParams
<*> pQueryFilter
<*> pNetworkId
<*> pMaybeOutputFile
Expand Down
Loading

0 comments on commit e798870

Please sign in to comment.