You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# MATIC data analytics - A squid example for CSV storage
2
2
3
-
This squid has been migrated from the [Gravatar subgraph](https://github.com/graphprotocol/example-subgraph). For a step-by-step migration guide, see the [migration docs page](https://docs.subsquid.io/migrate/migrate-subgraph/).
3
+
<palign="center">
4
+
<img src="assets/white-woodmark.svg">
5
+
</p>
4
6
5
-
## Quickstart
7
+
<divalign="center">
6
8
7
-
```bash
8
-
# 1. Update the Squid SDK and install dependencies
# transforming and storing it in the target database.
24
-
#
25
-
# To start the graphql server open the separate terminal
26
-
# and run. The GraphQL playground will be available at localhost:4350/graphql
27
-
make serve
28
-
```
17
+
[](https://gitpod.io#https://github.com/RaekwonIII/local-csv-indexing.git)
18
+
19
+
</div>
20
+
21
+
## Introduction
22
+
23
+
In this article I describe how to use Subsquid's indexing framework for data analytics prototyping.
24
+
I have built an indexer that processes MATIC transactions on Ethereum mainnet, and dumps them on a local CSV files.
25
+
I have then developed a simple Python script to demonstrate how to import these CSVs into a Pandas DataFrame, and perform aggregation operations on this data.
26
+
27
+
This project is a squid that indexes blockchain information generated by the Transfers of [MATIC](https://etherscan.io/token/0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0) tokens on Ethereum mainnet.
28
+
The indexer writes it to multiple files, divided in chunks of configurable size, in the CSV format.
29
+
30
+
It also contains a simple Python script (in the `data` folder), which reads the CSV files, imports the data in a [Pandas](http://pandas.pydata.org/) DataFrame, aggregates the data (albeit rather trivially) and plots a bar chart.
31
+
32
+
MATIC is the native token of the [Polygon project](https://polygon.technology/). It is defined by an ERC-20 standard smart contract, and the tokens are transferred via the contract's `transfer` function, which emits a `Transfer` event.
33
+
Such event is exactly what the squid ETL is indexing, and eventually writing to CSV files using [Subsqduid's `file-store`](https://www.npmjs.com/package/@subsquid/file-store) and [`file-store-csv` libraries](https://www.npmjs.com/package/@subsquid/file-store-csv).
34
+
35
+
The `analysis.py` Python script is using Pandas to read the data stored in all CSVs, and create a [DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html). Then, using the `groupby` function, it calculates the daily total value of transfers, and it creates a bar chart, using [`matplotlib` Python library](https://matplotlib.org/).
36
+
37
+
<palign="center">
38
+
<img src="assets/plot.png">
39
+
</p>
40
+
41
+
The project is relatively simple, because its purpose is purely demonstrative. Its intent is to showcase the capabilities of Subsquid SDK.
29
42
30
-
## Dev flow
43
+
### What is a Squid?
44
+
45
+
> A squid is a project that extracts and transforms on-chain data in order to present it as a GraphQL API. Squids are developed using the Subsquid SDK, which provides extensive tooling to define data schemas, data transfomation rules, and the shape of the resulting API.
46
+
47
+
We recommend that you read Subsquid docs to understand how it works: https://docs.subsquid.io/
48
+
49
+
## Prerequisites
50
+
51
+
- Node 16.x
52
+
- Docker
53
+
- NPM
54
+
55
+
## Quick-start local indexing
56
+
57
+
1. Clone the repository
58
+
2. Install dependencies (in a console window): `npm i`
59
+
3. Build the project `sqd build`
60
+
4. Launch the database container `sqd up`
61
+
5. Launch the processor `sqd process`
62
+
6. Launch the GraphQL server (in a separate console window) `sqd serve`
63
+
7. Access the GraphiQL Playground, by running `sqd open http://localhost:4350/graphql`<!-- markdown-link-check-disable-line -->
64
+
65
+
## Key components
66
+
67
+
* The `schema.graphql` file is used to define the database and API schemas. A command line tool will automatically generate code from it, which you can find in `src/model/generated`
68
+
* The `db/migrations` folder contains automatically files with SQL statements to modify the database (create, alter, delete tables), similarly to any ORM database interface.
69
+
* The `src/abi` folder contains facade TypeScript code, automatically generated by a command line tool from one, or multiple smart contract ABI(s). This code is used to programmatically interface with the smart contract(s) and decode events and function calls.
70
+
* The main logic of this project is defined in `src/processor.ts`. The `EvmBatchProcessor` class is configured and used to perform request to [Subsquid's Archive for Ethereum blockchain](https://app.subsquid.io/archives), to obtain necessary data. Then some custom logic is implemented to process this data in batches, and save it on the database with the custom defined structure.
71
+
72
+
[Subsquid documentation](https://docs.subsquid.io/) has dedicated sections and pages describing each of these concepts, it is advised to consult them, before starting to develop your own squid.
See [docs on schema updates](https://docs.subsquid.io/develop-a-squid/schema-file/schema-updates/) for more details.
63
107
64
-
65
108
### 4. Import ABI contract and generate interfaces to decode events
66
109
67
110
It is necessary to import the respective ABI definition to decode EVM logs.
68
111
69
-
To generate a type-safe facade class to decode EVM logs, use `squid-evm-typegen(1)`. For example, for Gravatar we fetch the public ABI by the address `0x2E645469f354BB4F5c8a05B3b30A929361cf77eC`and tell the `evm-typegen` to use `Gravity` as the base name:
112
+
To generate a type-safe facade class to decode EVM logs, place the ABI in the `assets` folder and use `squid-evm-typegen(1)`, e.g.:
It generates the files `abi.support.ts`, `Gravity.abi.ts` and `Gravity.ts` in the destination folder `src/abi`.
75
117
118
+
For more details about `squid-evm-typegen` read the [docs page](https://docs.subsquid.io/develop-a-squid/typegen/squid-evm-typegen/)
76
119
77
120
## Project conventions
78
121
@@ -83,14 +126,15 @@ The layout of `lib` must reflect `src`.
83
126
* All TypeORM classes must be exported by `src/model/index.ts` (`lib/model` module).
84
127
* Database schema must be defined in `schema.graphql`.
85
128
* Database migrations must reside in `db/migrations` and must be plain js files.
86
-
*`sqd(1)` and `squid-*(1)` executables consult `.env` file environment variables, in particular, to establish a database connection.
129
+
*`sqd(1)` and `squid-*(1)` executables consult `.env` file for a number of environment variables.
87
130
88
131
## GraphQL server extensions
89
132
90
133
It is possible to extend `squid-graphql-server(1)` with custom
91
134
[type-graphql](https://typegraphql.com) resolvers and to add request validation. See [the docs](https://docs.subsquid.io/develop-a-squid/graphql-api/custom-resolvers/) for more details.
92
135
136
+
## Learn More
93
137
94
-
## Disclaimer
138
+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
95
139
96
-
This is alpha-quality software. The Squid SDK may introduce breaking changes in future versions.
140
+
To learn React, check out the [React documentation](https://reactjs.org/).
0 commit comments