From f30cfacbd8a8298123b7c00db93ad8545642c024 Mon Sep 17 00:00:00 2001 From: parketh Date: Tue, 12 Nov 2024 16:19:09 +0000 Subject: [PATCH] feat: teardown script (#42) * 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 --- Makefile | 6 +- docker/docker-compose-babylon-integration.yml | 20 ++++-- scripts/babylon-integration/teardown.sh | 5 ++ scripts/babylon-integration/utils/Dockerfile | 3 +- scripts/babylon-integration/utils/teardown.sh | 69 +++++++++++++++++++ 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100755 scripts/babylon-integration/teardown.sh create mode 100755 scripts/babylon-integration/utils/teardown.sh diff --git a/Makefile b/Makefile index d9edad8..5d94905 100644 --- a/Makefile +++ b/Makefile @@ -96,4 +96,8 @@ register-consumer-chain: toggle-cw-killswitch: @./scripts/babylon-integration/toggle-cw-killswitch.sh -.PHONY: toggle-cw-killswitch \ No newline at end of file +.PHONY: toggle-cw-killswitch + +teardown: + @./scripts/babylon-integration/teardown.sh +.PHONY: teardown diff --git a/docker/docker-compose-babylon-integration.yml b/docker/docker-compose-babylon-integration.yml index f73d308..4564f72 100644 --- a/docker/docker-compose-babylon-integration.yml +++ b/docker/docker-compose-babylon-integration.yml @@ -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: @@ -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: @@ -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: @@ -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: @@ -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 diff --git a/scripts/babylon-integration/teardown.sh b/scripts/babylon-integration/teardown.sh new file mode 100755 index 0000000..d3d5e01 --- /dev/null +++ b/scripts/babylon-integration/teardown.sh @@ -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 \ No newline at end of file diff --git a/scripts/babylon-integration/utils/Dockerfile b/scripts/babylon-integration/utils/Dockerfile index 503f9da..366d81b 100644 --- a/scripts/babylon-integration/utils/Dockerfile +++ b/scripts/babylon-integration/utils/Dockerfile @@ -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 \ No newline at end of file +COPY toggle-cw-killswitch.sh /toggle-cw-killswitch.sh +COPY teardown.sh /teardown.sh \ No newline at end of file diff --git a/scripts/babylon-integration/utils/teardown.sh b/scripts/babylon-integration/utils/teardown.sh new file mode 100755 index 0000000..2ebd96e --- /dev/null +++ b/scripts/babylon-integration/utils/teardown.sh @@ -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"