|
6 | 6 |
|
7 | 7 | ## 流程概述 |
8 | 8 |
|
9 | | -- 在 Goerli 部署一个合约,并调用触发事件。 |
| 9 | +- 在 Optimism 部署一个合约,并调用触发事件。 |
10 | 10 | - 创建定义数据索引的 Subgraph。 |
11 | 11 | - 部署 Subgraph 到 TheGraph,实现数据索引。 |
12 | 12 | - 在前端 DApp 中查询索引数据。 |
@@ -61,191 +61,12 @@ TheGraph 中定义如何为数据建立索引,称为 Subgraph,它包含三 |
61 | 61 |
|
62 | 62 | 输入你的项目名称(例如 TEST01),以下称之为 `<SUBGRAPH_NAME>`,点击 continue 按钮,之后会跳转到 subgraph 的项目主页 |
63 | 63 |
|
64 | | - 注:最新版的 Graph CLI 仅支持在 mainnet 和 goerli 上部署,若要在其他网络上使用,需要使用 Github 账户登录后在 Hosted Service 上创建和部署 |
65 | 64 |
|
66 | 65 | 5. 开发和部署 subgraph |
67 | 66 |
|
68 | | - 先使用 yarn 在全局安装 Graph CLI |
| 67 | + 参考 [subgraphs-quick-start](https://thegraph.com/docs/en/subgraphs/quick-start/) 初始化、部署 subgraph 项目。 |
| 68 | + 其中需要注意的是,在 Initial Subgraph 项目的时候,需要选择真实的区块链网络 ( 如 Optimism ),并输入真实的 Contract address |
69 | 69 |
|
70 | | - ```bash |
71 | | - yarn global add @graphprotocol/graph-cli |
72 | | - ``` |
73 | | - |
74 | | -6. 初始化配置: |
75 | | - |
76 | | - ```bash |
77 | | - graph init --studio <SUBGRAPH_NAME> |
78 | | - ``` |
79 | | - |
80 | | - 若使用 Hosted Service,则初始化命令如下: |
81 | | - |
82 | | - ```bash |
83 | | - graph init --product hosted-service <GITHUB_USER>/<SUBGRAPH NAME> |
84 | | - ``` |
85 | | - |
86 | | - - Protocol 选择ethereum |
87 | | - - 在 "Subgraph slug" 和 "Directory to create the subgraph" 直接回车即可 |
88 | | - - Ethereum network 这里选择 sepolia |
89 | | - - "Contract address" 这里输入在步骤 3 中部署合约时生成的合约地址 |
90 | | - - 上面执行到 "fetch ABI from Etherscan" 时会报执行失败,然后出现 "ABI file (path)" 字样,提示输入本机中 abi 的文件路径,这里我们输入 SimpleToken.json 所在的路径即可(`./abis/SimpleToken.json`) |
91 | | - 。如果已经成功执行 07-hardhat , 同时在hardhat.config.js 里配置了ethescan,此处执行会通过 |
92 | | - -"fetch Start Block"执行失败后,retry输入n,“Start Block”,“Contract Name”默认回车。 “Add another contract?” 输入n |
93 | | - - 如果 yarn install 失败(例如网络错误),可以进入新生成的项目目录,手动安装 npm 依赖 |
94 | | - |
95 | | -7. 修改定义模式 |
96 | | - |
97 | | - - 两个文件的修改范例在 `./scripts/schema.graphql` 和 `./scripts/mapping.ts` |
98 | | - |
99 | | - - `<SUBGRAPH_NAME>/schema.graphql` 修改文件内容如下 |
100 | | - |
101 | | - ```graphql |
102 | | - type TransferEntity @entity { |
103 | | - id: ID! |
104 | | - from: Bytes! # address |
105 | | - to: Bytes! # address |
106 | | - value: BigInt! |
107 | | - } |
108 | | - |
109 | | - type ApprovalEntity @entity { |
110 | | - id: ID! |
111 | | - owner: Bytes! # address |
112 | | - spender: Bytes! # address |
113 | | - value: BigInt! |
114 | | - } |
115 | | - ``` |
116 | | - |
117 | | - - `<SUBGRAPH_NAME>/src/mapping.ts` 修改文件内容如下 |
118 | | - |
119 | | - ```ts |
120 | | - import { BigInt } from '@graphprotocol/graph-ts'; |
121 | | - import { SimpleToken, Transfer, Approval } from '../generated/SimpleToken/SimpleToken'; |
122 | | - import { TransferEntity, ApprovalEntity } from '../generated/schema'; |
123 | | - |
124 | | - export function handleTransfer(event: Transfer): void { |
125 | | - // Entities can be loaded from the store using a string ID; this ID |
126 | | - // needs to be unique across all entities of the same type |
127 | | - let entity = TransferEntity.load(event.transaction.from.toHex()); |
128 | | - |
129 | | - // Entities only exist after they have been saved to the store; |
130 | | - // `null` checks allow to create entities on demand |
131 | | - if (entity == null) { |
132 | | - entity = new TransferEntity(event.transaction.from.toHex()); |
133 | | - } |
134 | | - |
135 | | - // BigInt and BigDecimal math are supported |
136 | | - entity.value = event.params.value; |
137 | | - |
138 | | - // Entity fields can be set based on event parameters |
139 | | - entity.from = event.params.from; |
140 | | - entity.to = event.params.to; |
141 | | - |
142 | | - // Entities can be written to the store with `.save()` |
143 | | - entity.save(); |
144 | | - |
145 | | - // Note: If a handler doesn't require existing field values, it is faster |
146 | | - // _not_ to load the entity from the store. Instead, create it fresh with |
147 | | - // `new Entity(...)`, set the fields that should be updated and save the |
148 | | - // entity back to the store. Fields that were not set or unset remain |
149 | | - // unchanged, allowing for partial updates to be applied. |
150 | | - |
151 | | - // It is also possible to access smart contracts from mappings. For |
152 | | - // example, the contract that has emitted the event can be connected to |
153 | | - // with: |
154 | | - // |
155 | | - // let contract = Contract.bind(event.address) |
156 | | - // |
157 | | - // The following functions can then be called on this contract to access |
158 | | - // state variables and other data: |
159 | | - // |
160 | | - // - contract.approve(...) |
161 | | - // - contract.totalSupply(...) |
162 | | - // - contract.transferFrom(...) |
163 | | - // - contract.increaseAllowance(...) |
164 | | - // - contract.balanceOf(...) |
165 | | - // - contract.decreaseAllowance(...) |
166 | | - // - contract.transfer(...) |
167 | | - // - contract.allowance(...) |
168 | | - } |
169 | | - |
170 | | - export function handleApproval(event: Approval): void { |
171 | | - // Entities can be loaded from the store using a string ID; this ID |
172 | | - // needs to be unique across all entities of the same type |
173 | | - let entity = ApprovalEntity.load(event.transaction.from.toHex()); |
174 | | - |
175 | | - // Entities only exist after they have been saved to the store; |
176 | | - // `null` checks allow to create entities on demand |
177 | | - if (entity == null) { |
178 | | - entity = new ApprovalEntity(event.transaction.from.toHex()); |
179 | | - } |
180 | | - |
181 | | - // BigInt and BigDecimal math are supported |
182 | | - entity.value = event.params.value; |
183 | | - |
184 | | - // Entity fields can be set based on event parameters |
185 | | - entity.owner = event.params.owner; |
186 | | - entity.spender = event.params.spender; |
187 | | - |
188 | | - // Entities can be written to the store with `.save()` |
189 | | - entity.save(); |
190 | | - } |
191 | | - ``` |
192 | | - |
193 | | -8. 修改实体名字 |
194 | | - |
195 | | - - 进入 graphtest 目录 |
196 | | - - 修改 subgraph.yaml 中 entities 定义如下 |
197 | | - |
198 | | - ```yaml |
199 | | - --- |
200 | | - entities: |
201 | | - - TransferEntity |
202 | | - - ApprovalEntity |
203 | | - ``` |
204 | | - |
205 | | -9. 授权和部署 Subgraph |
206 | | - |
207 | | - 首先获取你的 `<DEPLOY KEY>`,在你的 subgraph 项目主页可以找到: |
208 | | - <center><img src="https://github.com/Dapp-Learning-DAO/Dapp-Learning-Arsenal/blob/main/images/basic/08-hardhat-graph/auth_deploy_key.png?raw=true" /></center> |
209 | | - |
210 | | - - 授权 |
211 | | - |
212 | | - ```bash |
213 | | - graph auth --studio <DEPLOY KEY> |
214 | | - |
215 | | - #注意需要按截图所示点击copy key按钮,并替换<DEPLOY KEY> , 不要直接copy 官网右侧的代码,因为key不全 |
216 | | - ``` |
217 | | - |
218 | | - 若使用 Hosted Service,则初始化命令如下: |
219 | | - |
220 | | - ```bash |
221 | | - graph auth --product hosted-service <ACCESS_TOKEN> |
222 | | - ``` |
223 | | - |
224 | | - - 进入 subgraph 的本地目录 |
225 | | - |
226 | | - ```bash |
227 | | - cd ./<SUBGRAPH_NAME> |
228 | | - ``` |
229 | | - |
230 | | - - BUILD SUBGRAPH |
231 | | - |
232 | | - ```bash |
233 | | - graph codegen && graph build |
234 | | - ``` |
235 | | - |
236 | | - - DEPLOY SUBGRAPH |
237 | | - |
238 | | - ```bash |
239 | | - graph deploy --studio <SUBGRAPH_NAME> |
240 | | - ``` |
241 | | - |
242 | | - 若使用 Hosted Service,则初始化命令如下: |
243 | | - |
244 | | - ```bash |
245 | | - graph deploy --product hosted-service <GITHUB_USER>/<SUBGRAPH NAME> |
246 | | - ``` |
247 | | - |
248 | | - - 这里必须输入 `Version Label` , 比如`0.0.1`, 否则会报错提示 `You must provide a version label.` |
249 | 70 |
|
250 | 71 | ## 检验 subgraph 是否部署成功 |
251 | 72 |
|
@@ -403,7 +224,7 @@ subgraph 定义了你希望通过 GraphQL API 提供的数据、数据源和数 |
403 | 224 | Alchemy 也提供了 Subgraph 功能,用户可以轻松的从 Thegraph 上把 Subgraph 迁移到 Alchemy 上来。 |
404 | 225 |
|
405 | 226 | - 部署 |
406 | | -部署流程和 thegraph host service 流程一样,编写完 ts 代码后进行 codegen、build,最后deploy 的时候需要输入 deploy-key 这个参数,这个 key 需要在 Dashboard 界面获取 |
| 227 | +部署流程和 thegraph 流程一样,编写完 ts 代码后进行 codegen、build,最后deploy 的时候需要输入 deploy-key 这个参数,这个 key 需要在 Dashboard 界面获取 |
407 | 228 |
|
408 | 229 | <center><img src="https://github.com/yingjingyang/Imgs-for-tasks-01/blob/main/basic-task/task-08/Alchemy_Subgraph.jpg?raw=true" /></center> |
409 | 230 |
|
|
0 commit comments