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 19, 2021
1 parent 9bd8152 commit df83163
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 129 deletions.
5 changes: 5 additions & 0 deletions cardano-api/src/Cardano/Api/Address.hs
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
4 changes: 4 additions & 0 deletions cardano-api/src/Cardano/Api/Query.hs
@@ -1,6 +1,8 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}

Expand Down Expand Up @@ -30,6 +32,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 @@ -171,6 +174,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
30 changes: 27 additions & 3 deletions cardano-api/src/Cardano/Api/TxBody.hs
@@ -1,3 +1,5 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -93,6 +95,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 @@ -104,7 +109,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 @@ -166,6 +173,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 @@ -221,11 +230,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 @@ -252,8 +266,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 @@ -324,6 +341,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 @@ -341,6 +361,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 @@ -583,7 +606,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
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 (Eq, Show)
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Expand Up @@ -677,7 +677,7 @@ pQueryCmd =
pQueryUTxO =
QueryUTxO
<$> pCardanoEra
<*> pProtocol
<*> pConsensusModeParams
<*> pQueryFilter
<*> pNetworkId
<*> pMaybeOutputFile
Expand Down

0 comments on commit df83163

Please sign in to comment.