|
| 1 | +# Default task: Show available commands |
| 2 | +default: |
| 3 | + just --list |
| 4 | + |
| 5 | +# Set environment variables for all commands |
| 6 | +export DATABASE_URL := "postgres://postgres:password@localhost:5432/mydatabase" |
| 7 | +export ELECTRUM_URL := "localhost:50001" |
| 8 | +export NODE_URL := "http://localhost:18443" |
| 9 | +export NODE_USERNAME := "custom_user" |
| 10 | +export NODE_PASSWORD := "custom_pass" |
| 11 | +export RUST_BACKTRACE := "1" |
| 12 | + |
| 13 | +# Setup network |
| 14 | +setup-network: |
| 15 | + # Check if the network exists first |
| 16 | + docker network inspect bitcoin-network >/dev/null 2>&1 || docker network create bitcoin-network |
| 17 | + # Verify network was created |
| 18 | + docker network inspect bitcoin-network >/dev/null 2>&1 && echo "Bitcoin network is ready" || (echo "Failed to create bitcoin-network" && exit 1) |
| 19 | + |
| 20 | +# PostgreSQL commands |
| 21 | +start-postgres: |
| 22 | + docker run -d --name postgres \ |
| 23 | + -p 5432:5432 \ |
| 24 | + -e POSTGRES_USER=postgres \ |
| 25 | + -e POSTGRES_PASSWORD=password \ |
| 26 | + -e POSTGRES_DB=mydatabase \ |
| 27 | + postgres:15 |
| 28 | + |
| 29 | +test-postgres: |
| 30 | + PGPASSWORD=password psql -h localhost -p 5432 -U postgres -d mydatabase -c "SELECT 1" |
| 31 | + |
| 32 | +# Bitcoin Core commands |
| 33 | +build-bitcoin: |
| 34 | + docker build -t bitcoin-nix \ |
| 35 | + --build-arg NODE_USERNAME=custom_user \ |
| 36 | + --build-arg NODE_PASSWORD=custom_pass \ |
| 37 | + -f bitcoin.Dockerfile . |
| 38 | + |
| 39 | +start-bitcoin: setup-network |
| 40 | + docker run -d --name bitcoin-node \ |
| 41 | + --network bitcoin-network \ |
| 42 | + -p 18443:18443 \ |
| 43 | + bitcoin-nix |
| 44 | + # Add a delay or check to ensure bitcoin is ready |
| 45 | + echo "Waiting for Bitcoin node to start..." |
| 46 | + for i in {1..30}; do \ |
| 47 | + if just test-bitcoin >/dev/null 2>&1; then \ |
| 48 | + echo "Bitcoin node is ready!"; \ |
| 49 | + break; \ |
| 50 | + fi; \ |
| 51 | + echo "Waiting... ($i/30)"; \ |
| 52 | + sleep 1; \ |
| 53 | + if [ $i -eq 30 ]; then \ |
| 54 | + echo "Timed out waiting for Bitcoin node to start"; \ |
| 55 | + exit 1; \ |
| 56 | + fi; \ |
| 57 | + done |
| 58 | + |
| 59 | +test-bitcoin: |
| 60 | + curl --silent --user custom_user:custom_pass \ |
| 61 | + --data-binary '{"jsonrpc": "1.0", "id":"bitcointest", "method": "getblockchaininfo", "params": []}' \ |
| 62 | + -H 'content-type: text/plain;' http://localhost:18443 |
| 63 | + |
| 64 | +generate-blocks: |
| 65 | + curl --silent --user custom_user:custom_pass \ |
| 66 | + --data-binary '{"jsonrpc": "1.0", "id":"bitcointest", "method": "generatetoaddress", "params": [101, "bcrt1prdfvfk2ddxe8y88qxhwkxn9cy0d2w6k98gj9smwz6tqjcnl4tdwsrf03aw"]}' \ |
| 67 | + -H 'content-type: text/plain;' http://localhost:18443 |
| 68 | + |
| 69 | +# Electrs commands |
| 70 | +build-electrs: |
| 71 | + docker build -t electrs-nix \ |
| 72 | + -f electrs.Dockerfile . |
| 73 | + |
| 74 | +start-electrs: setup-network |
| 75 | + docker run -d --name electrs \ |
| 76 | + --network bitcoin-network \ |
| 77 | + -p 50001:50001 \ |
| 78 | + electrs-nix |
| 79 | + echo "Electrs started" |
| 80 | + |
| 81 | +check-electrs: |
| 82 | + docker logs electrs |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +# Kill any services that might be using the ports |
| 87 | +kill-services: |
| 88 | + # Kill any process using PostgreSQL port |
| 89 | + lsof -ti:5432 | xargs kill -9 || true |
| 90 | + # Kill any process using Bitcoin RPC port |
| 91 | + lsof -ti:18443 | xargs kill -9 || true |
| 92 | + # Kill any process using Electrs port |
| 93 | + lsof -ti:50001 | xargs kill -9 || true |
| 94 | + |
| 95 | +# Stop and remove Docker containers |
| 96 | +stop-containers: |
| 97 | + docker stop postgres bitcoin-node electrs || true |
| 98 | + docker rm postgres bitcoin-node electrs || true |
| 99 | + |
| 100 | +# Clean everything |
| 101 | +clean-all: |
| 102 | + just kill-services |
| 103 | + just stop-containers |
| 104 | + docker network rm bitcoin-network || true |
| 105 | + # Make sure all orphaned networks are also removed |
| 106 | + docker network prune -f |
| 107 | + echo "Environment cleaned successfully" |
| 108 | + |
| 109 | +# Run the regtest example with all required services |
| 110 | +run-example-regtest: |
| 111 | + # Clean up any existing containers first |
| 112 | + just stop-containers |
| 113 | + # Start all required services |
| 114 | + just start-postgres |
| 115 | + just build-bitcoin |
| 116 | + just start-bitcoin |
| 117 | + just build-electrs |
| 118 | + just start-electrs |
| 119 | + # Generate initial blocks |
| 120 | + just generate-blocks |
| 121 | + # Run the regtest example |
| 122 | + cargo run --example regtest_bdk_sqlx_postgres |
0 commit comments