Skip to content

Commit 3dd330f

Browse files
committed
Feauting Quickstart for Autonity Testnet
1 parent a3bc2cc commit 3dd330f

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

docs/.vuepress/sidebar.ts

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

0 commit comments

Comments
 (0)