Skip to content

Commit 623d9c2

Browse files
committed
cli: Pretty print output of 'query filtered-utxo' command
1 parent 0f3c707 commit 623d9c2

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ data QueryCmd
116116
= QueryPoolId NodeAddress
117117
| QueryProtocolParameters ConfigYamlFilePath OutputFile
118118
| QueryTip NodeAddress
119-
| QueryFilteredUTxO Address ConfigYamlFilePath OutputFile
119+
| QueryFilteredUTxO Address ConfigYamlFilePath
120120
| QueryVersion NodeAddress
121121
| QueryStatus NodeAddress
122122
deriving (Eq, Show)
@@ -468,7 +468,6 @@ pQueryCmd =
468468
QueryFilteredUTxO
469469
<$> pHexEncodedAddress
470470
<*> (ConfigYamlFilePath <$> parseConfigFile)
471-
<*> pOutputFile
472471

473472
pQueryVersion :: Parser QueryCmd
474473
pQueryVersion = QueryVersion <$> parseNodeAddress

cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,33 @@ import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT
2323

2424
import Data.Aeson.Encode.Pretty (encodePretty)
2525
import qualified Data.ByteString.Lazy.Char8 as LBS
26+
import qualified Data.Map as Map
2627
import qualified Data.Set as Set
28+
import qualified Data.Text as Text
29+
import qualified Data.Text.IO as Text
2730

2831
import Ouroboros.Consensus.Cardano (Protocol (..), protocolInfo)
2932
import Ouroboros.Consensus.Config (configCodec)
3033
import Ouroboros.Consensus.Node.ProtocolInfo (ProtocolInfo(..))
3134
import Ouroboros.Consensus.Node.Run (nodeNetworkMagic)
35+
import Ouroboros.Consensus.Shelley.Protocol.Crypto (TPraosStandardCrypto)
3236
import Ouroboros.Network.NodeToClient (withIOManager)
3337

3438
import Ouroboros.Network.Block (getTipPoint)
3539

40+
import Shelley.Spec.Ledger.Coin (Coin (..))
3641
import Shelley.Spec.Ledger.PParams (PParams)
42+
import Shelley.Spec.Ledger.TxData (TxId (..), TxIn (..), TxOut (..))
43+
import Shelley.Spec.Ledger.UTxO (UTxO (..))
3744

3845

3946
runQueryCmd :: QueryCmd -> ExceptT CliError IO ()
4047
runQueryCmd cmd =
4148
case cmd of
4249
QueryProtocolParameters configFp outFile ->
4350
runQueryProtocolParameters configFp outFile
44-
QueryFilteredUTxO addr configFp outFile ->
45-
runQueryFilteredUTxO addr configFp outFile
51+
QueryFilteredUTxO addr configFp ->
52+
runQueryFilteredUTxO addr configFp
4653
_ -> liftIO $ putStrLn $ "runQueryCmd: " ++ show cmd
4754

4855
runQueryProtocolParameters
@@ -70,9 +77,8 @@ runQueryProtocolParameters configFp (OutputFile outFile) = do
7077
runQueryFilteredUTxO
7178
:: Address
7279
-> ConfigYamlFilePath
73-
-> OutputFile
7480
-> ExceptT CliError IO ()
75-
runQueryFilteredUTxO addr configFp (OutputFile _outFile) = do
81+
runQueryFilteredUTxO addr configFp = do
7682
nc <- liftIO $ parseNodeConfigurationFP configFp
7783
SomeConsensusProtocol p <- firstExceptT ProtocolError $ mkConsensusProtocol nc Nothing
7884

@@ -83,7 +89,7 @@ runQueryFilteredUTxO addr configFp (OutputFile _outFile) = do
8389
filteredUtxo <- firstExceptT NodeLocalStateQueryError $
8490
queryFilteredUTxOFromLocalState cfg nm sockPath
8591
(Set.singleton addr) (getTipPoint tip)
86-
liftIO $ putStrLn $ "Filtered UTxO: " ++ show filteredUtxo
92+
liftIO $ printFilteredUTxOs filteredUtxo
8793
where
8894
cfg = configCodec ptclcfg
8995
--FIXME: this works, but we should get the magic properly:
@@ -95,3 +101,29 @@ runQueryFilteredUTxO addr configFp (OutputFile _outFile) = do
95101
writeProtocolParameters :: FilePath -> PParams -> ExceptT CliError IO ()
96102
writeProtocolParameters fpath pparams =
97103
handleIOExceptT (IOError fpath) $ LBS.writeFile fpath (encodePretty pparams)
104+
105+
106+
printFilteredUTxOs :: UTxO TPraosStandardCrypto -> IO ()
107+
printFilteredUTxOs (UTxO utxo) = do
108+
Text.putStrLn title
109+
putStrLn $ replicate (Text.length title + 2) '-'
110+
mapM_ printUtxo $ Map.toList utxo
111+
where
112+
title :: Text
113+
title =
114+
" TxHash TxId Lovelace"
115+
116+
printUtxo :: (TxIn TPraosStandardCrypto, TxOut TPraosStandardCrypto) -> IO ()
117+
printUtxo (TxIn (TxId txhash) txin , TxOut _ (Coin coin)) =
118+
Text.putStrLn $
119+
mconcat
120+
[ Text.pack (show txhash)
121+
, textShowN 6 txin
122+
, textShowN 18 coin -- enough to display maxLovelaceVal
123+
]
124+
125+
textShowN :: Show a => Int -> a -> Text
126+
textShowN len x =
127+
let str = show x
128+
slen = length str
129+
in Text.pack $ replicate (max 1 (len - slen)) ' ' ++ str

0 commit comments

Comments
 (0)