// Import the Transfer event class generated from the ERC20 ABI
import { Transfer as TransferEvent } from '../generated/ERC20/ERC20'
// Import the Transfer entity type generated from the GraphQL schema
import { Transfer } from '../generated/schema'
// Transfer event handler
export function handleTransfer(event: TransferEvent): void {
// Create a Transfer entity, using the hexadecimal string representation
// of the transaction hash as the entity ID
let id = event.transaction.hash.toHex()
let transfer = new Transfer(id)
// Set properties on the entity, using the event parameters
transfer.from = event.params.from
transfer.to = event.params.to
transfer.amount = event.params.amount
// Save the entity to the store
transfer.save()
}
在处理区块链数据遇到 Transfer 事件时,它将使用生成的 Transfer 类型(实为 TransferEvent 的别名,以避免与实体类型的命名冲突)传递给handleTransfer事件处理程序。此类型允许访问数据,例如事件的父事务及其参数。
let id = event.transaction.hash.toHex() // or however the ID is constructed
let transfer = Transfer.load(id)
if (transfer == null) {
transfer = new Transfer(id)
}
// Use the Transfer entity as before
// This won't work
entity.numbers.push(BigInt.fromI32(1))
entity.save()
// This will work
let numbers = entity.numbers
numbers.push(BigInt.fromI32(1))
entity.numbers = numbers
entity.save()
let id = event.transaction.hash.toHex()
let transfer = new Transfer(id)
transfer.from = event.params.from
transfer.to = event.params.to
transfer.amount = event.params.amount
transfer.save()
事件、区块/交易数据
传递给事件处理程序的以太坊事件,例如前面示例中的 Transfer 事件,不仅提供了对事件参数的访问,还提供对其父事务及其所属的块的访问。可以从事件实例获得以下数据(这些类是graph-ts中以太坊模块的一部分):
// Import the generated contract class
import { ERC20Contract } from '../generated/ERC20Contract/ERC20Contract'
// Import the generated entity class
import { Transfer } from '../generated/schema'
export function handleTransfer(event: Transfer) {
// Bind the contract to the address that emitted the event
let contract = ERC20Contract.bind(event.address)
// Access state variables and functions by calling them
let erc20Symbol = contract.symbol()
}
以太坊上的 ERC20 智能合约具有一个叫 symbol 的公开的只读函数,就可以使用.symbol()进行调用。对于公共状态变量,将自动创建一个具有相同名称的方法。 子图的任何其他协定都可以从生成的代码中导入,并且可以绑定到有效地址。
记录和调试
import { log } from '@graphprotocol/graph-ts'
日志 API 允许子图将信息记录到 HyperGraph 节点的标准输出以及Graph 浏览器上。 可以使用不同的日志级别记录消息。提供了一种基本的格式字符串语法,以根据参数变量组成日志消息。
log.info('Message to be displayed: {}, {}, {}', [
value.toString(),
anotherValue.toString(),
'already a string',
])
记录一个或多个值 记录单个值
在下面的示例中,字符串值“ A”在被记录之前被传递到一个数组中成为['A']:
let myValue = 'A'
export function handleSomeEvent(event: SomeEvent): void {
// Displays : "My value is: A"
log.info('My value is: {}', [myValue])
}
从现有数组记录单个条目 在下面的示例中,尽管数组包含三个值,但仅记录了该参数数组的第一个值。
let myArray = ['A', 'B', 'C']
export function handleSomeEvent(event: SomeEvent): void {
// Displays : "My value is: A" (Even though three values are passed to `log.info`)
log.info('My value is: {}', myArray)
}
let myArray = ['A', 'B', 'C']
export function handleSomeEvent(event: SomeEvent): void {
// Displays : "My first value is: A, second value is: B, third value is: C"
log.info(
'My first value is: {}, second value is: {}, third value is: {}',
myArray,
)
}
从现有数组记录特定条目 要在数组中显示特定值,必须提供索引值。
export function handleSomeEvent(event: SomeEvent): void {
// Displays : "My third value is C"
log.info('My third value is: {}', [myArray[2]])
}
// Put this inside an event handler in the mapping
let hash = 'QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D'
let data = ipfs.cat(hash)
// Paths like `QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/Makefile`
// that include files in directories are also supported
let path = 'QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/Makefile'
let data = ipfs.cat(path)
import { JSONValue, Value } from '@graphprotocol/graph-ts'
export function processItem(value: JSONValue, userData: Value): void {
// See the JSONValue documentation for details on dealing
// with JSON values
let obj = value.toObject()
let id = obj.get('id').toString()
let title = obj.get('title').toString()
// Callbacks can also created entities
let newItem = new Item(id)
item.title = title
item.parent = userData.toString() // Set parent to "parentId"
item.save()
}
// Put this inside an event handler in the mapping
ipfs.map('Qm...', 'processItem', Value.fromString('parentId'), ['json'])
// Alternatively, use `ipfs.mapJSON`
ipfs.mapJSON('Qm...', 'processItem', Value.fromString('parentId'))