From d21aa1c0e01a6abcefbdbb0ab518161fb62faffa Mon Sep 17 00:00:00 2001 From: Tristan <122918260+TAdev0@users.noreply.github.com> Date: Tue, 16 Apr 2024 04:09:20 +0200 Subject: [PATCH] [feat] Load Apibara Script Variables from Environment (#25) * load from env * update * Environment setup to use a single script.js file for indexing --------- Co-authored-by: Brandon Roberts --- README.md | 1 + docker-compose.yml | 11 +++++++---- indexer/Dockerfile | 4 ++-- indexer/README.md | 6 +++++- indexer/docker-script.js | 26 -------------------------- indexer/script.js | 8 +++++--- tests/integration/docker/deploy.sh | 4 ++-- tests/integration/docker/initialize.sh | 2 +- tests/integration/local/run.sh | 9 +++++++-- 9 files changed, 30 insertions(+), 41 deletions(-) delete mode 100644 indexer/docker-script.js diff --git a/README.md b/README.md index 11f9907d..b42059f4 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ docker compose build ```bash # Must install all the dependencies first +# Use npm install inside the `frontend` directory # Change the user on `configs/database.config.json` for postgres make integration-test-local ``` diff --git a/docker-compose.yml b/docker-compose.yml index 78ebcc0e..25796567 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -54,7 +54,7 @@ services: environment: - SCARB=/root/.local/bin/scarb volumes: - - deployment:/deployment + - configs:/configs apibara: image: quay.io/apibara/starknet:1.5.0 command: @@ -86,8 +86,11 @@ services: links: - backend - apibara + environment: + - APIBARA_STREAM_URL=http://art-peace-apibara-1:7171 + - BACKEND_TARGET_URL=http://art-peace-backend-1:8080/consumeIndexerMsg volumes: - - deployment:/deployment + - configs:/configs restart: on-failure frontend: build: @@ -102,7 +105,7 @@ services: - backend - devnet volumes: - - deployment:/deployment + - configs:/configs - ./frontend/package.json:/app/package.json - ./frontend/package-lock.json:/app/package-lock.json - ./frontend/public/:/app/public @@ -113,4 +116,4 @@ volumes: postgres: devnet: apibara: - deployment: + configs: diff --git a/indexer/Dockerfile b/indexer/Dockerfile index 993bcb38..69dad95e 100644 --- a/indexer/Dockerfile +++ b/indexer/Dockerfile @@ -1,6 +1,6 @@ FROM quay.io/apibara/sink-webhook:0.6.0 as sink-webhook WORKDIR /indexer -COPY ./indexer/docker-script.js . +COPY ./indexer/script.js . -CMD ["run", "docker-script.js", "--allow-env", "/deployment/.env"] +CMD ["run", "script.js", "--allow-env", "/configs/configs.env", "--allow-env-from-env", "BACKEND_TARGET_URL,APIBARA_STREAM_URL"] diff --git a/indexer/README.md b/indexer/README.md index 9380a3d1..ebfff6f2 100644 --- a/indexer/README.md +++ b/indexer/README.md @@ -6,5 +6,9 @@ This directory contains the Apibara indexer setup for `art/peace`, which indexes ``` # Setup Indexer/DNA w/ docker compose or other options -apibara run scripts.js +# Create an indexer.env file with the following : +# ART_PEACE_CONTRACT_ADDRESS=... # Example: 0x78223f7ab13216727ed426380079c169578cafad83a3178c7b33ba7ca307713 +# APIBARA_STREAM_URL=... # Example: http://localhost:7171 +# BACKEND_TARGET_URL=... # Example: http://localhost:8080/consumeIndexerMsg +apibara run scripts.js --allow-env indexer.env ``` diff --git a/indexer/docker-script.js b/indexer/docker-script.js deleted file mode 100644 index 017ac4c4..00000000 --- a/indexer/docker-script.js +++ /dev/null @@ -1,26 +0,0 @@ -export const config = { - streamUrl: "http://art-peace-apibara-1:7171", - startingBlock: 0, - network: "starknet", - finality: "DATA_STATUS_PENDING", - filter: { - events: [ - { - fromAddress: Deno.env.get("ART_PEACE_CONTRACT_ADDRESS"), - keys: ["0x2D7B50EBF415606D77C7E7842546FC13F8ACFBFD16F7BCF2BC2D08F54114C23"], - includeReverted: false, - includeTransaction: false, - includeReceipt: false, - }, - ], - }, - sinkType: "webhook", - sinkOptions: { - targetUrl: "http://art-peace-backend-1:8080/consumeIndexerMsg" - }, -}; - -// This transform does nothing. -export default function transform(block) { - return block; -} diff --git a/indexer/script.js b/indexer/script.js index c0c0b29b..0390248a 100644 --- a/indexer/script.js +++ b/indexer/script.js @@ -1,5 +1,5 @@ export const config = { - streamUrl: "http://localhost:7171", + streamUrl: Deno.env.get("APIBARA_STREAM_URL"), startingBlock: 0, network: "starknet", finality: "DATA_STATUS_PENDING", @@ -7,7 +7,9 @@ export const config = { events: [ { fromAddress: Deno.env.get("ART_PEACE_CONTRACT_ADDRESS"), - keys: ["0x2D7B50EBF415606D77C7E7842546FC13F8ACFBFD16F7BCF2BC2D08F54114C23"], + keys: [ + "0x2D7B50EBF415606D77C7E7842546FC13F8ACFBFD16F7BCF2BC2D08F54114C23", + ], includeReverted: false, includeTransaction: false, includeReceipt: false, @@ -16,7 +18,7 @@ export const config = { }, sinkType: "webhook", sinkOptions: { - targetUrl: "http://localhost:8080/consumeIndexerMsg" + targetUrl: Deno.env.get("BACKEND_TARGET_URL"), }, }; diff --git a/tests/integration/docker/deploy.sh b/tests/integration/docker/deploy.sh index f517daa5..7204e68a 100755 --- a/tests/integration/docker/deploy.sh +++ b/tests/integration/docker/deploy.sh @@ -58,8 +58,8 @@ CONTRACT_ADDRESS=$(echo $CONTRACT_DEPLOY_RESULT | jq -r '.contract_address') echo "Deployed contract \"$CLASS_NAME\" with address $CONTRACT_ADDRESS" # TODO: Remove these lines? -echo "ART_PEACE_CONTRACT_ADDRESS=$CONTRACT_ADDRESS" > /deployment/.env -echo "REACT_APP_ART_PEACE_CONTRACT_ADDRESS=$CONTRACT_ADDRESS" >> /deployment/.env +echo "ART_PEACE_CONTRACT_ADDRESS=$CONTRACT_ADDRESS" > /configs/configs.env +echo "REACT_APP_ART_PEACE_CONTRACT_ADDRESS=$CONTRACT_ADDRESS" >> /configs/configs.env # TODO # MULTICALL_TEMPLATE_DIR=$CONTRACT_DIR/tests/multicalls diff --git a/tests/integration/docker/initialize.sh b/tests/integration/docker/initialize.sh index a12e888c..d21279b2 100755 --- a/tests/integration/docker/initialize.sh +++ b/tests/integration/docker/initialize.sh @@ -9,5 +9,5 @@ echo "Initializing the canvas" curl http://backend:8080/initCanvas -X POST echo "Set the contract address" -CONTRACT_ADDRESS=$(cat /deployment/.env | tail -n 1 | cut -d '=' -f2) +CONTRACT_ADDRESS=$(cat /configs/configs.env | tail -n 1 | cut -d '=' -f2) curl http://backend:8080/setContractAddress -X POST -d "$CONTRACT_ADDRESS" diff --git a/tests/integration/local/run.sh b/tests/integration/local/run.sh index 318fc40f..8e06d47b 100755 --- a/tests/integration/local/run.sh +++ b/tests/integration/local/run.sh @@ -77,7 +77,12 @@ INDEXER_SCRIPT_LOG_FILE=$LOG_DIR/indexer_script.log touch $INDEXER_SCRIPT_LOG_FILE cd $WORK_DIR/indexer #TODO: apibara -> postgres automatically? -ART_PEACE_CONTRACT_ADDRESS=$ART_PEACE_CONTRACT_ADDRESS apibara run script.js --allow-env-from-env ART_PEACE_CONTRACT_ADDRESS 2>&1 > $INDEXER_SCRIPT_LOG_FILE & +rm -f $TMP_DIR/indexer.env +touch $TMP_DIR/indexer.env +echo "ART_PEACE_CONTRACT_ADDRESS=$ART_PEACE_CONTRACT_ADDRESS" >> $TMP_DIR/indexer.env +echo "APIBARA_STREAM_URL=http://localhost:7171" >> $TMP_DIR/indexer.env +echo "BACKEND_TARGET_URL=http://localhost:8080/consumeIndexerMsg" >> $TMP_DIR/indexer.env +apibara run script.js --allow-env $TMP_DIR/indexer.env 2>&1 > $INDEXER_SCRIPT_LOG_FILE & INDEXER_SCRIPT_PID=$! sleep 2 # Wait for indexer script to start; TODO: Check if indexer script is actually running @@ -96,7 +101,7 @@ REACT_CANVAS_CONFIG_FILE=$WORK_DIR/frontend/src/configs/canvas.config.json REACT_BACKEND_CONFIG_FILE=$WORK_DIR/frontend/src/configs/backend.config.json cp $CANVAS_CONFIG_FILE $REACT_CANVAS_CONFIG_FILE #TODO: Use a symlink instead? cp $BACKEND_CONFIG_FILE $REACT_BACKEND_CONFIG_FILE -REACT_APP_ART_PEACE_CONTRACT_ADDRESS=$ART_PEACE_CONTRACT_ADDRESS REACT_APP_CANVAS_CONFIG_FILE=$REACT_CANVAS_CONFIG_FILE REACT_APP_BACKEND_CONFIG_FILE=$REACT_BACKEND_CONFIG_FILE npm start 2>&1 > $FRONTEND_LOG_FILE & +npm start 2>&1 > $FRONTEND_LOG_FILE & FRONTEND_PID=$! sleep 2 # Wait for frontend to start; TODO: Check if frontend is actually running