The Graph and Virtual TestNets
This guide provides detailed steps to set up and use Tenderly Virtual Testnets alongside The Graph.
To use The Graph with Virtual TestNets, you need to run your own Graph node. In case you’re staging your dapp, you’ll have to have The Graph instance hosted on your cloud provider of choice.
Prerequisites
Ensure you have the following prerequisites installed on your system:
- Node.js and npm
- Docker
- A Tenderly account
- A Virtual TestNet RPC
- A smart contract deployed to Virtual TestNet using Hardhat or Foundry
Set up The Graph
Install Graph CLI Globally
Install the Graph CLI globally on your system using npm:
npm install -g @graphprotocol/graph-cliRefer to the official Graph CLI documentation for more details.
Set up The Graph project
Inside your project directory, initialize The Graph project.
Notes:
- Adapt
--abito point to contrac ABI on your local system - Modify the
--contract-nameand--from-contractwith contract name and address, respectively - Set he
--start-block - Change the
--networkso it references the network you based your Virtual TestNet off of (e.g.mainnet,base, etc.).
We recommend having network match the original network’s name, without labeling it as virtual_. This enables effortless switching of RPCs when deploying to different staging and demoing environments.
graph init your-subgraph-name subgraph-dir \
--protocol ethereum \
--studio \
--abi path/to/abi/ERC20Token.json \
--contract-name ERC20TokenFactory \
--from-contract 0x7F2Fa32C991fD2F8dF3EB23C394f550376e9b30d \
--start-block 0 \
--network mainnetUpdate docker-compose.yml
Edit the docker-compose.yml file (below) and update environment.ethereum so it refers to the Virtual TestNet RPC.
version: '3'
services:
graph-node:
image: graphprotocol/graph-node
ports:
- '8020:8020'
- '8000:8000'
- '5001:5001'
environment:
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: 'ipfs:5001'
ethereum: 'mainnet:https://virtual.mainnet.rpc.tenderly.co/d0a6...-40..2c-81..0f-a5..90'Run Docker Compose locally
After setting up docker-compose config, run the following:
docker-compose upCreate and deploy your subgraph
Run the following commands, replacing your-subgraph-name with the desired name for your subgraph.
graph codegen
graph create --node http://localhost:8020 your-subgraph-name
graph deploy --node http://localhost:8020 --ipfs http://localhost:5001 your-subgraph-nameAccess the subgraph
After deployment, access your subgraph through the URL below. Open this URL in your browser to access the subgraph and run queries.
Deployed to http://localhost:8000/subgraphs/name/your-subgraph-nameDeploy to Cloud
For dapp staging, deploy The Graph node your cloud provider of choice, and connect your dapp UI/backend to that node for querying.