Skip to content

Instantly share code, notes, and snippets.

@mikekeke
Last active January 25, 2023 17:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mikekeke/883d56c38e0237444ac98ae5257e174f to your computer and use it in GitHub Desktop.
Save mikekeke/883d56c38e0237444ac98ae5257e174f to your computer and use it in GitHub Desktop.
PAB setup

Hosted PAB setup

This document describes how to prepare hosted PAB deployment from scratch that can operate on Alonzo purple testnet.

The following required to be run on host machine to use PAB with contracts in hosted scenario on testnet (for 2020-11-05 PAB Release):

  • Cardano node connected to Alonzo testnet
  • cardano wallet connected to node
  • chain-index connected to node
  • PAB executable connected to node, cardano-wallet and chain-index

Note: we are using somewhat more involved setup with a bit custom docker compose, and wallet and chain-index being built from sources, but there is ready to go node+wallet docker solution available

Starting cardano node

This section describes how to start Cardano node connected to Alonzo testnet using Docker (and docker-compose).

Step 1

Use official cardano node image to run contaner with docker:

docker run --rm \
  -e NETWORK=testnet \
  -v "$PWD"/socket:/ipc \
  -v "$PWD"/data:/data \
  inputoutput/cardano-node

or for specific node version

docker run --rm \
  -e NETWORK=testnet \
  -v "$PWD"/socket:/ipc \
  -v "$PWD"/data:/data \
  inputoutput/cardano-node:1.31.0

It will take some time for node to sync. Sync status could be checked with cardano-cli tool using tip query, e.g.:

cardano-cli query tip --testnet-magic 1097911063

Tip of synced node looks something like this (note syncProgress):

{
    "epoch": 1005,
    "hash": "162d6541cc5aa6b0e098add8fa08a94660a08b9463c0a86fcf84661b5f63375f",
    "slot": 7232440,
    "block": 322985,
    "era": "Alonzo",
    "syncProgress": "100.00"
}

Appendix

Starting cardano wallet

ℹ️ GtiHub repo

Step 1

Install cardano wallet and start it.

cardano-wallet serve \
  --node-socket $CARDANO_NODE_SOCKET_PATH \
  --database /path/to/wallet/database \
  --testnet /path/to/node/configuration/config/alonzo-purple-byron-genesis.json \
  --listen-address 0.0.0.0 \
  --port 8090

Step 2

Create wallet for tests (which PAB will use). We are usually using CLI to generate recovery-phrase and then create wallet from-recovery-phrase.

Step 3

Get some tAda on that wallet (as one possible option: https://testnets.cardano.org/en/testnets/cardano/tools/faucet/)

Start chain-index

We building chain-index from sources from desired commit of plutus-apps and staring it, like: Fast way with nix build (ty @ngua)

cd plutus-apps

nix build -f default.nix \ 
  plutus-apps.haskell.packages.plutus-chain-index.components.exes.plutus-chain-index

./result/bin/plutus-chain-index --socket-path $CARDANO_NODE_SOCKET_PATH \ 
  --db-path /path/to/chain-index.db --network-id 1097911063 start-index

or with cabal from nix-shell

cd plutus-apps
nix-shell
cd plutus-chain-index
cabal build
cabal run exe:plutus-chain-index -- --socket-path $CARDANO_NODE_SOCKET_PATH --db-path /path/to/chain-index.db --network-id 1097911063 start-index

Full config could be also provided with --config flag, can be found here: https://github.com/input-output-hk/plutus-apps/blob/main/plutus-pab/test-node/testnet/chain-index-config.json

It will probably take some time for chain-index to sync. Request to localhost:chain-index-port/tip can be performed to see current status.

Start PAB

Step 1

Add PAB executable (example) and build it.

Step 2

Prepare config (sample) - here you specify where PAB should send requests to cardano-wallet, chain-index and node socket. We currently have test config like this as example (note mscNodeMode: AlonzoNode):

dbConfig:
    dbConfigFile: pab-core.db
    dbConfigPoolSize: 20

pabWebserverConfig:
  baseUrl: http://localhost:9080
  staticDir: ./dist
  permissiveCorsPolicy: False
  # Optional timeout (in seconds) for calls to endpoints that are not currently
  # available. If this is not set, calls to unavailable endpoints fail
  # immediately.
  endpointTimeout: 5

walletServerConfig:
  baseUrl: http://localhost:8090
  wallet:
    getWallet: 1

nodeServerConfig:
  mscBaseUrl: "localhost"
  mscSocketPath: /our/path/to/node.socket
  mscKeptBlocks: 100
  mscNetworkId: "1097911063" # Testnet network ID (main net = empty string)
  mscSlotConfig:
    scSlotZeroTime: 1591566291000 # Wednesday, July 29, 2020 21:44:51 - shelley launch time in milliseconds
    scSlotLength: 1000 # In milliseconds
  mscFeeConfig:
    fcConstantFee:
      getLovelace: 10 # Constant fee per transaction in lovelace
    fcScriptsFeeFactor: 1.0 # Factor by which to multiply size-dependent scripts fee in lovelace
  mscInitialTxWallets:
    - getWallet: 1
    - getWallet: 2
    - getWallet: 3
  mscNodeMode: AlonzoNode

chainIndexConfig:
  ciBaseUrl: http://localhost:9083
  ciWatchedAddresses: []

requestProcessingConfig:
  requestProcessingInterval: 1

signingProcessConfig:
  spBaseUrl: http://localhost:9084
  spWallet:
    getWallet: 1

metadataServerConfig:
  mdBaseUrl: http://localhost:9085

# Optional EKG Server Config
# ----
# monitoringConfig:
#   monitoringPort: 9090

Now you cas start PAB and start serving your contracts at pabWebserverConfig.baseUrl, e.g.:

cabal exec my-dapp -- --config ./my-dapp/plutus-pab.yaml migrate (creates database)  
cabal exec my-dapp -- --config ./my-dapp/plutus-pab.yaml --passphrase WALLET_PASSPHRASE webserver  

For WALLET_PASSPHRASE specify passphrase of wallet created earlier with cardano-wallet.

After PAB started (watch for Starting PAB backend server on port 9080 in logs) contracts can be activated. E.g.:

curl --location --request POST 'localhost:9080/api/contract/activate' \
--header 'Content-Type: application/json' \
--data-raw '{
    "caID": {
        "tag": "UserContract",
        "contents": {
            "someParam": "param"
        }
    },
    "caWallet": {
        "getWalletId": "WALLET_ID"
    }
}'

