This is a template for creating a custom subgraph for Metis contracts. Follow this guide to create and deploy your own subgraph.
- Node.js version 20 (as specified in .nvmrc)
- Yarn package manager
- A deployed smart contract on Metis network (follow the docs)
- Go to The Graph Studio
- Connect your wallet
- Click "Create a Subgraph"
- Name your subgraph (e.g., "My Metis Subgraph")
- Note down the deployment key provided
# Install project dependencies
yarn install
Add your contract ABIs as JSON files in the abis
directory and delete redundant files. For example:
# Example: Copy your contract ABI to the abis directory
cp path/to/your/contract.json ./abis/
rm ./abis/StakeAndLock.json
- Open
schema.graphql
- Define your entities based on the data you want to index
- Each entity should have:
- Required fields (ID, timestamps)
- Fields matching your contract events
- Proper relationships between entities
Example:
type Entity @entity {
id: ID!
field1: String!
field2: BigInt!
timestamp: BigInt!
}
- Open
subgraph.yaml
- Update the following fields:
dataSources[].source.address
: Your contract addressdataSources[].source.abi
: Your contract ABI namedataSources[].source.startBlock
: The block number where your contract was deployeddataSources[].mapping.entities
: Your defined entities from schema.graphqldataSources[].mapping.abis[].name
: Your contract ABI namedataSources[].mapping.abis[].file
: Path to your ABI filedataSources[].mapping.eventHandlers
: Events you want to index and their handlers
Example configuration:
dataSources:
- kind: ethereum
name: YourContract
network: andromeda
source:
address: "0x..." # Your contract address
abi: YourContract # Your contract ABI name
startBlock: 12345678 # Your contract deployment block
mapping:
entities:
- YourEntity
abis:
- name: YourContract
file: ./abis/YourContract.json
eventHandlers:
- event: YourEvent(indexed uint256,address)
handler: handleYourEvent
- Open
mapping.ts
- Implement event handlers for each event you want to index
- Create/update entities based on event data
- Save entities using
entity.save()
Example:
export function handleEvent(event: Event): void {
let entity = new Entity(event.transaction.hash.toHex())
entity.field1 = event.params.value
entity.timestamp = event.block.timestamp
entity.save()
}
Note: every time you update the schema or the events or the subgraph configuration, you need to run
yarn codegen
to update the types.
- Open
package.json
- Update the
name
field to match your subgraph name - Update any other relevant fields
yarn codegen
yarn build
Authenticate with The Graph Studio and cli will prompt you to paste the deployment key:
yarn auth
Deploy the subgraph:
yarn deploy
- Go to The Graph Studio
- Select your subgraph
- Use the GraphQL playground to test queries
- Check the logs for any indexing issues
- If you get ABI errors, ensure your ABI file is valid JSON
- If events aren't being indexed, verify the event names match exactly
- If entities aren't saving, check your mapping logic and entity definitions