在Base上构建子图
Reading time: 3 min
本指南将带你快速了解如何在Base测试网上初始化、创建和部署子图。
你将需要:
- A Base Sepolia testnet contract address
- 一个加密货币钱包(例如MetaMask或Coinbase钱包)
Graph CLI(>=v0.41.0) 是用 JavaScript 编写的,你需要预安装npm 或yarn 包管理器才能使用。
# NPMnpm install -g @graphprotocol/graph-cli# Yarnyarn global add @graphprotocol/graph-cli
Go to and connect your crypto wallet.
Once connected, click "Create a Subgraph", enter a name for your subgraph and click Create a Subgraph.
You can find specific commands for your subgraph in Subgraph Studio.
确保graph-cli 更新到最新版本 (0.41.0以上)
graph --version
从现有合约初始化子图。
graph init --studio <SUBGRAPH_SLUG>
子图slug是子图的标识符。CLI工具将引导您完成创建子图的步骤,包括:
- 协议:ethereum
- 子图slug:
<SUBGRAPH_SLUG>
- 创建子图的目录:
<SUBGRAPH_SLUG>
- Ethereum network: base-sepolia
- Contract address:
<CONTRACT_ADDRESS>
- Start block (optional)
- Contract name:
<CONTRACT_NAME>
- Yes/no to indexing events (yes means your subgraph will be bootstrapped with entities in the schema and simple mappings for emitted events)
If emitted events are the only thing you want to index, then no additional work is required, and you can skip to the next step.
The previous command creates a scaffold subgraph that you can use as a starting point for building your subgraph. When making changes to the subgraph, you will mainly work with three files:
- Manifest (subgraph.yaml) - The manifest defines what datasources your subgraphs will index. Make sure to add
base-sepolia
as the network name in manifest file to deploy your subgraph on Base Sepolia. - Schema (schema.graphql) - The GraphQL schema defines what data you wish to retreive from the subgraph.
- AssemblyScript 映射(mapping.ts)--将数据源中的数据转换为模式中定义的实体的代码。
If you want to index additional data, you will need extend the manifest, schema and mappings.
For more information on how to write your subgraph, see .
Before you can deploy your subgraph, you will need to authenticate with Subgraph Studio. You can do this by running the following command:
Authenticate the subgraph on studio
graph auth --studio <DEPLOY_KEY>
Next, enter your subgraph's directory.
cd <SUBGRAPH_DIRECTORY>
Build your subgraph with the following command:
```graph codegen && graph build```
Finally, you can deploy your subgraph using this command:
```graph deploy --studio <SUBGRAPH_SLUG>```
Once your subgraph is deployed, you can query it from your dapp using the Development Query URL
in Subgraph Studio.
Note - Studio API is rate-limited. Hence should preferably be used for development and testing.
To learn more about querying data from your subgraph, see the page.