Skip to content

Commit

Permalink
cardano-cli: Expect AssetName in hex in --mint and --tx-out
Browse files Browse the repository at this point in the history
  • Loading branch information
cblp committed Oct 11, 2021
1 parent d2f3850 commit 57da24d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 29 deletions.
7 changes: 4 additions & 3 deletions cardano-api/src/Cardano/Api/ValueParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ module Cardano.Api.ValueParser

import Prelude

import Control.Applicative (many, some, (<|>))
import qualified Data.ByteString.Char8 as BSC
import qualified Data.Char as Char
import Data.Functor (void, ($>))
import Data.List (foldl')
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Word (Word64)

import Control.Applicative (many, some, (<|>))

import Text.Parsec as Parsec (notFollowedBy, try, (<?>))
import Text.Parsec.Char (alphaNum, char, digit, hexDigit, space, spaces, string)
import Text.Parsec.Expr (Assoc (..), Operator (..), buildExpressionParser)
Expand All @@ -24,6 +23,8 @@ import Cardano.Api.SerialiseRaw
import Cardano.Api.Utils (note)
import Cardano.Api.Value

import Cardano.CLI.Helpers (note)

-- | Parse a 'Value' from its string representation.
parseValue :: Parser Value
parseValue = evalValueExpr <$> parseValueExpr
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ test-suite cardano-cli-golden
type: exitcode-stdio-1.0

build-depends: aeson >= 1.5.6.0
, base16-bytestring
, bytestring
, cardano-api
, cardano-cli
Expand Down
9 changes: 8 additions & 1 deletion cardano-cli/src/Cardano/CLI/Helpers.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

Expand All @@ -14,12 +15,13 @@ module Cardano.CLI.Helpers
, hushM
) where

import Cardano.Prelude
import Cardano.Prelude hiding (note)
import Prelude (String)

import Codec.CBOR.Pretty (prettyHexEnc)
import Codec.CBOR.Read (DeserialiseFailure, deserialiseFromBytes)
import Codec.CBOR.Term (decodeTerm, encodeTerm)
import Control.Monad.Fail (fail)
import Control.Monad.Trans.Except.Extra (handleIOExceptT, left)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LB
Expand Down Expand Up @@ -126,3 +128,8 @@ hushM :: forall e m a. Monad m => Either e a -> (e -> m ()) -> m (Maybe a)
hushM r f = case r of
Right a -> return (Just a)
Left e -> f e >> return Nothing

note :: MonadFail m => String -> Maybe a -> m a
note msg = \case
Nothing -> fail msg
Just a -> pure a
28 changes: 12 additions & 16 deletions cardano-cli/src/Cardano/CLI/Run/Friendly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,21 @@ 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 _ ->
object
[ friendlyAssetId assetId .= quantity
| (assetId, quantity) <- valueToList v
]

friendlyAssetId :: AssetId -> Text
friendlyAssetId = \case
AdaAssetId -> "ADA"
AssetId policyId (AssetName assetName) ->
decodeUtf8 $ serialiseToRawBytesHex policyId <> suffix
where
suffix =
case assetName of
"" -> ""
_ -> "." <> assetName
TxMintValue _ v _ -> toJSON v

friendlyTxOutValue :: TxOutValue era -> Aeson.Value
friendlyTxOutValue = \case
Expand Down
8 changes: 5 additions & 3 deletions cardano-cli/test/Test/Golden/Shelley/Transaction/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Test.Golden.Shelley.Transaction.Build
import Cardano.Prelude
import Prelude (String)

import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Char8 as BSC
import Hedgehog (Property)
import Test.OptParse

Expand Down Expand Up @@ -76,7 +78,9 @@ golden_shelleyTransactionBuild_Minting =
, scriptWit
]

let dummyMA = filter (/= '\n') $ "50 " ++ polid ++ ".ethereum"
let dummyMA =
filter (/= '\n') $
"50 " ++ polid ++ "." ++ BSC.unpack (Base16.encode "ethereum")

txBodyOutFile <- noteTempFile tempDir "tx-body-out"

Expand Down Expand Up @@ -134,5 +138,3 @@ golden_shelleyTransactionBuild_TxInScriptWitnessed =
H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile

H.assertEndsWithSingleNewline txBodyOutFile


16 changes: 13 additions & 3 deletions cardano-cli/test/Test/Golden/TxView.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,21 @@ golden_view_mary =
, "--fee", "139"
, "--invalid-before", "140"
, "--mint"
, "42 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
, "130 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
\ + \
\43 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.snow\
\132 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.cafe\
\ + \
\44 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf.sky"
\134 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf.f00d\
\ + \
\136 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.dead\
\ + \
\138\
\ d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
\.736e6f77\
\ + \
\142\
\ 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528\
\.736b79"
, "--minting-script-file", "test/data/golden/mary/scripts/mint.all"
, "--minting-script-file", "test/data/golden/mary/scripts/mint.any"
, "--out-file", transactionBodyFile
Expand Down
11 changes: 8 additions & 3 deletions cardano-cli/test/data/golden/mary/transaction-view.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ inputs:
- fe5dd07fb576bff960d6e066eade5b26cdb5afebe29f76ea58d0a098bce5d891#135
metadata: null
mint:
52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.snow: 43
d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf: 42
d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf.sky: 44
52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
736b79: 142
cafe: 132
dead: 136
d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf:
'': 130
736e6f77: 138
f00d: 134
outputs:
- address: addr_test1qrefnr4k09pvge6dq83v6s67ruter8sftmky8qrmkqqsxy7q5psgn8tgqmupq4r79jmxlyk4eqt6z6hj5g8jd8393msqaw47f4
address era: Shelley
Expand Down

0 comments on commit 57da24d

Please sign in to comment.