Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ Supported features:
- pre balance tx (adding minimum amount of tx inputs based on fee and tx output value, balancing non ada outputs)
- mint tokens, and send them to arbitrary address(es)
- redeem utxos from validator scripts, using the correct datum and redeemer (scripts, datums and redeemers are persisted in files for now)
- use validity time ranges
- waiting for slots

Unsupported/In development

- wallet integration
- chain-index integration (in progress)
- handling on-chain events (utxo set change, waiting for slot, etc.)
- multisig
- automated tests
- handling on-chain events (utxo set change, etc.)

## How to use this?

Expand All @@ -40,6 +39,10 @@ data MyContracts
2. Define a HasDefinitions instance for the endpoints

```haskell
import BotPlutusInterface.Types (HasDefinitions (..), SomeBuiltin (..), endpointsToSchemas)
import Playground.Types (FunctionSchema)
import Schema (FormSchema)

instance HasDefinitions MyContracts where
getDefinitions :: [MyContract]
getDefinitions = []
Expand All @@ -62,21 +65,38 @@ instance HasDefinitions MyContracts where
3. Write your main entrypoint for the application, with the preferred configurations

```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))

main :: IO ()
main = do
protocolParams <- JSON.decode <$> LazyByteString.readFile "protocol.json"
let pabConf =
PABConfig
{ -- Calling the cli through ssh when set to Remote
pcCliLocation = Remote "11.22.33.44"
{ -- Calling the cli locally or through an ssh connection
pcCliLocation = Local
, pcNetwork = Testnet (NetworkMagic 42)
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
, pcPort = 9080
, pcProtocolParams = protocolParams
, -- | 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 = "result-scripts"
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
, -- Protocol params file location relative to the cardano-cli working directory (needed for the cli)
pcProtocolParamsFile = "./protocol.json"
, pcProtocolParamsFile = "./protocol.json"
}
BotPlutusInterface.runPAB @MyContracts pabConf
```
Expand Down Expand Up @@ -107,7 +127,7 @@ The fake PAB consists of the following modules:
- **BotPlutusInterface.Contract** handling contract effects by creating the necessary files and calling cardano-cli commands (a few effects are mocked)
- **BotPlutusInterface.PreBalance** 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
- For development purposes, I created an ssh wrapper, so I can call relay these commands through an ssh connection. This is not nice, unsafe, and pretty slow, so I'm hoping to get rid of this pretty soon.
- For development purposes, I created an ssh wrapper, so I can call relay these commands through an ssh connection. This is not nice, unsafe, and pretty slow, avoid using it if you can.
- **BotPlutusInterface.UtxoParser** parse the output of the `cardano-cli query utxo` command
- **BotPlutusInterface.Files** functions for handling script, datum and redeemer files
- **BotPlutusInterface.Types** configuration for the fake pab
Expand Down
8 changes: 2 additions & 6 deletions examples/plutus-game/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
### plutus-helloworld
### plutus-game

This directory contains a simple "Hello World" script. There are two versions: one using an integer literal (needed because the Plutus interpreter doesn't currently accept byte string literals) and a slighly more complicated one using a bytestring parameter.

``plutus-helloworld'' -- very simple numeric version

``plutus-helloworld-bytestring'' -- more compex version using bytestring constant
Simple guessing game contract to demonstrate locking and redeeming funds with datums and redeemers
2 changes: 2 additions & 0 deletions examples/plutus-game/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Cardano.PlutusExample.Game (
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)
Expand Down Expand Up @@ -57,6 +58,7 @@ main = do
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
, pcPort = 9080
, pcProtocolParams = protocolParams
, pcSlotConfig = def
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
, pcScriptFileDir = "./scripts"
, pcSigningKeyFileDir = "./signing-keys"
Expand Down
1 change: 1 addition & 0 deletions examples/plutus-game/plutus-game.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ executable plutus-game-pab
, bot-plutus-interface
, bytestring
, cardano-api
, data-default
, playground-common
, plutus-game
, plutus-ledger
Expand Down
8 changes: 2 additions & 6 deletions examples/plutus-nft/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
### plutus-helloworld
### plutus-nft

This directory contains a simple "Hello World" script. There are two versions: one using an integer literal (needed because the Plutus interpreter doesn't currently accept byte string literals) and a slighly more complicated one using a bytestring parameter.

``plutus-helloworld'' -- very simple numeric version

``plutus-helloworld-bytestring'' -- more compex version using bytestring constant
Simple NFT schema to demonstrate token minting.
2 changes: 2 additions & 0 deletions examples/plutus-nft/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Cardano.PlutusExample.NFT (
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)
Expand Down Expand Up @@ -57,6 +58,7 @@ main = do
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
, pcPort = 9080
, pcProtocolParams = protocolParams
, pcSlotConfig = def
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
, pcScriptFileDir = "./scripts"
, pcSigningKeyFileDir = "./signing-keys"
Expand Down
1 change: 1 addition & 0 deletions examples/plutus-nft/plutus-nft.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ executable plutus-nft-pab
, bot-plutus-interface
, bytestring
, cardano-api
, data-default
, playground-common
, plutus-ledger
, plutus-nft
Expand Down
3 changes: 3 additions & 0 deletions examples/plutus-transfer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scripts
signing-keys
txs
5 changes: 5 additions & 0 deletions examples/plutus-transfer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### plutus-transfer

Simple value transfer from an address to multiple addresses. With the tfpOutputPerTx option,
payment tx outputs can be grouped together into separate txs, the Contract waits for at least one
block in between.
19 changes: 19 additions & 0 deletions examples/plutus-transfer/ada-transfer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
CONTRACT_INST_ID=$(curl --location --request POST 'localhost:9080/api/contract/activate' \
--header 'Content-Type: application/json' \
--data-raw '{
"caID": {
"tfpOutputPerTx": 50,
"tfpPayments": [
[ {"getPubKeyHash": "981fc565bcf0c95c0cfa6ee6693875b60d529d87ed7082e9bf03c6a4"},
{"getValue": [[{"unCurrencySymbol":""},[[{"unTokenName": ""}, 1500000]]]]}
]
]
}
}' | jq -r .unContractInstanceId )

echo $CONTRACT_INST_ID


echo "{ \"tag\": \"Subscribe\", \"contents\": { \"Left\": { \"unContractInstanceId\":\"$CONTRACT_INST_ID\" } } }" | websocat -n ws://localhost:9080/ws

69 changes: 69 additions & 0 deletions examples/plutus-transfer/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TemplateHaskell #-}

module Main (main) where

import BotPlutusInterface qualified
import BotPlutusInterface.Types (
CLILocation (Local),
HasDefinitions (..),
LogLevel (Debug),
PABConfig (..),
SomeBuiltin (..),
endpointsToSchemas,
)
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
import Cardano.PlutusExample.Transfer (
TransferParams,
TransferSchema,
transfer,
)
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 TransferContracts where
getDefinitions :: [TransferContracts]
getDefinitions = []

getSchema :: TransferContracts -> [FunctionSchema FormSchema]
getSchema _ = endpointsToSchemas @TransferSchema

getContract :: (TransferContracts -> SomeBuiltin)
getContract = \case
Transfer payments ->
SomeBuiltin $ transfer payments

newtype TransferContracts = Transfer TransferParams
deriving stock (Show)

$(deriveJSON defaultOptions ''TransferContracts)

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
, pcSlotConfig = def
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
, pcScriptFileDir = "./scripts"
, pcSigningKeyFileDir = "./signing-keys"
, pcTxFileDir = "./txs"
, pcDryRun = True
, pcLogLevel = Debug
, pcProtocolParamsFile = "./protocol.json"
}
BotPlutusInterface.runPAB @TransferContracts pabConf
14 changes: 14 additions & 0 deletions examples/plutus-transfer/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Bump this if you need newer packages
index-state: 2021-10-20T00:00:00Z

packages:
./.
../../.

-- You never, ever, want this.
write-ghc-environment-files: never

-- Always build tests and benchmarks.
tests: true
benchmarks: true

7 changes: 7 additions & 0 deletions examples/plutus-transfer/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cradle:
cabal:
- path: "./src"
component: "lib:plutus-nft"

- path: "./app"
component: "exe:plutus-nft-pab"
Loading