(for WALLET_ID we are using wallet which passphrase was provided to start PAB, as that wallet will be doing transaction signing)

@bhoudebert
Copy link

bhoudebert commented Nov 15, 2021

@mikekeke

  • I use dockerfile from wallet
  • start index from pab repo
  • start pab

Everything seems ok, wallet creation, fund with faucet ok btw slow to sync but ok, contract activation but then as soon as PAB is sending a message to wallet, when I trigger a smart contract endpoint I have something like this (copy paste from contract status)
NB: smart contract used was lock & guess to avoid saying that was my code :p

"FailureResponse (Request {requestPath = (BaseUrl {baseUrlScheme = Http, baseUrlHost = \"localhost\", baseUrlPort = 8090, baseUrlPath = \"\"},\"/v2/wallets/.../transactions-balance\"), requestQueryString = fromList [], requestBody = Just ((),application/json;charset=utf-8), requestAccept = fromList [application/json;charset=utf-8,application/json], requestHeaders = fromList []), requestHttpVersion = HTTP/1.1, requestMethod = \"POST\"} (Response {responseStatusCode = Status {statusCode = 400, statusMessage = \"Bad Request\"}, responseHeaders = fromList [(\"Transfer-Encoding\",\"chunked\"),(\"Date\",\"Mon, 15 Nov 2021 08:56:47 GMT\"),(\"Server\",\"Warp/3.3.17\"),(\"Content-Type\",\"application/json;charset=utf-8\")], responseHttpVersion = HTTP/1.1, responseBody = \"{\\\"code\\\":\\\"bad_request\\\",\\\"message\\\":\\\"Error in $.transaction: parsing 'Base64 ByteString failed, expected String, but encountered Object\\\"}\"})"

To me it seems that pab has created the balanceTx but when he tried to send it to the wallet it fails as the contract is not yet right between the two.

@ngua
Copy link

ngua commented Nov 15, 2021

To speed up the chain-index step, you could also use Nix as follows:

cd plutus-apps

nix build -f default.nix \ 
  plutus-apps.haskell.packages.plutus-chain-index.components.exes.plutus-chain-index

./result/bin/plutus-chain-index --socket-path $CARDANO_NODE_SOCKET_PATH \ 
  --db-path /path/to/chain-index.db --network-id 8 start-index

@mikekeke
Copy link
Author

mikekeke commented Nov 15, 2021

@bhoudebert
It looks like JSON representation of partial transaction that PAB generates doesn't match to what cardano-wallet expects. Could be some versions mismatch?

@bhoudebert
Copy link

