Skip to content

Commit

Permalink
feat: add teardown script
Browse files Browse the repository at this point in the history
  • Loading branch information
parketh committed Nov 9, 2024
1 parent cc1a401 commit 58120ec
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/publish-babylon-deployment-utils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- main
# TODO: remove this temp branch once merged
- feat/teardown-script
paths:
- 'scripts/babylon-integration/utils/**'

Expand Down
12 changes: 12 additions & 0 deletions docker/docker-compose-babylon-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ services:
- -c
- /toggle-cw-killswitch.sh

teardown:
container_name: teardown
image: # TODO: add this
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
- ${PWD}/.deploy/babylond:/home/.babylond
entrypoint:
- /bin/bash
- -c
- /teardown.sh

btc-staker:
container_name: btc-staker
# https://github.com/babylonlabs-io/btc-staker/commit/484bcb8fd9b7b0b525234d704dd049b1ef18e29f
Expand Down
5 changes: 5 additions & 0 deletions scripts/babylon-integration/teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail

docker compose -f docker/docker-compose-babylon-integration.yml up -d teardown
docker logs -f teardown
3 changes: 2 additions & 1 deletion scripts/babylon-integration/utils/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ COPY set-babylon-keys.sh /set-babylon-keys.sh
COPY register-consumer-chain.sh /register-consumer-chain.sh
COPY common.sh /common.sh
COPY deploy-cw-contract.sh /deploy-cw-contract.sh
COPY toggle-cw-killswitch.sh /toggle-cw-killswitch.sh
COPY toggle-cw-killswitch.sh /toggle-cw-killswitch.sh
COPY teardown.sh /teardown.sh
64 changes: 64 additions & 0 deletions scripts/babylon-integration/utils/teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
set -uo pipefail

source "./common.sh"

# Get the consumer FP address
KEYRING_FILENAME=$(ls .consumer-finality-provider/keyring-test | grep '\.address$')
CONSUMER_FP_ADDRESS=$(babylond keys parse "$KEYRING_FILENAME" | grep -o '^[^- ]*' | head -n 1)
echo "Consumer FP address: $CONSUMER_FP_ADDRESS"

# Get the prefunded key address
KEYRING_DIR=/home/.babylond
PREFUNDED_ADDRESS=$(babylond keys show "$BABYLON_PREFUNDED_KEY" \
--keyring-dir "$KEYRING_DIR" \
--keyring-backend test \
--output json \
| jq -r '.address')
echo "Prefunded address: $PREFUNDED_ADDRESS"

# Check remaining balance
CONSUMER_FP_BALANCE=$(babylond query bank balances "$CONSUMER_FP_ADDRESS" \
--node "$BABYLON_RPC_URL" \
-o json | jq -r '.balances[0].amount')
echo "Consumer FP balance: $CONSUMER_FP_BALANCE"

# If balance is less than gas transfer cost, don't send funds
TRANSFER_GAS_COST=1000
if [ "$CONSUMER_FP_BALANCE" -lt "$TRANSFER_GAS_COST" ]; then
echo "Consumer FP balance is less than gas transfer cost, skipping funds transfer"
exit 0
fi

# Otherwise, send out funds to prefunded key
# Reserve 0.001 bbn = 1000 ubbn for gas
AMOUNT_TO_SEND=$((CONSUMER_FP_BALANCE - TRANSFER_GAS_COST))
echo "Sending funds to prefunded key..."
SEND_TX_HASH=$(babylond tx bank send "$CONSUMER_FP_ADDRESS" "$PREFUNDED_ADDRESS" "$AMOUNT_TO_SEND"ubbn \
--keyring-dir "$KEYRING_DIR" \
--keyring-backend test \
--chain-id "$BABYLON_CHAIN_ID" \
--node "$BABYLON_RPC_URL" \
--gas auto \
--gas-adjustment 1.5 \
--gas-prices 0.2ubbn \
--dry-run \ # TODO: remove this once ready
--output json -y \
| jq -r '.txhash')


# Wait for transaction to complete
if ! wait_for_tx "$SEND_TX_HASH" 10 3; then
echo "Failed to send funds back to prefunded key"
exit 1
fi

echo "Successfully sent $AMOUNT_TO_SEND ubbn back to prefunded key"
echo "Transaction hash: $SEND_TX_HASH"
echo

# Verify final balance
FINAL_BALANCE=$(babylond query bank balances "$CONSUMER_FP_ADDRESS" \
--node "$BABYLON_RPC_URL" \
-o json | jq -r '.balances[0].amount')
echo "Final consumer FP balance: $FINAL_BALANCE"

0 comments on commit 58120ec

Please sign in to comment.