Skip to content

Commit

Permalink
Add recover cmd and refactor a bit
Browse files Browse the repository at this point in the history
Signed-off-by: Sasha Bogicevic <[email protected]>
  • Loading branch information
v0d1ch committed Aug 28, 2024
1 parent 5d1fa04 commit 9e2d038
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
24 changes: 16 additions & 8 deletions hydra-tx/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 15 additions & 7 deletions hydra-tx/exe/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e2d038

Please sign in to comment.