BlockChain區塊鏈
  • 本書簡介
  • 區塊鏈運作原理
  • Bitcoin介紹
    • 簡介
    • Bitcoin其他知識
  • Bitcoin原理與實作
  • BitcoinJS
    • BTC 地址格式種類
    • 從 Mnemonic 轉為地址
  • Serverless 架構實作
  • Docker常用指令
  • ethereum初探
    • ethereum歷史
    • EVM
  • ethereum PoS 節點架設
  • ethereum(Docker)
  • ethereum(Geth)
    • Parity
  • ethereum(智能合約)
    • 合約測試 Unit Test
    • DAO
    • 可升級合約
    • 使用合約進行 multiswap
    • 合約安全
    • 開發工具
    • Hardhat 教學
      • Hardhat 寫測試
    • ERC-721 範例
      • 白名單機制
    • OpenZeppelin 合約 library
    • Truffle
    • 合約部屬
    • solidity 教學
  • ethereum(Dapp)
    • 相關 SDK
    • Multicall
    • Ethers.js 使用
    • Remix IDE
    • web3.js 使用
    • 在網頁上使用 web3 並操作區塊鏈
      • solidity筆記
  • Hyperledger Fabric
  • blockchainDB
  • 挖礦程式使用教學
    • 門羅幣/Monero (XMR)
  • Bitfinex API 使用
  • FTX API
  • CCXT 通用交易所 API
  • Solana 教學
  • Ethereum BigQuery
  • The Graph
    • yaml 定義
    • mapping 語法
    • Schema 定義
    • Query 範例
    • Unit test
  • DeFi 筆記
    • MEV 相關
    • Dex 聚合
    • Yearn
    • Curve
    • Uniswap
      • Swap 互動
    • AAVE、Compound
      • Compound 原理
      • AAVE 合約開發
Powered by GitBook
On this page
  • 官方文件
  • 本地架設 Graph node 教學
  • 探索已發佈的 Subgraph
  • 使用 OpenZeppelin 範例
  • 查看目前 subgraph 健康度
  • 相關事項
  • 相關範例

Was this helpful?

The Graph

一個用來查詢區塊鏈數據的工具

PreviousEthereum BigQueryNextyaml 定義

Last updated 3 years ago

Was this helpful?

使用 graphQL 數據結構,讓你可以監聽區塊鏈節點的事件或數據,並且發佈到 graph node 上面。之後可以讓大家 Query 使用。需要自己寫一個 subgraph 的邏輯,然後發布到 graph node上。官方有提供 名為 的 graph node。也可以在自己本地架設。

官方文件

本地架設 Graph node 教學

探索已發佈的 Subgraph

使用 OpenZeppelin 範例

將以上 clone 到你的專案或是 npm install @openzeppelin/subgraphs,之後 copy node_module 裡面 config file,然後放到你的專案內

之後輸入以下(要把 --config 後的路徑改成剛的 config file)

npx graph-compiler \
  --config configs/sample.json \
  --include node_modules/@openzeppelin/subgraphs/src/datasources \
  --export-schema \
  --export-subgraph

之後要去把 編譯出來的 yaml 改名為 subgraph.yaml,然後增加 network 與 contract address 等 config field

ERC721 的 subgraph.yaml 範例:

specVersion: 0.0.2
schema:
  file: erc721.schema.graphql
dataSources:
  - kind: ethereum/contract
    name: erc721
    network: mainnet
    source:
      abi: IERC721
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.5
      language: wasm/assemblyscript
      entities:
        - ERC721Contract
      abis:
        - name: IERC721
          file: ../node_modules/@openzeppelin/contracts/build/contracts/IERC721Metadata.json
      eventHandlers:
        - event: Approval(indexed address,indexed address,indexed uint256)
          handler: handleApproval
        - event: ApprovalForAll(indexed address,indexed address,bool)
          handler: handleApprovalForAll
        - event: Transfer(indexed address,indexed address,indexed uint256)
          handler: handleTransfer
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/erc721.ts
graph deploy --studio <yaml 內 dataSources 的 name>

部署到 local 節點就參考:https://thegraph.academy/developers/local-development/

查看目前 subgraph 健康度

hosted service 可用此 endpoint https://api.thegraph.com/index-node/graphq

indexingStatusForCurrentVersion(subgraphName: "org/subgraph") {
  synced
  health
  fatalError {
    message
    block {
      number
      hash
    }
    handler
  }
  chains {
    chainHeadBlock {
      number
    }
    latestBlock {
      number
    }
  }
}

相關事項

graph-cli 版本 0.16 後有改變指令,如果要用舊版 cli 部署到 studio 或 host-service 可以如下使用 --node 參數

const HOSTED_SERVICE_URL = 'https://api.thegraph.com/deploy/'

const SUBGRAPH_STUDIO_URL = 'https://api.studio.thegraph.com/deploy/'

npx graph deploy --access-token=<...> --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs <github username>/<subgraph name>

相關範例

OpenZeppelin:

NFT:

DeFi:

之後看你要部署到 studio 還是自己 host 的節點,如果要部署到 studio 就先去 create subgraph 然後從 cli auth 後 deploy

https://github.com/OpenZeppelin/openzeppelin-subgraphs
https://docs.openzeppelin.com/subgraphs/0.1.x/
https://thegraph.com/studio/
https://github.com/OpenZeppelin/openzeppelin-subgraphs
https://github.com/dabit3/custom-nft-subgraph-workshop
https://github.com/dabit3/bored-ape-yacht-club-api-and-subgraph
https://github.com/dabit3/building-a-subgraph-workshop
https://github.com/token-terminal/tt-subgraphs
https://github.com/messari/subgraphs
studio node
https://thegraph.com/docs/en/
https://thegraph.academy/developers/local-development/
Subgraphs | Graph ExplorerGraph Explorer
Logo