DEV Community

Cover image for A faster way of creating subgraphs
cptMoh
cptMoh

Posted on

A faster way of creating subgraphs

This is a follow-up post to my guide on Creating ERC721 Subgraphs. I suggest that you read that first in order to fully appreciate this faster/easier way. Also, you'd gain more in-depth knowledge on creating subgraphs if you read that first.

Getting Started

  • For this guide, we are going to be using the hosted service. Head over there by clicking on this link and either create an account or login to your dashboard.

  • Once on your dashboard, click on the 'Add Subgraph' Button.

  • Fill in the required fields on the form. Make sure the name that you give to the subgraph is the same as the name you will give to your github repository for the subgraph.

  • Submit the form.

  • Next, You can click on the button to 'show commands' of how to install the graph CLI globally using either NPM or Yarn if you don't already have it installed. This is because you will need it to build and deploy subgraphs.

Initialising the Subgraph.

The Next step is to initialise the subgraph from your terminal. But before this, you need to cd into the directory/folder that you want to write your code in.

While in the directory, this is the command to run:

graph init --index-events --from-contract {Contract-address} --network {network-name} {subgraph-name}

Enter fullscreen mode Exit fullscreen mode

Make sure you replace the 'contract-address' with the address of the contract that you want to index, the 'network-name' with the name of the network, and the 'subgraph-name' with the name of the subgraph that you want to create.

For instance, if we want to create a subgraph named 'raffle' that indexes the events from a contract on the arbitrum-one network, we run the following command:

graph init --index-events --from-contract 0xf53d383525117d1f51bf234966e39bd1508a5948 --network arbitrum-one raffle

Enter fullscreen mode Exit fullscreen mode
  • When you open the subgraph.yaml file, you'll notice that that all the events on the contract have been automatically indexed!
specVersion: 0.0.4
schema:
  file: ./schema.graphql
dataSources:
  - kind: ethereum
    name: Contract
    network: arbitrum-one
    source:
      address: "0xf53d383525117d1f51bf234966e39bd1508a5948"
      abi: Contract
      startBlock: 16977962
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.5
      language: wasm/assemblyscript
      entities:
        - NewAuctionWinner
        - NewBid
        - NewGoldenTicketWinner
        - NewRaffleWinner
        - OwnershipTransferred
      abis:
        - name: Contract
          file: ./abis/Contract.json
      eventHandlers:
        - event: NewAuctionWinner(uint256)
          handler: handleNewAuctionWinner
        - event: NewBid(address,uint256,uint256)
          handler: handleNewBid
        - event: NewGoldenTicketWinner(uint256)
          handler: handleNewGoldenTicketWinner
        - event: NewRaffleWinner(uint256)
          handler: handleNewRaffleWinner
        - event: OwnershipTransferred(indexed address,indexed address)
          handler: handleOwnershipTransferred
      file: ./src/contract.ts
Enter fullscreen mode Exit fullscreen mode
  • The schema.graphql file has also been automatically generated for you with all the contract events schema automatically generated!
type NewAuctionWinner @entity {
  id: ID!
  bidderID: BigInt! # uint256
  block: BigInt! # uint256
  timestamp: BigInt! # uint256
}

type NewBid @entity {
  id: ID!
  bidder: Bytes! # address
  bidderID: BigInt! # uint256
  bidAmount: BigInt! # uint256
  block: BigInt! # uint256
  timestamp: BigInt! # uint256
}

type NewGoldenTicketWinner @entity {
  id: ID!
  bidderID: BigInt! # uint256
  block: BigInt! # uint256
  timestamp: BigInt! # uint256
}

type NewRaffleWinner @entity {
  id: ID!
  bidderID: BigInt! # uint256
  block: BigInt! # uint256
  timestamp: BigInt! # uint256
}

type OwnershipTransferred @entity {
  id: ID!
  previousOwner: Bytes! # address
  newOwner: Bytes! # address
  block: BigInt! # uint256
  timestamp: BigInt! # uint256
}
Enter fullscreen mode Exit fullscreen mode
  • Now when we head over to the 'src/' directory, and open up the 'contract.ts' file, you'll also notice that all the mapping functions have already been generated! This is so easy, haha!

So, since all the heavy lifting has already been done for us, we can just go on and deploy our subgraph. You don't have to even change anything on the code as literally all the work has been done.

To deploy, run graph auth on your terminal

  • Choose the hosted service

  • It will ask you to enter the deploy key. Head to your dashboard, copy your access token, and then paste it.

  • Finally, run npm run deploy

  • Wait for it to compile your subgraph and deploy it.

If you head to your dashboard, you will see that your Subgraph is deployed and syncing.

Congratulations!!!

for more information about building subgraphs and the graph protocol in general, go to the graph academy

Top comments (0)