Skip to content

Commit d7d8f85

Browse files
author
Aleksandr Penskoi
committed
Update README.md and examples.
1 parent d24fef3 commit d7d8f85

File tree

7 files changed

+163
-131
lines changed

7 files changed

+163
-131
lines changed

README.md

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,49 +62,85 @@ 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-
, -- | Slot configuration of the network, the default value can be used for the mainnet
88-
pcSlotConfig = def
89-
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
90-
, -- Directory name of the script and data files
91-
pcScriptFileDir = "./scripts"
92-
, -- Directory for the signing key file(s)
93-
pcSigningKeyFileDir = "./signing-keys"
94-
, -- Directory where the encoded transaction files will be saved
95-
pcTxFileDir = "./txs"
96-
, -- Dry run mode will build the tx, but skip the submit step
97-
pcDryRun = False
98-
, pcLogLevel = Debug
99-
, -- | Forced budget for scripts, as optional (CPU Steps, Memory Units)
100-
pcForceBudget = Nothing
101-
, -- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
102-
, pcProtocolParamsFile = "./protocol.json"
103-
, pcEnableTxEndpoint = True
104-
}
74+
pabConf <-
75+
either error id
76+
<$> BotPlutusInterface.loadPABConfig "config/pabConfig.value"
10577
BotPlutusInterface.runPAB @MyContracts pabConf
10678
```
10779

80+
Configuration format (example: <examples/plutus-game/config/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: `mainnet` or 32-bit unsigned integral number
97+
(default: 42)
98+
slotConfig: SlotConfig configuration
99+
(see default in example)
100+
scriptFileDir: path text
101+
Directory name of the script and data files (default:
102+
"./result-scripts")
103+
signingKeyFileDir: path text
104+
Directory name of the signing key files (default: "./signing-keys")
105+
txFileDir: path text
106+
Directory name of the transaction files (default: "./txs")
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: `nothing` or StakePubKeyHash text
119+
(default: nothing)
120+
tipPollingInterval: non-negative integral number
121+
(default: 10000000)
122+
forceBudget: `nothing` or ExecutionUnits configuration
123+
Forced budget for scripts, as optional (CPU Steps, Memory Units)
124+
(default: nothing)
125+
port: port non-negative integral number
126+
(default: 9080)
127+
enableTxEndpoint: `true` or `false`
128+
(default: false)
129+
130+
ExecutionUnits configuration
131+
steps: REQUIRED non-negative integral number
132+
This corresponds roughly to the time to execute a script.
133+
memory: REQUIRED non-negative integral number
134+
This corresponds roughly to the peak memory used during script
135+
execution.
136+
137+
SlotConfig configuration
138+
length: REQUIRED integral number
139+
Length (number of milliseconds) of one slot
140+
zeroTime: REQUIRED integral number
141+
Beginning of slot 0 (in milliseconds)
142+
```
143+
108144
To run the fake PAB, you need to prepare a few more things:
109145

110146
4. Save the protocol params file to the root folder of your project using the cardano-cli
@@ -128,6 +164,7 @@ The fake PAB consists of the following modules:
128164

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

examples/plutus-game/app/Main.hs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +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.Default (def)
26-
import Data.Maybe (fromMaybe)
2720
import Playground.Types (FunctionSchema)
2821
import Schema (FormSchema)
29-
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
3022
import Prelude
3123

3224
instance HasDefinitions GameContracts where
@@ -48,27 +40,7 @@ $(deriveJSON defaultOptions ''GameContracts)
4840

4941
main :: IO ()
5042
main = do
51-
protocolParams <-
52-
fromMaybe (error "protocol.json file not found") . JSON.decode
53-
<$> LazyByteString.readFile "protocol.json"
54-
let pabConf =
55-
PABConfig
56-
{ pcCliLocation = Local
57-
, pcNetwork = Testnet (NetworkMagic 1097911063)
58-
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
59-
, pcPort = 9080
60-
, pcProtocolParams = protocolParams
61-
, pcTipPollingInterval = 10_000_000
62-
, pcSlotConfig = def
63-
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
64-
, pcOwnStakePubKeyHash = Nothing
65-
, pcScriptFileDir = "./scripts"
66-
, pcSigningKeyFileDir = "./signing-keys"
67-
, pcTxFileDir = "./txs"
68-
, pcDryRun = True
69-
, pcLogLevel = Debug
70-
, pcProtocolParamsFile = "./protocol.json"
71-
, pcForceBudget = Just (9_000_000_000, 15_000_000)
72-
, pcEnableTxEndpoint = True
73-
}
43+
pabConf <-
44+
either error id
45+
<$> BotPlutusInterface.loadPABConfig "config/pabConfig.value"
7446
BotPlutusInterface.runPAB @GameContracts pabConf
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- Calling the cli locally or through an ssh connection
2+
cliLocation: local
3+
chainIndexUrl: "http://localhost:9083"
4+
networkId: 1097911063
5+
-- Slot configuration of the network, the default value can be used for the mainnet
6+
slotConfig:
7+
length: 1000
8+
zeroTime: 1596059091000
9+
-- Directory name of the script and data files
10+
scriptFileDir: "./scripts"
11+
-- Directory for the signing key file(s)
12+
signingKeyFileDir: "./signing-keys"
13+
-- Directory where the encoded transaction files will be saved
14+
txFileDir: "./txs"
15+
-- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
16+
protocolParamsFile: "./config/protocol.json"
17+
-- Dry run mode will build the tx, but skip the submit step
18+
dryRun: true
19+
logLevel: debug
20+
ownPubKeyHash: "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
21+
ownStakePubKeyHash: nothing
22+
tipPollingInterval: 10000000
23+
forceBudget:
24+
steps: 9000000000
25+
memory: 15000000
26+
port: 9080
27+
enableTxEndpoint: true

