From 7d90db71ed1b2f081d6c405bd1d4f8fe2016fdd0 Mon Sep 17 00:00:00 2001 From: jaybuidl Date: Sat, 9 Sep 2023 10:19:16 +0800 Subject: [PATCH] fix: subgraph update script --- subgraph/package.json | 3 ++- subgraph/scripts/update.sh | 35 +++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/subgraph/package.json b/subgraph/package.json index cc9ca7499..7d70d40d7 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -5,6 +5,7 @@ "update:arbitrum-goerli": "./scripts/update.sh arbitrumGoerli arbitrum-goerli", "update:arbitrum-goerli-devnet": "./scripts/update.sh arbitrumGoerliDevnet arbitrum-goerli", "update:arbitrum": "./scripts/update.sh arbitrum arbitrum", + "update:local": "./scripts/update.sh localhost mainnet", "codegen": "graph codegen", "build": "graph build", "clean": "graph clean && rm subgraph.yaml.bak.*", @@ -14,7 +15,7 @@ "create-local": "graph create --node http://localhost:8020/ kleros/kleros-v2-core-local", "remove-local": "graph remove --node http://localhost:8020/ kleros/kleros-v2-core-local", "deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 kleros/kleros-v2-core-local --version-label v$(date +%s)", - "rebuild-deploy-local": "./scripts/update.sh localhost mainnet && yarn codegen && yarn create-local && yarn deploy-local", + "rebuild-deploy-local": "yarn update:local && yarn codegen && yarn create-local && yarn deploy-local", "start-local-indexer": "docker compose -f ../services/graph-node/docker-compose.yml up -d && docker compose -f ../services/graph-node/docker-compose.yml logs -f", "stop-local-indexer": "docker compose -f ../services/graph-node/docker-compose.yml down && rm -rf ../services/graph-node/data" }, diff --git a/subgraph/scripts/update.sh b/subgraph/scripts/update.sh index 980b70a3c..b36e7ed0b 100755 --- a/subgraph/scripts/update.sh +++ b/subgraph/scripts/update.sh @@ -2,18 +2,33 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -function update() #file #dataSourceIndex #graphNetwork +function update() #hardhatNetwork #graphNetwork #dataSourceIndex #contract { - local f="$1" - local dataSourceIndex="$2" - - graphNetwork=$3 yq -i ".dataSources[$dataSourceIndex].network=env(graphNetwork)" "$SCRIPT_DIR"/../subgraph.yaml + local hardhatNetwork="$1" + local graphNetwork="$2" + local dataSourceIndex="$3" + local contract="$4" + local artifact="$SCRIPT_DIR/../../contracts/deployments/$hardhatNetwork/$contract.json" + + # Set the address + address=$(cat "$artifact" | jq '.address') + yq -i ".dataSources[$dataSourceIndex].source.address=$address" "$SCRIPT_DIR"/../subgraph.yaml + + # Set the start block + blockNumber="$(cat "$artifact" | jq '.receipt.blockNumber')" + yq -i ".dataSources[$dataSourceIndex].source.startBlock=$blockNumber" "$SCRIPT_DIR"/../subgraph.yaml - address=$(cat "$f" | jq '.address') - yq -i ".dataSources[$dataSourceIndex].source.address=$address" "$SCRIPT_DIR"/../subgraph.yaml + # Set the Graph network + graphNetwork=$graphNetwork yq -i ".dataSources[$dataSourceIndex].network=env(graphNetwork)" "$SCRIPT_DIR"/../subgraph.yaml - blockNumber="$(cat "$f" | jq '.receipt.blockNumber')" - yq -i ".dataSources[$dataSourceIndex].source.startBlock=$blockNumber" "$SCRIPT_DIR"/../subgraph.yaml + # Set the ABIs path for this Hardhat network + abiIndex=0 + for f in $(yq e .dataSources[$dataSourceIndex].mapping.abis[].file subgraph.yaml -o json -I 0 | jq -sr '.[]') + do + f2=$(echo $f | sed "s|\(.*\/deployments\/\).*\/|\1$hardhatNetwork\/|") + yq -i ".dataSources[$dataSourceIndex].mapping.abis[$abiIndex].file=\"$f2\"" "$SCRIPT_DIR"/../subgraph.yaml + (( ++abiIndex )) + done } # as per ../contracts/hardhat.config.js @@ -28,6 +43,6 @@ cp "$SCRIPT_DIR"/../subgraph.yaml "$SCRIPT_DIR"/../subgraph.yaml.bak.$(date +%s) for contract in $(yq .dataSources[].name "$SCRIPT_DIR"/../subgraph.yaml) do - update "$SCRIPT_DIR/../../contracts/deployments/$hardhatNetwork/$contract.json" $i $graphNetwork + update $hardhatNetwork $graphNetwork $i $contract (( ++i )) done