# 快速入门

快速入门

本教程主要是引导您来通过示例和针对合约创建一个子图，以演示整个环境搭建的过程。

1. 首先去[https](https://dashboard.hg.network/)[://dashboard.hg.network](https://dashboard.hg.network/) 去添加一个子图，这个子图请选择部署方式为 API 方式，选择所部署的公链为火币生态链。

![](/files/-MZ3l0v_TuSkmt-Fd6NE)

1. 进入子图列表页，可以看到子图列表出现了刚加的子图，所给的提示也跟其他类型方式添加的子图不同，有一个提示按钮

![](/files/-MZ3l0vaqRanOggYI_qv)

1. 点击提示按钮，显示出来了详细的部署开发过程中的命令

![](/files/-MZ3ov3c8408DvWTlil4)

下面，我们就根据这些命令来开启子图开发。

1. 初始化一个新子图

初始化新子图要安装 graph-cli 开发包，以使用graph命令来初始化新子图。

可以使用 npm 或者 yarn 来进行全局安装，命令如下：

`# NPM`

`$ npm install -g @hgdotnetwork/graph-cli`

`# Yarn`

`$ yarn global add @hgdotnetwork/graph-cli`

在安装的过程中，可能会出错找不到libsecret 库的情况，可以另行安装，比如在Ubuntu 上，通过apt search libsecret ，找到，然后再使用命令 apt install libsecret-1-dev 安装即可。

安装完成，我们执行 graph --help 会得到如下所示的内容：

`graph version 0.20.2`

&#x20;`graph -`

&#x20;`build Builds a subgraph and (optionally) uploads it to IPFS`

&#x20;`codegen Generates AssemblyScript types for a subgraph`

&#x20;`create Registers a subgraph name`

&#x20;`auth Sets the access token to use when deploying to a Graph node`

&#x20;`init Creates a new subgraph with basic scaffolding`

&#x20;`remove Unregisters a subgraph name`

&#x20;`test Runs tests against a Graph Node environment (using Ganache by default)`

&#x20;`help (h) -`

&#x20;`version (v) Output the version number`

&#x20;`deploy Deploys the subgraph to a Graph node`

这些命令后面都有比较详细的说明，在后续的流程中，也会提到这些命令的用法，所以这里暂时不展开。

通过这些步骤，我们就安装好了graph 的命令行环境，就可以进行subgraph的开发和部署了。

首先我们通过graph init 创建一个示例项目来了解。

`graph init [options] [subgraph-name] [directory]`

`Options:`

&#x20;`--allow-simple-name Use a subgraph name without a prefix (default: false)`

&#x20;`-h, --help Show usage information`

`Choose mode with one of:`

&#x20;`--from-contract <address> Creates a scaffold based on an existing contract`

&#x20;`--from-example Creates a scaffold based on an example subgraph`

`Options for --from-contract:`

&#x20;`--abi <path> Path to the contract ABI (default: download from Etherscan)`

&#x20;`--network <mainnet|kovan|rinkeby|ropsten|goerli|poa-core|poa-sokol|xdai|matic|mumbai|fantom|bsc|heco|clover>`

&#x20;`Selects the network the contract is deployed to`

&#x20;`--index-events Index contract events as entities`

&#x20;`--contract-name Name of the contract (default: Contract)`

从上面的参数可以看到，可以从示例项目创建项目的脚手架

`$ graph init --from-example <SUBGRAPH_NAME> [<DIRECTORY>]`

也可以从合约创建项目

`$ graph init --from-contract <CONTRACT_ADDRESS> <SUBGRAPH_NAME> --network <NETWORK> [<DIRECTORY>]`

\<SUBGRAPH\_NAME>  参数在HyperGraph网络中，建议就是在创建的“子图名称/公链名称”构成的，也就是在子图详情页看到的子图名称，比如“subgraphdemo/heco”。

\<DIRECTORY> 就是项目的目录

\<CONTRACT\_ADDRESS> 就是合约地址，就是子图想要解析的合约地址

\<NETWORK> 即想合约所部署公链所在的网络名称，比如币安智能链就是bsc、火币生态链就是 Heco

下面演示使用一下：

`graph init --from-example subgraphdemo/heco --network heco`

我们就可以成功地基于示例项目创建一个脚手架项目。

执行输出如下图所示：

![](/files/-MZ3l0vc1gJ9QSLXsT5E)

如何基于合约创建也演示一下：

`graph init subgraphdemo/heco --network heco --from-contract 0x0bb480582ecae1d22bbaeaccfbb849b441450026`

如果正常运行，可以得到如下输出：

![](/files/-MZ3l0vdZpZdUmW2_1lb)

从上面的输出可以看到，指定合约和网络，生成一个子图项目，会连接相应的网络的公链浏览器，取回来相应的合约ABI 合约接口定义文件。

如果真的要部署此子图，请在HyperGraph控制台，即[https://dashboard.hg.network](https://dashboard.hg.network/) 也要提交此项目。

![](/files/-MZ3l0veNIdfj0Wys_At)

有了这个脚手架项目，我们就能正式进行子图开发，来在子图代码中，对合约的事情进行解析等，后续章节将继续进行讲解。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hg.network/product-chan-pin-bang-zhu/cheng-xu-kai-fa/ben-di-kai-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
