Deploy Subgraph

In this section, we will guide you through deploying a Subgraph using the Alchemy Subgraph Service. Ensure you have your DEPLOY_KEY ready, as it will be used later for deployment.

For more information, refer to the documentation provided by The Graph and Alchemy

Purpose:

Prerequisites:

How to Obtain DEPLOY_KEY:

  • Select Add Subgraph, then choose Deploy a Subgraph.

  • You will see a command similar to the one below, which you will use later to deploy your Subgraph.

Deploying the Subgraph:

  1. Install the Graph CLI:

yarn global add @graphprotocol/graph-cli
# OR
npm install -g @graphprotocol/graph-cli
  1. Initialize your Subgraph:

# You can change "kip-token-example" by your name
graph init --studio kip-token-example
  1. Follow the prompt instructions and choose your options. Once done, you will see output similar to the following.

(base)   Subgraph graph init --studio kip-token-example
 Protocol · ethereum
 Subgraph slug · kip-token-example
 Directory to create the subgraph in · kip-token-example
 Ethereum network · base-sepolia
 Contract address · 0x4b565A132347064Ca7F1eDC9489a00b3E68431dd
 Fetching ABI from Etherscan
 Fetching Start Block
 Fetching Contract Name
 Start Block · 11458527
 Contract Name · Token20
 Index contract events as entities (Y/n) · true
  Generate subgraph
  Write subgraph to directory
 Create subgraph scaffold
 Initialize networks config
 Initialize subgraph repository
 Install dependencies with yarn
 Generate ABI and schema types with yarn codegen
Add another contract? (y/n):
Subgraph kip-token-example created in kip-token-example

Next steps:

  1. Run `graph auth` to authenticate with your deploy key.

  2. Type `cd kip-token-example` to enter the subgraph.

  3. Run `yarn deploy` to deploy the subgraph.

Make sure to visit the documentation on https://thegraph.com/docs/ for further information.
  1. Before deploying the Subgraph, we need to modify schema.graphql to query the balance of an account.

  • Open kip-token-example/schema.graphql.

  • Append the following code at the end of the file:

type Account @entity {
  id: ID!
  balance: BigInt!
}
  1. Run the compile command to update ./generated/schema.ts file:

cd kip-token-example && graph codegen
  1. Modify ./src/token-20.ts as follows:

import { BigInt } from "@graphprotocol/graph-ts";
import {
  Approval as ApprovalEvent,
  Transfer as TransferEvent,
} from "../generated/Token20/Token20";
import { Approval, Transfer, Account } from "../generated/schema";

export function handleApproval(event: ApprovalEvent): void {
  let entity = new Approval(
    event.transaction.hash.concatI32(event.logIndex.toI32())
  );
  entity.owner = event.params.owner;
  entity.spender = event.params.spender;
  entity.value = event.params.value;

  entity.blockNumber = event.block.number;
  entity.blockTimestamp = event.block.timestamp;
  entity.transactionHash = event.transaction.hash;

  entity.save();
}

export function handleTransfer(event: TransferEvent): void {
  let entity = new Transfer(
    event.transaction.hash.concatI32(event.logIndex.toI32())
  );
  entity.from = event.params.from;
  entity.to = event.params.to;
  entity.value = event.params.value;

  entity.blockNumber = event.block.number;
  entity.blockTimestamp = event.block.timestamp;
  entity.transactionHash = event.transaction.hash;

  entity.save();

  // Load or create the 'from' account
  let fromAccount = Account.load(event.params.from.toHex());
  if (fromAccount == null) {
    fromAccount = new Account(event.params.from.toHex());
    fromAccount.balance = BigInt.fromI32(0);
  }

  // Load or create the 'to' account
  let toAccount = Account.load(event.params.to.toHex());
  if (toAccount == null) {
    toAccount = new Account(event.params.to.toHex());
    toAccount.balance = BigInt.fromI32(0);
  }

  // Update balances
  fromAccount.balance = fromAccount.balance.minus(event.params.value);
  toAccount.balance = toAccount.balance.plus(event.params.value);

  // Save the updated accounts
  fromAccount.save();
  toAccount.save();
}
  1. Deploy the Subgraph using the Alchemy Subgraph Service:

# Run a following command to deploy
# Ensure "DEPLOY_KEY" is provided
graph deploy kip-token-example \
  --version-label v0.0.1-new-version \
  --node https://subgraphs.alchemy.com/api/subgraphs/deploy \
  --deploy-key <YOUR_DEPLOY_KEY> \
  --ipfs https://ipfs.satsuma.xyz
  1. Once successful, you will see output similar to the following:

(base)   kip-token-example git:(main)  graph deploy kip-token-example \
  --version-label v0.0.1-new-version \
  --node https://subgraphs.alchemy.com/api/subgraphs/deploy \
  --deploy-key <DEPLOY_KEY> \
  --ipfs https://ipfs.satsuma.xyz
  Skip migration: Bump mapping apiVersion from 0.0.1 to 0.0.2
  Skip migration: Bump mapping apiVersion from 0.0.2 to 0.0.3
  Skip migration: Bump mapping apiVersion from 0.0.3 to 0.0.4
  Skip migration: Bump mapping apiVersion from 0.0.4 to 0.0.5
  Skip migration: Bump mapping apiVersion from 0.0.5 to 0.0.6
  Skip migration: Bump manifest specVersion from 0.0.1 to 0.0.2
  Skip migration: Bump manifest specVersion from 0.0.2 to 0.0.4
 Apply migrations
 Load subgraph from subgraph.yaml
  Compile data source: Token20 => build/Token20/Token20.wasm
 Compile subgraph
  Copy schema file build/schema.graphql
  Write subgraph file build/Token20/abis/Token20.json
  Write subgraph manifest build/subgraph.yaml
 Write compiled subgraph to build/
  Add file to IPFS build/schema.graphql
                .. QmZv5PwfaCWqc8YBTsDTeWAy6tk7Z8BmBXuCbM2jfxogyJ
  Add file to IPFS build/Token20/abis/Token20.json
                .. QmS8XZ3qoU9qndqT7GCmhzfP2NxGECp1cY1rMzEPh6cCjE
  Add file to IPFS build/Token20/Token20.wasm
                .. QmXJpkYRAbzZEYo4VP335YsQJ1fGHdT84aW74pXQZBitUD
 Upload subgraph to IPFS

Build completed: QmQq1d3Vu6qDho8KrNPzrkfqdvgo4LX2ou7W5PUbJ5tQVm

Deployed to https://subgraphs.alchemy.com/subgraphs/7004/versions/23759

Subgraph endpoints:
Queries (HTTP):     https://subgraph.satsuma-prod.com/kip-team/kip-token-example/version/v0.0.1-new-version/api

Congratulations! You have successfully deployed the Subgraph to the Alchemy Subgraph Hosted Service.

Last updated