TheGraph 智能合约
npm install -g @graphprotocol/graph-cli
yarn global add @graphprotocol/graph-cli
specVersion: 版本
description: 描述
repository: 子图git仓库地址
schema:
file: 子图数据目录结构文件 (相对路径 ./)
dataSources:
- kind: ethereum/contract (子图类型 合约)
name: 合约名称
network: 网络 (bsc,eth 等)
source:
address: 子图对应合约地址
abi: abi名称
startBlock: 开始查询的区块数
mapping:
kind: ethereum/events (子图类型 合约里的事件)
apiVersion: 0.0.4 (版本)
language: wasm/assemblyscript
entities:
- OptionNft
abis:
- name: OptionNft
file: ./abis/OptionNft.json (相对路径)
eventHandlers:
- event: Transfer(indexed address,indexed address,indexed uint256) (查询事件名称)
handler: handleTransfer (对应函数)
file: ./src/OptionNft.ts (对应函数文件 相对路径)
type Option @entity {
id: ID!
from: Bytes!
to: Bytes!
tokenId: BigInt!
}
或(单层数据结构 多层数据结构)
type Option @entity {
id: ID!
creator: Bytes!
collateral: Bytes!
underlying: Bytes!
strikePrice: BigInt!
expiry: BigInt!
long: Bytes!
short: Bytes!
asks: [OrderAsk!]! @derivedFrom(field: "option")
}
type OrderAsk @entity {
id: ID!
askID: BigInt!
binds: [OrderBid!]! @derivedFrom(field: "orderAsk")
seller: Bytes!
long: Bytes!
volume: BigInt!
settleToken: Bytes!
price: BigInt!
isCancel: Boolean!
option: Option!
}
type OrderBid @entity {
id: ID!
bidID: BigInt!
askID: BigInt!
buyer: Bytes!
volume: BigInt!
amount: BigInt!
orderAsk: OrderAsk!
}
npm i @graphprotocol/graph-ts
yarn add @graphprotocol/graph-ts
import { Transfer } from '../generated/OptionNft/OptionNft'
import { Option } from '../generated/schema'
export function handleTransfer(event: Transfer): void {
let option = new Option(event.params.tokenId.toHex())
option.from = event.params.from
option.to = event.params.to
option.tokenId = event.params.tokenId
option.save()
}
|