🟢APIs

This section provides comprehensive guidance on setting up, deploying, and using a custom subgraph based on Goldsky. It is designed to simplify querying blockchain data for StoryHunt’s native AMM DEX.

Prerequisites


Before you begin, ensure your system has the following tools installed:

  • Node.js (>=16.x) and Yarn (>=1.22.x)

  • Git

  • The Graph CLI: Install globally with npm install -g @graphprotocol/graph-cli.

Installation Steps


1. Install Goldsky CLI and Log In

  1. Install Goldsky CLI:

    curl https://goldsky.com | sh
  2. Log In via CLI:

    • Create an API Key in your Goldsky project’s Settings.

    • Log in using:

      goldsky login
  3. Verify Installation: Run:

    goldsky

    This should display the available commands.


Set Up Your Subgraph

Clone Repository

git clone https://github.com/0xstoryhunt/v3-subgraph.git
cd v3-subgraph

Install Dependencies

yarn install

Create subgraph.yaml

Create a subgraph.yaml file in the root directory with the following:

specVersion: 0.0.4
description: StoryHunt - first IPFi Story Native AMM DEX to trade Tokens, Memes and IPs. Anything and Everything.
repository: https://github.com/0xstoryhunt/v3-subgraph.git
schema:
  file: ./schema.graphql
features:
  - nonFatalErrors
  - grafting
dataSources:
  - kind: ethereum/contract
    name: Factory
    network: odyssey-testnet
    source:
      address: <FACTORY_CONTRACT_ADDRESS>
      startBlock: <START_BLOCK>
      abi: Factory
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.7
      language: wasm/assemblyscript
      file: ./src/mappings/factory.ts
      entities:
        - Pool
        - Token
      abis:
        - name: Factory
          file: ./abis/factory.json
        - name: ERC20
          file: ./abis/ERC20.json
        - name: ERC20SymbolBytes
          file: ./abis/ERC20SymbolBytes.json
        - name: ERC20NameBytes
          file: ./abis/ERC20NameBytes.json
        - name: Pool
          file: ./abis/pool.json
      eventHandlers:
        - event: PoolCreated(indexed address,indexed address,indexed uint24,int24,address)
          handler: handlePoolCreated

  - kind: ethereum/contract
    name: NonfungiblePositionManager
    network: odyssey-testnet
    source:
      address: <NONFUNGIBLEPOSITIONMANAGER_CONTRACT_ADDRESS>
      startBlock: <START_BLOCK>
      abi: NonfungiblePositionManager
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.7
      language: wasm/assemblyscript
      file: ./src/mappings/position-manager.ts
      entities:
        - Pool
        - Token
        - Position # Include the Position entity
      abis:
        - name: NonfungiblePositionManager
          file: ./abis/NonfungiblePositionManager.json
        - name: Pool
          file: ./abis/pool.json
        - name: Factory
          file: ./abis/factory.json
        - name: ERC20
          file: ./abis/ERC20.json
      eventHandlers:
        - event: Collect(indexed uint256,address,uint256,uint256)
          handler: handleCollect
        - event: DecreaseLiquidity(indexed uint256,uint128,uint256,uint256)
          handler: handleDecreaseLiquidity
        - event: IncreaseLiquidity(indexed uint256,uint128,uint256,uint256)
          handler: handleIncreaseLiquidity
        - event: Transfer(indexed address,indexed address,indexed uint256)
          handler: handleTransfer

templates:
  - kind: ethereum/contract
    name: Pool
    network: odyssey-testnet
    source:
      abi: Pool
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.7
      language: wasm/assemblyscript
      file: ./src/mappings/pool/index.ts
      entities:
        - Pool
        - Token
      abis:
        - name: Pool
          file: ./abis/pool.json
        - name: Factory
          file: ./abis/factory.json
        - name: ERC20
          file: ./abis/ERC20.json
      eventHandlers:
        - event: Initialize(uint160,int24)
          handler: handleInitialize
        - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24)
          handler: handleSwap
        - event: Mint(address,indexed address,indexed int24,indexed int24,uint128,uint256,uint256)
          handler: handleMint
        - event: Burn(indexed address,indexed int24,indexed int24,uint128,uint256,uint256)
          handler: handleBurn
        - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128)
          handler: handleCollect

Customize the File

Replace placeholders like <START_BLOCK> and <FACTORY_CONTRACT_ADDRESS> with your actual contract details.


Define the Schema

In schema.graphql, define entities:

type Pool @entity {
  id: ID!
  token0: Token!
  token1: Token!
  feeTier: BigInt!
}

type Token @entity {
  id: ID!
  symbol: String!
  name: String!
  decimals: BigInt!
}
...
# Refer to git repository for more info

Implement Mappings

Mappings transform events into queryable data. Example for factory.ts:

import { PoolCreated } from "../../generated/Factory/Factory";
import { Pool, Token } from "../../generated/schema";
import { Pool as PoolTemplate } from "../../generated/templates";

export function handlePoolCreated(event: PoolCreated): void {
  let token0 = new Token(event.params.token0.toHexString());
  token0.symbol = fetchTokenSymbol(event.params.token0);
  token0.save();

  let token1 = new Token(event.params.token1.toHexString());
  token1.symbol = fetchTokenSymbol(event.params.token1);
  token1.save();

  let pool = new Pool(event.params.pool.toHexString());
  pool.token0 = token0.id;
  pool.token1 = token1.id;
  pool.feeTier = event.params.fee;
  pool.save();

  PoolTemplate.create(event.params.pool);
}

Generate Code and Build

npm run codegen
npm run build

Deploy to Goldsky

goldsky subgraph deploy <your-subgraph-name>/<version>

Subgraph initialization complete!

Our subgraph has now been successfully deployed to Goldsky. The wizard provides a summary of the files written locally, the builds and deploys that were performed, and links to the subgraph dashboard and the GraphiQL web interface to query the subgraph data.

you can make use of the subgraph queries url to init the wrapper-sdk

Example subgraph queries url/ endpoint :

https://api.goldsky.com/api/public/…/subgraphs/your-subgraph-name/1.0.0/gn

Testing and Debugging

Additional Resources


By following these steps and examples, you can fully integrate subgraph with the Storyhunt V3 contracts while leveraging Goldsky for efficient querying.