Web3 数据索引新利器:用 The Graph 打造 NFT 市场子图全攻略
Web3 数据索引新利器:用 The Graph 打造 NFT 市场子图全攻略
Web3 浪潮席卷而来,区块链数据的查询与索引成为去中心化应用(dApp)开发的核心挑战。The Graph 作为 Web3 数据索引的“新利器”,以其去中心化协议和高效查询能力,彻底简化了 NFT 市场等场景下的数据处理流程。无论是追踪交易事件还是分析市场动态,The Graph 都能让开发者事半功倍。本文将为您献上一份从零到部署的 NFT 市场子图全攻略,涵盖环境搭建、子图配置、映射编写及部署实战,助您快速掌握 The Graph,解锁 Web3 数据索引的无限可能!
本文深入剖析 The Graph 在 Web3 数据索引中的强大功能,以 NFT 市场为例,详细讲解如何利用 The Graph 构建高效子图。通过清晰的步骤指南,您将学会安装 Graph CLI、初始化子图、配置 NFT 市场合约事件、定义 schema、编写映射逻辑,并最终部署到 The Graph Studio。文章还提供部署失败的解决方案(如使用 Alchemy)和丰富的学习资源,适合 Web3 开发者快速上手。无论是初学者还是资深开发者,都能通过这篇全攻略掌握 The Graph 的核心技能,轻松应对区块链数据挑战。
The Graph是一个强大的去中心化协议,可以无缝地查询区块链数据并将其索引。 它简化了查询区块链数据的复杂过程,使开发dapp 更快和更容易。
为NFTMarket创建一个The Graph子图
学习资料
- 快速入门: <https://thegraph.com/docs/zh/quick-start/>
- 如何编写一个子图的详细介绍 <https://thegraph.com/docs/zh/developing/creating-a-subgraph/>
- 如何查询一个子图的详细介绍 <https://thegraph.com/docs/zh/querying/querying-from-an-application/>
- 中文相关资源列表: <https://www.notion.so/graphprotocolcn/The-Graph-49977afa44644ebf9052b9220f539396>
- The graph bounty中一个比较好的子图的例子: <https://github.com/Autosaida/Zircuit-Restaking-Subgraph/>
- The graph bounty中一个比较好的Usage of Subgraph的例子:<https://github.com/ttttonyhe/stader-graph-dashboard>
步骤
- 安装Graph CLI 在本地环境中安装Graph CLI工具
- 初始化子图 使用Graph CLI初始化一个新的子图
- 配置子图(subgraph.yaml) 设置要索引的NFTMarket合约和List、Buy事件
- 定义Schema(schema.graphql) 定义List和Buy实体
- 编写映射(mapping.ts) 编写映射逻辑,以处理合约事件并更新子图的存储
- 部署子图 使用Graph CLI工具部署子图到The Graph Studio。
实操
第一步:创建子图
第二步:填写子图名称
第三步:填写描述信息和源码链接(注意:描述信息必填否则不能保存)
第四步:点击 Save 后即可根据右方的命令去执行对应的操作
第五步:安装 GRAPH CLI
pnpm install -g @graphprotocol/graph-cli
第六步:初始化子图
graph init --studio nftmarkethub
第七步:认证
graph auth --studio c982cd704d2e5525feae40467e1937db
第八步:切换目录
cd nftmarkethub
第九步:BUILD 子图
graph codegen && graph build
第十步:部署子图
graph deploy --studio nftmarkethub
部署失败
试图查询失败信息
方法一:使用 alchemy 问题解决,部署成功!
方法二:重新初始化部署,部署成功
graph init --studio nftmarkethub
› Warning: In next major version, this flag will be removed. By default we will deploy to the Graph Studio. Learn more about Sunrise of
› Decentralized Data https://thegraph.com/blog/unveiling-updated-sunrise-decentralized-data/
› Warning: In next major version, this flag will be removed. By default we will deploy to the Graph Studio. Learn more about Sunrise of
› Decentralized Data https://thegraph.com/blog/unveiling-updated-sunrise-decentralized-data/
› Warning: In next major version, this flag will be removed. By default we will stop initializing a Git repository.
? Protocol … (node:28795) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Protocol · ethereum
Subgraph slug · nftmarkethub
Directory to create the subgraph in · nftmarkethub
? Ethereum network …
? Ethereum network …
? Ethereum network …
Ethereum network · sepolia
Contract address · 0xbba4229cD53442D56E306379E99332687E1fb31f
Fetching ABI from Etherscan
Failed to fetch Start Block: Failed to fetch contract creation transaction hash
Do you want to retry? (Y/n) · true
Fetching Start Block
Failed to fetch Contract Name: Failed to fetch contract source code
Do you want to retry? (Y/n) · true
Failed to fetch Contract Name: Failed to fetch contract source code
Do you want to retry? (Y/n) · true
Failed to fetch Contract Name: Failed to fetch contract source code
Do you want to retry? (Y/n) · true
Fetching Contract Name
S...
版权声明
本文仅代表作者观点,不代表区块链技术网立场。
本文系作者授权本站发表,未经许可,不得转载。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。