@@ -8,11 +8,15 @@ module BotPlutusInterface.Config (
8
8
docPABConfig ,
9
9
loadPABConfig ,
10
10
savePABConfig ,
11
- savePABConfigPartial ,
12
11
) where
13
12
13
+ import BotPlutusInterface.Effects (
14
+ ShellArgs (.. ),
15
+ callLocalCommand ,
16
+ )
14
17
import BotPlutusInterface.Types (CLILocation (.. ), LogLevel (.. ), PABConfig (.. ))
15
- import Cardano.Api (ExecutionUnits (.. ))
18
+
19
+ import Cardano.Api (ExecutionUnits (.. ), NetworkId (Mainnet , Testnet ), unNetworkMagic )
16
20
import Config (Section (Section ), Value (Atom , Sections , Text ))
17
21
import Config.Schema (
18
22
HasSpec (anySpec ),
@@ -25,8 +29,9 @@ import Config.Schema (
25
29
(<!>) ,
26
30
)
27
31
import Data.Default (def )
28
- import Data.Functor ((<&>) )
29
32
import Data.String.ToString (toString )
33
+ import Data.Text (Text )
34
+ import Data.Text qualified as Text
30
35
import PlutusConfig.Base (
31
36
enumToAtom ,
32
37
filepathSpec ,
@@ -37,7 +42,6 @@ import PlutusConfig.Base (
37
42
import PlutusConfig.Cardano.Api ()
38
43
import PlutusConfig.Cardano.Api.Shelley (
39
44
readProtocolParametersJSON ,
40
- writeProtocolParametersJSON ,
41
45
)
42
46
import PlutusConfig.Ledger ()
43
47
import PlutusConfig.Types (
@@ -220,27 +224,57 @@ pabConfigSpec = sectionsSpec "PABConfig" $ do
220
224
docPABConfig :: String
221
225
docPABConfig = show $ generateDocs pabConfigSpec
222
226
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
+ -}
223
238
loadPABConfig :: FilePath -> IO (Either String PABConfig )
224
239
loadPABConfig fn = do
225
240
confE <- deserialize <$> readFile fn
226
241
case confE of
227
242
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.
238
278
-}
239
279
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 "
0 commit comments