Skip to content

Commit 1ab405a

Browse files
authored
Merge pull request #91 from mlabs-haskell/aleksandr/config-2
Using a config file
2 parents 0908ade + 5b4bb6e commit 1ab405a

File tree

18 files changed

+1328
-133
lines changed

18 files changed

+1328
-133
lines changed

README.md

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,48 +62,72 @@ instance HasDefinitions MyContracts where
6262
MyContract.contract params
6363
```
6464

65-
3. Write your main entrypoint for the application, with the preferred configurations
65+
3. Write your main entrypoint for the application and the configuration file
6666

6767
```haskell
68-
import BotPlutusInterface.Types (CLILocation (Local), LogLevel (Debug), PABConfig (..))
69-
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
70-
import Data.Aeson qualified as JSON
71-
import Data.ByteString.Lazy qualified as LazyByteString
72-
import Data.Default (def)
73-
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
68+
import BotPlutusInterface qualified
69+
import BotPlutusInterface.Config qualified as BotPlutusInterface
70+
import Prelude
7471

7572
main :: IO ()
7673
main = do
77-
protocolParams <- JSON.decode <$> LazyByteString.readFile "protocol.json"
78-
let pabConf =
79-
PABConfig
80-
{ -- Calling the cli locally or through an ssh connection
81-
pcCliLocation = Local
82-
, pcNetwork = Testnet (NetworkMagic 42)
83-
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
84-
, pcPort = 9080
85-
, pcProtocolParams = protocolParams
86-
, pcTipPollingInterval = 10_000_000
87-
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
88-
, pcOwnStakePubKeyHash = Nothing
89-
, -- Directory name of the script and data files
90-
pcScriptFileDir = "./scripts"
91-
, -- Directory for the signing key file(s)
92-
pcSigningKeyFileDir = "./signing-keys"
93-
, -- Directory where the encoded transaction files will be saved
94-
pcTxFileDir = "./txs"
95-
, -- Dry run mode will build the tx, but skip the submit step
96-
pcDryRun = False
97-
, pcLogLevel = Debug
98-
, -- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
99-
, pcProtocolParamsFile = "./protocol.json"
100-
, pcEnableTxEndpoint = True
101-
-- Save some stats during contract run (only transactions execution budgets supported atm)
102-
, pcCollectStats = False
103-
}
74+
pabConf <-
75+
either error id
76+
<$> BotPlutusInterface.loadPABConfig "./pabConfig.value"
10477
BotPlutusInterface.runPAB @MyContracts pabConf
10578
```
10679

80+
Configuration format (example: <examples/plutus-game/pabConfig.value>):
81+
82+
``` console
83+
$ cabal repl --disable-optimisation --repl-options -Wwarn
84+
...
85+
BotPlutusInterface> :m Prelude
86+
...
87+
Prelude> :l BotPlutusInterface.Config
88+
...
89+
Prelude BotPlutusInterface.Config> putStrLn docPABConfig
90+
Top-level configuration file fields:
91+
cliLocation: `local` or destination text
92+
calling the cli through ssh when set to destination (default:
93+
local)
94+
chainIndexUrl: url text
95+
(default: "http://localhost:9083")
96+
networkId: case insensitive `mainnet` atom or 32-bit unsigned integral number
97+
(default: 42)
98+
scriptFileDir: path text
99+
Directory name of the script and data files (default:
100+
"./result-scripts")
101+
signingKeyFileDir: path text
102+
Directory name of the signing key files (default: "./signing-keys")
103+
txFileDir: path text
104+
Directory name of the transaction files (default: "./txs")
105+
metadataDir: path text
106+
Directory name of metadata files (default: "/metadata")
107+
protocolParamsFile: filepath text
108+
Protocol params file location relative to the cardano-cli working
109+
directory (needed for the cli) in JSON format. (default:
110+
"./protocol.json")
111+
dryRun: `true` or `false`
112+
Dry run mode will build the tx, but skip the submit step (default:
113+
true)
114+
logLevel: `error` or `warn` or `notice` or `info` or `debug`
115+
(default: info)
116+
ownPubKeyHash: PubKeyHash text
117+
(default: "")
118+
ownStakePubKeyHash: case insensitive `nothing` atom or StakePubKeyHash text
119+
(default: nothing)
120+
tipPollingInterval: non-negative integral number
121+
(default: 10000000)
122+
port: port non-negative integral number
123+
(default: 9080)
124+
enableTxEndpoint: `true` or `false`
125+
(default: false)
126+
collectStats: `true` or `false`
127+
Save some stats during contract run (only transactions execution
128+
budgets supported atm) (default: false)
129+
```
130+
107131
To run the fake PAB, you need to prepare a few more things:
108132

109133
4. Save the protocol params file to the root folder of your project using the cardano-cli
@@ -127,6 +151,7 @@ The fake PAB consists of the following modules:
127151

128152
- **BotPlutusInterface** main entry point
129153
- **BotPlutusInterface.Server** Servant server, handling http endpoint calls and websockets
154+
- **BotPlutusInterface.Config** load/save PAB configuration file
130155
- **BotPlutusInterface.Contract** handling contract effects by creating the necessary files and calling cardano-cli commands (a few effects are mocked)
131156
- **BotPlutusInterface.Balance** doing some preparations so the cli can process the rest (non-ada asset balancing, addig tx inputs, adding minimum lovelaces, add signatories)
132157
- **BotPlutusInterface.CardanoCLI** wrappers for cardano-cli commands

bot-plutus-interface.cabal

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ library
8080
BotPlutusInterface.BodyBuilder
8181
BotPlutusInterface.CardanoCLI
8282
BotPlutusInterface.ChainIndex
83+
BotPlutusInterface.Config
8384
BotPlutusInterface.Contract
8485
BotPlutusInterface.Effects
8586
BotPlutusInterface.ExBudget
@@ -90,6 +91,12 @@ library
9091
BotPlutusInterface.TimeSlot
9192
BotPlutusInterface.Types
9293
BotPlutusInterface.UtxoParser
94+
PlutusConfig.Base
95+
PlutusConfig.Cardano.Api
96+
PlutusConfig.Cardano.Api.Shelley
97+
PlutusConfig.Ledger
98+
PlutusConfig.Misc
99+
PlutusConfig.Types
93100

94101
build-depends:
95102
, aeson ^>=1.5.0.0
@@ -101,6 +108,8 @@ library
101108
, cardano-ledger-core
102109
, cardano-prelude
103110
, cardano-slotting
111+
, config-schema
112+
, config-value
104113
, containers
105114
, data-default
106115
, data-default-class
@@ -127,19 +136,24 @@ library
127136
, plutus-pab
128137
, plutus-tx
129138
, plutus-tx-plugin
139+
, pretty
130140
, prettyprinter
131141
, process
132142
, QuickCheck
143+
, regex-compat
133144
, row-types
134145
, serialise
135146
, servant
136147
, servant-client
148+
, servant-client-core
137149
, servant-server
138150
, servant-websockets
139151
, split
140152
, stm
153+
, temporary
141154
, text ^>=1.2.4.0
142155
, time
156+
, tostring
143157
, transformers
144158
, transformers-either
145159
, transformers-except
@@ -159,6 +173,7 @@ test-suite bot-plutus-interface-test
159173
ghc-options: -fplugin-opt PlutusTx.Plugin:defer-errors
160174
other-modules:
161175
Spec.BotPlutusInterface.Balance
176+
Spec.BotPlutusInterface.Config
162177
Spec.BotPlutusInterface.Contract
163178
Spec.BotPlutusInterface.ContractStats
164179
Spec.BotPlutusInterface.Server

examples/plutus-game/app/Main.hs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,22 @@
33
module Main (main) where
44

55
import BotPlutusInterface qualified
6+
import BotPlutusInterface.Config qualified as BotPlutusInterface
67
import BotPlutusInterface.Types (
7-
CLILocation (Local),
88
HasDefinitions (..),
9-
LogLevel (Debug),
10-
PABConfig (..),
119
SomeBuiltin (..),
1210
endpointsToSchemas,
1311
)
14-
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
1512
import Cardano.PlutusExample.Game (
1613
GameSchema,
1714
GuessParams,
1815
LockParams,
1916
guess,
2017
lock,
2118
)
22-
import Data.Aeson qualified as JSON
2319
import Data.Aeson.TH (defaultOptions, deriveJSON)
24-
import Data.ByteString.Lazy qualified as LazyByteString
25-
import Data.Maybe (fromMaybe)
2620
import Playground.Types (FunctionSchema)
2721
import Schema (FormSchema)
28-
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
2922
import Prelude
3023

3124
instance HasDefinitions GameContracts where
@@ -47,27 +40,7 @@ $(deriveJSON defaultOptions ''GameContracts)
4740

4841
main :: IO ()
4942
main = do
50-
protocolParams <-
51-
fromMaybe (error "protocol.json file not found") . JSON.decode
52-
<$> LazyByteString.readFile "protocol.json"
53-
let pabConf =
54-
PABConfig
55-
{ pcCliLocation = Local
56-
, pcNetwork = Testnet (NetworkMagic 1097911063)
57-
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
58-
, pcPort = 9080
59-
, pcProtocolParams = protocolParams
60-
, pcTipPollingInterval = 10_000_000
61-
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
62-
, pcOwnStakePubKeyHash = Nothing
63-
, pcScriptFileDir = "./scripts"
64-
, pcSigningKeyFileDir = "./signing-keys"
65-
, pcTxFileDir = "./txs"
66-
, pcDryRun = True
67-
, pcLogLevel = Debug
68-
, pcProtocolParamsFile = "./protocol.json"
69-
, pcEnableTxEndpoint = True
70-
, pcMetadataDir = "./metadata"
71-
, pcCollectStats = False
72-
}
43+
pabConf <-
44+
either error id
45+
<$> BotPlutusInterface.loadPABConfig "./pabConfig.value"
7346
BotPlutusInterface.runPAB @GameContracts pabConf

examples/plutus-game/pabConfig.value

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- Calling the cli locally or through an ssh connection
2+
cliLocation: local
3+
chainIndexUrl: "http://localhost:9083"
4+
networkId: 1097911063
5+
6+
-- Directory name of the script and data files
7+
scriptFileDir: "./scripts"
8+
9+
-- Directory for the signing key file(s)
10+
signingKeyFileDir: "./signing-keys"
11+
12+
-- Directory where the encoded transaction files will be saved
13+
txFileDir: "./txs"
14+
15+
-- Directory name of metadata files
16+
metadataDir: "./metadata"
17+
18+
-- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
19+
protocolParamsFile: "./protocol.json"
20+
21+
-- Dry run mode will build the tx, but skip the submit step
22+
dryRun: true
23+
logLevel: debug
24+
ownPubKeyHash: "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
25+
ownStakePubKeyHash: nothing
26+
tipPollingInterval: 10000000
27+
port: 9080
28+
enableTxEndpoint: true
29+
30+
-- Save some stats during contract run (only transactions execution budgets supported atm)
31+
collectStats: false

examples/plutus-nft/app/Main.hs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,16 @@
44
module Main (main) where
55

66
import BotPlutusInterface qualified
7+
import BotPlutusInterface.Config qualified as BotPlutusInterface
78
import BotPlutusInterface.Types (
8-
CLILocation (Local),
99
HasDefinitions (..),
10-
LogLevel (Debug),
11-
PABConfig (..),
1210
SomeBuiltin (..),
1311
endpointsToSchemas,
1412
)
15-
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
1613
import Cardano.PlutusExample.NFT
17-
import Data.Aeson qualified as JSON
1814
import Data.Aeson.TH (defaultOptions, deriveJSON)
19-
import Data.ByteString.Lazy qualified as LazyByteString
20-
import Data.Maybe (fromMaybe)
2115
import Playground.Types (FunctionSchema)
2216
import Schema (FormSchema)
23-
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
2417
import Prelude
2518

2619
instance HasDefinitions MintNFTContracts where
@@ -43,27 +36,7 @@ $(deriveJSON defaultOptions ''MintNFTContracts)
4336

4437
main :: IO ()
4538
main = do
46-
protocolParams <-
47-
fromMaybe (error "protocol.json file not found") . JSON.decode
48-
<$> LazyByteString.readFile "protocol.json"
49-
let pabConf =
50-
PABConfig
51-
{ pcCliLocation = Local
52-
, pcNetwork = Testnet (NetworkMagic 1097911063)
53-
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
54-
, pcPort = 9080
55-
, pcProtocolParams = protocolParams
56-
, pcTipPollingInterval = 10_000_000
57-
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
58-
, pcOwnStakePubKeyHash = Nothing
59-
, pcScriptFileDir = "./scripts"
60-
, pcSigningKeyFileDir = "./signing-keys"
61-
, pcTxFileDir = "./txs"
62-
, pcDryRun = False
63-
, pcLogLevel = Debug
64-
, pcProtocolParamsFile = "./protocol.json"
65-
, pcEnableTxEndpoint = True
66-
, pcMetadataDir = "./metadata"
67-
, pcCollectStats = False
68-
}
39+
pabConf <-
40+
either error id
41+
<$> BotPlutusInterface.loadPABConfig "./pabConfig.value"
6942
BotPlutusInterface.runPAB @MintNFTContracts pabConf

examples/plutus-nft/pabConfig.value

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- Calling the cli locally or through an ssh connection
2+
cliLocation: local
3+
chainIndexUrl: "http://localhost:9083"
4+
networkId: 1097911063
5+
6+
-- Directory name of the script and data files
7+
scriptFileDir: "./scripts"
8+
9+
-- Directory for the signing key file(s)
10+
signingKeyFileDir: "./signing-keys"
11+
12+
-- Directory where the encoded transaction files will be saved
13+
txFileDir: "./txs"
14+
15+
-- Directory name of metadata files
16+
metadataDir: "./metadata"
17+
18+
-- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
19+
protocolParamsFile: "./protocol.json"
20+
21+
-- Dry run mode will build the tx, but skip the submit step
22+
dryRun: true
23+
logLevel: debug
24+
ownPubKeyHash: "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
25+
ownStakePubKeyHash: nothing
26+
tipPollingInterval: 10000000
27+
port: 9080
28+
enableTxEndpoint: true
29+
30+
-- Save some stats during contract run (only transactions execution budgets supported atm)
31+
collectStats: false

0 commit comments

Comments
 (0)