Skip to content

Commit 32d2aa6

Browse files
author
Aleksandr Penskoi
committedMay 12, 2022
Add auto fetch protocolParamsFile from CLI
1 parent 6967f79 commit 32d2aa6

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed
 

‎src/BotPlutusInterface/Config.hs

+55-21
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ module BotPlutusInterface.Config (
88
docPABConfig,
99
loadPABConfig,
1010
savePABConfig,
11-
savePABConfigPartial,
1211
) where
1312

13+
import BotPlutusInterface.Effects (
14+
ShellArgs (..),
15+
callLocalCommand,
16+
)
1417
import BotPlutusInterface.Types (CLILocation (..), LogLevel (..), PABConfig (..))
15-
import Cardano.Api (ExecutionUnits (..))
18+
19+
import Cardano.Api (ExecutionUnits (..), NetworkId (Mainnet, Testnet), unNetworkMagic)
1620
import Config (Section (Section), Value (Atom, Sections, Text))
1721
import Config.Schema (
1822
HasSpec (anySpec),
@@ -25,8 +29,9 @@ import Config.Schema (
2529
(<!>),
2630
)
2731
import Data.Default (def)
28-
import Data.Functor ((<&>))
2932
import Data.String.ToString (toString)
33+
import Data.Text (Text)
34+
import Data.Text qualified as Text
3035
import PlutusConfig.Base (
3136
enumToAtom,
3237
filepathSpec,
@@ -37,7 +42,6 @@ import PlutusConfig.Base (
3742
import PlutusConfig.Cardano.Api ()
3843
import PlutusConfig.Cardano.Api.Shelley (
3944
readProtocolParametersJSON,
40-
writeProtocolParametersJSON,
4145
)
4246
import PlutusConfig.Ledger ()
4347
import PlutusConfig.Types (
@@ -220,27 +224,57 @@ pabConfigSpec = sectionsSpec "PABConfig" $ do
220224
docPABConfig :: String
221225
docPABConfig = show $ generateDocs pabConfigSpec
222226

227+
{- |Load 'PABConfig' from the file:
228+
229+
NOTE:
230+
231+
- default value for: @pcProtocolParamsFile@ is "./protocol.json"
232+
233+
- if @pcProtocolParamsFile == "./protocol.json"@ and file don't exist, try to
234+
fetch in from @cardano-cli@ on the fly (only for local)
235+
236+
- in other cases -- fail
237+
-}
223238
loadPABConfig :: FilePath -> IO (Either String PABConfig)
224239
loadPABConfig fn = do
225240
confE <- deserialize <$> readFile fn
226241
case confE of
227242
Left err -> return $ Left $ "PABConfig: " <> fn <> ": " <> err
228-
Right conf@PABConfig {pcProtocolParamsFile} -> do
229-
readProtocolParametersJSON (toString pcProtocolParamsFile)
230-
<&> \case
231-
Left err -> Left $ "protocolParamsFile: " <> toString pcProtocolParamsFile <> ": " <> err
232-
Right pcProtocolParams -> Right conf {pcProtocolParams}
233-
234-
{- |Save 'PABConfig' into two files:
235-
236-
- Save 'PABConfig' into a file without `pcProtocolParams`, which specified as the argument
237-
- Save 'pcProtocolParams' into file, which specified in `pcProtocolParamsFile` field of 'PABConfig'
243+
Right conf@PABConfig {pcProtocolParamsFile, pcNetwork, pcCliLocation} -> do
244+
pparamsE <- readProtocolParametersJSON (toString pcProtocolParamsFile)
245+
case pparamsE of
246+
Left err
247+
| pcProtocolParamsFile == "./protocol.json"
248+
&& pcCliLocation == Local -> do
249+
let shellArgs =
250+
ShellArgs
251+
{ cmdName = "cardano-cli"
252+
, cmdArgs =
253+
[ "query"
254+
, "protocol-parameters"
255+
, "--out-file"
256+
, "./protocol.json"
257+
]
258+
++ networkArg pcNetwork
259+
, cmdOutParser = id
260+
}
261+
callLocalCommand shellArgs
262+
>>= \case
263+
Left errPParams -> return $ Left $ Text.unpack errPParams
264+
Right _ -> loadPABConfig fn
265+
| otherwise ->
266+
return $ pparamsError pcProtocolParamsFile err
267+
Right pcProtocolParams -> return $ Right conf {pcProtocolParams}
268+
where
269+
pparamsError f e = Left $ "protocolParamsFile: " <> toString f <> ": " <> e
270+
271+
networkArg :: NetworkId -> [Text]
272+
networkArg Mainnet = ["--mainnet"]
273+
networkArg (Testnet magic) = ["--testnet-magic", Text.pack $ show $ unNetworkMagic magic]
274+
275+
{- |Save 'PABConfig'.
276+
277+
NOTE: The functions don't save @pcProtocolParams@ because don't expect that it can be changed.
238278
-}
239279
savePABConfig :: FilePath -> PABConfig -> IO ()
240-
savePABConfig fn conf@PABConfig {pcProtocolParams, pcProtocolParamsFile} = do
241-
writeProtocolParametersJSON (toString pcProtocolParamsFile) pcProtocolParams
242-
savePABConfigPartial fn conf
243-
244-
-- |Partly save 'PABConfig' to file, without `pcProtocolParams`
245-
savePABConfigPartial :: FilePath -> PABConfig -> IO ()
246-
savePABConfigPartial fn conf = writeFile fn $ serialize conf <> "\n"
280+
savePABConfig fn conf = writeFile fn $ serialize conf <> "\n"

‎src/BotPlutusInterface/Effects.hs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module BotPlutusInterface.Effects (
1919
writeFileJSON,
2020
writeFileTextEnvelope,
2121
callCommand,
22+
callLocalCommand,
2223
) where
2324

2425
import BotPlutusInterface.ChainIndex (handleChainIndexReq)

0 commit comments

Comments
 (0)
Please sign in to comment.