GithubHelp home page GithubHelp logo

graph-example-template's Introduction

tenk-subgraph-template

Quick Start

Prerequisites

Creating the Graph project in The Graph dashboard

To get started, open The Graph dashboard and either sign in or create a new account.

Next, click on Add Subgraph to create a new subgraph.

Configure your subgraph with the following properties:

  • Subgraph Name - your-collection-subgraph-name

Once, you see an instance of the subgraph that says “Undeployed”, note down your Access Key seen at the top of the dashboard UI.

Configure template package.json and subgraph.yaml files to your subgraph

package.json

Required fields to replace →

name: with your subgraph name created in the hosted service subgraph dashboard

scripts.auth: with your subgraph access key found on your hosted service dashboard

scripts.deploy: your GitHub user name ‘/’ your subgraph name

scripts. fields: your GitHub user name ‘/’ your subgraph name

{
  "name": "subgraph-name",
  "version": "0.0.0",
  "author": "Ryan Strawser",
  "repository": "https://github.com/r-strawser/tenk-subgraph-template.git",
  "license": "MIT",
  "scripts": {
    "codegen": "graph codegen",
    "build": "graph codegen && graph build",
    "auth": "graph auth --product=hosted-service <your-graph-access-key>",
    "deploy": "graph codegen && graph deploy <github-account/subgraph-name>",
    "create-local": "graph create --node http://localhost:8020/ <github-account/subgraph-name>",
    "remove-local": "graph remove --node http://localhost:8020/ <github-account/subgraph-name>",
    "deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 <github-account/subgraph-name>"
  },
  "devDependencies": {
    "@graphprotocol/graph-cli": "^0.26.0",
    "@graphprotocol/graph-ts": "^0.24.1"
  }
}

subgraph.yaml

Required fields to replace →

dataSources.name: name of contract

dataSources.source.account: contract account_id to index

dataSources.source.startBlock: the block height to start indexing on-chain events from the account.

specVersion: 0.0.4
repository: https://github.com/r-strawser/tenk-subgraph-template.git
schema:
  file: ./schema.graphql
features:
  - fullTextSearch
dataSources:
  - kind: near
    name: futurenft
    network: near-mainnet
    source:
      account: "futurenft.near"
      startBlock: 60461000
    mapping:
      apiVersion: 0.0.5
      language: wasm/assemblyscript
      entities:
        - Account
        - Token
        - Transfer
        - Contract
        - Mint
        - Burn
      receiptHandlers:
        - handler: handleReceipt
      file: ./src/mapping.ts

Configure Mappings

The mappings file in this version of the template is ingesting receipts by checking for the methodNames of the function calls. With the onset of formalizing event standards for NEAR, this topic of ingestion methods is discussed in future work.

mapping.ts

Replace contract fields with nft metdata (3 occurences in mappings) fetched from near-api. Refer to future work for obtained from on-chain activity.

						let contract = Contract.load("futurenft.near")
            if(!contract) {
                contract = new Contract("futurenft.near")
                contract.id = "futurenft.near"
                contract.name = "NEAR Future"
                contract.symbol = "nearfuture"
                contract.base_uri = "https://bafybeigxj7qbjear55zwzfbam42via5c7ycawfpb5lpy2qqxhnomn2lnga.ipfs.dweb.link/"
                contract.media_uri = "https://bafybeiazrw5wem27wql5wojx44yzhoezeh4frkrujdvctrxftv24km7lse.ipfs.dweb.link/"
                contract.copies = BigInt.zero();
                contract.total_minted = BigInt.zero();
                contract.total_burned = BigInt.zero();
            }

Codegen, Build, Auth, and Deploy Subgraph

Run codegen and build the subgraph.

$ yarn build

Assign you access key to the subgraph you are going to deploy.

$ yarn auth

Deploy the subgraph.

$ yarn deploy

Logging On-Chain Activity with The Graph

Handlers

The Graph integration with NEAR allows us to log any on-chain information aggregated through the Firehose, which is maintained by StreamingFast. The Graph currently supports on-chain activity handlers via Block or Receipt triggers. Meaning as the firehose propagates through each indexed block or receipt, a function handler is triggered within the mappings if the account matches an on-chain activity where the receiver_id matches the defined account field in the subgraph manifest. If the account field equals the receiver_id, the trigger or handler is fired.

💡 The account field in the subgraph manifest *equals* the receiver_id of in a receipt

Future Work

  • refactored mappings to ingest and filter NEP formatted event receipt data by the outcome logs
  • time travel queries for snapshot

Better Ingestion Methods

We use functionCall logging in the template, but I have found that there are much better methods of logging that offer for more dynamic event support and contract upgrades. This is due to the way developers include logs in their contracts and solved with best practice and formalized event support which is happening as I mention this!

Function call logging is useful for some instances, such as fetching the result data from a called methodName. Other than that, ingesting receipts and writing mappings from outcomes logs is one of the best ways to write subgraphs. This allows the subgraph to directly search for specific events emitted from the outcome logs, rather than searching for hardcoded methods you anticipate to be called from the code.

Data Source Template Support Integration

Data source template support for NEAR subgraphs open a world of possibilities! One method we discussed was with a .wit file integration within the schema. This is future work and would love to dive more into. Another method that is being discussed is the Event Trigger which can be integrated by The Graph on finalization of the NEP format.

We use functionCall logging in the template,

FAQ

Data source templates → not yet supported. Investigating with .wit files!

graph-example-template's People

Contributors

willemneal avatar

Watchers

Jordan avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.