Skip to content

Commit a1676f2

Browse files
authored
ZetaChain Quickstart Featured (#541)
1 parent c2d3831 commit a1676f2

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

docs/.vuepress/sidebar.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ export const getSidebar = (locale: string) =>
233233
text: "XDC Apothem",
234234
link: `${locale}/indexer/quickstart/quickstart_chains/xdc-apothem.md`,
235235
},
236+
{
237+
text: "ZetaChain",
238+
link: `${locale}/indexer/quickstart/quickstart_chains/zetachain.md`,
239+
},
236240
{
237241
text: "And more EVM networks",
238242
link: `${locale}/indexer/quickstart/quickstart_chains/evm.md`,
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Zetachain Quick Start
2+
3+
The goal of this quick start guide is to index all transfers and approval events from [ETH](https://zetachain.blockscout.com/token/0xd97B1de3619ed2c6BEb3860147E30cA8A7dC9891) on ZetaChain's Mainnet.
4+
5+
<!-- @include: ../snippets/evm-quickstart-reference.md -->
6+
7+
::: tip Note
8+
The final code of this project can be found [here](https://github.com/subquery/ethereum-subql-starter/tree/main/Zetachain/zetachain-starter).
9+
10+
We use Ethereum packages, runtimes, and handlers (e.g. `@subql/node-ethereum`, `ethereum/Runtime`, and `ethereum/*Hander`) for ZetaChain. Since ZetaChain is an EVM-compatible layer-2 scaling solution, we can use the core Ethereum framework to index it.
11+
:::
12+
13+
<!-- @include: ../snippets/evm-manifest-intro.md#level2 -->
14+
15+
As we are indexing all transfers and approvals from the ETH contract on ZetaChain's network, the first step is to import the contract abi definition which can be obtained from from any standard [ERC-20 contract](https://ethereum.org/en/developers/docs/standards/tokens/erc-20/). Copy the entire contract ABI and save it as a file called `erc20.abi.json` in the `/abis` directory.
16+
17+
**Update the `datasources` section as follows:**
18+
19+
```ts
20+
dataSources: [
21+
{
22+
kind: EthereumDatasourceKind.Runtime,
23+
startBlock: 3000000,
24+
options: {
25+
abi: "erc20",
26+
// This is the contract address for ETH
27+
address: "0xd97B1de3619ed2c6BEb3860147E30cA8A7dC9891",
28+
},
29+
assets: new Map([["erc20", { file: "./abis/erc20.abi.json" }]]),
30+
mapping: {
31+
file: "./dist/index.js",
32+
handlers: [
33+
{
34+
kind: EthereumHandlerKind.Call, // We use ethereum handlers since Zetachain is EVM-compatible
35+
handler: "handleTransaction",
36+
filter: {
37+
/**
38+
* The function can either be the function fragment or signature
39+
* function: '0x095ea7b3'
40+
* function: '0x7ff36ab500000000000000000000000000000000000000000000000000000000'
41+
*/
42+
function: "approve(address spender, uint256 amount)",
43+
},
44+
},
45+
{
46+
kind: EthereumHandlerKind.Event,
47+
handler: "handleLog",
48+
filter: {
49+
/**
50+
* Follows standard log filters https://docs.ethers.io/v5/concepts/events/
51+
* address: "0x60781C2586D68229fde47564546784ab3fACA982"
52+
*/
53+
topics: [
54+
"Transfer(address indexed from, address indexed to, uint256 amount)",
55+
],
56+
},
57+
},
58+
],
59+
},
60+
},
61+
]
62+
```
63+
64+
The above code indicates that you will be running a `handleTransaction` mapping function whenever there is a `approve` method being called on any transaction from the [ETH contract](https://zetachain.blockscout.com/token/0xd97B1de3619ed2c6BEb3860147E30cA8A7dC9891).
65+
66+
The code also indicates that you will be running a `handleLog` mapping function whenever there is a `Transfer` event being emitted from the ETH contract.
67+
68+
<!-- @include: ../snippets/ethereum-manifest-note.md -->
69+
70+
<!-- @include: ../snippets/schema-intro.md#level2 -->
71+
72+
Remove all existing entities and update the `schema.graphql` file as follows. Here you can see we are indexing block information such as the id, blockHeight, transfer receiver and transfer sender along with an approvals and all of the attributes related to them (such as owner and spender etc.).
73+
74+
```graphql
75+
type Transfer @entity {
76+
id: ID! # Transaction hash
77+
blockHeight: BigInt
78+
to: String!
79+
from: String!
80+
value: BigInt!
81+
contractAddress: String!
82+
}
83+
84+
type Approval @entity {
85+
id: ID! # Transaction hash
86+
blockHeight: BigInt
87+
owner: String!
88+
spender: String!
89+
value: BigInt!
90+
contractAddress: String!
91+
}
92+
```
93+
94+
<!-- @include: ../snippets/note-on-entity-relationships.md -->
95+
96+
<!-- @include: ../snippets/evm-codegen.md -->
97+
98+
```ts
99+
import { Approval, Transfer } from "../types";
100+
import {
101+
ApproveTransaction,
102+
TransferLog,
103+
} from "../types/abi-interfaces/Erc20Abi";
104+
```
105+
106+
<!-- @include: ../snippets/schema-note.md -->
107+
108+
<!-- @include: ../snippets/mapping-intro.md#level2 -->
109+
110+
Navigate to the default mapping function in the `src/mappings` directory. You will be able to see two exported functions `handleLog` and `handleTransaction`:
111+
112+
```ts
113+
export async function handleLog(log: TransferLog): Promise<void> {
114+
logger.info(`New transfer transaction log at block ${log.blockNumber}`);
115+
assert(log.args, "No log.args");
116+
117+
const transaction = Transfer.create({
118+
id: log.transactionHash,
119+
blockHeight: BigInt(log.blockNumber),
120+
to: log.args.to,
121+
from: log.args.from,
122+
value: log.args.value.toBigInt(),
123+
contractAddress: log.address,
124+
});
125+
126+
await transaction.save();
127+
}
128+
129+
export async function handleTransaction(tx: ApproveTransaction): Promise<void> {
130+
logger.info(`New Approval transaction at block ${tx.blockNumber}`);
131+
assert(tx.args, "No tx.args");
132+
133+
const approval = Approval.create({
134+
id: tx.hash,
135+
owner: tx.from,
136+
spender: await tx.args[0],
137+
value: BigInt(await tx.args[1].toString()),
138+
contractAddress: tx.to,
139+
});
140+
141+
await approval.save();
142+
}
143+
```
144+
145+
The `handleLog` function receives a `log` parameter of type `TransferLog` which includes log data in the payload. We extract this data and then save this to the store using the `.save()` function (_Note that SubQuery will automatically save this to the database_).
146+
147+
The `handleTransaction` function receives a `tx` parameter of type `ApproveTransaction` which includes transaction data in the payload. We extract this data and then save this to the store using the `.save()` function (_Note that SubQuery will automatically save this to the database_).
148+
149+
<!-- @include: ../snippets/ethereum-mapping-note.md -->
150+
151+
<!-- @include: ../snippets/build.md -->
152+
153+
<!-- @include: ../snippets/run-locally.md -->
154+
155+
<!-- @include: ../snippets/query-intro.md -->
156+
157+
```graphql
158+
159+
# Write your query or mutation here
160+
{
161+
query {
162+
transfers(first: 5, orderBy: VALUE_DESC) {
163+
totalCount
164+
nodes {
165+
id
166+
blockHeight
167+
from
168+
to
169+
value
170+
contractAddress
171+
}
172+
}
173+
}
174+
approvals(first: 5, orderBy: BLOCK_HEIGHT_DESC) {
175+
nodes {
176+
id
177+
blockHeight
178+
owner
179+
spender
180+
value
181+
contractAddress
182+
}
183+
}
184+
}
185+
```
186+
187+
::: tip Note
188+
The final code of this project can be found [here](https://github.com/subquery/ethereum-subql-starter/tree/main/Zetachain/zetachain-starter).
189+
:::
190+
191+
<!-- @include: ../snippets/whats-next.md -->

0 commit comments

Comments
 (0)