@@ -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+ )
1417import BotPlutusInterface.Types (CLILocation (.. ), LogLevel (.. ), PABConfig (.. ))
15- import Cardano.Api (ExecutionUnits (.. ))
18+
19+ import Cardano.Api (ExecutionUnits (.. ), NetworkId (Mainnet , Testnet ), unNetworkMagic )
1620import Config (Section (Section ), Value (Atom , Sections , Text ))
1721import Config.Schema (
1822 HasSpec (anySpec ),
@@ -25,8 +29,9 @@ import Config.Schema (
2529 (<!>) ,
2630 )
2731import Data.Default (def )
28- import Data.Functor ((<&>) )
2932import Data.String.ToString (toString )
33+ import Data.Text (Text )
34+ import Data.Text qualified as Text
3035import PlutusConfig.Base (
3136 enumToAtom ,
3237 filepathSpec ,
@@ -37,7 +42,6 @@ import PlutusConfig.Base (
3742import PlutusConfig.Cardano.Api ()
3843import PlutusConfig.Cardano.Api.Shelley (
3944 readProtocolParametersJSON ,
40- writeProtocolParametersJSON ,
4145 )
4246import PlutusConfig.Ledger ()
4347import PlutusConfig.Types (
@@ -220,27 +224,57 @@ pabConfigSpec = sectionsSpec "PABConfig" $ do
220224docPABConfig :: String
221225docPABConfig = 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+ -}
223238loadPABConfig :: FilePath -> IO (Either String PABConfig )
224239loadPABConfig 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-}
239279savePABConfig :: 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 "
0 commit comments