bhoudebert commented Nov 15, 2021

@bhoudebert It looks like JSON representation of partial transaction that PAB generates doesn't match to what cardano-wallet expects

@mikekeke yes it was due to plutus-starter out of date regarding plutus-apps! I fixed it (cabal-project pointing to previous tag) and it seems working fine.

I use a simple game contract so not a lot of things are involved but that is a nice start.

@bhoudebert
Copy link

bhoudebert commented Nov 15, 2021

Some feedback:

  • right issue on socket from docker, could be maybe fixed directly in compose
  • chain index is not really working? Lots of error like this [chain-index:Error:45] [2021-11-15 18:35:28.53 UTC] {"contents":{"contents":"SQLite3 returned ErrorMisuse while attempting to perform prepare \"BEGIN TRANSACTION\": bad parameter or other API misuse","tag":"BeamEffectError"},"tag":"Err"}
  • Cannot test is further ... recovering my wallet did not work resulting in ADA lost ;p

@bhoudebert
Copy link

Cannot move forward at the moment, my wallet on cardano explorer is provided with ADA still local wallet from cardano-wallet is not ...

@mikekeke
Copy link
Author

mikekeke commented Nov 16, 2021

@bhoudebert

This setup I've posted could be a bit outdated, as it uses testnet magic 8, and current public testnet is 1097911063. I will update it asap.
As quickfix: it is better to run node with

docker run \
  -e NETWORK=testnet \
  -v node/socket:/ipc \
  -v node/data:/data \
  inputoutput/cardano-node

It will use public testnet network-id 1097911063

And then run chain index using --network-id 1097911063, or use full config instead of just --network-id: https://github.com/input-output-hk/plutus-apps/blob/main/plutus-pab/test-node/testnet/chain-index-config.json
GitHub
plutus-apps/chain-index-config.json at main · input-output-hk/plutu...
The Plutus application platform. Contribute to input-output-hk/plutus-apps development by creating an account on GitHub.
That's why, probably, you don't see finds from faucet - your wallet in another network. We already had wallets with funds on network 8, so I never used faucet for them. Test wallet, probably, has to be recreated. Sorry for inconveniences 🙄

@bhoudebert
Copy link

bhoudebert commented Nov 16, 2021

@mikekeke I have recreated a node from the last available config from https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/index.html

It is pulling again everything from the internet ;D I have now to wait.

Really it is overwhelming maybe because I came from plutus pioneer and not cardano background (still in the learning curve), I hope I will find some answer on how ultimately to pass a tx as a "client" not as PAB wallet ;D

Anyway thanks for all the tips

@bhoudebert
Copy link

Got my ADA back nice! I really had to wait a long time even with sync status.

Like others said, chain sync is OMG so slow to sync still not done at this point.

For signing I do not think it is in fact ready in PAB, have a look:

So maybe I am wrong but against a real node/wallet noone is using any wallet signing except the one used by PAB.

@bhatt-deep
Copy link

It would be better If you also include the way to interact with the contract endpoints, I am facing some problems while trying to call "start" endpoint of UniswapOwner contract.

@bhoudebert
Copy link

@bhatt-deep outside Simulator or not?

I tried to summarize as simple as possible the current/my situation over there IntersectMBO/plutus-apps#121

So far no luck, it is also referenced it on Cardano forum but it really seems a not so crowd place...

@mikekeke
Copy link
Author

mikekeke commented Nov 18, 2021

@bhoudebert
They way PAB beta release works now is that cardano wallet integration allows to use only one wallet to sign transactions - the one which passphrase you use to start PAB.

@bhoudebert
Copy link

@bhoudebert They way PAB beta release works now is that cardano wallet integration allow to use only one wallet to sign transactions - the one which passphrase you use to start PAB.

Okay that was my assumption sadly :'( thanks for the confirmation

@bhoudebert
Copy link

@mikekeke
Copy link
Author

mikekeke commented Nov 23, 2021

@bhoudebert thanks a lot for sharing info!

@Shahrozpluton
Copy link

If i deploy the contract on mainnet, how do i use the pab for integration on my website?

@mikekeke
Copy link
Author

mikekeke commented Nov 10, 2022

@Shahrozpluton
There are several deployment scenarios for PAB, see here. AFAIK all of them imply that you run some infrastructure services at your side, including PAB itself as web-server in some scenarios.

When PAB is run as web-service, you site can interact with it (and Contract endpoints it provides) via REST API.
In-browser version of PAB is under development.

Also, check out CTL - it runs in browser, has a lot of features, light wallets support and well maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment