How to Deploy and Use Subgraphs on Mantle Network
07/10/234 min read
by Mantle
Developers
Tutorials
Web3

The Graph runs an indexing protocol that can take raw transaction data from a blockchain and organize into more usable formats. This data can then be made accessible to applications via customizable GraphQL APIs referred to as subgraphs.
We run a Graph node on Mantle Network that indexes L2 transaction data, which means anyone can deploy subgraphs and connect their dApps to Mantle Network! The following tutorial uses a sample contract and a simple subgraph configuration to demonstrate how to deploy one to Mantle, and then query it.
Install Dependencies
✅ Make sure you have Foundry installed and set up in your local environment before moving forward! Check out Foundry's GitHub for more help with installation on your system.
Our example contract and subgraph configuration can be found on GitHub. We can easily download all the necessary scripts and files using the following command.
1 git clone https://github.com/mantlenetworkio/mantle-example-subgraph.git
Next, run yarn in the main directory to download all other necessary dependencies, which includes GraphQL libraries and
graph-cli
utility required to deploy subgraphs.Deploy Contract
Once you feel your contract is ready to be deployed, use the following command to deploy it to testnet.
1 bin/forge create --legacy --rpc-url https://rpc.testnet.mantle.xyz \ 2 --private-key 0x... \ # wallet private key to pay gas fees 3 --from 0x...\ # address used to deploy 4 contracts/Gravity.sol:GravatarRegistry # contract path 5 6 Deployer: 0x00000500E87eE83A1BFa233512af25a4003836C8 7 Deployed to: 0x1de239E2D98dea46C10D6D14F5E9EB427fdE6059 8 Transaction hash: 0x4cf8fa85b6cb253cf8222c0863eea3607b31c5c168a9170262723077d674eb22
Deploy Subgraph
With the contract deployed successfully, you can update the contract address and
startBlock
values specific to your contract to the subgraph.yaml
file in the main directory.1 source: 2 address: # your contract address 3 abi: # name assigned to the contract ABI file, path defined under abis 4 startBlock: # block number for your contract deployment transaction
Now run the following commands to deploy the subgraph indexing your contract. Feel free to modify the parameters for your desired deploy action. The definitions can be found in
package.json
.1 yarn codegen 2 yarn deploy-testnet
Querying the Subgraph
With the subgraph deployed, any events emitted from the contract will now be indexed and can be queried. You can start making contract function calls to test it out. If you're using the example contract, you can send a transaction like this:
1 bin/cast send --legacy --rpc-url https://rpc.testnet.mantle.xyz \ 2 --private-key 0x.. \ # wallet private key to pay gas fees 3 --from 0x.. \ 4 0x1de239E2D98dea46C10D6D14F5E9EB427fdE6059 \ # your contract address 5 "createGravatar(string,string)" "Alice" "https://google.com/a.jpg" # function call from your contract
At this point, you can make HTTP requests to the Graph service deployed on Mantle Network and query your subgraph using the subgraph
name
or id
. 📖 You can refer to the Graph endpoint reference to see the endpoint URLs that can be used to interact with the graph service deployed on Mantle testnet.