Skip to content

Commit

Permalink
cardano-cli: Friendly output multi-asset with ASCII version where pos…
Browse files Browse the repository at this point in the history
…sible

As desktop client does
  • Loading branch information
cblp committed Oct 13, 2021
1 parent 0880f94 commit d24e226
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 35 deletions.
48 changes: 34 additions & 14 deletions cardano-cli/src/Cardano/CLI/Run/Friendly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import Cardano.Prelude
import Data.Aeson (Value (..), object, toJSON, (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString.Char8 as BSC
import qualified Data.Map.Strict as Map
import qualified Data.Text as Text
import Data.Yaml (array)
import Data.Yaml.Pretty (defConfig, encodePretty, setConfCompare)

import Cardano.Api
import Cardano.Api as Api
import Cardano.Api.Shelley (Address (ShelleyAddress), StakeAddress (..))
import Cardano.Ledger.Crypto (Crypto)
import qualified Cardano.Ledger.Shelley.API as Shelley
Expand Down Expand Up @@ -162,26 +165,43 @@ friendlyFee = \case
friendlyLovelace :: Lovelace -> Aeson.Value
friendlyLovelace (Lovelace value) = String $ textShow value <> " Lovelace"

{-
current output:
52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
736b79: 142
TODO:
policy 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
asset 736b79 (sky): 142
-}
friendlyMintValue :: TxMintValue ViewTx era -> Aeson.Value
friendlyMintValue = \case
TxMintNone -> Null
TxMintValue _ v _ -> toJSON v
TxMintValue _ v _ -> friendlyValue v

friendlyTxOutValue :: TxOutValue era -> Aeson.Value
friendlyTxOutValue = \case
TxOutAdaOnly _ lovelace -> friendlyLovelace lovelace
TxOutValue _ multiasset -> toJSON multiasset
TxOutValue _ v -> friendlyValue v

friendlyValue :: Api.Value -> Aeson.Value
friendlyValue v =
object
[ case bundle of
ValueNestedBundleAda q -> "lovelace" .= q
ValueNestedBundle policy assets ->
friendlyPolicyId policy .= friendlyAssets assets
| bundle <- bundles
]
where

ValueNestedRep bundles = valueToNestedRep v

friendlyPolicyId = ("policy " <>) . serialiseToRawBytesHexText

friendlyAssets = Map.mapKeys friendlyAssetName

friendlyAssetName = \case
"" -> "default asset"
name@(AssetName nameBS) ->
"asset " <> serialiseToRawBytesHexText name <> nameAsciiSuffix
where
nameAsciiSuffix
| nameIsAscii = " (" <> nameAscii <> ")"
| otherwise = ""
nameIsAscii = BSC.all (\c -> isAscii c && isAlphaNum c) nameBS
nameAscii = Text.pack $ BSC.unpack nameBS

friendlyMetadata :: TxMetadataInEra era -> Aeson.Value
friendlyMetadata = \case
Expand Down
27 changes: 24 additions & 3 deletions cardano-cli/test/Test/Golden/TxView.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ golden_view_allegra =
, "fe5dd07fb576bff960d6e066eade5b26cdb5afebe29f76ea58d0a098bce5d891\
\#94"
, "--tx-out"
, "addr_test1qrefnr4k09pvge6dq83v6s67ruter8sftmky8qrmkqqsxy7q5psgn8tgqmupq4r79jmxlyk4eqt6z6hj5g8jd8393msqaw47f4\
, "addr_test1\
\qrefnr4k09pvge6dq83v6s67ruter8sftmky8qrmkqqsxy7q5psgn8tgqmupq4r7\
\9jmxlyk4eqt6z6hj5g8jd8393msqaw47f4\
\+99"
, "--fee", "100"
, "--invalid-hereafter", "101"
Expand Down Expand Up @@ -122,8 +124,27 @@ golden_view_mary =
, "fe5dd07fb576bff960d6e066eade5b26cdb5afebe29f76ea58d0a098bce5d891\
\#135"
, "--tx-out"
, "addr_test1qrefnr4k09pvge6dq83v6s67ruter8sftmky8qrmkqqsxy7q5psgn8tgqmupq4r79jmxlyk4eqt6z6hj5g8jd8393msqaw47f4\
\+138"
, "addr_test1\
\qrefnr4k09pvge6dq83v6s67ruter8sftmky8qrmkqqsxy7q5psgn8tgqmupq4r7\
\9jmxlyk4eqt6z6hj5g8jd8393msqaw47f4\
\ + \
\138\
\ + \
\130 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
\ + \
\132 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.cafe\
\ + \
\134 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf.f00d\
\ + \
\136 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.dead\
\ + \
\138\
\ d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
\.736e6f77\
\ + \
\142\
\ 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528\
\.736b79"
, "--fee", "139"
, "--invalid-before", "140"
, "--mint"
Expand Down
24 changes: 16 additions & 8 deletions cardano-cli/test/data/golden/mary/transaction-view.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ inputs:
- fe5dd07fb576bff960d6e066eade5b26cdb5afebe29f76ea58d0a098bce5d891#135
metadata: null
mint:
52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
736b79: 142
cafe: 132
dead: 136
d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf:
'': 130
736e6f77: 138
f00d: 134
policy 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
asset 736b79 (sky): 142
asset cafe: 132
asset dead: 136
policy d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf:
asset 736e6f77 (snow): 138
asset f00d: 134
default asset: 130
outputs:
- address: addr_test1qrefnr4k09pvge6dq83v6s67ruter8sftmky8qrmkqqsxy7q5psgn8tgqmupq4r79jmxlyk4eqt6z6hj5g8jd8393msqaw47f4
address era: Shelley
amount:
lovelace: 138
policy 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
asset 736b79 (sky): 142
asset cafe: 132
asset dead: 136
policy d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf:
asset 736e6f77 (snow): 138
asset f00d: 134
default asset: 130
network: Testnet
payment credential:
key hash: f2998eb67942c4674d01e2cd435e1f17919e095eec43807bb0010313
Expand Down
2 changes: 2 additions & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ test-suite cardano-testnet-tests
type: exitcode-stdio-1.0

build-depends: aeson
, base16-bytestring
, bytestring
, cardano-api
, cardano-testnet
, containers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ import Prelude
import Cardano.Api

import Control.Monad
import Data.Monoid (Last (..))
import Data.String
import Hedgehog (Property, (===))
import System.Environment (getEnvironment)
import System.FilePath ((</>))

import qualified Data.Aeson as J
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Char8 as BSC
import qualified Data.Map.Strict as Map
import Data.Monoid (Last (..))
import Data.String
import qualified Data.Text as T
import Hedgehog (Property, (===))
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Stock.IO.Network.Sprocket as IO
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.Concurrent as H
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Process as H
import qualified System.Directory as IO
import System.Environment (getEnvironment)
import System.FilePath ((</>))

import qualified Test.Base as H
import Test.Process (execCreateScriptContext, execCreateScriptContext')
import qualified Test.Process as H
Expand All @@ -41,6 +43,9 @@ import qualified Testnet.Conf as H
{- HLINT ignore "Redundant return" -}
{- HLINT ignore "Use let" -}

millarCoin :: String
millarCoin = BSC.unpack $ Base16.encode "MillarCoin"

hprop_plutus_script_context_mint_equality :: Property
hprop_plutus_script_context_mint_equality = H.integration . H.runFinallies . H.workspace "chairman" $ \tempAbsBasePath' -> do
projectBase <- H.note =<< H.noteIO . IO.canonicalizePath =<< H.getProjectBase
Expand Down Expand Up @@ -183,8 +188,8 @@ hprop_plutus_script_context_mint_equality = H.integration . H.runFinallies . H.w
, "--tx-in-collateral", T.unpack $ renderTxIn txinCollateral
, "--mint-script-file", plutusContextEqualityMintScript
, "--mint-redeemer-file", scriptDummyRedeemer
, "--tx-out", dummyaddress <> "+" <> show @Integer 10000000 <> "+ 5 " <> (policyId <> ".MillarCoin")
, "--mint", "5 " <> (policyId <> ".MillarCoin")
, "--tx-out", dummyaddress <> "+" <> show @Integer 10000000 <> "+ 5 " <> (policyId <> "." <> millarCoin)
, "--mint", "5 " <> (policyId <> "." <> millarCoin)
, "--protocol-params-file", work </> "pparams.json"
, "--out-file", work </> "mint-dummy.body"
]
Expand Down Expand Up @@ -223,8 +228,8 @@ hprop_plutus_script_context_mint_equality = H.integration . H.runFinallies . H.w
, "--tx-in-collateral", T.unpack $ renderTxIn txinCollateral
, "--mint-script-file", plutusContextEqualityMintScript
, "--mint-redeemer-file", scriptContextRedeemer
, "--tx-out", dummyaddress <> "+" <> show @Integer 10000000 <> "+ 5 " <> (policyId <> ".MillarCoin")
, "--mint", "5 " <> (policyId <> ".MillarCoin")
, "--tx-out", dummyaddress <> "+" <> show @Integer 10000000 <> "+ 5 " <> (policyId <> "." <> millarCoin)
, "--mint", "5 " <> (policyId <> "." <> millarCoin)
, "--protocol-params-file", work </> "pparams.json"
, "--out-file", work </> "mint-final.body"
]
Expand Down

0 comments on commit d24e226

Please sign in to comment.