Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a config file #91

Merged
merged 19 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
substituters = https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/
extra-experimental-features = nix-command flakes
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- uses: cachix/cachix-action@v10
with:
name: mlabs
Expand Down
107 changes: 72 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,49 +62,85 @@ instance HasDefinitions MyContracts where
MyContract.contract params
```

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

```haskell
import BotPlutusInterface.Types (CLILocation (Local), LogLevel (Debug), PABConfig (..))
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
import Data.Aeson qualified as JSON
import Data.ByteString.Lazy qualified as LazyByteString
import Data.Default (def)
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
import BotPlutusInterface qualified
import BotPlutusInterface.Config qualified as BotPlutusInterface
import Prelude

main :: IO ()
main = do
protocolParams <- JSON.decode <$> LazyByteString.readFile "protocol.json"
let pabConf =
PABConfig
{ -- Calling the cli locally or through an ssh connection
pcCliLocation = Local
, pcNetwork = Testnet (NetworkMagic 42)
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
, pcPort = 9080
, pcProtocolParams = protocolParams
, pcTipPollingInterval = 10_000_000
, -- | Slot configuration of the network, the default value can be used for the mainnet
pcSlotConfig = def
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
, -- Directory name of the script and data files
pcScriptFileDir = "./scripts"
, -- Directory for the signing key file(s)
pcSigningKeyFileDir = "./signing-keys"
, -- Directory where the encoded transaction files will be saved
pcTxFileDir = "./txs"
, -- Dry run mode will build the tx, but skip the submit step
pcDryRun = False
, pcLogLevel = Debug
, -- | Forced budget for scripts, as optional (CPU Steps, Memory Units)
pcForceBudget = Nothing
, -- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
, pcProtocolParamsFile = "./protocol.json"
, pcEnableTxEndpoint = True
}
pabConf <-
either error id
<$> BotPlutusInterface.loadPABConfig "config/pabConfig.value"
BotPlutusInterface.runPAB @MyContracts pabConf
```

Configuration format (example: <examples/plutus-game/config/pabConfig.value>):

``` console
$ cabal repl --disable-optimisation --repl-options -Wwarn
...
BotPlutusInterface> :m Prelude
...
Prelude> :l BotPlutusInterface.Config
...
Prelude BotPlutusInterface.Config> putStrLn docPABConfig
Top-level configuration file fields:
cliLocation: `local` or destination text
calling the cli through ssh when set to destination (default:
local)
chainIndexUrl: url text
(default: "http://localhost:9083")
networkId: `mainnet` or 32-bit unsigned integral number
(default: 42)
slotConfig: SlotConfig configuration
(see default in example)
scriptFileDir: path text
Directory name of the script and data files (default:
"./result-scripts")
signingKeyFileDir: path text
Directory name of the signing key files (default: "./signing-keys")
txFileDir: path text
Directory name of the transaction files (default: "./txs")
protocolParamsFile: filepath text
Protocol params file location relative to the cardano-cli working
directory (needed for the cli) in JSON format. (default:
"./protocol.json")
dryRun: `true` or `false`
Dry run mode will build the tx, but skip the submit step (default:
true)
logLevel: `error` or `warn` or `notice` or `info` or `debug`
(default: info)
ownPubKeyHash: PubKeyHash text
(default: "")
ownStakePubKeyHash: `nothing` or StakePubKeyHash text
(default: nothing)
tipPollingInterval: non-negative integral number
(default: 10000000)
forceBudget: `nothing` or ExecutionUnits configuration
Forced budget for scripts, as optional (CPU Steps, Memory Units)
(default: nothing)
port: port non-negative integral number
(default: 9080)
enableTxEndpoint: `true` or `false`
(default: false)

ExecutionUnits configuration
steps: REQUIRED non-negative integral number
This corresponds roughly to the time to execute a script.
memory: REQUIRED non-negative integral number
This corresponds roughly to the peak memory used during script
execution.

