|
| 1 | +#! /usr/bin/env nix-shell |
| 2 | +# -*-Shell-script-*- |
| 3 | +#! nix-shell -i bash ../shell.nix --pure --arg pure true |
| 4 | + |
| 5 | +set -eEuo pipefail |
| 6 | + |
| 7 | +cabal update |
| 8 | + |
| 9 | +cabal build plutus-starter-pab |
| 10 | + |
| 11 | +# We run plutus-starter-pab under a coproc to enable backgrounding when in an interactive shell |
| 12 | +coproc cabal exec -- plutus-starter-pab |
| 13 | +kill_pab() { |
| 14 | + echo >&${COPROC[1]} |
| 15 | + |
| 16 | + wait |
| 17 | +} |
| 18 | +trap kill_pab EXIT |
| 19 | + |
| 20 | +# Start a background loop to print out the output from plutus-starter-pab |
| 21 | +exec 3<&${COPROC[0]} |
| 22 | +(while IFS= read -u 3 -r line; do echo $line; done) & |
| 23 | + |
| 24 | +wait_with_backoff() { |
| 25 | + cmd="$1" |
| 26 | + msg="$2" |
| 27 | + final_cmd="${3:-$cmd}" |
| 28 | + up=0 |
| 29 | + for ((i=0;i<=6;i++)) |
| 30 | + do |
| 31 | + if eval "$cmd" |
| 32 | + then |
| 33 | + up=1 |
| 34 | + break |
| 35 | + fi |
| 36 | + let delay=2**i |
| 37 | + echo "$msg, sleeping for ${delay} seconds then trying again" >&2 |
| 38 | + sleep "${delay}" |
| 39 | + done |
| 40 | + if [[ "$up" == "0" ]] |
| 41 | + then |
| 42 | + eval "$final_cmd" |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +wait_with_backoff "curl --fail http://localhost:9080/api/contract/definitions &>/dev/null" "Connecting to PAB failed" "curl --fail http://localhost:9080/api/contract/definitions" |
| 47 | + |
| 48 | +play() { |
| 49 | + secret="$1" |
| 50 | + guess="$2" |
| 51 | + |
| 52 | + WALLET_ID=$(curl --silent --fail --data '' http://localhost:9080/wallet/create | jq -r '.wiWallet.getWalletId') |
| 53 | + wait_with_backoff '[[ $(curl --fail --silent http://localhost:9080/wallet/$WALLET_ID/total-funds | jq '\''.getValue|length'\'') != "0" ]]' "Wallet not populated" |
| 54 | + |
| 55 | + CONTRACT_ID=$(curl --fail --silent \ |
| 56 | + --header "Content-Type: application/json" \ |
| 57 | + --data '{"caID": "GameContract", "caWallet":{"getWalletId": "'$WALLET_ID'"}}' \ |
| 58 | + http://localhost:9080/api/contract/activate | jq -r '.unContractInstanceId') |
| 59 | + |
| 60 | + log_count=0 |
| 61 | + wait_for_contract() { |
| 62 | +wait_with_backoff 'curl --fail --silent http://localhost:9080/api/contract/instance/'$CONTRACT_ID'/status > status.json && [[ "$(jq -r '\''.cicCurrentState.logs | length'\'' < status.json)" -gt "'$log_count'" && ("$(jq -r .cicCurrentState.lastLogs[0]._logMessageContent < status.json)" == "Waiting for guess or lock endpoint..." || "$(jq -r .cicStatus < status.json)" == "Done") ]]' "Contract not ready" |
| 63 | +log_count="$(jq -r '.cicCUrrentState.logs | length' <status.json)" |
| 64 | + } |
| 65 | + |
| 66 | + wait_for_contract |
| 67 | + |
| 68 | + curl --fail --silent --header "Content-Type: application/json" \ |
| 69 | + --data '{"amount":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},90]]]]},"secretWord":"'"$secret"'"}' \ |
| 70 | + http://localhost:9080/api/contract/instance/$CONTRACT_ID/endpoint/lock |
| 71 | + wait_for_contract |
| 72 | + |
| 73 | + curl --fail -s -H "Content-Type: application/json" \ |
| 74 | + --request POST \ |
| 75 | + --data '{"guessWord": "'"$guess"'"}' \ |
| 76 | + http://localhost:9080/api/contract/instance/$CONTRACT_ID/endpoint/guess |
| 77 | + wait_for_contract |
| 78 | + |
| 79 | + jq -r < status.json |
| 80 | +} |
| 81 | + |
| 82 | +play "eagle" "duck" |
| 83 | + |
| 84 | +play "lion" "lion" |
0 commit comments