From e275a4d0addedff1721741b7fcd5566ace08a202 Mon Sep 17 00:00:00 2001 From: Luke Nadur <19835357+intricate@users.noreply.github.com> Date: Thu, 26 Nov 2020 18:13:14 -0500 Subject: [PATCH] Add CLI parser for new multi-asset tx output value format --- .../src/Cardano/CLI/Shelley/Parsers.hs | 45 +++++++++++++++---- cardano-cli/src/Cardano/CLI/Types.hs | 1 - 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs index c9bdd2fc30f..4831f6a185b 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs @@ -24,6 +24,7 @@ import Cardano.CLI.Shelley.Commands import Cardano.CLI.Shelley.Key (InputFormat (..), VerificationKeyOrFile (..), VerificationKeyOrHashOrFile (..), VerificationKeyTextOrFile (..), deserialiseInput, renderInputDecodeError) +import Cardano.CLI.Mary.Parser (stringToValue) import Cardano.CLI.Types import Control.Monad.Fail (fail) import Data.Attoparsec.Combinator (()) @@ -44,6 +45,7 @@ import qualified Data.Text.Encoding as Text import qualified Options.Applicative as Opt import qualified Shelley.Spec.Ledger.BaseTypes as Shelley import qualified Shelley.Spec.Ledger.TxBody as Shelley +import qualified Text.ParserCombinators.Parsec.Error as Parsec -- -- Shelley CLI command parsers @@ -1475,13 +1477,25 @@ parseTxIx = toEnum <$> Atto.decimal pTxOut :: Parser TxOutAnyEra pTxOut = - Opt.option (readerFromAttoParser parseTxOut) - ( Opt.long "tx-out" - <> Opt.metavar "TX-OUT" - <> Opt.help "The transaction output as Address+Lovelace where Address is \ - \the Bech32-encoded address followed by the amount in \ - \Lovelace." - ) + Opt.option (readerFromAttoParser parseTxOut) + ( Opt.long "tx-out" + <> Opt.metavar "TX-OUT" + <> Opt.help "The transaction output as Address+Lovelace where Address is \ + \the Bech32-encoded address followed by the amount in \ + \Lovelace." + ) + <|> + TxOutAnyEra + <$> pAddressAny + <*> + Opt.option (eitherReader readValue) + ( Opt.long "tx-out-value" + <> Opt.metavar "TX-OUT-VALUE" + <> Opt.help + ("The transaction output value formatted in the new " + <> "multi-asset style." + ) + ) where parseTxOut :: Atto.Parser TxOutAnyEra parseTxOut = @@ -1492,6 +1506,14 @@ pTxOut = pAdaOnlyValue :: Atto.Parser Value pAdaOnlyValue = lovelaceToValue <$> parseLovelace +pAddressAny :: Parser AddressAny +pAddressAny = + Opt.option (readerFromAttoParser parseAddressAny) + ( Opt.long "address" + <> Opt.metavar "ADDRESS" + <> Opt.help "A Cardano address." + ) + pMintMultiAsset :: Parser Value pMintMultiAsset = Opt.option @@ -1502,7 +1524,14 @@ pMintMultiAsset = ) readValue :: String -> Either String Value -readValue _maCliSyntax = Left "Need 2072 for MA cli syntax parser" +readValue = first renderParseError . stringToValue + where + -- TODO: Improve error message + renderParseError :: Parsec.ParseError -> String + renderParseError = + intercalate ", " + . map Parsec.messageString + . Parsec.errorMessages pTxLowerBound :: Parser SlotNo pTxLowerBound = diff --git a/cardano-cli/src/Cardano/CLI/Types.hs b/cardano-cli/src/Cardano/CLI/Types.hs index 21a8ac780ae..3c7554fa4b2 100644 --- a/cardano-cli/src/Cardano/CLI/Types.hs +++ b/cardano-cli/src/Cardano/CLI/Types.hs @@ -106,4 +106,3 @@ withCardanoEra UseMaryEra f = f MaryEra -- data TxOutAnyEra = TxOutAnyEra AddressAny Value deriving (Eq, Show) -