SlotConfig configuration
length: REQUIRED integral number
Length (number of milliseconds) of one slot
zeroTime: REQUIRED integral number
Beginning of slot 0 (in milliseconds)
```

To run the fake PAB, you need to prepare a few more things:

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

- **BotPlutusInterface** main entry point
- **BotPlutusInterface.Server** Servant server, handling http endpoint calls and websockets
- **BotPlutusInterface.Config** load/save PAB configuration file
- **BotPlutusInterface.Contract** handling contract effects by creating the necessary files and calling cardano-cli commands (a few effects are mocked)
- **BotPlutusInterface.Balance** doing some preparations so the cli can process the rest (non-ada asset balancing, addig tx inputs, adding minimum lovelaces, add signatories)
- **BotPlutusInterface.CardanoCLI** wrappers for cardano-cli commands
Expand Down
20 changes: 14 additions & 6 deletions bot-plutus-interface.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,27 @@ library
import: common-lang
exposed-modules:
BotPlutusInterface
BotPlutusInterface.Balance
BotPlutusInterface.CardanoCLI
BotPlutusInterface.ChainIndex
BotPlutusInterface.Config
BotPlutusInterface.Contract
BotPlutusInterface.Effects
BotPlutusInterface.Files
BotPlutusInterface.Balance
BotPlutusInterface.Helpers
BotPlutusInterface.Server
BotPlutusInterface.Types
BotPlutusInterface.UtxoParser
BotPlutusInterface.Server
BotPlutusInterface.Helpers

build-depends:
, aeson ^>=1.5.0.0
, attoparsec >=0.13.2.2
, bytestring ^>=0.10.12.0
, cardano-api
, cardano-crypto
, cardano-ledger-alonzo
, config-schema
, config-value
, containers
, data-default
, data-default-class
Expand All @@ -110,6 +114,7 @@ library
, playground-common
, plutus-chain-index
, plutus-chain-index-core
, plutus-config
, plutus-contract
, plutus-core
, plutus-ledger
Expand All @@ -129,6 +134,7 @@ library
, split
, stm
, text ^>=1.2.4.0
, tostring
, transformers
, transformers-either
, unordered-containers
Expand All @@ -145,10 +151,11 @@ test-suite bot-plutus-interface-test
main-is: Spec.hs
ghc-options: -fplugin-opt PlutusTx.Plugin:defer-errors
other-modules:
Spec.BotPlutusInterface.Contract
Spec.BotPlutusInterface.Balance
Spec.BotPlutusInterface.UtxoParser
Spec.BotPlutusInterface.Config
Spec.BotPlutusInterface.Contract
Spec.BotPlutusInterface.Server
Spec.BotPlutusInterface.UtxoParser
Spec.MockContract

build-depends:
Expand Down Expand Up @@ -177,6 +184,7 @@ test-suite bot-plutus-interface-test
, playground-common
, plutus-chain-index
, plutus-chain-index-core
, plutus-config
, plutus-contract
, plutus-core
, plutus-ledger
Expand All @@ -198,8 +206,8 @@ test-suite bot-plutus-interface-test
, tasty-quickcheck
, temporary
, text ^>=1.2.4.0
, uuid
, utf8-string
, uuid
, warp

hs-source-dirs: test
36 changes: 4 additions & 32 deletions examples/plutus-game/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,22 @@
module Main (main) where

import BotPlutusInterface qualified
import BotPlutusInterface.Config qualified as BotPlutusInterface
import BotPlutusInterface.Types (
CLILocation (Local),
HasDefinitions (..),
LogLevel (Debug),
PABConfig (..),
SomeBuiltin (..),
endpointsToSchemas,
)
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
import Cardano.PlutusExample.Game (
GameSchema,
GuessParams,
LockParams,
guess,
lock,
)
import Data.Aeson qualified as JSON
import Data.Aeson.TH (defaultOptions, deriveJSON)
import Data.ByteString.Lazy qualified as LazyByteString
import Data.Default (def)
import Data.Maybe (fromMaybe)
import Playground.Types (FunctionSchema)
import Schema (FormSchema)
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
import Prelude

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

main :: IO ()
main = do
protocolParams <-
fromMaybe (error "protocol.json file not found") . JSON.decode
<$> LazyByteString.readFile "protocol.json"
let pabConf =
PABConfig
{ pcCliLocation = Local
, pcNetwork = Testnet (NetworkMagic 1097911063)
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
, pcPort = 9080
, pcProtocolParams = protocolParams
, pcTipPollingInterval = 10_000_000
, pcSlotConfig = def
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
, pcOwnStakePubKeyHash = Nothing
, pcScriptFileDir = "./scripts"
, pcSigningKeyFileDir = "./signing-keys"
, pcTxFileDir = "./txs"
, pcDryRun = True
, pcLogLevel = Debug
, pcProtocolParamsFile = "./protocol.json"
, pcForceBudget = Just (9_000_000_000, 15_000_000)
, pcEnableTxEndpoint = True
}
pabConf <-
either error id
<$> BotPlutusInterface.loadPABConfig "config/pabConfig.value"
BotPlutusInterface.runPAB @GameContracts pabConf
27 changes: 27 additions & 0 deletions examples/plutus-game/pabConfig.value
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Calling the cli locally or through an ssh connection
cliLocation: local
chainIndexUrl: "http://localhost:9083"
networkId: 1097911063
-- Slot configuration of the network, the default value can be used for the mainnet
slotConfig:
length: 1000
zeroTime: 1596059091000
-- Directory name of the script and data files
scriptFileDir: "./scripts"
-- Directory for the signing key file(s)
signingKeyFileDir: "./signing-keys"
-- Directory where the encoded transaction files will be saved
txFileDir: "./txs"
-- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
protocolParamsFile: "./config/protocol.json"
-- Dry run mode will build the tx, but skip the submit step
dryRun: true
logLevel: debug
ownPubKeyHash: "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
ownStakePubKeyHash: nothing
tipPollingInterval: 10000000
forceBudget:
steps: 9000000000
memory: 15000000
port: 9080
enableTxEndpoint: true
2 changes: 1 addition & 1 deletion examples/plutus-game/plutus-game.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ library
build-depends:
, aeson ^>=1.5.0.0
, attoparsec >=0.13.2.2
, bot-plutus-interface
, bytestring ^>=0.10.12.0
, cardano-api
, cardano-crypto
, cardano-ledger-alonzo
, containers
, bot-plutus-interface
, data-default
, data-default-class
, directory
Expand Down
36 changes: 4 additions & 32 deletions examples/plutus-nft/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@
module Main (main) where

import BotPlutusInterface qualified
import BotPlutusInterface.Config qualified as BotPlutusInterface
import BotPlutusInterface.Types (
CLILocation (Local),
HasDefinitions (..),
LogLevel (Debug),
PABConfig (..),
SomeBuiltin (..),
endpointsToSchemas,
)
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
import Cardano.PlutusExample.NFT (
NFTSchema,
mintNft,
)
import Data.Aeson qualified as JSON
import Data.Aeson.TH (defaultOptions, deriveJSON)
import Data.ByteString.Lazy qualified as LazyByteString
import Data.Default (def)
import Data.Maybe (fromMaybe)
import Ledger.Value (TokenName)
import Playground.Types (FunctionSchema)
import Schema (FormSchema)
import Servant.Client.Core (BaseUrl (BaseUrl), Scheme (Http))
import Prelude

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

main :: IO ()
main = do
protocolParams <-
fromMaybe (error "protocol.json file not found") . JSON.decode
<$> LazyByteString.readFile "protocol.json"
let pabConf =
PABConfig
{ pcCliLocation = Local
, pcNetwork = Testnet (NetworkMagic 1097911063)
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
, pcPort = 9080
, pcProtocolParams = protocolParams
, pcTipPollingInterval = 10_000_000
, pcSlotConfig = def
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
, pcOwnStakePubKeyHash = Nothing
, pcScriptFileDir = "./scripts"
, pcSigningKeyFileDir = "./signing-keys"
, pcTxFileDir = "./txs"
, pcDryRun = True
, pcLogLevel = Debug
, pcProtocolParamsFile = "./protocol.json"
, pcForceBudget = Just (1000, 1000)
, pcEnableTxEndpoint = True
}
pabConf <-
either error id
<$> BotPlutusInterface.loadPABConfig "config/pabConfig.value"
BotPlutusInterface.runPAB @MintNFTContracts pabConf
Loading