From 9e2d03828ac83171fe0e3f6289f1ad59b49e7616 Mon Sep 17 00:00:00 2001 From: Sasha Bogicevic Date: Wed, 28 Aug 2024 18:28:19 +0200 Subject: [PATCH] Add recover cmd and refactor a bit Signed-off-by: Sasha Bogicevic --- hydra-tx/exe/Main.hs | 24 ++++++++++++++++-------- hydra-tx/exe/Options.hs | 22 +++++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/hydra-tx/exe/Main.hs b/hydra-tx/exe/Main.hs index 9e915b4ff56..d59f7fe794b 100644 --- a/hydra-tx/exe/Main.hs +++ b/hydra-tx/exe/Main.hs @@ -6,16 +6,24 @@ import Hydra.Prelude import Cardano.Api.UTxO (UTxO) import Data.Aeson (eitherDecodeFileStrict) import Hydra.Tx.Deposit (depositTx) +import Hydra.Tx.Recover (recoverTx) import Options main :: IO () main = do cmd <- parseHydraCommand - case cmd of - Deposit DepositOptions{networkId, headId, outFile, utxoFilePath, depositDeadline} -> - eitherDecodeFileStrict utxoFilePath >>= \case - Left err -> die $ "failed to parse provided UTXO file! " <> err - Right (utxo :: UTxO) -> do - let depositTransaction = depositTx networkId headId utxo depositDeadline - writeFileLBS outFile $ textEnvelopeToJSON (Just "Deposit transaction for depositing funds into a running Head") depositTransaction - putStrLn $ "Wrote deposit transaction to " <> outFile + let (txName, Options{networkId, headId, outFile, utxoFilePath, deadline}, fn) = + case cmd of + Deposit opts -> + ("deposit", opts, depositTx) + Recover opts -> + ("recover", opts, recoverTx) + utxo <- decodeUTxOFile utxoFilePath + let tx = fn networkId headId utxo deadline + writeFileLBS outFile $ textEnvelopeToJSON Nothing tx + putStrLn $ "Wrote " <> txName <> " transaction to " <> outFile + where + decodeUTxOFile fp = + eitherDecodeFileStrict fp >>= \case + Left err -> die $ "Failed to parse provided UTxO file! " <> err + Right (utxo :: UTxO) -> pure utxo diff --git a/hydra-tx/exe/Options.hs b/hydra-tx/exe/Options.hs index 1ed7112bd79..99dfef6bc91 100644 --- a/hydra-tx/exe/Options.hs +++ b/hydra-tx/exe/Options.hs @@ -7,26 +7,34 @@ import Data.Time.Clock.POSIX (posixSecondsToUTCTime) import Hydra.Tx.HeadId (HeadId (..)) import Options.Applicative -newtype Command - = Deposit DepositOptions +data Command + = Deposit Options + | Recover Options deriving stock (Show, Eq) -data DepositOptions = DepositOptions +data Options = Options { utxoFilePath :: FilePath , headId :: HeadId , outFile :: FilePath , networkId :: NetworkId - , depositDeadline :: UTCTime + , deadline :: UTCTime } deriving stock (Show, Eq) commandParser :: Parser Command commandParser = - hsubparser $ command "deposit" (info (Deposit <$> depositOptionsParser) (progDesc "TODO")) + subcommands + where + subcommands = + depositCommand <|> recoverCommand + depositCommand = + hsubparser $ command "deposit" (info (Deposit <$> depositOptionsParser) (progDesc "Request a deposit transaction.")) + recoverCommand = + hsubparser $ command "recover" (info (Recover <$> depositOptionsParser) (progDesc "Request a recover transaction")) -depositOptionsParser :: Parser DepositOptions +depositOptionsParser :: Parser Options depositOptionsParser = - DepositOptions + Options <$> utxoParser <*> headIdParser <*> outputFileParser