Skip to content

Commit

Permalink
feat: teardown script (#42)
Browse files Browse the repository at this point in the history
* feat: add teardown script

* chore: set teardown script image

* update: permissions

* chore: update docker image

* update docker image

* fix script

* set docker image

* fix keyring filename

* set docker image

* fix address extraction

* set docker image

* fix: send tx error

* update: docker image

* fix script

* set docker image

* feat: update makefile

* delete dry run

* chore: update image

* fix gas cost

* set docker image

* add back gas params

* remove duplicate volume

* print tx output

* update docker images

* chore: remove temp branch from docker ci

* simplify commands

* nit: cleanup script

* publish new image
  • Loading branch information
parketh authored Nov 12, 2024
1 parent 45c4a16 commit f30cfac
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ register-consumer-chain:

toggle-cw-killswitch:
@./scripts/babylon-integration/toggle-cw-killswitch.sh
.PHONY: toggle-cw-killswitch
.PHONY: toggle-cw-killswitch

teardown:
@./scripts/babylon-integration/teardown.sh
.PHONY: teardown
20 changes: 16 additions & 4 deletions docker/docker-compose-babylon-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
# This is a one-off container just for setting the Babylon keys
set-babylon-keys:
container_name: set-babylon-keys
image: snapchain/babylon-deployment-utils:3c5e38e
image: snapchain/babylon-deployment-utils:ed6e196
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
Expand All @@ -15,7 +15,7 @@ services:
# This is a one-off container just for registering the consumer chain
register-consumer-chain:
container_name: register-consumer-chain
image: snapchain/babylon-deployment-utils:3c5e38e
image: snapchain/babylon-deployment-utils:ed6e196
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
Expand All @@ -28,7 +28,7 @@ services:
# This is a one-off container just for deploying cw contract
deploy-cw-contract:
container_name: deploy-cw-contract
image: snapchain/babylon-deployment-utils:3c5e38e
image: snapchain/babylon-deployment-utils:ed6e196
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
Expand All @@ -42,7 +42,7 @@ services:
# This is a one-off container just for setting the finality contract enabled value
toggle-cw-killswitch:
container_name: toggle-cw-killswitch
image: snapchain/babylon-deployment-utils:3c5e38e
image: snapchain/babylon-deployment-utils:ed6e196
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
Expand All @@ -53,6 +53,18 @@ services:
- -c
- /toggle-cw-killswitch.sh

teardown:
container_name: teardown
image: snapchain/babylon-deployment-utils:ed6e196
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
69 changes: 69 additions & 0 deletions scripts/babylon-integration/utils/teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash
set -uo pipefail

source "./common.sh"

# Get the consumer FP address
KEYRING_DIR=/home/.babylond
CONSUMER_FP_KEYRING_DIR=$KEYRING_DIR/$CONSUMER_FINALITY_PROVIDER_KEY
CONSUMER_FP_ADDRESS=$(babylond keys show -a consumer-finality-provider \
--keyring-dir $CONSUMER_FP_KEYRING_DIR \
--keyring-backend test)
echo "Consumer FP address: $CONSUMER_FP_ADDRESS"

# Get the prefunded key address
PREFUNDED_ADDRESS=$(babylond keys show -a "$BABYLON_PREFUNDED_KEY" \
--keyring-dir "$KEYRING_DIR" \
--keyring-backend test)
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=100000
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 $AMOUNT_TO_SEND ubbn to prefunded key..."
echo "Consumer FP keyring dir: $CONSUMER_FP_KEYRING_DIR"
SEND_TX_OUTPUT=$(babylond tx bank send \
${CONSUMER_FINALITY_PROVIDER_KEY} \
${PREFUNDED_ADDRESS} \
"${AMOUNT_TO_SEND}ubbn" \
--keyring-dir $CONSUMER_FP_KEYRING_DIR \
--keyring-backend test \
--chain-id $BABYLON_CHAIN_ID \
--node $BABYLON_RPC_URL \
--gas auto \
--gas-adjustment 1.5 \
--gas-prices 0.2ubbn \
--output json -y)
echo "$SEND_TX_OUTPUT"
SEND_TX_HASH=$(echo "$SEND_TX_OUTPUT" | 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 = initial balance - amount sent
FINAL_BALANCE=$(babylond query bank balances "$CONSUMER_FP_ADDRESS" \
--node "$BABYLON_RPC_URL" \
-o json | jq -r '.balances[0].amount')
echo "Initial consumer FP balance: $CONSUMER_FP_BALANCE"
echo "Final consumer FP balance: $FINAL_BALANCE"

0 comments on commit f30cfac

Please sign in to comment.