子图 > 开发 > 创建 > 汇编脚本API
3 分钟
图形类型脚本库 (graph-ts)
要部署到 The Graph写入子图的 TypeScript/AssemblyScript 库。
使用方法
关于如何创建子图的详细指南,请参阅 GraphCLI 文档。
创建子图的步骤是编写将处理区块链事件的映射,并将实体 写入存储。 这些映射都以 TypeScript/AssemblyScript 编写。
graph-ts
库提供 API,访问the Graph节点存储、区块链数据、智能
合约、IPFS数据、加密功能等数据。 若要使用它,您必须做的就是添加一个
依赖于它:
1npm install --dev @graphprotocol/graph-ts # NPM2yarn add --dev @graph/graph-ts # Yarn
然后,您可以在您的映射中导入这个库的 store
API 和其他功能。
几个例子:
1import { crypto, store } from '@graphprotocol/graph-ts'2// This is just an example event type generated by `graph-cli`3// from an Ethereum smart contract ABI4import { NameRegistered } from './types/abis/SomeContract'5// This is an example of an entity type generated from a6// subgraph's GraphQL schema7import { Domain } from './types/schema'89function handleNameRegistered(event: NameRegistered) {10 // Example use of a crypto function11 let id = crypto.keccak256(name).toHexString()1213 // Example use of the generated `Entry` class14 let domain = new Domain()15 domain.name = name16 domain.owner = event.params.owner17 domain.timeRegistered = event.block.timestamp1819 // Example use of the store API20 store.set('Name', id, entity)21}
AssemblyScript 的辅助函数
参考
这个
版本库用于一些共同的函数的助手函数
,这些函数有助于在 AssemblyScript 库顶端上建构, 例如
字节数组会合等。
API
API 上的文档可以在这里找到 。
对于所用的graph-ts
的示例,请看下面的子图之一:
- https://github.com/graphprotocol/ens-subgraph
- https://github.com/graphprotocol/decentraland-subgraph
- https://github.com/graphprotocol/adchain-subgraph
- https://github.com/graphprotocol/0x-subgraph
- https://github.com/graphprotocol/aragon-subgraph
- https://github.com/graphprotocol/dharma-subgraph
许可协议
版权所有 © 2018 Graph协议、 公司和贡献者。
GraphTypeScript 库是 MIT license 和 Apache License, 版本 2.0的双向授权。
除非适用法律要求或书面同意,否则根据许可证分发的软件 按“原样”分发,不附带任何明示或明示的保证或条件 暗指的。有关管理许可和限制的特定语言,请参阅许可证 许可证。