From e3f8b18dda85936a607b69ec3aff08b4e99cacea Mon Sep 17 00:00:00 2001 From: Jordan Millar Date: Mon, 1 Feb 2021 11:09:40 +0000 Subject: [PATCH] Fix JSON output for utxo command --- cardano-api/src/Cardano/Api/Query.hs | 20 +++++++++-- cardano-api/src/Cardano/Api/TxBody.hs | 34 ++++++++++++++----- cardano-cli/src/Cardano/CLI/Shelley/Run.hs | 2 +- .../src/Cardano/CLI/Shelley/Run/Query.hs | 4 +-- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/cardano-api/src/Cardano/Api/Query.hs b/cardano-api/src/Cardano/Api/Query.hs index 0e2ebad4a9d..165f34e3586 100644 --- a/cardano-api/src/Cardano/Api/Query.hs +++ b/cardano-api/src/Cardano/Api/Query.hs @@ -31,15 +31,19 @@ module Cardano.Api.Query ( ProtocolState(..), ) where -import Data.Aeson (ToJSON) +import Data.Aeson (ToJSON(..), object, (.=)) +import qualified Data.Aeson as Aeson import Data.Bifunctor (bimap) import Data.Map (Map) import qualified Data.Map as Map import Data.Maybe (mapMaybe) import Data.Set (Set) +import qualified Data.Text as Text +import qualified Data.Text.Encoding as Text import qualified Data.Set as Set import Data.SOP.Strict (SListI) import Prelude +import qualified Data.Vector as Vector import Ouroboros.Network.Protocol.LocalStateQuery.Client (Some (..)) @@ -64,6 +68,7 @@ import qualified Shelley.Spec.Ledger.LedgerState as Shelley import Cardano.Api.Address import Cardano.Api.Block import Cardano.Api.Certificate +import Cardano.Api.SerialiseRaw import Cardano.Api.Eras import Cardano.Api.KeysShelley import Cardano.Api.Modes @@ -158,12 +163,23 @@ newtype ByronUpdateState = ByronUpdateState Byron.Update.State newtype UTxO era = UTxO (Map TxIn (TxOut era)) +instance IsCardanoEra era => ToJSON (UTxO era) where + toJSON (UTxO m) = Aeson.Array . Vector.fromList . map convert $ Map.toList m + where + convert :: (TxIn, TxOut era) -> Aeson.Value + convert ((TxIn txId (TxIx ix), txout)) = + let txin = ( Text.decodeUtf8 (serialiseToRawBytesHex txId) + <> "#" + <> Text.pack (show ix) + ) + in object [ txin .= toJSON txout] + + newtype LedgerState era = LedgerState (Serialised (Shelley.NewEpochState (ShelleyLedgerEra 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 diff --git a/cardano-api/src/Cardano/Api/TxBody.hs b/cardano-api/src/Cardano/Api/TxBody.hs index e3017c8394a..4166ef5459c 100644 --- a/cardano-api/src/Cardano/Api/TxBody.hs +++ b/cardano-api/src/Cardano/Api/TxBody.hs @@ -96,9 +96,8 @@ module Cardano.Api.TxBody ( import Prelude -import Data.Aeson (ToJSON (..)) +import Data.Aeson (ToJSON (..), (.=), object) 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 @@ -111,6 +110,7 @@ import qualified Data.Sequence.Strict as Seq import qualified Data.Set as Set import Data.String (IsString) import qualified Data.Text as Text +import qualified Data.Text.Encoding as Text import Data.Word (Word64) import GHC.Generics @@ -234,10 +234,6 @@ getTxId (ShelleyTxBody era tx _) = data TxIn = TxIn TxId TxIx deriving (Eq, Generic, Ord, Show) -deriving instance ToJSON TxIn -instance ToJSONKey TxIn where - toJSONKey = toJSONKeyText (Text.pack . show) - newtype TxIx = TxIx Word deriving stock (Eq, Ord, Show) deriving newtype (Enum) @@ -272,7 +268,26 @@ data TxOut era = TxOut (AddressInEra era) (TxOutValue era) deriving Generic -deriving instance IsCardanoEra era => ToJSON (TxOut era) +instance IsCardanoEra era => ToJSON (TxOut era) where + toJSON (TxOut (AddressInEra addrType addr) val) = + case addrType of + ByronAddressInAnyEra -> + let hexAddr = Text.decodeUtf8 $ serialiseToRawBytesHex addr + in object [ hexAddr .= toJSON val ] + ShelleyAddressInEra sbe -> + case sbe of + ShelleyBasedEraShelley -> + let hexAddr = Text.decodeUtf8 $ serialiseToRawBytesHex addr + in object [ hexAddr .= toJSON val ] + ShelleyBasedEraAllegra -> + let hexAddr = Text.decodeUtf8 $ serialiseToRawBytesHex addr + in object [ hexAddr .= toJSON val ] + ShelleyBasedEraMary -> + let hexAddr = Text.decodeUtf8 $ serialiseToRawBytesHex addr + in object [ hexAddr .= toJSON val ] + + + deriving instance Eq (TxOut era) deriving instance Show (TxOut era) @@ -611,9 +626,12 @@ data TxOutValue era where deriving instance Eq (TxOutValue era) deriving instance Show (TxOutValue era) -deriving instance ToJSON (TxOutValue era) deriving instance Generic (TxOutValue era) +instance ToJSON (TxOutValue era) where + toJSON (TxOutAdaOnly _ (Lovelace int)) = Aeson.Number $ fromInteger int + toJSON (TxOutValue _ val) = toJSON val + -- ---------------------------------------------------------------------------- -- Transaction fees -- diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run.hs index 4ee7f8ea0db..e5d70af1abf 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run.hs @@ -61,7 +61,7 @@ renderShelleyClientCmdError cmd err = where renderError :: ShelleyCommand -> (a -> Text) -> a -> Text renderError shelleyCmd renderer shelCliCmdErr = - mconcat [ "Shelley command failed: " + mconcat [ "Command failed: " , renderShelleyCommand shelleyCmd , " Error: " , renderer shelCliCmdErr diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs index 0a3be276fb2..ceb5a15712a 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs @@ -92,8 +92,8 @@ renderShelleyQueryCmdError err = "Consensus mode and era mismatch. Consensus mode: " <> show cMode <> " Era: " <> show era ShelleyQueryCmdEraMismatch (EraMismatch ledgerEra queryEra) -> - "An error mismatch occured. Specified query era: " <> queryEra <> - "Current ledger era: " <> ledgerEra + "\nAn error mismatch occured." <> "\nSpecified query era: " <> queryEra <> + "\nCurrent ledger era: " <> ledgerEra runQueryCmd :: QueryCmd -> ExceptT ShelleyQueryCmdError IO () runQueryCmd cmd =