Links
Comment on page

Deploying and Using Subgraphs

In this tutorial, we'll be working with a sample contract deployed to Mantle testnet and index it using a subgraph. To learn more about The Graph's protocol specifics, head over to their docs.

Install Dependencies

You'll need to install Foundry in your local environment to follow along. Execute the following commands to download Foundry from GitHub.
1
# for Apple silicon chip, please select the version compatible with your system
2
pkg=https://github.com/foundry-rs/foundry/releases/download/nightly/foundry_nightly_darwin_arm64.tar.gz
3
4
curl -sSL $pkg | tar xzf - -C ./bin
5
6
export $PATH=$PWD/bin:$PATH
Next, download the subgraph example from Mantle GitHub by running the following command.
git clone https://github.com/mantlenetworkio/mantle-example-subgraph.git
You can now run yarn in the main directory to download all other necessary dependencies, which includes GraphQL libraries, and the graph-cli utility required to deploy subgraphs.

Deploy Contract

Use the following command with modified parameters to deploy your contract to Mantle testnet. You can also experiment with the sample contract provided.
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
9

Deploy Subgraph

Now navigate to the subgraph.yaml file in the main directory, and update the contract address, and the startBlock values specific to your contract.
source:
address: # your contract address
abi: # name assigned to the contract ABI file, path defined under abis
startBlock: # block number for your contract deployment transaction
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.
yarn codegen
yarn deploy-testnet

Trying Out the Subgraph

You'll now be able to make contract calls and query the indexed data for emitted events from the subgraph. Call a function from your contract in place of the example shown below. You can also run the sample code directly.
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

Querying the Subgraph

You can now make HTTP requests to the Graph service deployed on Mantle testnet and query your subgraph using its name or id.
curl -g -X POST \
-H "Content-Type: application/json" \
-d '{"query":" "}' \ # your query here
https://graph.testnet.mantle.xyz/subgraphs/name/<name>
Navigate to the Graph Endpoints page to see the endpoint URLs to interact with the graph service on Mantle testnet.