Web3 数据索引新利器:用 The Graph 打造 NFT 市场子图全攻略

Web3数据索引新利器:用TheGraph打造NFT市场子图全攻略Web3浪潮席卷而来,区块链数据的查询与索引成为去中心化应用(dApp)开发的核心挑战。TheGraph作为Web3数据索引的“新利器”,以其去中心化协议和高效查询能力,彻底简化了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 更快和更容易。

thegraph

为NFTMarket创建一个The Graph子图

学习资料

步骤

  1. 安装Graph CLI 在本地环境中安装Graph CLI工具
  2. 初始化子图 使用Graph CLI初始化一个新的子图
  3. 配置子图(subgraph.yaml) 设置要索引的NFTMarket合约和List、Buy事件
  4. 定义Schema(schema.graphql) 定义List和Buy实体
  5. 编写映射(mapping.ts) 编写映射逻辑,以处理合约事件并更新子图的存储
  6. 部署子图 使用Graph CLI工具部署子图到The Graph Studio。

实操

第一步:创建子图

image-20240719152946198.png

第二步:填写子图名称

image-20240719153052059.png

第三步:填写描述信息和源码链接(注意:描述信息必填否则不能保存)

image-20240719153315148.png

第四步:点击 Save 后即可根据右方的命令去执行对应的操作

image-20240719153716869.png

第五步:安装 GRAPH CLI

pnpm install -g @graphprotocol/graph-cli

第六步:初始化子图

graph init --studio nftmarkethub

image-20240719155025236.png

第七步:认证

graph auth --studio c982cd704d2e5525feae40467e1937db

image-20240719155212470.png

第八步:切换目录

cd nftmarkethub

image-20240719155321324.png

第九步:BUILD 子图

graph codegen && graph build

image-20240719155441488.png

第十步:部署子图

graph deploy --studio nftmarkethub

部署失败

image-20240719161346275.png

试图查询失败信息

image-20240720105810386.png

方法一:使用 alchemy 问题解决,部署成功!

image-20240721113552746.png

方法二:重新初始化部署,部署成功


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...

剩余50%的内容订阅专栏后可查看

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
寻月隐君
寻月隐君
0xE91e...6bE5
不要放弃,如果你喜欢这件事,就不要放弃。如果你不喜欢,那这也不好,因为一个人不应该做自己不喜欢的事。