examples/plutus-nft/app/Main.hs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@
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 (
1714
NFTSchema,
1815
mintNft,
1916
)
20-
import Data.Aeson qualified as JSON
2117
import Data.Aeson.TH (defaultOptions, deriveJSON)
22-
import Data.ByteString.Lazy qualified as LazyByteString
23-
import Data.Default (def)
24-
import Data.Maybe (fromMaybe)
2518
import Ledger.Value (TokenName)
2619
import Playground.Types (FunctionSchema)
2720
import Schema (FormSchema)
28-
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
2921
import Prelude
3022

3123
instance HasDefinitions MintNFTContracts where
@@ -48,27 +40,7 @@ $(deriveJSON defaultOptions ''MintNFTContracts)
4840

4941
main :: IO ()
5042
main = do
51-
protocolParams <-
52-
fromMaybe (error "protocol.json file not found") . JSON.decode
53-
<$> LazyByteString.readFile "protocol.json"
54-
let pabConf =
55-
PABConfig
56-
{ pcCliLocation = Local
57-
, pcNetwork = Testnet (NetworkMagic 1097911063)
58-
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
59-
, pcPort = 9080
60-
, pcProtocolParams = protocolParams
61-
, pcTipPollingInterval = 10_000_000
62-
, pcSlotConfig = def
63-
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
64-
, pcOwnStakePubKeyHash = Nothing
65-
, pcScriptFileDir = "./scripts"
66-
, pcSigningKeyFileDir = "./signing-keys"
67-
, pcTxFileDir = "./txs"
68-
, pcDryRun = True
69-
, pcLogLevel = Debug
70-
, pcProtocolParamsFile = "./protocol.json"
71-
, pcForceBudget = Just (1000, 1000)
72-
, pcEnableTxEndpoint = True
73-
}
43+
pabConf <-
44+
either error id
45+
<$> BotPlutusInterface.loadPABConfig "config/pabConfig.value"
7446
BotPlutusInterface.runPAB @MintNFTContracts pabConf
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- Calling the cli locally or through an ssh connection
2+
cliLocation: local
3+
chainIndexUrl: "http://localhost:9083"
4+
networkId: 1097911063
5+
-- Slot configuration of the network, the default value can be used for the mainnet
6+
slotConfig:
7+
length: 1000
8+
zeroTime: 1596059091000
9+
-- Directory name of the script and data files
10+
scriptFileDir: "./scripts"
11+
-- Directory for the signing key file(s)
12+
signingKeyFileDir: "./signing-keys"
13+
-- Directory where the encoded transaction files will be saved
14+
txFileDir: "./txs"
15+
-- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
16+
protocolParamsFile: "./config/protocol.json"
17+
-- Dry run mode will build the tx, but skip the submit step
18+
dryRun: true
19+
logLevel: debug
20+
ownPubKeyHash: "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
21+
ownStakePubKeyHash: nothing
22+
tipPollingInterval: 10000000
23+
forceBudget:
24+
steps: 1000
25+
memory: 1000
26+
port: 9080
27+
enableTxEndpoint: true

examples/plutus-transfer/app/Main.hs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@
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.Transfer (
1714
TransferParams,
1815
TransferSchema,
1916
transfer,
2017
)
21-
import Data.Aeson qualified as JSON
2218
import Data.Aeson.TH (defaultOptions, deriveJSON)
23-
import Data.ByteString.Lazy qualified as LazyByteString
24-
import Data.Default (def)
25-
import Data.Maybe (fromMaybe)
2619
import Playground.Types (FunctionSchema)
2720
import Schema (FormSchema)
28-
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
2921
import Prelude
3022

3123
instance HasDefinitions TransferContracts where
@@ -47,27 +39,7 @@ $(deriveJSON defaultOptions ''TransferContracts)
4739

4840
main :: IO ()
4941
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-
, pcSlotConfig = def
62-
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
63-
, pcOwnStakePubKeyHash = Nothing
64-
, pcScriptFileDir = "./scripts"
65-
, pcSigningKeyFileDir = "./signing-keys"
66-
, pcTxFileDir = "./txs"
67-
, pcDryRun = True
68-
, pcLogLevel = Debug
69-
, pcProtocolParamsFile = "./protocol.json"
70-
, pcForceBudget = Nothing
71-
, pcEnableTxEndpoint = True
72-
}
42+
pabConf <-
43+
either error id
44+
<$> BotPlutusInterface.loadPABConfig "config/pabConfig.value"
7345
BotPlutusInterface.runPAB @TransferContracts pabConf
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Calling the cli locally or through an ssh connection
2+
cliLocation: local
3+
chainIndexUrl: "http://localhost:9083"
4+
networkId: 1097911063
5+
-- Slot configuration of the network, the default value can be used for the mainnet
6+
slotConfig:
7+
length: 1000
8+
zeroTime: 1596059091000
9+
-- Directory name of the script and data files
10+
scriptFileDir: "./scripts"
11+
-- Directory for the signing key file(s)
12+
signingKeyFileDir: "./signing-keys"
13+
-- Directory where the encoded transaction files will be saved
14+
txFileDir: "./txs"
15+
-- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
16+
protocolParamsFile: "./config/protocol.json"
17+
-- Dry run mode will build the tx, but skip the submit step
18+
dryRun: true
19+
logLevel: debug
20+
ownPubKeyHash: "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
21+
ownStakePubKeyHash: nothing
22+
tipPollingInterval: 10000000
23+
forceBudget: nothing
24+
port: 9080
25+
enableTxEndpoint: true

0 commit comments

Comments
 (0)