Subgraph 示例 - OpenZeppelin 文档

本文提供了两个 subgraph 的示例,展示了如何配置 subgraph manifest 文件来索引以太坊 Goerli 网络上的智能合约事件。第一个示例针对简单的 ERC20 代币,第二个示例扩展到包括 Ownable、Pausable 和 TimelockController 合约,演示了如何处理多个数据源。

Subgraph 示例

基础应用

App 描述

我们将考虑一个假设的 dapp,在 Goerli 网络上,它包含:

  • 一个地址为 0x163AE1e077232D6C34E1BF14fA58aA74518886Cc,部署在 5059780 区块的 ERC20 token。

Manifest

该 subgraph 的 manifest 如下所示。请注意,根据你的文件夹布局,相对路径可能会更改:

specVersion: 0.0.2
schema:
  file: ../node_modules/@openzeppelin/subgraphs/generated/erc20.schema.graphql
dataSources:
  - kind: ethereum/contract
    name: erc20
    network: goerli
    source:
      address: "0x163AE1e077232D6C34E1BF14fA58aA74518886Cc"
      abi: IERC20
      startBlock: 5059780
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.4
      language: wasm/assemblyscript
      entities:
        - ERC20Contract
      abis:
        - name: IERC20
          file: ../node_modules/@openzeppelin/contracts/build/contracts/IERC20Metadata.json
      eventHandlers:
        - event: Approval(indexed address,indexed address,uint256)
          handler: handleApproval
        - event: Transfer(indexed address,indexed address,uint256)
          handler: handleTransfer
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/erc20.ts

包含 2 个数据源的应用

App 描述

对于第二个例子,我们将扩展我们的例子以包含更多功能:

  • 一个 ERC20 token,地址为 0x163AE1e077232D6C34E1BF14fA58aA74518886Cc,部署在 5059780 区块,并且具有 OwnablePausable 属性。

  • 一个 OpenZeppelin TimelockController,它在内部使用 AccessControl 模式,地址为 0x9949d9507A26b9639B882E15A897A0A79DDf2c94,部署在 5059781 区块。

Manifest

对于第二个例子,我们必须支持多个模块,为了简单起见,我们使用 all.schema.graphql

specVersion: 0.0.2
schema:
  file: ../node_modules/@openzeppelin/subgraphs/generated/all.schema.graphql
dataSources:
  - kind: ethereum/contract
    name: erc20
    network: goerli
    source:
      address: "0x163AE1e077232D6C34E1BF14fA58aA74518886Cc"
      abi: IERC20
      startBlock: 5059780
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.4
      language: wasm/assemblyscript
      entities:
        - ERC20Contract
      abis:
        - name: IERC20
          file: ../node_modules/@openzeppelin/contracts/build/contracts/IERC20Metadata.json
      eventHandlers:
        - event: Approval(indexed address,indexed address,uint256)
          handler: handleApproval
        - event: Transfer(indexed address,indexed address,uint256)
          handler: handleTransfer
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/erc20.ts
  - kind: ethereum/contract
    name: ownable
    network: goerli
    source:
      address: "0x163AE1e077232D6C34E1BF14fA58aA74518886Cc"
      abi: Ownable
      startBlock: 5059780
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.4
      language: wasm/assemblyscript
      entities:
        - Ownable
      abis:
        - name: Ownable
          file: ../node_modules/@openzeppelin/contracts/build/contracts/Ownable.json
      eventHandlers:
        - event: OwnershipTransferred(indexed address,indexed address)
          handler: handleOwnershipTransferred
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/ownable.ts
  - kind: ethereum/contract
    name: pausable
    network: goerli
    source:
      address: "0x163AE1e077232D6C34E1BF14fA58aA74518886Cc"
      abi: Pausable
      startBlock: 5059780
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.4
      language: wasm/assemblyscript
      entities:
        - Pausable
      abis:
        - name: Pausable
          file: ../node_modules/@openzeppelin/contracts/build/contracts/Pausable.json
      eventHandlers:
        - event: Paused(address)
          handler: handlePaused
        - event: Unpaused(address)
          handler: handleUnpaused
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/pausable.ts
  - kind: ethereum/contract
    name: timelock
    network: goerli
    source:
      address: "0x9949d9507A26b9639B882E15A897A0A79DDf2c94"
      abi: Timelock
      startBlock: 5059781
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.4
      language: wasm/assemblyscript
      entities:
        - Timelock
      abis:
        - name: Timelock
          file: ../node_modules/@openzeppelin/contracts/build/contracts/TimelockController.json
      eventHandlers:
        - event: CallScheduled(indexed bytes32,indexed uint256,address,uint256,bytes,bytes32,uint256)
          handler: handleCallScheduled
        - event: CallExecuted(indexed bytes32,indexed uint256,address,uint256,bytes)
          handler: handleCallExecuted
        - event: Cancelled(indexed bytes32)
          handler: handleCancelled
        - event: MinDelayChange(uint256,uint256)
          handler: handleMinDelayChange
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/timelock.ts
  - kind: ethereum/contract
    name: accesscontrol
    network: goerli
    source:
      address: "0x9949d9507A26b9639B882E15A897A0A79DDf2c94"
      abi: AccessControl
      startBlock: 5059781
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.4
      language: wasm/assemblyscript
      entities:
        - AccessControl
      abis:
        - name: AccessControl
          file: ../node_modules/@openzeppelin/contracts/build/contracts/AccessControl.json
      eventHandlers:
        - event: RoleAdminChanged(indexed bytes32,indexed bytes32,indexed bytes32)
          handler: handleRoleAdminChanged
        - event: RoleGranted(indexed bytes32,indexed address,indexed address)
          handler: handleRoleGranted
        - event: RoleRevoked(indexed bytes32,indexed address,indexed address)
          handler: handleRoleRevoked
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/accesscontrol.ts

← 自动生成

查询示例 →

  • 原文链接: docs.openzeppelin.com/su...
  • 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
OpenZeppelin
OpenZeppelin
江湖只有他的大名,没有他的介绍。