-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathParsers.hs
134 lines (118 loc) · 4.34 KB
/
Parsers.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Cardano.TxSubmit.CLI.Parsers
( opts
, pTxSubmitNodeParams
, pConfigFile
, pNetworkId
, pProtocol
, pSocketPath
) where
import Cardano.Api (AnyConsensusModeParams (..), ConsensusModeParams (..),
EpochSlots (..), NetworkId (..), NetworkMagic (..))
import Cardano.TxSubmit.CLI.Types (ConfigFile (..), SocketPath (..),
TxSubmitNodeParams (..))
import Cardano.TxSubmit.Rest.Parsers (pWebserverConfig)
import Control.Applicative (Alternative (..), Applicative (..), (<**>))
import Data.Function ((.))
import Data.Functor (Functor (fmap), (<$>))
import Data.Int
import Data.Semigroup (Semigroup ((<>)))
import Data.Word (Word64)
import Options.Applicative (Parser, ParserInfo)
import qualified Options.Applicative as Opt
opts :: ParserInfo TxSubmitNodeParams
opts = Opt.info (pTxSubmitNodeParams <**> Opt.helper)
( Opt.fullDesc
<> Opt.progDesc "Cardano transaction submission web API."
)
pTxSubmitNodeParams :: Parser TxSubmitNodeParams
pTxSubmitNodeParams = TxSubmitNodeParams
<$> pConfigFile
<*> pProtocol
<*> pNetworkId
<*> pSocketPath
<*> pWebserverConfig 8090
<*> pMetricsPort 8081
pConfigFile :: Parser ConfigFile
pConfigFile = ConfigFile <$> Opt.strOption
( Opt.long "config"
<> Opt.help "Path to the tx-submit web API configuration file"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)
-- TODO: This was ripped from `cardano-cli` because, unfortunately, it's not
-- exported. Once we export this parser from the appropriate module and update
-- our `cardano-cli` dependency, we should remove this and import the parser
-- from there.
pNetworkId :: Parser NetworkId
pNetworkId = pMainnet <|> fmap Testnet pTestnetMagic
where
pMainnet :: Parser NetworkId
pMainnet = Opt.flag' Mainnet
( Opt.long "mainnet"
<> Opt.help "Use the mainnet magic id."
)
pTestnetMagic :: Parser NetworkMagic
pTestnetMagic = NetworkMagic <$> Opt.option Opt.auto
( Opt.long "testnet-magic"
<> Opt.metavar "NATURAL"
<> Opt.help "Specify a testnet magic id."
)
-- TODO: This was ripped from `cardano-cli` because, unfortunately, it's not
-- exported. Once we export this parser from the appropriate module and update
-- our `cardano-cli` dependency, we should remove this and import the parser
-- from there.
pProtocol :: Parser AnyConsensusModeParams
pProtocol =
( Opt.flag' ()
( Opt.long "shelley-mode"
<> Opt.help "For talking to a node running in Shelley-only mode."
)
*> pShelley
)
<|> ( Opt.flag' ()
( Opt.long "byron-mode"
<> Opt.help "For talking to a node running in Byron-only mode."
)
*> pByron
)
<|> ( Opt.flag' ()
( Opt.long "cardano-mode"
<> Opt.help "For talking to a node running in full Cardano mode (default)."
)
*> pCardano
)
<|> -- Default to the Cardano protocol.
pure (AnyConsensusModeParams (CardanoModeParams (EpochSlots defaultByronEpochSlots)))
where
pByron :: Parser AnyConsensusModeParams
pByron = AnyConsensusModeParams . ByronModeParams <$> pEpochSlots
pShelley :: Parser AnyConsensusModeParams
pShelley = pure (AnyConsensusModeParams ShelleyModeParams)
pCardano :: Parser AnyConsensusModeParams
pCardano = AnyConsensusModeParams . CardanoModeParams <$> pEpochSlots
pEpochSlots :: Parser EpochSlots
pEpochSlots = EpochSlots <$> Opt.option Opt.auto
( Opt.long "epoch-slots"
<> Opt.metavar "NATURAL"
<> Opt.help "The number of slots per epoch for the Byron era."
<> Opt.value defaultByronEpochSlots -- Default to the mainnet value.
<> Opt.showDefault
)
defaultByronEpochSlots :: Word64
defaultByronEpochSlots = 21600
pSocketPath :: Parser SocketPath
pSocketPath = SocketPath <$> Opt.strOption
( Opt.long "socket-path"
<> Opt.help "Path to a cardano-node socket"
<> Opt.completer (Opt.bashCompleter "file")
<> Opt.metavar "FILEPATH"
)
pMetricsPort :: Int -> Parser Int
pMetricsPort defaultValue = Opt.option Opt.auto
( Opt.long "metrics-port"
<> Opt.help "Metrics port"
<> Opt.metavar "PORT"
<> Opt.value defaultValue
)